/* * archive_test.c - testa a tinyblocks-archive: .tar.gz e .tar com pastas e * nome longo (GNU/pax), e um tar malicioso com "../" que nao pode sair da * pasta de destino. Os arquivos de teste sao feitos pelo tar e pelo Python. */ #define _POSIX_C_SOURCE 200809L #include #include #include #include #include #include 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 bool file_is(const char* path, const char* want) { FILE* f = fopen(path, "r"); if (!f) return false; char buf[256] = ""; size_t n = fread(buf, 1, sizeof(buf) - 1, f); buf[n] = 0; fclose(f); return !strcmp(buf, want); } int main(void) { const char* longdir = "pasta_com_um_nome_bem_comprido_para_passar_dos_cem_caracteres_do_cabecalho_ustar_antigo"; char cmd[2048]; snprintf(cmd, sizeof(cmd), "rm -rf /tmp/tb_arch && mkdir -p /tmp/tb_arch/src/a/%s && cd /tmp/tb_arch/src && " "printf 'ola' > a/um.txt && printf 'longo' > a/%s/arquivo_de_nome_longo_tambem.txt && " "tar czf ../teste.tar.gz a && tar cf ../teste.tar a && " "python3 -I -c \"import tarfile,io; t=tarfile.open('../mau.tar.gz','w:gz'); " "i=tarfile.TarInfo('../fora.txt'); d=b'x'; i.size=1; t.addfile(i, io.BytesIO(d)); " "i=tarfile.TarInfo('/abs.txt'); i.size=1; t.addfile(i, io.BytesIO(d)); " "i=tarfile.TarInfo('dentro.txt'); i.size=1; t.addfile(i, io.BytesIO(d)); t.close()\"", longdir, longdir); if (system(cmd) != 0) { printf("FAIL não criou os arquivos de teste\n"); return 1; } TinyBlocks* rt = NULL; tinyblocks_init(&rt, NULL); tb_library_register(rt, tb_archive_get_library()); TinyBlocks_JsonValue* l = tb_archive_list("/tmp/tb_arch/teste.tar.gz"); CHECK(l && tb_json_array_length(l) == 2, "lista os 2 arquivos do .tar.gz"); tb_json_release(l); int n = tb_archive_extract("/tmp/tb_arch/teste.tar.gz", "/tmp/tb_arch/out"); char p[1024]; snprintf(p, sizeof(p), "/tmp/tb_arch/out/a/%s/arquivo_de_nome_longo_tambem.txt", longdir); CHECK(n == 2 && file_is("/tmp/tb_arch/out/a/um.txt", "ola") && file_is(p, "longo"), "extrai o .tar.gz com pastas e nome longo"); n = tb_archive_extract("/tmp/tb_arch/teste.tar", "/tmp/tb_arch/out2"); CHECK(n == 2 && file_is("/tmp/tb_arch/out2/a/um.txt", "ola"), "extrai .tar sem compressão"); n = tb_archive_extract("/tmp/tb_arch/mau.tar.gz", "/tmp/tb_arch/out3"); CHECK(file_is("/tmp/tb_arch/out3/dentro.txt", "x") && access("/tmp/tb_arch/fora.txt", F_OK) != 0 && access("/abs.txt", F_OK) != 0, "\"../\" e caminho absoluto não saem da pasta"); CHECK(tb_archive_extract("/tmp/tb_arch/src/a/um.txt", "/tmp/tb_arch/out4") < 0 && strstr(tb_archive_last_error(), "tar"), "arquivo que não é tar dá erro"); /* pelo bloco */ TinyBlocks_Block* b = tb_block_create(rt, tb_type_block_find(rt, "Archive.List")); TinyBlocks_Value v = tb_value_text("/tmp/tb_arch/teste.tar"); tb_block_set_default(b, 0, v); tb_value_free(&v); tb_block_eval(b, rt); CHECK(b->outputs[0].kind == TB_VALUE_JSON && tb_json_array_length(b->outputs[0].as.json) == 2, "bloco \"arquivos dentro de\""); tinyblocks_deinit(&rt); if (fails) printf("\n%d FALHA(S)\n", fails); else printf("\nTUDO OK\n"); return fails != 0; }