diff options
| author | J08nY | 2025-10-01 14:56:28 +0200 |
|---|---|---|
| committer | J08nY | 2025-10-01 14:56:28 +0200 |
| commit | 67f8e7b36abed5cb1fca0c740632a6431b256e4b (patch) | |
| tree | c471aa5155855e9ad362c352ac047e3a5ac05f2a /test | |
| parent | a765e7a2d8d19b62fb1031b2551b6cb75cf84e38 (diff) | |
| download | pyecsca-codegen-67f8e7b36abed5cb1fca0c740632a6431b256e4b.tar.gz pyecsca-codegen-67f8e7b36abed5cb1fca0c740632a6431b256e4b.tar.zst pyecsca-codegen-67f8e7b36abed5cb1fca0c740632a6431b256e4b.zip | |
More tests for sliding windows.
Diffstat (limited to 'test')
| -rw-r--r-- | test/Makefile | 2 | ||||
| -rw-r--r-- | test/test_bn.c | 69 |
2 files changed, 48 insertions, 23 deletions
diff --git a/test/Makefile b/test/Makefile index d13b487..eb6724a 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,3 +1,3 @@ test_bn: test_bn.c ../pyecsca/codegen/bn/bn.c - gcc -o $@ $^ -fsanitize=address -fsanitize=undefined -I ../pyecsca/codegen/ -I ../pyecsca/codegen/tommath/ -L ../pyecsca/codegen/tommath/ -l:libtommath-HOST.a
\ No newline at end of file + gcc -g -o $@ $^ -fsanitize=address -fsanitize=undefined -I ../pyecsca/codegen/ -I ../pyecsca/codegen/tommath/ -L ../pyecsca/codegen/tommath/ -l:libtommath-HOST.a diff --git a/test/test_bn.c b/test/test_bn.c index 66de22f..1f6d14d 100644 --- a/test/test_bn.c +++ b/test/test_bn.c @@ -32,30 +32,55 @@ int test_wsliding_ltr() { int test_wsliding_rtl() { printf("test_wsliding_rtl: "); - bn_t bn; - bn_init(&bn); - bn_from_int(181, &bn); - wsliding_t *ws = bn_wsliding_rtl(&bn, 3); - if (ws == NULL) { - printf("NULL\n"); - return 1; - } - if (ws->length != 8) { - printf("Bad length (%li instead of 8)\n", ws->length); - return 1; - } - uint8_t expected[8] = {1, 0, 0, 3, 0, 0, 0, 5}; - for (int i = 0; i < 8; i++) { - if (ws->data[i] != expected[i]) { - printf("Bad data (%i instead of %i)\n", ws->data[i], expected[i]); - return 1; + int failed = 0; + struct { + int value; + int w; + int expected_len; + uint8_t expected[16]; // max expected length + } cases[] = { + // Original test + {181, 3, 8, {1, 0, 0, 3, 0, 0, 0, 5}}, + // Edge case: 1 + {1, 3, 1, {1}}, + // w = 2, value = 1234 + {1234, 2, 11, {1, 0, 0, 0, 3, 0, 1, 0, 0, 1, 0}}, + // w = 4, value = 0b10101010 + {0b10101010, 4, 6, {5, 0, 0, 0, 5, 0}}, + }; + int num_cases = sizeof(cases) / sizeof(cases[0]); + for (int t = 0; t < num_cases; t++) { + bn_t bn; + bn_init(&bn); + bn_from_int(cases[t].value, &bn); + wsliding_t *ws = bn_wsliding_rtl(&bn, cases[t].w); + if (ws == NULL) { + printf("Case %d: NULL\n", t); + failed++; + bn_clear(&bn); + continue; + } + if (ws->length != cases[t].expected_len) { + printf("Case %d: Bad length (%li instead of %i)\n", t, ws->length, cases[t].expected_len); + failed++; } + for (int i = 0; i < cases[t].expected_len; i++) { + if (ws->data[i] != cases[t].expected[i]) { + printf("Case %d: Bad data at %d (%i instead of %i)\n", t, i, ws->data[i], cases[t].expected[i]); + failed++; + break; + } + } + bn_clear(&bn); + free(ws->data); + free(ws); } - printf("OK\n"); - bn_clear(&bn); - free(ws->data); - free(ws); - return 0; + if (failed == 0) { + printf("OK\n"); + } else { + printf("FAILED (%d cases)\n", failed); + } + return failed; } int test_convert_base() { |
