aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/exhaustive/exhaustive.c3
-rw-r--r--src/gen/field.c3
-rw-r--r--src/gen/seed.c51
-rw-r--r--src/io/input.c16
-rw-r--r--src/io/output.c41
-rw-r--r--src/util/memory.c13
-rw-r--r--src/util/memory.h4
-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
19 files changed, 258 insertions, 133 deletions
diff --git a/src/exhaustive/exhaustive.c b/src/exhaustive/exhaustive.c
index 6b77f3a..3553efb 100644
--- a/src/exhaustive/exhaustive.c
+++ b/src/exhaustive/exhaustive.c
@@ -176,8 +176,7 @@ int exhaustive_gen_retry(curve_t *curve, const config_t *cfg,
if (diff <= 0) {
if (diff == INT_MIN || state + diff < 0) {
- fprintf(err, "Error generating a curve. state = %i\n",
- state);
+ fprintf(err, "Error generating a curve. state = %i\n", state);
return 0;
}
// record try
diff --git a/src/gen/field.c b/src/gen/field.c
index 482bcc5..9a908cc 100644
--- a/src/gen/field.c
+++ b/src/gen/field.c
@@ -13,7 +13,8 @@ static GEN field_binaryr(unsigned long bits) {
if (poly_exists(bits)) {
return poly_find_gen(bits);
} else {
- fprintf(err, "Unable to find a suitable binary field. Use an explicit one.");
+ fprintf(err,
+ "Unable to find a suitable binary field. Use an explicit one.");
exit(1);
}
}
diff --git a/src/gen/seed.c b/src/gen/seed.c
index 505493a..49a33ff 100644
--- a/src/gen/seed.c
+++ b/src/gen/seed.c
@@ -4,6 +4,7 @@
*/
#include "seed.h"
+#include <io/config.h>
#include "io/output.h"
#include "util/memory.h"
@@ -41,33 +42,34 @@ void seed_free(seed_t **seed) {
static GEN seed_stoi(const char *cstr) {
pari_sp ltop = avma;
- GEN seed = gen_0;
size_t len = strlen(cstr);
- for (size_t i = 0; i < len; ++i) {
- pari_sp btop = avma;
- GEN s = stoi(cstr[i]);
- s = shifti(s, (len - i - 1) * 8);
- seed = addii(seed, s);
- gerepileall(btop, 1, &seed);
+ char *seed_str;
+ if (len <= 3 || !(cstr[0] == '0' && (cstr[1] == 'x' || cstr[1] == 'X'))) {
+ seed_str = try_malloc((size_t)(len + 3));
+ strncpy(seed_str + 2, cstr, len);
+ seed_str[0] = '0';
+ seed_str[1] = 'x';
+ seed_str[len + 2] = 0;
+ } else {
+ seed_str = try_malloc(len + 1);
+ strncpy(seed_str, cstr, len);
}
+ GEN i = strtoi(seed_str);
- return gerepilecopy(ltop, seed);
+ return gerepilecopy(ltop, i);
}
static char *seed_itos(GEN seed) {
pari_sp ltop = avma;
- GEN bits = binary_zv(seed);
+ char *result = pari_sprintf("%Px", seed);
- long len = glength(bits);
- long bytes = (len / 8) + (len % 8 == 0 ? 0 : 1);
- char *result = try_malloc((size_t)bytes);
+ size_t seed_len = strlen(result);
+ char *seed_str = try_malloc(seed_len + 1);
+ strcpy(seed_str, result);
- for (long i = 0; i < len; ++i) {
- // TODO
- }
avma = ltop;
- return result;
+ return seed_str;
}
GENERATOR(seed_gen_random) {
@@ -81,8 +83,13 @@ GENERATOR(seed_gen_random) {
GENERATOR(seed_gen_argument) {
curve->seed = seed_new();
curve->seed->seed = seed_stoi(cfg->seed);
- curve->seed->raw = cfg->seed;
- curve->seed->raw_len = strlen(cfg->seed);
+
+ size_t seed_len = strlen(cfg->seed);
+ char *seed = try_malloc(seed_len + 1);
+ strcpy(seed, cfg->seed);
+
+ curve->seed->raw = seed;
+ curve->seed->raw_len = seed_len;
return 1;
}
@@ -96,10 +103,16 @@ GENERATOR(seed_gen_input) {
avma = ltop;
return 0;
}
-
GEN seed = seed_stoi(cstr);
curve->seed = seed_new();
curve->seed->seed = gerepilecopy(ltop, seed);
+
+ size_t seed_len = strlen(cstr);
+ char *seed_str = try_malloc(seed_len + 1);
+ strcpy(seed_str, cfg->seed);
+
+ curve->seed->raw = seed_str;
+ curve->seed->raw_len = seed_len;
return 1;
}
diff --git a/src/io/input.c b/src/io/input.c
index 2c0398a..f86afe4 100644
--- a/src/io/input.c
+++ b/src/io/input.c
@@ -26,20 +26,8 @@ static GEN input_i(const char *prompt, unsigned long bits) {
free(line);
return gen_m1;
}
- for (size_t i = 0, j = 0; (line[j] = line[i]); j += !isspace(line[i++]));
-
-/* if (len <= 3 || !(line[0] == '0' && (line[1] == 'x' || line[1] == 'X'))) {
- char *new_line = try_realloc(line, (size_t)(len + 2));
- memmove(new_line + 2, new_line, (size_t)len);
- new_line[0] = '0';
- new_line[1] = 'x';
- if (!feof(in)) {
- new_line[len + 1] = 0;
- } else {
- new_line[len + 2] = 0;
- }
- line = new_line;
- }*/
+ for (size_t i = 0, j = 0; (line[j] = line[i]); j += !isspace(line[i++]))
+ ;
pari_sp ltop = avma;
GEN in = strtoi(line);
diff --git a/src/io/output.c b/src/io/output.c
index 2d05bec..4cabafd 100644
--- a/src/io/output.c
+++ b/src/io/output.c
@@ -29,23 +29,23 @@ char *output_scsv(curve_t *curve, const config_t *cfg) {
switch (cfg->field) {
case FIELD_PRIME:
params[OFFSET_FIELD] =
- pari_sprintf("%P0#*x", cfg->hex_digits, curve->field);
+ pari_sprintf("%P0#*x", cfg->hex_digits, curve->field);
break;
case FIELD_BINARY: {
GEN field = field_params(curve->field);
params[OFFSET_FIELD] =
- pari_sprintf("%P#x,%P#x,%P#x,%P#x", gel(field, 1),
- gel(field, 2), gel(field, 3), gel(field, 4));
+ pari_sprintf("%P#x,%P#x,%P#x,%P#x", gel(field, 1),
+ gel(field, 2), gel(field, 3), gel(field, 4));
break;
}
}
if (curve->a)
params[OFFSET_A] =
- pari_sprintf("%P0#*x", cfg->hex_digits, field_elementi(curve->a));
+ pari_sprintf("%P0#*x", cfg->hex_digits, field_elementi(curve->a));
if (curve->b)
params[OFFSET_B] =
- pari_sprintf("%P0#*x", cfg->hex_digits, field_elementi(curve->b));
+ pari_sprintf("%P0#*x", cfg->hex_digits, field_elementi(curve->b));
if (curve->generators) {
char *gens[curve->ngens];
@@ -55,8 +55,8 @@ char *output_scsv(curve_t *curve, const config_t *cfg) {
GEN x = field_elementi(gel(generator->point, 1));
GEN y = field_elementi(gel(generator->point, 2));
gens[i] = pari_sprintf("%P0#*x,%P0#*x,%P#x,%P#x", cfg->hex_digits,
- x, cfg->hex_digits, y, generator->order,
- generator->cofactor);
+ x, cfg->hex_digits, y, generator->order,
+ generator->cofactor);
len += strlen(gens[i]);
}
size_t lenn = sizeof(char) * (len + curve->ngens);
@@ -70,7 +70,7 @@ char *output_scsv(curve_t *curve, const config_t *cfg) {
if (curve->order)
params[OFFSET_ORDER] =
- pari_sprintf("%P0#*x", cfg->hex_digits, curve->order);
+ pari_sprintf("%P0#*x", cfg->hex_digits, curve->order);
if (curve->points) {
char *points[curve->npoints];
@@ -80,7 +80,7 @@ char *output_scsv(curve_t *curve, const config_t *cfg) {
GEN x = field_elementi(gel(point->point, 1));
GEN y = field_elementi(gel(point->point, 2));
points[i] = pari_sprintf("%P0#*x,%P0#*x,%P#x", cfg->hex_digits, x,
- cfg->hex_digits, y, point->order);
+ cfg->hex_digits, y, point->order);
len += strlen(points[i]);
}
size_t lenn = sizeof(char) * (len + curve->npoints);
@@ -150,7 +150,8 @@ static JSON_Value *output_jjson(curve_t *curve, const config_t *cfg) {
pari_free(e3);
break;
}
- default: fprintf(err, "Error, field has unknown amount of elements.\n");
+ default:
+ fprintf(err, "Error, field has unknown amount of elements.\n");
exit(1);
}
@@ -172,13 +173,13 @@ static JSON_Value *output_jjson(curve_t *curve, const config_t *cfg) {
JSON_Object *point_object = json_value_get_object(point_value);
char *x = pari_sprintf(
- "%P0#*x", cfg->hex_digits,
- field_elementi(gel(curve->generators[i]->point, 1)));
+ "%P0#*x", cfg->hex_digits,
+ field_elementi(gel(curve->generators[i]->point, 1)));
json_object_set_string(point_object, "x", x);
pari_free(x);
char *y = pari_sprintf(
- "%P0#*x", cfg->hex_digits,
- field_elementi(gel(curve->generators[i]->point, 2)));
+ "%P0#*x", cfg->hex_digits,
+ field_elementi(gel(curve->generators[i]->point, 2)));
json_object_set_string(point_object, "y", y);
pari_free(y);
char *p_order = pari_sprintf("%P#x", curve->generators[i]->order);
@@ -186,7 +187,7 @@ static JSON_Value *output_jjson(curve_t *curve, const config_t *cfg) {
pari_free(p_order);
if (curve->generators[i]->cofactor) {
char *cofactor =
- pari_sprintf("%P#x", curve->generators[i]->cofactor);
+ pari_sprintf("%P#x", curve->generators[i]->cofactor);
json_object_set_string(point_object, "cofactor", cofactor);
pari_free(cofactor);
}
@@ -206,13 +207,13 @@ static JSON_Value *output_jjson(curve_t *curve, const config_t *cfg) {
JSON_Object *point_object = json_value_get_object(point_value);
char *x =
- pari_sprintf("%P0#*x", cfg->hex_digits,
- field_elementi(gel(curve->points[i]->point, 1)));
+ pari_sprintf("%P0#*x", cfg->hex_digits,
+ field_elementi(gel(curve->points[i]->point, 1)));
json_object_set_string(point_object, "x", x);
pari_free(x);
char *y =
- pari_sprintf("%P0#*x", cfg->hex_digits,
- field_elementi(gel(curve->points[i]->point, 2)));
+ pari_sprintf("%P0#*x", cfg->hex_digits,
+ field_elementi(gel(curve->points[i]->point, 2)));
json_object_set_string(point_object, "y", y);
pari_free(y);
char *p_order = pari_sprintf("%P#x", curve->points[i]->order);
@@ -220,7 +221,7 @@ static JSON_Value *output_jjson(curve_t *curve, const config_t *cfg) {
pari_free(p_order);
if (curve->points[i]->cofactor) {
char *cofactor =
- pari_sprintf("%P#x", curve->points[i]->cofactor);
+ pari_sprintf("%P#x", curve->points[i]->cofactor);
json_object_set_string(point_object, "cofactor", cofactor);
pari_free(cofactor);
}
diff --git a/src/util/memory.c b/src/util/memory.c
index 53f68c9..b506011 100644
--- a/src/util/memory.c
+++ b/src/util/memory.c
@@ -5,7 +5,6 @@
#include "memory.h"
#include <pari/pari.h>
-
static void *(*malloc_func)(size_t) = pari_malloc;
static void *(*calloc_func)(size_t) = pari_calloc;
@@ -25,9 +24,7 @@ void *alloc(void *(*fun)(size_t), size_t size) {
void *try_malloc(size_t size) { return alloc(malloc_func, size); }
-void *try_calloc(size_t size) {
- return alloc(calloc_func, size);
-}
+void *try_calloc(size_t size) { return alloc(calloc_func, size); }
void *try_realloc(void *ptr, size_t size) {
void *result = realloc_func(ptr, size);
@@ -38,13 +35,11 @@ void *try_realloc(void *ptr, size_t size) {
return result;
}
-void try_free(void *ptr) {
- free_func(ptr);
-}
+void try_free(void *ptr) { free_func(ptr); }
void set_mem_funcs(void *(*malloc_fun)(size_t), void *(*calloc_fun)(size_t),
- void *(*realloc_fun)(void *, size_t),
- void(*free_fun)(void *)) {
+ void *(*realloc_fun)(void *, size_t),
+ void (*free_fun)(void *)) {
malloc_func = malloc_fun;
calloc_func = calloc_fun;
realloc_func = realloc_fun;
diff --git a/src/util/memory.h b/src/util/memory.h
index 7070430..408052f 100644
--- a/src/util/memory.h
+++ b/src/util/memory.h
@@ -46,7 +46,7 @@ void try_free(void *ptr);
* @param free_fun
*/
void set_mem_funcs(void *(*malloc_fun)(size_t), void *(*calloc_fun)(size_t),
- void *(*realloc_fun)(void *, size_t),
- void(*free_fun)(void *));
+ void *(*realloc_fun)(void *, size_t),
+ void (*free_fun)(void *));
#endif // ECGEN_MEMORY_H
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, );
}
}
-
-