aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests/test_ffi.cpp
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2018-08-14 21:42:42 -0400
committerJack Lloyd <[email protected]>2018-08-14 21:52:24 -0400
commit759d55886dcc1f5351c22e568c87b3a30644e055 (patch)
tree64fd9b69344152fa618983045bce7dd1b519c80b /src/tests/test_ffi.cpp
parentb100a4f1de538e120413d72f09fd2ab9e43d81b5 (diff)
Cleanup of BigInt encoding/decoding functions
Instigated by finding a bug where BigInt::encode with decimal output would often have a leading '0' char. Which is papered over in the IO operator, but was exposed by botan_mp_to_str which called BigInt::encode directly. Split BigInt::encode/decode into two versions, one taking the Base argument and the other using the (previously default) binary base. With a view of eventually deprecating the versions taking a base. Add BigInt::to_dec_string() and BigInt::to_hex_string()
Diffstat (limited to 'src/tests/test_ffi.cpp')
-rw-r--r--src/tests/test_ffi.cpp20
1 files changed, 16 insertions, 4 deletions
diff --git a/src/tests/test_ffi.cpp b/src/tests/test_ffi.cpp
index 8bcdbeffc..eb0eacdf4 100644
--- a/src/tests/test_ffi.cpp
+++ b/src/tests/test_ffi.cpp
@@ -1100,6 +1100,9 @@ class FFI_Unit_Tests final : public Test
{
Test::Result result("FFI MP");
+ char str_buf[1024] = { 0 };
+ size_t str_len = 0;
+
botan_mp_t x;
botan_mp_init(&x);
TEST_FFI_RC(0, botan_mp_is_odd, (x));
@@ -1118,10 +1121,22 @@ class FFI_Unit_Tests final : public Test
TEST_FFI_OK(botan_mp_num_bytes, (x, &bn_bytes));
result.test_eq("Expected size for MP 5", bn_bytes, 1);
+ botan_mp_set_from_int(x, 80);
+ TEST_FFI_OK(botan_mp_num_bytes, (x, &bn_bytes));
+ result.test_eq("Expected size for MP 80", bn_bytes, 1);
+
+ str_len = sizeof(str_buf);
+ TEST_FFI_OK(botan_mp_to_str, (x, 10, str_buf, &str_len));
+ result.test_eq("botan_mp_add", std::string(str_buf), "80");
+
botan_mp_set_from_int(x, 259);
TEST_FFI_OK(botan_mp_num_bytes, (x, &bn_bytes));
result.test_eq("Expected size for MP 259", bn_bytes, 2);
+ str_len = sizeof(str_buf);
+ TEST_FFI_OK(botan_mp_to_str, (x, 10, str_buf, &str_len));
+ result.test_eq("botan_mp_add", std::string(str_buf), "259");
+
TEST_FFI_RC(1, botan_mp_is_odd, (x));
TEST_FFI_RC(0, botan_mp_is_even, (x));
TEST_FFI_RC(0, botan_mp_is_negative, (x));
@@ -1173,9 +1188,6 @@ class FFI_Unit_Tests final : public Test
TEST_FFI_OK(botan_mp_num_bits, (x, &x_bits));
result.test_eq("botan_mp_num_bits", x_bits, 9);
- char str_buf[1024] = { 0 };
- size_t str_len = 0;
-
TEST_FFI_OK(botan_mp_to_hex, (x, str_buf));
result.test_eq("botan_mp_to_hex", std::string(str_buf), "0103");
@@ -1218,7 +1230,7 @@ class FFI_Unit_Tests final : public Test
str_len = sizeof(str_buf);
TEST_FFI_OK(botan_mp_to_str, (q, 10, str_buf, &str_len));
- result.test_eq("botan_mp_div_q", std::string(str_buf), "073701");
+ result.test_eq("botan_mp_div_q", std::string(str_buf), "73701");
str_len = sizeof(str_buf);
TEST_FFI_OK(botan_mp_to_str, (r, 10, str_buf, &str_len));