a2a_send posts SendMessage with a text part and, when a path is given, a file part: base64 bytes, a name (the path's last component) and a media type guessed from the name, at most 8 MiB -- the same cap the server enforces on the way in. The answer is text; a file the other side sends back is saved by collect_parts and named by path, which the tool returns. sp_b64_encode is the missing direction of the base64 helper, with vectors of its own in test_b64: empty input, one and two trailing bytes, a NUL. test_a2a: a real PNG is attached, the request carries name, mimeType and the base64 bytes, and the other side names it in the answer; a missing path is an error, not a silent empty message.
83 lines
1.9 KiB
C
83 lines
1.9 KiB
C
/*
|
|
* splinter -- base64 for A2A raw parts.
|
|
*
|
|
* The vectors are the usual ones; the interesting cases are optional padding,
|
|
* whitespace (Telegram/JSON pretty-printing), and a byte that must not become
|
|
* a silently different byte.
|
|
*/
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#include "splinter.h"
|
|
#include "b64.h"
|
|
|
|
static int failures;
|
|
|
|
#define CHECK(cond) do { \
|
|
if (!(cond)) { \
|
|
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
|
|
failures++; \
|
|
} \
|
|
} while (0)
|
|
|
|
static void
|
|
decode_is(const char *in, const char *want, size_t want_len)
|
|
{
|
|
struct sp_buf b;
|
|
int rc;
|
|
|
|
CHECK(sp_buf_init(&b) == 0);
|
|
rc = sp_b64_decode(in, strlen(in), &b);
|
|
if (rc != 0) {
|
|
fprintf(stderr, "FAIL %s:%d: rc %d for \"%s\"\n", __FILE__,
|
|
__LINE__, rc, in);
|
|
failures++;
|
|
} else {
|
|
CHECK(b.len == want_len);
|
|
CHECK(memcmp(b.p != NULL ? b.p : "", want, want_len) == 0);
|
|
}
|
|
sp_buf_free(&b);
|
|
}
|
|
|
|
static void
|
|
encode_is(const char *in, size_t len, const char *want)
|
|
{
|
|
struct sp_buf b;
|
|
|
|
CHECK(sp_buf_init(&b) == 0);
|
|
CHECK(sp_b64_encode(in, len, &b) == 0);
|
|
CHECK(b.p != NULL && strcmp(b.p, want) == 0);
|
|
sp_buf_free(&b);
|
|
}
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
encode_is("", 0, "");
|
|
encode_is("hello", 5, "aGVsbG8=");
|
|
encode_is("Hello, World!", 13, "SGVsbG8sIFdvcmxkIQ==");
|
|
encode_is("\0", 1, "AA==");
|
|
encode_is("PNGDATA", 7, "UE5HREFUQQ==");
|
|
|
|
decode_is("", "", 0);
|
|
decode_is("aGVsbG8=", "hello", 5);
|
|
decode_is("aGVsbG8", "hello", 5); /* padding is optional */
|
|
decode_is("aGVs bG8=\n", "hello", 5); /* whitespace is ignored */
|
|
decode_is("AA==", "\0", 1);
|
|
decode_is("SGVsbG8sIFdvcmxkIQ==", "Hello, World!", 13);
|
|
|
|
{
|
|
struct sp_buf b;
|
|
|
|
CHECK(sp_buf_init(&b) == 0);
|
|
CHECK(sp_b64_decode("!!!!", 4, &b) != 0);
|
|
CHECK(sp_b64_decode(NULL, 0, &b) != 0);
|
|
CHECK(sp_b64_decode("aGVsbG8=", 8, NULL) != 0);
|
|
sp_buf_free(&b);
|
|
}
|
|
|
|
if (failures == 0)
|
|
printf("test_b64: ok\n");
|
|
return failures == 0 ? 0 : 1;
|
|
}
|