92 lines
3.2 KiB
C
92 lines
3.2 KiB
C
/*
|
|
* base64_test.c - testa tinyblocks-base64: encode/decode (RFC 4648) e os
|
|
* blocos Base64.Encode / Base64.Decode.
|
|
*/
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <tinyblocks/tinyblocks.h>
|
|
#include <tb_base64.h>
|
|
|
|
static int fails = 0;
|
|
#define CHECK(c, m) do { if (c) printf("ok %s\n", m); else { printf("FAIL %s\n", m); fails++; } } while (0)
|
|
|
|
static void set_in(TinyBlocks_Block* b, uint32_t i, const char* v) {
|
|
TinyBlocks_Value vv = tb_value_text(v);
|
|
tb_block_set_default(b, i, vv);
|
|
tb_value_free(&vv);
|
|
}
|
|
|
|
int main(void) {
|
|
/* -- API C --- */
|
|
char* e = tb_base64_encode_str("Ola");
|
|
CHECK(e && strcmp(e, "T2xh") == 0, "encode 'Ola' -> 'T2xh'");
|
|
free(e);
|
|
|
|
e = tb_base64_encode_str("");
|
|
CHECK(e && strcmp(e, "") == 0, "encode vazio -> vazio");
|
|
free(e);
|
|
|
|
e = tb_base64_encode("f", 1); /* 1 byte -> 2 chars + '==' */
|
|
CHECK(e && strcmp(e, "Zg==") == 0, "encode 1 byte -> 'Zg=='");
|
|
free(e);
|
|
|
|
e = tb_base64_encode("fo", 2);
|
|
CHECK(e && strcmp(e, "Zm8=") == 0, "encode 2 bytes -> 'Zm8='");
|
|
free(e);
|
|
|
|
char* d = tb_base64_decode_str("T2xh", NULL);
|
|
CHECK(d && strcmp(d, "Ola") == 0, "decode 'T2xh' -> 'Ola'");
|
|
free(d);
|
|
|
|
size_t n = 0;
|
|
unsigned char* raw = tb_base64_decode("Zm9vYmFy", &n); /* "foobar" */
|
|
CHECK(raw && n == 6 && memcmp(raw, "foobar", 6) == 0, "decode 'Zm9vYmFy' -> 'foobar'");
|
|
free(raw);
|
|
|
|
/* round-trip de bytes arbitrarios (0..255) */
|
|
unsigned char orig[256];
|
|
for (int i = 0; i < 256; i++) orig[i] = (unsigned char)i;
|
|
e = tb_base64_encode(orig, sizeof(orig));
|
|
raw = tb_base64_decode(e, &n);
|
|
CHECK(raw && n == 256 && memcmp(raw, orig, 256) == 0, "round-trip 256 bytes");
|
|
free(raw); free(e);
|
|
|
|
/* entrada invalida */
|
|
CHECK(tb_base64_decode("@@@", NULL) == NULL, "decode invalido -> NULL");
|
|
CHECK(strlen(tb_base64_last_error()) > 0, "mensagem de erro presente");
|
|
|
|
/* -- Blocos --- */
|
|
TinyBlocks* rt;
|
|
tinyblocks_init(&rt, NULL);
|
|
tb_library_register(rt, tb_base64_get_library());
|
|
|
|
TinyBlocks_TypeBlock* tenc = tb_rt_find_type(rt, "Base64.Encode");
|
|
TinyBlocks_TypeBlock* tdec = tb_rt_find_type(rt, "Base64.Decode");
|
|
CHECK(tenc && tdec, "blocos Base64.Encode/Decode registrados");
|
|
const TinyBlocks_Category* cat = tb_library_category((const TinyBlocks_LibraryBlock*)tenc->base.parent);
|
|
CHECK(cat && strcmp(cat->name, "Base64") == 0, "categoria 'Base64'");
|
|
|
|
if (tenc) {
|
|
TinyBlocks_Block* b = tb_block_create(rt, tenc);
|
|
set_in(b, 0, "TinyBlocks");
|
|
tb_block_eval(b, rt);
|
|
CHECK(b->outputs[0].kind == TB_VALUE_TEXT && b->outputs[0].as.text &&
|
|
strcmp(b->outputs[0].as.text, "VGlueUJsb2Nrcw==") == 0, "bloco Encode('TinyBlocks')");
|
|
tb_block_destroy(b, rt);
|
|
}
|
|
if (tdec) {
|
|
TinyBlocks_Block* b = tb_block_create(rt, tdec);
|
|
set_in(b, 0, "VGlueUJsb2Nrcw==");
|
|
tb_block_eval(b, rt);
|
|
CHECK(b->outputs[0].kind == TB_VALUE_TEXT && b->outputs[0].as.text &&
|
|
strcmp(b->outputs[0].as.text, "TinyBlocks") == 0, "bloco Decode(...) -> 'TinyBlocks'");
|
|
tb_block_destroy(b, rt);
|
|
}
|
|
|
|
tinyblocks_deinit(&rt);
|
|
if (fails) printf("\n%d FALHA(S)\n", fails);
|
|
else printf("\nTUDO OK\n");
|
|
return fails != 0;
|
|
}
|