Commit 4c3da8d6 authored by Franziskus Kiefer's avatar Franziskus Kiefer

Bug 1317924 - resurrect mpi tests, r=ttaubert

Differential Revision: https://nss-review.dev.mozaws.net/D68

--HG--
rename : lib/freebl/mpi/mpi-test.c => cmd/mpitests/mpi-test.c
rename : gtests/util_gtest/util_gtest.gyp => cmd/mpitests/mpitests.gyp
rename : lib/freebl/mpi/test-info.c => cmd/mpitests/test-info.c
rename : gtests/util_gtest/util_gtest.gyp => gtests/freebl_gtest/freebl_gtest.gyp
extra : rebase_source : 163b288b5fac99b04e7de8376628a5a6b0e3f1da
extra : amend_source : 5850535f6d2114601a4bed890315b130245f28c5
parent d1a94bd7
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
{
'includes': [
'../../coreconf/config.gypi',
'../../cmd/platlibs.gypi'
],
'targets': [
{
'target_name': 'mpi_tests',
'type': 'executable',
'sources': [
'mpi-test.c',
],
'dependencies': [
'<(DEPTH)/lib/freebl/freebl.gyp:<(freebl_name)',
]
}
],
'target_defaults': {
'include_dirs': [
'<(DEPTH)/lib/freebl/mpi',
'<(DEPTH)/lib/util',
]
},
'variables': {
'module': 'nss'
}
}
......@@ -8,7 +8,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* Table mapping test suite names to index numbers */
const int g_count = 42;
const int g_count = 41;
const char *g_names[] = {
"list", /* print out a list of the available test suites */
"copy", /* test assignment of mp-int structures */
......@@ -31,7 +31,6 @@ const char *g_names[] = {
"expt-digit", /* test digit exponentiation */
"expt", /* test full exponentiation */
"expt-2", /* test power-of-two exponentiation */
"square-root", /* test integer square root function */
"modulo-digit", /* test digit modular reduction */
"modulo", /* test full modular reduction */
"mod-add", /* test modular addition */
......@@ -76,7 +75,6 @@ int test_div(void);
int test_expt_d(void);
int test_expt(void);
int test_2expt(void);
int test_sqrt(void);
int test_mod_d(void);
int test_mod(void);
int test_addmod(void);
......@@ -105,7 +103,7 @@ int (*g_tests[])(void) = {
test_add, test_sub_d, test_sub, test_mul_d,
test_mul, test_sqr, test_div_d, test_div_2,
test_div_2d, test_div, test_expt_d, test_expt,
test_2expt, test_sqrt, test_mod_d, test_mod,
test_2expt, test_mod_d, test_mod,
test_addmod, test_submod, test_mulmod, test_sqrmod,
test_exptmod, test_exptmod_d, test_invmod, test_cmp_d,
test_cmp_z, test_cmp, test_cmp_mag, test_parity,
......@@ -136,7 +134,6 @@ const char *g_descs[] = {
"test digit exponentiation",
"test full exponentiation",
"test power-of-two exponentiation",
"test integer square root function",
"test digit modular reduction",
"test full modular reduction",
"test modular addition",
......
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
{
'includes': [
'../../coreconf/config.gypi',
'../common/gtest.gypi',
],
'targets': [
{
'target_name': 'freebl_gtest',
'type': 'executable',
'sources': [
'mpi_unittest.cc',
'<(DEPTH)/gtests/common/gtests.cc'
],
'dependencies': [
'<(DEPTH)/exports.gyp:nss_exports',
'<(DEPTH)/lib/freebl/freebl.gyp:<(freebl_name)',
'<(DEPTH)/gtests/google_test/google_test.gyp:gtest',
],
'defines': [
'CT_VERIF',
],
}
],
'target_defaults': {
'include_dirs': [
'<(DEPTH)/gtests/google_test/gtest/include',
'<(DEPTH)/gtests/common',
'<(DEPTH)/lib/freebl/mpi',
]
},
'variables': {
'module': 'nss'
}
}
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.
#include "secdert.h"
#include "secitem.h"
#include "secport.h"
#include "gtest/gtest.h"
#include <stdint.h>
#include <string.h>
#include <string>
#include "mpi.h"
namespace nss_test {
class MPITest : public ::testing::Test {
protected:
void TestCmp(const std::string a_string, const std::string b_string,
int result) {
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));
mp_read_radix(&a, a_string.c_str(), 16);
mp_read_radix(&b, b_string.c_str(), 16);
EXPECT_EQ(result, mp_cmp(&a, &b));
}
};
TEST_F(MPITest, MpiCmp01Test) { TestCmp("0", "1", -1); }
TEST_F(MPITest, MpiCmp10Test) { TestCmp("1", "0", 1); }
TEST_F(MPITest, MpiCmp00Test) { TestCmp("0", "0", 0); }
TEST_F(MPITest, MpiCmp11Test) { TestCmp("1", "1", 0); }
TEST_F(MPITest, MpiCmpConstTest) {
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));
mp_read_radix(&a, const_cast<char *>("FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551"), 16);
mp_read_radix(&b, const_cast<char *>("FF0FFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551"), 16);
mp_read_radix(&c, const_cast<char *>("FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632550"), 16);
mp_taint(&b);
mp_taint(&c);
uint32_t runs = 5000000;
uint32_t time_b = 0, time_c = 0;
for (uint32_t i = 0; i < runs; ++i) {
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
int r = mp_cmp(&a, &b);
clock_gettime(CLOCK_MONOTONIC, &end);
unsigned long long used = end.tv_sec * 1000000000L + end.tv_nsec;
used -= (unsigned long long)start.tv_sec * 1000000000L + start.tv_nsec;
time_b += used;
ASSERT_EQ(1, r);
}
printf("time b: %u\n", time_b / runs);
for (uint32_t i = 0; i < runs; ++i) {
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
int r = mp_cmp(&a, &c);
clock_gettime(CLOCK_MONOTONIC, &end);
unsigned long long used = end.tv_sec * 1000000000L + end.tv_nsec;
used -= (unsigned long long)start.tv_sec * 1000000000L + start.tv_nsec;
time_c += used;
ASSERT_EQ(1, r);
}
printf("time c: %u\n", time_c / runs);
}
} // nss_test
......@@ -103,6 +103,9 @@
'dependencies': [
'<(DEPTH)/lib/util/util.gyp:nssutil3',
],
'defines': [
'CT_VERIF',
],
}],
[ 'target_arch=="x64"', {
'sources': [
......
......@@ -37,6 +37,10 @@
#include "logtab.h"
#endif
#ifdef CT_VERIF
#include <valgrind/memcheck.h>
#endif
/* {{{ Constant strings */
/* Constant strings returned by mp_strerror() */
......@@ -82,6 +86,26 @@ mp_set_prec(mp_size prec)
/* }}} */
#ifdef CT_VERIF
void
mp_taint(mp_int *mp)
{
size_t i;
for (i = 0; i < mp->used; ++i) {
VALGRIND_MAKE_MEM_UNDEFINED(&(mp->dp[i]), sizeof(mp_digit));
}
}
void
mp_untaint(mp_int *mp)
{
size_t i;
for (i = 0; i < mp->used; ++i) {
VALGRIND_MAKE_MEM_DEFINED(&(mp->dp[i]), sizeof(mp_digit));
}
}
#endif
/*------------------------------------------------------------------------*/
/* {{{ mp_init(mp) */
......
......@@ -12,6 +12,9 @@
#include "mpi-config.h"
#include "seccomon.h"
SEC_BEGIN_PROTOS
#if MP_DEBUG
#undef MP_IOFUNC
#define MP_IOFUNC 1
......@@ -273,7 +276,6 @@ void freebl_cpuid(unsigned long op, unsigned long *eax,
if (MP_OKAY > (res = (x))) \
goto CLEANUP
#if defined(MP_API_COMPATIBLE)
#define NEG MP_NEG
#define ZPOS MP_ZPOS
#define DIGIT_MAX MP_DIGIT_MAX
......@@ -300,6 +302,12 @@ void freebl_cpuid(unsigned long op, unsigned long *eax,
#else
#define ARGCHK(X, Y) /* */
#endif
#endif /* defined MP_API_COMPATIBLE */
#ifdef CT_VERIF
void mp_taint(mp_int *mp);
void mp_untaint(mp_int *mp);
#endif
SEC_END_PROTOS
#endif /* end _H_MPI_ */
......@@ -33,7 +33,6 @@ divide:test_div:test full division
expt-digit:test_expt_d:test digit exponentiation
expt:test_expt:test full exponentiation
expt-2:test_2expt:test power-of-two exponentiation
square-root:test_sqrt:test integer square root function
modulo-digit:test_mod_d:test digit modular reduction
modulo:test_mod:test full modular reduction
mod-add:test_addmod:test modular addition
......
......@@ -195,6 +195,12 @@
'cmd/pkix-errcodes/pkix-errcodes.gyp:pkix-errcodes',
],
}],
[ 'test_build==1', {
'dependencies': [
'cmd/mpitests/mpitests.gyp:mpi_tests',
'gtests/freebl_gtest/freebl_gtest.gyp:freebl_gtest',
],
}],
],
},
],
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment