summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile2
-rw-r--r--test/minunit.h2
-rw-r--r--test/test_cli.c19
-rw-r--r--test/test_compress.c21
-rw-r--r--test/test_crypto.c14
-rw-r--r--test/test_encoding.c13
-rw-r--r--test/test_header.c17
-rw-r--r--test/test_io.c67
-rw-r--r--test/test_run.c23
-rw-r--r--test/test_table.c31
10 files changed, 130 insertions, 79 deletions
diff --git a/Makefile b/Makefile
index 97165b1..74ffdd0 100644
--- a/Makefile
+++ b/Makefile
@@ -51,7 +51,9 @@ $(OBJDIR)/%.o: $(TSTDIR)/%.c
@$(CC) $(CFLAGS) -c -o $@ -I $(SRCDIR) $(TSTDIR)/$*.c
test: $(BINDIR)/test
+ @echo
@$(BINDIR)/test
+ @echo
clean:
rm -f $(BINDIR)/* $(OBJDIR)/*
diff --git a/test/minunit.h b/test/minunit.h
index 80ef040..0b9df8e 100644
--- a/test/minunit.h
+++ b/test/minunit.h
@@ -1,4 +1,4 @@
#pragma once
#define mu_assert(message, test) do { if (!(test)) return message; } while (0)
-#define mu_run_test(test) do { char *message = test(); tests_run++; if (message) return message; } while (0)
+#define mu_run_test(test) do { const char *message = test(); tests_run++; if (message) return message; } while (0)
extern int tests_run;
diff --git a/test/test_cli.c b/test/test_cli.c
index 86d6a2e..37ef9a2 100644
--- a/test/test_cli.c
+++ b/test/test_cli.c
@@ -20,36 +20,39 @@
#include <stdlib.h>
#include <string.h>
-#include "minunit.h"
-
#include "cli.h"
#include "crypto.h"
+#include "minunit.h"
+
extern int tests_run;
-char *test_out_path(void) {
+const char *test_out_path(void) {
char *argv[] = {"nekopack", "-o", "/tmp", "a.xp3"};
struct params p = parse_args(4, argv);
- mu_assert("Trailing slash not appended to path", !strcmp(p.out, "/tmp/"));
+ mu_assert("[cli] test_out_path: Trailing slash not appended to path",
+ !strcmp(p.out, "/tmp/"));
free(p.out);
return NULL;
}
-char *test_game_id(void) {
+const char *test_game_id(void) {
char *argv[] = {"nekopack", "-g", "nekopara_volume_1", "a.xp3"};
struct params p = parse_args(4, argv);
- mu_assert("Incorrect game ID", p.game == NEKOPARA_VOLUME_1);
+ mu_assert("[cli] test_game_id: Incorrect game ID",
+ p.game == NEKOPARA_VOLUME_1);
free(p.out);
return NULL;
}
-char *test_vararg_index(void) {
+const char *test_vararg_index(void) {
char *argv[] = {"nekopack", "-l", "a.xp3"};
struct params p = parse_args(3, argv);
- mu_assert("Invalid vararg index", !strcmp("a.xp3", argv[p.vararg_index]));
+ mu_assert("[cli] test_vararg_index: Invalid vararg index",
+ !strcmp("a.xp3", argv[p.vararg_index]));
free(p.out);
return NULL;
}
diff --git a/test/test_compress.c b/test/test_compress.c
index 583dd8b..34bfeb8 100644
--- a/test/test_compress.c
+++ b/test/test_compress.c
@@ -19,11 +19,12 @@
#include <stdbool.h>
-#include "minunit.h"
-
#include "compress.h"
#include "io.h"
+#include "minunit.h"
+
+
extern int tests_run;
/* FIXME: This won't hold up for the future. Even if it works across
@@ -42,13 +43,15 @@ static bool streams_eq(struct stream *a, struct stream *b) {
}
-char *test_compress(void) {
+const char *test_compress(void) {
struct stream *s = stream_from_file("test/vectors/test.png");
struct stream *expected = stream_from_file("test/vectors/test.bin");
- mu_assert("Could not open test vectors", s != NULL && expected != NULL);
+ mu_assert("[compress] test_compress: Could not open test vectors",
+ s != NULL && expected != NULL);
struct stream *res = stream_deflate(s, s->len);
- mu_assert("Expected compression not met", streams_eq(res, expected));
+ mu_assert("[compress] test_compress: Expected compression not met",
+ streams_eq(res, expected));
stream_free(s);
stream_free(expected);
@@ -57,13 +60,15 @@ char *test_compress(void) {
}
-char *test_decompress(void) {
+const char *test_decompress(void) {
struct stream *s = stream_from_file("test/vectors/test.bin");
struct stream *expected = stream_from_file("test/vectors/test.png");
- mu_assert("Could not open test vectors", s != NULL && expected != NULL);
+ mu_assert("[compress] test_decompress: Could not open test vectors",
+ s != NULL && expected != NULL);
struct stream *res = stream_inflate(s, s->len, 176608);
- mu_assert("Expected compression not met", streams_eq(res, expected));
+ mu_assert("[compress] test_decompress: Expected compression not met",
+ streams_eq(res, expected));
stream_free(s);
stream_free(expected);
diff --git a/test/test_crypto.c b/test/test_crypto.c
index 7071c03..0871b3f 100644
--- a/test/test_crypto.c
+++ b/test/test_crypto.c
@@ -20,22 +20,24 @@
#include <stddef.h>
#include <stdint.h>
-#include "minunit.h"
-
#include "crypto.h"
+#include "minunit.h"
+
extern int tests_run;
-char *test_derive_initial(void) {
+const char *test_derive_initial(void) {
struct game_key k = get_key(NEKOPARA_VOLUME_0);
- mu_assert("Initial key failure", derive_initial(k, 0xdeadbeefcafebabe) == 0x22);
+ mu_assert("[crypto] test_derive_initial: Initial key failure",
+ derive_initial(k, 0xdeadbeefcafebabe) == 0x22);
return NULL;
}
-char *test_derive_primary(void) {
+const char *test_derive_primary(void) {
struct game_key k = get_key(NEKOPARA_VOLUME_1);
- mu_assert("Initial key failure", derive_primary(k, 0xdeadbeefcafebabe) == 0x13);
+ mu_assert("[crypto] test_derive_primary: Initial key failure",
+ derive_primary(k, 0xdeadbeefcafebabe) == 0x13);
return NULL;
}
diff --git a/test/test_encoding.c b/test/test_encoding.c
index bd63426..a19f68f 100644
--- a/test/test_encoding.c
+++ b/test/test_encoding.c
@@ -20,26 +20,27 @@
#include <stdlib.h>
#include <string.h>
-#include "minunit.h"
-
#include "encoding.h"
+#include "minunit.h"
+
-char *test_decode_utf16le(void) {
+const char *test_decode_utf16le(void) {
char *in = "\x41\x00\x42\x00\x43\x00\x00\x00", *out = malloc(8);
memset(out, '\xff', 8);
utf16le_decode(in, out, 8);
- mu_assert("UTF16-LE decoding failure", !strcmp(out, "ABC"));
+ mu_assert("[encoding] test_decode_utf16le: UTF16-LE decoding failure",
+ !strcmp(out, "ABC"));
free(out);
return NULL;
}
-char *test_encode_utf16le(void) {
+const char *test_encode_utf16le(void) {
char *in = "ABC", *out = malloc(8);
memset(out, '\xff', 8);
utf16le_encode(in, out, 3);
- mu_assert("UTF16-LE encoding failure",
+ mu_assert("[encoding] test_encode_utf16le: UTF16-LE encoding failure",
!memcmp(out, "\x41\x00\x42\x00\x43\x00\x00\x00", 8));
free(out);
return NULL;
diff --git a/test/test_header.c b/test/test_header.c
index 9a1e680..9ac0e6b 100644
--- a/test/test_header.c
+++ b/test/test_header.c
@@ -20,36 +20,37 @@
#include <stdlib.h>
#include <string.h>
-#include "minunit.h"
-
#include "header.h"
#include "io.h"
+#include "minunit.h"
+
extern int tests_run;
-char *test_header_read(void) {
+const char *test_header_read(void) {
struct stream *s = stream_new(sizeof(struct header));
memset(s->_start, '\x00', sizeof(struct header));
struct header *h = read_header(s);
- mu_assert("Header assertions failed", h == NULL);
+ mu_assert("[header] test_header_read: Header assertions failed", h == NULL);
stream_rewind(s);
memcpy(s->_start, "\x58\x50\x33\x0d\x0a\x20\x0a\x1a\x8b\x67\x01\x17\x00"
"\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x80\x00\x00\x00\x00\x00"
"\x00\x00\x00\xd1\xfe\x56\x0b\x00\x00\x00\x00", 42);
h = read_header(s);
- mu_assert("Header parsing failure", h != NULL);
+ mu_assert("[header] test_header_read: Header parsing failure", h != NULL);
stream_free(s);
return NULL;
}
-char *test_header_creation(void) {
+const char *test_header_creation(void) {
struct header *h = create_header();
- mu_assert("Header allocation failed", h != NULL);
- mu_assert("Incorrect XP3 header", !memcmp(h->magic, XP3_MAGIC, 11));
+ mu_assert("[header] test_header_creation: Header allocation failed", h != NULL);
+ mu_assert("[header] test_header_creation: Incorrect XP3 header",
+ !memcmp(h->magic, XP3_MAGIC, 11));
return NULL;
}
diff --git a/test/test_io.c b/test/test_io.c
index 0596dbc..688ac31 100644
--- a/test/test_io.c
+++ b/test/test_io.c
@@ -21,20 +21,20 @@
#include <stdio.h>
#include <string.h>
-#include "minunit.h"
-
#include "io.h"
+#include "minunit.h"
+
extern int tests_run;
-char *test_stream_obj(void) {
+const char *test_stream_obj(void) {
struct stream *s_1 = stream_new(0x10);
struct stream *s_2 = stream_new(0x20);
struct stream *s_3 = stream_new(0x40);
- mu_assert("Incorrect size for stream `s_1`", s_1->len == 0x10);
- mu_assert("Incorrect size for stream `s_2`", s_2->len == 0x20);
- mu_assert("Incorrect size for stream `s_3`", s_3->len == 0x40);
+ mu_assert("[io] test_stream_obj: Incorrect size for stream `s_1`", s_1->len == 0x10);
+ mu_assert("[io] test_stream_obj: Incorrect size for stream `s_2`", s_2->len == 0x20);
+ mu_assert("[io] test_stream_obj: Incorrect size for stream `s_3`", s_3->len == 0x40);
stream_free(s_1);
stream_free(s_2);
stream_free(s_3);
@@ -42,23 +42,27 @@ char *test_stream_obj(void) {
}
-char *test_stream_rw(void) {
+const char *test_stream_rw(void) {
char dest[4];
struct stream *s = stream_new(0x4);
stream_write(s, "\x00\x01\x02\x03", 4);
- mu_assert("Stream write failure", !memcmp(s->_start, "\x00\x01\x02\x03", 4));
- mu_assert("Stream write did not increment cursor", s->_cur - s->_start == 4);
+ mu_assert("[io] test_stream_rw: Stream write failure",
+ !memcmp(s->_start, "\x00\x01\x02\x03", 4));
+ mu_assert("[io] test_stream_rw: Write did not increment cursor",
+ s->_cur - s->_start == 4);
s->_cur = s->_start;
stream_read(dest, s, 4);
- mu_assert("Stream read failure", !memcmp(dest, "\x00\x01\x02\x03", 4));
- mu_assert("Stream read did not increment cursor", s->_cur - s->_start == 4);
+ mu_assert("[io] test_stream_rw: Stream read failure",
+ !memcmp(dest, "\x00\x01\x02\x03", 4));
+ mu_assert("[io] test_stream_rw: Read did not increment cursor",
+ s->_cur - s->_start == 4);
stream_free(s);
return NULL;
}
-char *test_stream_dump(void) {
+const char *test_stream_dump(void) {
char buf[4];
FILE *fp = tmpfile();
struct stream *s = stream_new(0x4);
@@ -67,43 +71,58 @@ char *test_stream_dump(void) {
stream_dump(fp, s, 4);
fseek(fp, 0, SEEK_SET);
fread(buf, 4, 1, fp);
- mu_assert("Stream dump failure", !memcmp(buf, "\x00\x01\x02\x03", 4));
+ mu_assert("[io] test_stream_dump: Stream dump failure",
+ !memcmp(buf, "\x00\x01\x02\x03", 4));
fclose(fp);
return NULL;
}
-char *test_stream_realloc(void) {
+const char *test_stream_realloc(void) {
struct stream *s = stream_new(0x2);
stream_write(s, "\x00\x01\x02\x03", 4);
- mu_assert("Stream realloc did not modify `len`", s->len > 0x2);
+ mu_assert("[io] test_stream_realloc: Stream realloc did not modify `len`",
+ s->len > 0x2);
return NULL;
}
-char *test_stream_xor(void) {
+const char *test_stream_xor(void) {
struct stream *s = stream_new(2);
stream_write(s, "\x01\x01", 2);
stream_xor(s, 1, 0);
- mu_assert("Stream initial key failure", !memcmp(s->_start, "\x00\x01", 2));
+ mu_assert("[io] test_stream_xor: Stream initial key failure",
+ !memcmp(s->_start, "\x00\x01", 2));
+
stream_xor(s, 0, 1);
- mu_assert("Stream primary key failure", !memcmp(s->_start, "\x01\x00", 2));
+ mu_assert("[io] test_stream_xor: Stream primary key failure",
+ !memcmp(s->_start, "\x01\x00", 2));
+
stream_free(s);
return NULL;
}
-char *test_stream_nav(void) {
+const char *test_stream_nav(void) {
struct stream *s = stream_new(2);
- mu_assert("Stream cursor not at beginning", stream_tell(s) == 0);
+ mu_assert("[io] test_stream_nav: Stream cursor not at beginning",
+ stream_tell(s) == 0);
+
stream_seek(s, 2, SEEK_CUR);
- mu_assert("Stream cursor not advanced (SEEK_CUR)", stream_tell(s) == 2);
+ mu_assert("[io] test_stream_nav: Stream cursor not advanced (SEEK_CUR)",
+ stream_tell(s) == 2);
+
stream_seek(s, 2, SEEK_SET);
- mu_assert("Stream cursor not advanced (SEEK_SET)", stream_tell(s) == 2);
+ mu_assert("[io] test_stream_nav: Stream cursor not advanced (SEEK_SET)",
+ stream_tell(s) == 2);
+
stream_seek(s, 0, SEEK_END);
- mu_assert("Stream cursor not advanced (SEEK_END)", stream_tell(s) == 2);
+ mu_assert("[io] test_stream_nav: Stream cursor not advanced (SEEK_END)",
+ stream_tell(s) == 2);
+
stream_rewind(s);
- mu_assert("Stream cursor not rewinded", stream_tell(s) == 0);
+ mu_assert("[io] test_stream_nav: Stream cursor not rewinded",
+ stream_tell(s) == 0);
stream_free(s);
return NULL;
}
diff --git a/test/test_run.c b/test/test_run.c
index 4263c7f..7ed5f6f 100644
--- a/test/test_run.c
+++ b/test/test_run.c
@@ -20,7 +20,6 @@
#include <stdio.h>
#include "minunit.h"
-
#include "test_cli.h"
#include "test_compress.h"
#include "test_crypto.h"
@@ -29,10 +28,16 @@
#include "test_io.h"
#include "test_table.h"
+#define ANSI_RED "\033[31m"
+#define ANSI_GREEN "\033[32m"
+#define ANSI_BLUE "\033[34m"
+#define ANSI_BLINK "\033[5m"
+#define ANSI_END "\033[0m"
+
int tests_run = 0;
-static char *run_all_tests(void) {
+static const char *all_tests(void) {
mu_run_test(test_out_path);
mu_run_test(test_vararg_index);
mu_run_test(test_game_id);
@@ -58,11 +63,17 @@ static char *run_all_tests(void) {
int main(void) {
- char *ret = run_all_tests();
- if (ret) {
- printf("%s\n", ret);
+ const char *res;
+
+ printf("Test suite started.\n\n");
+ res = all_tests();
+
+ if (res != NULL) {
+ printf(ANSI_RED "Test failed! " ANSI_END "\"%s\"\n", res);
return 1;
}
- printf("Successful tests: %d\n", tests_run);
+
+ printf(ANSI_GREEN "Test suite finished successfully.\n" ANSI_END);
+ printf(ANSI_BLUE "%d tests were run.\n" ANSI_END, tests_run);
return 0;
}
diff --git a/test/test_table.c b/test/test_table.c
index f5a3a04..cf8ba33 100644
--- a/test/test_table.c
+++ b/test/test_table.c
@@ -20,43 +20,47 @@
#include <stdlib.h>
#include <string.h>
-#include "minunit.h"
-
#include "io.h"
#include "table.h"
+#include "minunit.h"
+
-char *test_table_list(void) {
+const char *test_table_list(void) {
struct table_entry *root = calloc(sizeof(struct table_entry), 1);
struct table_entry *next = calloc(sizeof(struct table_entry), 1);
next->key = 0xffffffff;
entry_append(root, next);
- mu_assert("Table entry not inserted", root->next->key == 0xffffffff);
+ mu_assert("[table] test_table_list: Table entry not inserted",
+ root->next->key == 0xffffffff);
entry_free(root);
return NULL;
}
-char *test_table_elif(void) {
+const char *test_table_elif(void) {
struct stream *s = stream_new(10);
struct table_entry *root = calloc(sizeof(struct table_entry), 1);
stream_write(s, "\xff\xff\xff\xff\x01\x00\x41\x00\x00\x00", 10);
stream_rewind(s);
read_elif(s, root);
- mu_assert("Table entry not inserted (eliF)", root->next != NULL);
- mu_assert("Filename handling failure", !strcmp(root->next->filename, "A"));
+ mu_assert("[table] test_table_elif: Table entry not inserted (eliF)",
+ root->next != NULL);
+ mu_assert("[table] test_table_elif: Filename handling failure",
+ !strcmp(root->next->filename, "A"));
free(root->next->filename);
root->next->filename = NULL;
stream_rewind(s);
read_elif(s, root);
- mu_assert("Existing entry ignored (eliF)", root->next->next == NULL);
+ mu_assert("[table] test_table_elif: Existing entry ignored (eliF)",
+ root->next->next == NULL);
entry_free(root);
stream_free(s);
return NULL;
}
-char *test_table_file(void) {
+const char *test_table_file(void) {
struct stream *s = stream_new(0x100);
struct table_entry *root = calloc(sizeof(struct table_entry), 1);
stream_write(s, "\x61\x64\x6c\x72\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff"
@@ -69,11 +73,14 @@ char *test_table_file(void) {
"\x00\x00\x00\x00\x00\x00\x00\x00", 113);
stream_rewind(s);
read_file(s, root);
- mu_assert("Table entry not inserted (eliF)", root->next != NULL);
- mu_assert("Key read failure", root->next->key == 0xffffffff);
+ mu_assert("[table] test_table_file: Table entry not inserted (eliF)",
+ root->next != NULL);
+ mu_assert("[table] test_table_file: Key read failure",
+ root->next->key == 0xffffffff);
root->next->ctime = 420;
stream_rewind(s);
read_file(s, root);
- mu_assert("Existing entry ignored (eliF)", root->next->ctime == 0);
+ mu_assert("[table] test_table_file: Existing entry ignored (eliF)",
+ root->next->ctime == 0);
return NULL;
}