summaryrefslogtreecommitdiffstats
path: root/security/nss/gtests/freebl_gtest/mpi_unittest.cc
diff options
context:
space:
mode:
Diffstat (limited to 'security/nss/gtests/freebl_gtest/mpi_unittest.cc')
-rw-r--r--security/nss/gtests/freebl_gtest/mpi_unittest.cc51
1 files changed, 44 insertions, 7 deletions
diff --git a/security/nss/gtests/freebl_gtest/mpi_unittest.cc b/security/nss/gtests/freebl_gtest/mpi_unittest.cc
index 2ccb8c351..56b7454dc 100644
--- a/security/nss/gtests/freebl_gtest/mpi_unittest.cc
+++ b/security/nss/gtests/freebl_gtest/mpi_unittest.cc
@@ -6,6 +6,7 @@
#include <stdint.h>
#include <string.h>
+#include <memory>
#ifdef __MACH__
#include <mach/clock.h>
@@ -27,7 +28,7 @@ void gettime(struct timespec* tp) {
tp->tv_sec = mts.tv_sec;
tp->tv_nsec = mts.tv_nsec;
#else
- clock_gettime(CLOCK_MONOTONIC, tp);
+ ASSERT_NE(0, timespec_get(tp, TIME_UTC));
#endif
}
@@ -84,8 +85,9 @@ class MPITest : public ::testing::Test {
mp_int a;
ASSERT_EQ(MP_OKAY, mp_init(&a));
ASSERT_EQ(MP_OKAY, mp_read_unsigned_octets(&a, ref.data(), ref.size()));
- uint8_t buf[len];
- ASSERT_EQ(MP_OKAY, mp_to_fixlen_octets(&a, buf, len));
+ std::unique_ptr<uint8_t[]> buf(new uint8_t[len]);
+ ASSERT_NE(buf, nullptr);
+ ASSERT_EQ(MP_OKAY, mp_to_fixlen_octets(&a, buf.get(), len));
size_t compare;
if (len > ref.size()) {
for (size_t i = 0; i < len - ref.size(); ++i) {
@@ -96,9 +98,9 @@ class MPITest : public ::testing::Test {
compare = len;
}
dump("value", ref.data(), ref.size());
- dump("output", buf, len);
- ASSERT_EQ(0, memcmp(buf + len - compare, ref.data() + ref.size() - compare,
- compare))
+ dump("output", buf.get(), len);
+ ASSERT_EQ(0, memcmp(buf.get() + len - compare,
+ ref.data() + ref.size() - compare, compare))
<< "comparing " << compare << " octets";
mp_clear(&a);
}
@@ -146,6 +148,41 @@ TEST_F(MPITest, MpiCmpUnalignedTest) {
}
#endif
+// The two follow tests ensure very similar mp_set_* functions are ok.
+TEST_F(MPITest, MpiSetUlong) {
+ mp_int a, b, c;
+ MP_DIGITS(&a) = 0;
+ MP_DIGITS(&b) = 0;
+ MP_DIGITS(&c) = 0;
+ ASSERT_EQ(MP_OKAY, mp_init(&a));
+ ASSERT_EQ(MP_OKAY, mp_init(&b));
+ ASSERT_EQ(MP_OKAY, mp_init(&c));
+ EXPECT_EQ(MP_OKAY, mp_set_ulong(&a, 1));
+ EXPECT_EQ(MP_OKAY, mp_set_ulong(&b, 0));
+ EXPECT_EQ(MP_OKAY, mp_set_ulong(&c, -1));
+
+ mp_clear(&a);
+ mp_clear(&b);
+ mp_clear(&c);
+}
+
+TEST_F(MPITest, MpiSetInt) {
+ mp_int a, b, c;
+ MP_DIGITS(&a) = 0;
+ MP_DIGITS(&b) = 0;
+ MP_DIGITS(&c) = 0;
+ ASSERT_EQ(MP_OKAY, mp_init(&a));
+ ASSERT_EQ(MP_OKAY, mp_init(&b));
+ ASSERT_EQ(MP_OKAY, mp_init(&c));
+ EXPECT_EQ(MP_OKAY, mp_set_int(&a, 1));
+ EXPECT_EQ(MP_OKAY, mp_set_int(&b, 0));
+ EXPECT_EQ(MP_OKAY, mp_set_int(&c, -1));
+
+ mp_clear(&a);
+ mp_clear(&b);
+ mp_clear(&c);
+}
+
TEST_F(MPITest, MpiFixlenOctetsZero) {
std::vector<uint8_t> zero = {0};
TestToFixedOctets(zero, 1);
@@ -253,4 +290,4 @@ TEST_F(DISABLED_MPITest, MpiCmpConstTest) {
mp_clear(&c);
}
-} // nss_test
+} // namespace nss_test