aboutsummaryrefslogtreecommitdiff
path: root/test/src
diff options
context:
space:
mode:
Diffstat (limited to 'test/src')
-rw-r--r--test/src/gen/test_point.c2
-rw-r--r--test/src/gen/test_seed.c37
-rw-r--r--test/src/io/test_cli.c25
-rw-r--r--test/src/io/test_input.c54
-rw-r--r--test/src/math/test_subgroups.c2
-rw-r--r--test/src/test/default.c (renamed from test/src/test/utils.c)8
-rw-r--r--test/src/test/default.h (renamed from test/src/test/utils.h)6
-rw-r--r--test/src/test/input.c27
-rw-r--r--test/src/test/input.h16
-rw-r--r--test/src/test/output.c49
-rw-r--r--test/src/test/output.h18
-rw-r--r--test/src/util/test_random.c16
12 files changed, 194 insertions, 66 deletions
diff --git a/test/src/gen/test_point.c b/test/src/gen/test_point.c
index 48872be..d77f83d 100644
--- a/test/src/gen/test_point.c
+++ b/test/src/gen/test_point.c
@@ -5,7 +5,7 @@
#include <criterion/criterion.h>
#include "gen/point.h"
-#include "test/utils.h"
+#include "test/default.h"
TestSuite(point, .init = default_setup, .fini = default_teardown);
diff --git a/test/src/gen/test_seed.c b/test/src/gen/test_seed.c
new file mode 100644
index 0000000..28c24d3
--- /dev/null
+++ b/test/src/gen/test_seed.c
@@ -0,0 +1,37 @@
+/*
+ * ecgen, tool for generating Elliptic curve domain parameters
+ * Copyright (C) 2017 J08nY
+ */
+
+#include <criterion/criterion.h>
+#include "gen/seed.h"
+#include "gen/types.h"
+#include "test/default.h"
+
+TestSuite(seed, .init = default_setup, .fini = default_teardown);
+
+Test(seed, test_seed_random) {
+ curve_t curve = {};
+ config_t cfg = {};
+ int ret = seed_gen_random(&curve, &cfg, NULL);
+
+ cr_assert_eq(ret, 1, );
+ cr_assert_not_null(curve.seed, );
+ cr_assert_str_eq(pari_sprintf("%Px", curve.seed->seed), curve.seed->raw, );
+ cr_assert_eq(strlen(curve.seed->raw), curve.seed->raw_len, );
+}
+
+Test(seed, test_seed_argument) {
+ curve_t curve = {};
+ char *seed = "abcdefabcdefabcdefab";
+ config_t cfg = {.seed = seed};
+ int ret = seed_gen_argument(&curve, &cfg, NULL);
+
+ cr_assert_eq(ret, 1, );
+ cr_assert_not_null(curve.seed, );
+ cr_assert_str_eq(curve.seed->raw, seed, );
+ cr_assert_str_eq(pari_sprintf("%Px", curve.seed->seed), curve.seed->raw, );
+ cr_assert_eq(strlen(curve.seed->raw), curve.seed->raw_len, );
+}
+
+Test(seed, test_seed_input) {} \ No newline at end of file
diff --git a/test/src/io/test_cli.c b/test/src/io/test_cli.c
index c90bbfa..1c7a208 100644
--- a/test/src/io/test_cli.c
+++ b/test/src/io/test_cli.c
@@ -3,26 +3,25 @@
* Copyright (C) 2017 J08nY
*/
-#include "test/utils.h"
-#include "io/cli.h"
-#include "io/config.h"
#include <criterion/criterion.h>
#include <unistd.h>
+#include "io/cli.h"
+#include "io/config.h"
+#include "test/default.h"
static struct argp test_argp = {cli_options, cli_parse, cli_args_doc,
- cli_doc, 0, cli_filter};
+ cli_doc, 0, cli_filter};
TestSuite(cli, .init = default_setup, .fini = default_teardown);
-
Test(cli, test_memory) {
int argc = 4;
char *argv[] = {"ecgen", "--memory=2k", "--fp", "1"};
config_t cfg;
memset(&cfg, 0, sizeof(cfg));
int ret = argp_parse(&test_argp, argc, argv, 0, 0, &cfg);
- cr_assert_eq(ret, 0,);
- cr_assert_eq(cfg.memory, 2000,);
+ cr_assert_eq(ret, 0, );
+ cr_assert_eq(cfg.memory, 2000, );
}
Test(cli, test_thread_memory) {
@@ -31,8 +30,8 @@ Test(cli, test_thread_memory) {
config_t cfg;
memset(&cfg, 0, sizeof(cfg));
int ret = argp_parse(&test_argp, argc, argv, 0, 0, &cfg);
- cr_assert_eq(ret, 0,);
- cr_assert_eq(cfg.thread_memory, 2000,);
+ cr_assert_eq(ret, 0, );
+ cr_assert_eq(cfg.thread_memory, 2000, );
}
Test(cli, test_threads) {
@@ -41,8 +40,8 @@ Test(cli, test_threads) {
config_t cfg;
memset(&cfg, 0, sizeof(cfg));
int ret = argp_parse(&test_argp, argc, argv, 0, 0, &cfg);
- cr_assert_eq(ret, 0,);
- cr_assert_eq(cfg.threads, 2,);
+ cr_assert_eq(ret, 0, );
+ cr_assert_eq(cfg.threads, 2, );
}
Test(cli, test_auto_threads) {
@@ -51,6 +50,6 @@ Test(cli, test_auto_threads) {
config_t cfg;
memset(&cfg, 0, sizeof(cfg));
int ret = argp_parse(&test_argp, argc, argv, 0, 0, &cfg);
- cr_assert_eq(ret, 0,);
- cr_assert_eq(cfg.threads, sysconf(_SC_NPROCESSORS_ONLN),);
+ cr_assert_eq(ret, 0, );
+ cr_assert_eq(cfg.threads, sysconf(_SC_NPROCESSORS_ONLN), );
} \ No newline at end of file
diff --git a/test/src/io/test_input.c b/test/src/io/test_input.c
index de4ac0f..d32d71c 100644
--- a/test/src/io/test_input.c
+++ b/test/src/io/test_input.c
@@ -3,92 +3,78 @@
* Copyright (C) 2017 J08nY
*/
-#include "test/utils.h"
-#include "io/input.h"
-#include "io/output.h"
#include <criterion/criterion.h>
+#include "io/input.h"
+#include "test/default.h"
+#include "test/input.h"
+#include "test/output.h"
-static FILE *write_in;
-
-void input_setup() {
+void input_suite_setup(void) {
default_setup();
- config_t cfg;
- memset(&cfg, 0, sizeof(cfg));
-
- cfg.output = "/dev/null";
- input_init(&cfg);
- output_init(&cfg);
-
- int in_fd[2];
- pipe(in_fd);
-
- write_in = fdopen(in_fd[1], "w");
- setlinebuf(write_in);
- in = fdopen(in_fd[0], "r");
- err = fopen("/dev/null", "w");
+ input_setup();
+ output_setup();
}
-void input_teardown() {
+void input_suite_teardown(void) {
default_teardown();
- input_quit();
- output_quit();
- fclose(write_in);
+ input_teardown();
+ output_teardown();
}
-TestSuite(input, .init = input_setup, .fini = input_teardown);
+TestSuite(input, .init = input_suite_setup, .fini = input_suite_teardown);
Test(input, test_prime) {
fprintf(write_in, "5\n");
GEN p = input_prime(NULL, 10);
- cr_assert(gequal(p, stoi(5)),);
+ cr_assert(gequal(p, stoi(5)), );
}
Test(input, test_prime_nan) {
fprintf(write_in, "....\n");
GEN p = input_prime(NULL, 10);
- cr_assert(gequal(p, gen_m1),);
+ cr_assert(gequal(p, gen_m1), );
}
Test(input, test_prime_nonprime) {
fprintf(write_in, "6\n");
GEN p = input_prime(NULL, 10);
- cr_assert(gequal(p, gen_m1),);
+ cr_assert(gequal(p, gen_m1), );
}
Test(input, test_prime_newline) {
fprintf(write_in, "\n");
GEN p = input_prime(NULL, 10);
- cr_assert(gequal(p, gen_m1),);
+ cr_assert(gequal(p, gen_m1), );
}
Test(input, test_int) {
fprintf(write_in, "256\n");
GEN i = input_int(NULL, 10);
- cr_assert(gequal(i, stoi(256)),);
+ cr_assert(gequal(i, stoi(256)), );
}
Test(input, test_int_too_big) {
fprintf(write_in, "256\n");
GEN i = input_int(NULL, 4);
- cr_assert(gequal(i, gen_m1),);
+ cr_assert(gequal(i, gen_m1), );
}
Test(input, test_int_newline) {
fprintf(write_in, "\n");
GEN i = input_int(NULL, 4);
- cr_assert(gequal(i, gen_m1),);
+ cr_assert(gequal(i, gen_m1), );
}
Test(input, test_str) {
fprintf(write_in, "something\n");
GEN s = input_string(NULL);
GEN expected = strtoGENstr("something");
- cr_assert(gequal(s, expected),);
+ cr_assert(gequal(s, expected), );
}
Test(input, test_str_newline) {
fprintf(write_in, "\n");
GEN s = input_string(NULL);
GEN expected = strtoGENstr("");
- cr_assert(gequal(s, expected),);
+ cr_assert(gequal(s, expected), );
}
diff --git a/test/src/math/test_subgroups.c b/test/src/math/test_subgroups.c
index b9368d0..8a3c8da 100644
--- a/test/src/math/test_subgroups.c
+++ b/test/src/math/test_subgroups.c
@@ -5,7 +5,7 @@
#include <criterion/criterion.h>
#include "gen/point.h"
#include "math/subgroups.h"
-#include "test/utils.h"
+#include "test/default.h"
TestSuite(subgroups, .init = default_setup, .fini = default_teardown);
diff --git a/test/src/test/utils.c b/test/src/test/default.c
index a5de092..665edbf 100644
--- a/test/src/test/utils.c
+++ b/test/src/test/default.c
@@ -2,17 +2,15 @@
* ecgen, tool for generating Elliptic curve domain parameters
* Copyright (C) 2017 J08nY
*/
-#include "utils.h"
+#include "default.h"
#include <criterion/criterion.h>
#include <pari/pari.h>
-static void *cr_simple_calloc(size_t size) {
- return cr_calloc(1, size);
-}
+static void *cr_simple_calloc(size_t size) { return cr_calloc(1, size); }
void default_setup(void) {
pari_init(1000000, 1000000);
- //set_mem_funcs(cr_malloc, cr_simple_calloc, cr_realloc, cr_free);
+ // set_mem_funcs(cr_malloc, cr_simple_calloc, cr_realloc, cr_free);
}
void default_teardown(void) { pari_close(); } \ No newline at end of file
diff --git a/test/src/test/utils.h b/test/src/test/default.h
index 2780bd2..12ee4cb 100644
--- a/test/src/test/utils.h
+++ b/test/src/test/default.h
@@ -2,11 +2,11 @@
* ecgen, tool for generating Elliptic curve domain parameters
* Copyright (C) 2017 J08nY
*/
-#ifndef ECGEN_UTILS_H
-#define ECGEN_UTILS_H
+#ifndef ECGEN_TEST_DEFAULT_H
+#define ECGEN_TEST_DEFAULT_H
void default_setup(void);
void default_teardown(void);
-#endif //ECGEN_UTILS_H
+#endif // ECGEN_UTILS_H
diff --git a/test/src/test/input.c b/test/src/test/input.c
new file mode 100644
index 0000000..f171ca8
--- /dev/null
+++ b/test/src/test/input.c
@@ -0,0 +1,27 @@
+/*
+ * ecgen, tool for generating Elliptic curve domain parameters
+ * Copyright (C) 2017 J08nY
+ */
+#include "input.h"
+#include "io/input.h"
+
+FILE *write_in;
+
+void input_setup(void) {
+ config_t cfg;
+ memset(&cfg, 0, sizeof(cfg));
+ input_init(&cfg);
+
+ int in_fd[2];
+ pipe(in_fd);
+
+ write_in = fdopen(in_fd[1], "w");
+ setlinebuf(write_in);
+ in = fdopen(in_fd[0], "r");
+ setlinebuf(in);
+}
+
+void input_teardown(void) {
+ input_quit();
+ fclose(write_in);
+} \ No newline at end of file
diff --git a/test/src/test/input.h b/test/src/test/input.h
new file mode 100644
index 0000000..5e57c01
--- /dev/null
+++ b/test/src/test/input.h
@@ -0,0 +1,16 @@
+/*
+ * ecgen, tool for generating Elliptic curve domain parameters
+ * Copyright (C) 2017 J08nY
+ */
+#ifndef ECGEN_TEST_INPUT_H
+#define ECGEN_TEST_INPUT_H
+
+#include <stdio.h>
+
+extern FILE *write_in;
+
+void input_setup(void);
+
+void input_teardown(void);
+
+#endif // ECGEN_TEST_INPUT_H
diff --git a/test/src/test/output.c b/test/src/test/output.c
new file mode 100644
index 0000000..0ecdf43
--- /dev/null
+++ b/test/src/test/output.c
@@ -0,0 +1,49 @@
+/*
+ * ecgen, tool for generating Elliptic curve domain parameters
+ * Copyright (C) 2017 J08nY
+ */
+#include "output.h"
+#include "gen/types.h"
+#include "io/output.h"
+
+FILE *read_out = NULL;
+FILE *read_err = NULL;
+FILE *read_verbose = NULL;
+
+static void setup_stream(FILE **original_out, FILE **redirected_out) {
+ int fd[2];
+ pipe(fd);
+
+ *redirected_out = fdopen(fd[0], "r");
+ setlinebuf(*redirected_out);
+ *original_out = fdopen(fd[1], "w");
+ setlinebuf(*original_out);
+}
+
+void output_setup(void) {
+ config_t cfg;
+ memset(&cfg, 0, sizeof(cfg));
+ output_init(&cfg);
+
+ int in_fd[2];
+ pipe(in_fd);
+
+ setup_stream(&out, &read_out);
+ setup_stream(&err, &read_err);
+ setup_stream(&verbose, &read_verbose);
+}
+
+void output_teardown(void) {
+ if (read_out) {
+ fclose(out);
+ fclose(read_out);
+ }
+ if (read_err) {
+ fclose(err);
+ fclose(read_err);
+ }
+ if (read_verbose) {
+ fclose(verbose);
+ fclose(read_verbose);
+ }
+}
diff --git a/test/src/test/output.h b/test/src/test/output.h
new file mode 100644
index 0000000..733cb5c
--- /dev/null
+++ b/test/src/test/output.h
@@ -0,0 +1,18 @@
+/*
+ * ecgen, tool for generating Elliptic curve domain parameters
+ * Copyright (C) 2017 J08nY
+ */
+#ifndef ECGEN_TEST_OUTPUT_H
+#define ECGEN_TEST_OUTPUT_H
+
+#include <stdio.h>
+
+extern FILE *read_out;
+extern FILE *read_err;
+extern FILE *read_verbose;
+
+void output_setup(void);
+
+void output_teardown(void);
+
+#endif // ECGEN_TEST_OUTPUT_H
diff --git a/test/src/util/test_random.c b/test/src/util/test_random.c
index 5e7f854..bb632a6 100644
--- a/test/src/util/test_random.c
+++ b/test/src/util/test_random.c
@@ -3,9 +3,9 @@
* Copyright (C) 2017 J08nY
*/
-#include "test/utils.h"
-#include "util/random.h"
#include <criterion/criterion.h>
+#include "test/default.h"
+#include "util/random.h"
void random_setup() {
default_setup();
@@ -17,18 +17,16 @@ TestSuite(random, .init = random_setup, .fini = default_teardown);
Test(random, test_random_prime) {
for (size_t i = 0; i < 100; ++i) {
GEN p = random_prime(10);
- cr_assert(isprime(p),);
- cr_assert_lt(gcmp(p, int2n(10)), 0,);
- cr_assert_gt(gcmp(p, int2n(9)), 0,);
+ cr_assert(isprime(p), );
+ cr_assert_leq(cmpii(p, int2n(10)), 0, );
+ cr_assert_geq(cmpii(p, int2n(9)), 0, );
}
}
Test(random, test_random_int) {
for (size_t i = 0; i < 100; ++i) {
GEN j = random_int(10);
- cr_assert_lt(gcmp(j, int2n(10)), 0,);
- cr_assert_gt(gcmp(j, int2n(9)), 0,);
+ cr_assert_leq(cmpii(j, int2n(10)), 0, );
+ cr_assert_geq(cmpii(j, int2n(9)), 0, );
}
}
-
-