/* * 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 #include #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; }