1 /*############################################################################
2 # Copyright 2016-2017 Intel Corporation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 ############################################################################*/
16
17 /*!
18 * \file
19 * \brief OctStr2Bnu unit tests.
20 *
21 * OctStr2Bnu is an internal function used in the IPP implementation of the
22 * math libraries. These tests can be omitted if you do not use this function.
23 */
24
25 #include "epid/common-testhelper/epid_gtest-testhelper.h"
26 #include "epid/common/stdtypes.h"
27 #include "gtest/gtest.h"
28
29 extern "C" {
30 #include "epid/common/math/src/bignum-internal.h"
31 }
32
33 namespace {
34
35 const uint8_t bnstr1[] = {0x01, 0x02, 0x03, 0x04};
36 const uint8_t bnstr2[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
37 uint32_t bnustr1[sizeof(bnstr1) / sizeof(uint32_t)] = {0x01020304};
38 uint32_t bnustr2[sizeof(bnstr2) / sizeof(uint32_t)] = {0x05060708, 0x01020304};
39
TEST(OctStr2Bnu,octstr2bnuFailsGivenNullBnu)40 TEST(OctStr2Bnu, octstr2bnuFailsGivenNullBnu) {
41 int len = OctStr2Bnu(nullptr, bnstr1, sizeof(bnstr1) / sizeof(uint8_t));
42 EXPECT_EQ(-1, len);
43 }
TEST(OctStr2Bnu,octstr2bnuFailsGivenNullOctstr)44 TEST(OctStr2Bnu, octstr2bnuFailsGivenNullOctstr) {
45 uint32_t bnustr_res[sizeof(bnstr1) / sizeof(uint32_t)] = {0};
46 int len = OctStr2Bnu(bnustr_res, nullptr, sizeof(bnstr1) / sizeof(uint8_t));
47 EXPECT_EQ(-1, len);
48 }
TEST(OctStr2Bnu,octstr2bnuFailsGivenInvalidOctsrtLen)49 TEST(OctStr2Bnu, octstr2bnuFailsGivenInvalidOctsrtLen) {
50 uint32_t bnustr_res[sizeof(bnstr1) / sizeof(uint32_t)] = {0};
51 int len = OctStr2Bnu(bnustr_res, bnstr1, -1);
52 EXPECT_EQ(-1, len);
53 len = OctStr2Bnu(bnustr_res, bnstr1, 0);
54 EXPECT_EQ(-1, len);
55 len = OctStr2Bnu(bnustr_res, bnstr1, 3);
56 EXPECT_EQ(-1, len);
57 len = OctStr2Bnu(bnustr_res, bnstr1, 5);
58 EXPECT_EQ(-1, len);
59 }
TEST(OctStr2Bnu,octstr2bnuWorksGivenOctstr1)60 TEST(OctStr2Bnu, octstr2bnuWorksGivenOctstr1) {
61 uint32_t bnustr_res[sizeof(bnstr1) / sizeof(uint32_t)] = {0};
62 int len = OctStr2Bnu(bnustr_res, bnstr1, sizeof(bnstr1) / sizeof(uint8_t));
63 EXPECT_EQ(1, len);
64 EXPECT_EQ(0,
65 memcmp(bnustr1, bnustr_res, sizeof(bnustr_res) / sizeof(uint32_t)))
66 << "OctStr2Bnu: bnu string result does not match with predefined value\n";
67 }
TEST(OctStr2Bnu,octstr2bnuWorksGivenOctstr2)68 TEST(OctStr2Bnu, octstr2bnuWorksGivenOctstr2) {
69 uint32_t bnustr_res[sizeof(bnstr2) / sizeof(uint32_t)] = {0};
70 int len = OctStr2Bnu(bnustr_res, bnstr2, sizeof(bnstr2) / sizeof(uint8_t));
71 EXPECT_EQ(2, len);
72 EXPECT_EQ(0,
73 memcmp(bnustr2, bnustr_res, sizeof(bnustr_res) / sizeof(uint32_t)))
74 << "OctStr2Bnu: bnu string result does not match with predefined value\n";
75 }
76 } // namespace
77