summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/catalog.c327
-rw-r--r--tests/i18n.c23
-rw-r--r--tests/lib.c8
-rw-r--r--tests/logerr.c283
-rw-r--r--tests/random.c73
-rw-r--r--tests/testing.c43
6 files changed, 757 insertions, 0 deletions
diff --git a/tests/catalog.c b/tests/catalog.c
new file mode 100644
index 0000000..d357175
--- /dev/null
+++ b/tests/catalog.c
@@ -0,0 +1,327 @@
+#include "../src/catalog.c"
+
+#include "../src/testing.h"
+#include "slurp.h"
+
+
+static const char *const
+FNAME = __FILE__ ".txt";
+
+enum TEST_MSGCATALOG_ID {
+ MSG_X_FIRST = 1,
+ MSG_X_1,
+ MSG_X_2,
+ MSG_X_LAST,
+ MSG_STANDALONE,
+};
+
+static const char *const
+TEST_MSGS[] = {
+ "",
+ [MSG_X_FIRST]="First line\n",
+ [MSG_X_1]="a second\n",
+ [MSG_X_2]="a third\n",
+ [MSG_X_LAST]="and the last one\n",
+ [MSG_STANDALONE]="single line message\n",
+ NULL
+};
+
+static int
+test_i18n_init(void) {
+ int rc = 0;
+
+ test_start("i18n_init()");
+
+ {
+ testing("simple call without touching the environment");
+
+ const int should_overwrite = 1;
+ if (setenv(NLSPATH_KEY, "src/%N.en.cat", should_overwrite)) {
+ logerr("setenv(\"%s\", \"src/%%N.en.cat\", 1): %s\n",
+ NLSPATH_KEY, strerror(errno));
+ rc = -1;
+ goto out;
+ }
+
+ if (i18n_init()) {
+ logerr("i18n_init()\n");
+ rc = -1;
+ goto out;
+ }
+
+ test_ok();
+ }
+
+out:
+ if (i18n_destroy()) {
+ logerr("i18n_destroy()\n");
+ rc = -1;
+ }
+ return rc;
+}
+
+static int
+test_s_print_msgs(void) {
+ int rc = 0;
+
+ test_start("s_print_msgs()");
+ FILE *file = NULL;
+ char *str = NULL;
+
+ {
+ testing("message in range");
+
+ file = fopen(FNAME, "w");
+ if (!file) {
+ perror("fopen(FNAME, \"w\")");
+ rc = -1;
+ goto out;
+ }
+
+ if (s_print_msgs(TEST_MSGS, file, MSG_X_FIRST, MSG_X_LAST)) {
+ logerr("print_msgs(TEST_MSGS, file, MSG_X_FIRST, MSG_X_LAST)\n");
+ rc = -1;
+ goto out;
+ }
+
+ const int ret = fclose(file);
+ file = NULL;
+ if (ret) {
+ logerr("fclose(file): %s\n", strerror(errno));
+ rc = -1;
+ goto out;
+ }
+
+ if (slurp_for_tests(FNAME, &str)) {
+ logerr("slurp_for_tests(FNAME, &str)\n");
+ rc = -1;
+ goto out;
+ }
+
+ const char *const expected =
+ "First line\n"
+ "a second\n"
+ "a third\n"
+ "and the last one\n"
+ ;
+
+ assert(strcmp(expected, str) == 0);
+
+ free(str);
+ str = NULL;
+
+ test_ok();
+ }
+ {
+ testing("range begin and end is the same");
+
+ file = fopen(FNAME, "w");
+ if (!file) {
+ logerr("fopen(FNAME, \"w\"): %s\n", strerror(errno));
+ rc = -1;
+ goto out;
+ }
+
+ if (s_print_msgs(TEST_MSGS, file, MSG_X_FIRST, MSG_X_FIRST)) {
+ logerr("s_print_msgs(TEST_MSGS, file, MSG_X_FIRST, MSG_X_FIRST)\n");
+ rc = -1;
+ goto out;
+ }
+
+ const int ret = fclose(file);
+ file = NULL;
+ if (ret) {
+ logerr("fclose(file): %s\n", strerror(errno));
+ rc = -1;
+ goto out;
+ }
+
+ if (slurp_for_tests(FNAME, &str)) {
+ logerr("slurp_for_tests(FNAME, &str)\n");
+ rc = -1;
+ goto out;
+ }
+
+ const char *const expected =
+ "First line\n";
+
+ assert(strcmp(expected, str) == 0);
+
+ free(str);
+ str = NULL;
+
+ test_ok();
+ }
+
+out:
+ if (str) {
+ free(str);
+ }
+ if (file) {
+ if (fclose(file)) {
+ logerr("fclose(file): %s\n", strerror(errno));
+ rc = -1;
+ }
+ }
+ return rc;
+}
+
+static int
+test_s(void) {
+ int rc = 0;
+
+ test_start("_()");
+ FILE *file = NULL;
+ char *str = NULL;
+
+ {
+ testing("empty string");
+
+ file = fopen(FNAME, "w");
+ if (!file) {
+ perror("fopen(FNAME, \"w\")");
+ rc = -1;
+ goto out;
+ }
+
+ // FIXME: implement correct test
+
+
+
+ test_ok();
+ }
+
+out:
+ if (str) {
+ free(str);
+ }
+ if (file) {
+ if (fclose(file)) {
+ logerr("fclose(file): %s\n", strerror(errno));
+ rc = -1;
+ }
+ }
+ return rc;
+}
+
+static int
+test_i18n_destroy(void) {
+ int rc = 0;
+
+ test_start("i18n_destroy()");
+
+ {
+ testing("simple call without init first");
+
+ if (i18n_destroy()) {
+ logerr("i18n_destroy()\n");
+ rc = -1;
+ goto out;
+ }
+
+ test_ok();
+ }
+
+out:
+ return rc;
+}
+
+static int
+test_s_print_msg(void) {
+ int rc = 0;
+
+ test_start("s_print_msg()");
+ FILE *file = NULL;
+ char *str = NULL;
+
+ {
+ testing("simple individual message");
+
+ file = fopen(FNAME, "w");
+ if (!file) {
+ logerr("fopen(FNAME, \"w\"): %s\n");
+ rc = -1;
+ goto out;
+ }
+
+ if (s_print_msg(TEST_MSGS, file, MSG_STANDALONE)) {
+ logerr("s_print_msg(TEST_MSGS, file, MSG_STANDALONE)\n");
+ rc = -1;
+ goto out;
+ }
+
+ const int ret = fclose(file);
+ file = NULL;
+ if (ret) {
+ logerr("fopen(file): %s\n", strerror(errno));
+ rc = -1;
+ goto out;
+ }
+
+ if (slurp_for_tests(FNAME, &str)) {
+ logerr("slurp_for_tests(FNAME, &str)\n");
+ rc = -1;
+ goto out;
+ }
+
+ const char *const expected =
+ "single line message\n";
+
+ assert(strcmp(expected, str) == 0);
+
+ free(str);
+ str = NULL;
+
+ test_ok();
+ }
+
+out:
+ if (str) {
+ free(str);
+ }
+ if (file) {
+ if (fclose(file)) {
+ logerr("fclose(file): %s\n", strerror(errno));
+ rc = -1;
+ }
+ }
+ return rc;
+}
+
+int
+main(void) {
+ int rc = 0;
+
+ if (test_i18n_init()) {
+ logerr("test_i18n_init()\n");
+ rc = -1;
+ goto out;
+ }
+
+ if (test_i18n_destroy()) {
+ logerr("test_i18n_destroy()\n");
+ rc = -1;
+ goto out;
+ }
+
+ if (test_s()) {
+ logerr("test_s()\n");
+ rc = -1;
+ goto out;
+ }
+
+ if (test_s_print_msgs()) {
+ logerr("test_s_print_msgs()\n");
+ rc = -1;
+ goto out;
+ }
+
+ if (test_s_print_msg()) {
+ logerr("test_s_print_msg()\n");
+ rc = -1;
+ goto out;
+ }
+
+out:
+ return !!rc;
+}
diff --git a/tests/i18n.c b/tests/i18n.c
new file mode 100644
index 0000000..47a3247
--- /dev/null
+++ b/tests/i18n.c
@@ -0,0 +1,23 @@
+#include "../src/i18n.c"
+
+#include <stdlib.h>
+
+#include "../src/logerr.h"
+#include "../src/catalog.h"
+
+
+int
+main(void) {
+ int rc = 0;
+
+ if (getenv("DUMP_TRANSLATABLE_STRINGS")) {
+ if (dump_translatable_strings(MSGS)) {
+ logerr("dump_translatable_strings(MSGS)\n");
+ rc = -1;
+ goto out;
+ }
+ }
+
+out:
+ return !!rc;
+}
diff --git a/tests/lib.c b/tests/lib.c
new file mode 100644
index 0000000..14fdd4f
--- /dev/null
+++ b/tests/lib.c
@@ -0,0 +1,8 @@
+#include "../src/lib.c"
+
+
+int
+main(void) {
+ int rc = 0;
+ return rc;
+}
diff --git a/tests/logerr.c b/tests/logerr.c
new file mode 100644
index 0000000..78cdf10
--- /dev/null
+++ b/tests/logerr.c
@@ -0,0 +1,283 @@
+#include "../src/logerr.c"
+
+
+#include <assert.h>
+#include <errno.h>
+#include <string.h>
+
+#include "../src/testing.h"
+#include "slurp.h"
+
+
+static const char *const
+FNAME = __FILE__ ".txt";
+
+static int
+test_vlogerr(void) {
+ int rc = 0;
+
+ test_start("vlogerr()");
+ FILE *file = NULL;
+ char *str = NULL;
+
+ {
+ testing("empty varargs");
+
+ file = fopen(FNAME, "w");
+ if (!file) {
+ perror("fopen(FNAME, \"w\")");
+ rc = -1;
+ goto out;
+ }
+
+ #line 100
+ vlogerr(__FILE__, __func__, __LINE__, file,
+ "");
+
+ const int ret = fclose(file);
+ file = NULL;
+ if (ret) {
+ perror("fclose(file)");
+ rc = -1;
+ goto out;
+ }
+
+ if (slurp_for_tests(FNAME, &str)) {
+ perror("slurp_for_tests(FNAME, &str)");
+ rc = -1;
+ goto out;
+ }
+
+ const char *const expected =
+ "tests/logerr.c:test_vlogerr:100: ";
+ assert(strcmp(expected, str) == 0);
+
+ free(str);
+ str = NULL;
+
+ test_ok();
+ }
+ {
+ testing("a newline only");
+
+ file = fopen(FNAME, "w");
+ if (!file) {
+ perror("fopen(FNAME, \"w\")");
+ rc = -1;
+ goto out;
+ }
+
+ #line 200
+ vlogerr(__FILE__, __func__, __LINE__, file,
+ "\n");
+
+ const int ret = fclose(file);
+ file = NULL;
+ if (ret) {
+ perror("fclose(file)");
+ rc = -1;
+ goto out;
+ }
+
+ if (slurp_for_tests(FNAME, &str)) {
+ perror("slurp_for_tests(FNAME, &str)");
+ rc = -1;
+ goto out;
+ }
+
+ const char *const expected =
+ "tests/logerr.c:test_vlogerr:200: \n";
+ assert(strcmp(expected, str) == 0);
+
+ free(str);
+ str = NULL;
+
+ test_ok();
+ }
+ {
+ testing("static format string");
+
+ file = fopen(FNAME, "w");
+ if (!file) {
+ perror("fopen(FNAME, \"w\")");
+ rc = -1;
+ goto out;
+ }
+
+ #line 300
+ vlogerr(__FILE__, __func__, __LINE__, file,
+ "some static string\n");
+
+ const int ret = fclose(file);
+ file = NULL;
+ if (ret) {
+ perror("fclose(file)");
+ rc = -1;
+ goto out;
+ }
+
+ if (slurp_for_tests(FNAME, &str)) {
+ perror("slurp_for_tests(FNAME, &str)");
+ rc = -1;
+ goto out;
+ }
+
+ const char *const expected =
+ "tests/logerr.c:test_vlogerr:300: some static string\n";
+ assert(strcmp(expected, str) == 0);
+
+ free(str);
+ str = NULL;
+
+ test_ok();
+ }
+ {
+ testing("single arg format string");
+
+ file = fopen(FNAME, "w");
+ if (!file) {
+ perror("fopen(FNAME, \"w\")");
+ rc = -1;
+ goto out;
+ }
+
+ #line 400
+ vlogerr(__FILE__, __func__, __LINE__, file,
+ "fn(%s)\n", "an-arg");
+
+ const int ret = fclose(file);
+ file = NULL;
+ if (ret) {
+ perror("fclose(file)");
+ rc = -1;
+ goto out;
+ }
+
+ if (slurp_for_tests(FNAME, &str)) {
+ perror("slurp_for_tests(FNAME, &str)");
+ rc = -1;
+ goto out;
+ }
+
+ const char *const expected =
+ "tests/logerr.c:test_vlogerr:400: fn(an-arg)\n";
+ assert(strcmp(expected, str) == 0);
+
+ free(str);
+ str = NULL;
+
+ test_ok();
+ }
+ {
+ testing("multiple format strings");
+
+ file = fopen(FNAME, "w");
+ if (!file) {
+ perror("fopen(FNAME, \"w\")");
+ rc = -1;
+ goto out;
+ }
+
+ #line 500
+ vlogerr(__FILE__, __func__, __LINE__, file,
+ "int (%d), string (%s) and char (%c)\n",
+ 123,
+ "another-str",
+ 'z');
+
+ const int ret = fclose(file);
+ file = NULL;
+ if (ret) {
+ perror("fclose(file)");
+ rc = -1;
+ goto out;
+ }
+
+ if (slurp_for_tests(FNAME, &str)) {
+ perror("slurp_for_tests(FNAME, &str)");
+ rc = -1;
+ goto out;
+ }
+
+ const char *const expected =
+ "tests/logerr.c:test_vlogerr:500: "
+ "int (123), string (another-str) and char (z)\n";
+ assert(strcmp(expected, str) == 0);
+
+ free(str);
+ str = NULL;
+
+ test_ok();
+ }
+
+out:
+ if (str) {
+ free(str);
+ }
+ if (file) {
+ if (fclose(file)) {
+ perror("fclose(file)");
+ rc = -1;
+ }
+ }
+ return rc;
+}
+
+static int
+test_logerr(void) {
+ int rc = 0;
+
+ test_start("logerr()");
+
+ {
+ testing("can be called with an empty string");
+
+ logerr("");
+
+ test_ok();
+ }
+ {
+ testing("can be called with a static string");
+
+ logerr("some err\n");
+
+ test_ok();
+ }
+ {
+ testing("can be called with a formatted string");
+
+ logerr("some err: %s\n", strerror(errno));
+
+ test_ok();
+ }
+ {
+ testing("can be called with formatting arguments");
+
+ logerr("int: %d\nstr: %s\n", 123, "an example string");
+
+ test_ok();
+ }
+
+ return rc;
+}
+
+
+int
+main(void) {
+ int rc = 0;
+
+ if (test_vlogerr()) {
+ perror("test_vlogerr()");
+ rc = -1;
+ goto out;
+ }
+
+ if (test_logerr()) {
+ perror("test_logerr()");
+ rc = -1;
+ goto out;
+ }
+
+out:
+ return !!rc;
+}
diff --git a/tests/random.c b/tests/random.c
new file mode 100644
index 0000000..eec83c5
--- /dev/null
+++ b/tests/random.c
@@ -0,0 +1,73 @@
+#include "../src/random.c"
+#include "../src/testing.h"
+
+
+static int
+test_urandom_bytes(void) {
+ int rc = 0;
+
+ test_start("urandom_bytes()");
+
+ {
+ testing("we get to pick the size that comes out");
+
+ const size_t LEN = 256;
+ uint8_t arr[256 /* LEN */] = { 0 };
+
+ for (size_t n = 0; n < LEN; n++) {
+ if (urandom_bytes(n, &arr)) {
+ logerr("urandom_bytes(n, &arr);\n");
+ rc = -1;
+ goto out;
+ }
+ for (size_t i = n; i < LEN; i++) {
+ assert(arr[i] == 0);
+ }
+ }
+
+ test_ok();
+ }
+
+ {
+ testing("we always get a new value as a result");
+
+ const size_t LEN = 64;
+ uint8_t arr1[64 /* LEN */] = { 0 };
+ uint8_t arr2[64 /* LEN */] = { 0 };
+
+ if (urandom_bytes(LEN, &arr1)) {
+ logerr("urandom_bytes(LEN, &arr1);\n");
+ rc = -1;
+ goto out;
+ }
+
+ const size_t attempts = 10;
+ for (size_t n = 0; n < attempts; n++) {
+ if (urandom_bytes(LEN, &arr2)) {
+ logerr("urandom_bytes(LEN, &arr2);\n");
+ rc = -1;
+ goto out;
+ }
+ assert(memcmp(arr1, arr2, LEN) != 0);
+ }
+
+ test_ok();
+ }
+
+out:
+ return rc;
+}
+
+int
+main(void) {
+ int rc = 0;
+
+ if (test_urandom_bytes()) {
+ logerr("test_urandom_bytes();\n");
+ rc = -1;
+ goto out;
+ }
+
+out:
+ return !!rc;
+}
diff --git a/tests/testing.c b/tests/testing.c
new file mode 100644
index 0000000..65012f9
--- /dev/null
+++ b/tests/testing.c
@@ -0,0 +1,43 @@
+#include "../src/testing.c"
+
+
+int
+main(void) {
+ int rc = 0;
+
+ test_start("testing.c");
+ const int should_overwrite = 1;
+
+ if (unsetenv(ENVVAR_NAME)) {
+ perror("unsetenv(\"NO_COLOR\")");
+ rc = -1;
+ goto out;
+ }
+ {
+ testing("unset NO_COLOR");
+ test_ok();
+ }
+
+ if (setenv(ENVVAR_NAME, "", should_overwrite)) {
+ perror("setenv(\"NO_COLOR\", \"\", 1)");
+ rc = -1;
+ goto out;
+ }
+ {
+ testing("empty NO_COLOR");
+ test_ok();
+ }
+
+ if (setenv(ENVVAR_NAME, "something", should_overwrite)) {
+ perror("setenv(\"NO_COLOR\", \"something\", 1)");
+ rc = -1;
+ goto out;
+ }
+ {
+ testing("defined NO_COLOR");
+ test_ok();
+ }
+
+out:
+ return !!rc;
+}