Add test for the pckr binary

This commit is contained in:
Abdelrahman Said 2023-10-21 20:00:23 +01:00
parent f035650038
commit 42f5489a36

View File

@ -1,6 +1,8 @@
#include "aliases.h" #include "aliases.h"
#include "io.h" #include "io.h"
#include "pak.h" #include "pak.h"
#include <linux/limits.h>
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -13,7 +15,10 @@ typedef struct {
} asset_t; } asset_t;
i32 main(i32 argc, char *argv[]) { i32 main(i32 argc, char *argv[]) {
FILE *fp = fopen("assets.pak", "rb"); char filepath[PATH_MAX] = {0};
realpath("assets.pak", filepath);
FILE *fp = fopen(filepath, "rb");
if (!fp) { if (!fp) {
printf("Failed to open file\n"); printf("Failed to open file\n");
@ -22,7 +27,7 @@ i32 main(i32 argc, char *argv[]) {
asset_t assets[] = { asset_t assets[] = {
(asset_t){.name = "file01", .contents = "Hello\n"}, (asset_t){.name = "file01", .contents = "Hello\n"},
(asset_t){.name = "NAME", .contents = "Abdelrahman\n"}, (asset_t){.name = "name.txt", .contents = "Abdelrahman\n"},
(asset_t){.name = "file02", .contents = "This is the second file\n"}, (asset_t){.name = "file02", .contents = "This is the second file\n"},
}; };
@ -75,21 +80,26 @@ i32 main(i32 argc, char *argv[]) {
return EXIT_FAILURE; return EXIT_FAILURE;
} }
toc_entry_t *entries[pack.toc.count]; toc_entry_t *toc_entries[pack.toc.count];
memset((void *)entries, 0, sizeof(toc_entry_t *) * 3); memset((void *)toc_entries, 0, sizeof(toc_entry_t *) * pack.toc.count);
pak_entry_t *pak_entries[pack.toc.count];
memset((void *)pak_entries, 0, sizeof(pak_entry_t *) * pack.toc.count);
bool match = true;
for (u64 i = 0; i < pack.toc.count; ++i) { for (u64 i = 0; i < pack.toc.count; ++i) {
u64 pos = ftell(fp); u64 pos = ftell(fp);
u64 data[2] = {0}; u64 data[2] = {0};
fread(data, ARRLEN(data), 1, fp); fread(data, sizeof(u64) * ARRLEN(data), 1, fp);
u64 size = sizeof(toc_entry_t) + data[1]; u64 toc_entry_size = sizeof(toc_entry_t) + data[1];
entries[i] = malloc(size); toc_entries[i] = malloc(toc_entry_size);
if (!(entries[i])) { if (!(toc_entries[i])) {
printf("Allocation failure\n"); printf("TOC entry allocation failed\n");
fclose(fp); fclose(fp);
@ -98,14 +108,71 @@ i32 main(i32 argc, char *argv[]) {
fseek(fp, pos, SEEK_SET); fseek(fp, pos, SEEK_SET);
fread(&(entries[i]), size, 1, fp); fread(toc_entries[i], toc_entry_size, 1, fp);
i64 entry_in_assets = -1;
for (u64 j = 0; j < pack.toc.count; ++j) {
if (STREQ((toc_entries[i])->name, assets[j].name)) {
match = true;
entry_in_assets = j;
break;
} else {
match = false;
}
}
pos = ftell(fp);
u64 new_pos =
toc_entries[i]->offset + sizeof(pak_t) + pack.toc.size - sizeof(u64);
fseek(fp, new_pos, SEEK_SET);
u64 entry_size = 0;
fread(&entry_size, sizeof(u64), 1, fp);
u64 pak_entry_size = entry_size + sizeof(pak_entry_t);
pak_entries[i] = malloc(pak_entry_size);
if (!(pak_entries[i])) {
printf("PAK entry allocation failed\n");
fclose(fp);
return EXIT_FAILURE;
}
fread(pak_entries[i]->data, entry_size, 1, fp);
if (!(STREQ((char *)(pak_entries[i]->data),
assets[entry_in_assets].contents))) {
printf("%s, %s\n", (char *)(pak_entries[i]->data),
assets[entry_in_assets].contents);
match = false;
break;
}
fseek(fp, pos, SEEK_SET);
} }
for (u64 i = 0; i < pack.toc.count; ++i) { for (u64 i = 0; i < pack.toc.count; ++i) {
free(entries[i]); if (toc_entries[i]) {
free(toc_entries[i]);
}
if (pak_entries[i]) {
free(pak_entries[i]);
}
} }
fclose(fp); fclose(fp);
if (!match) {
printf("Pack contents doesn't match expected contents\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }