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 Bignum C++ wrapper unit tests.
20  */
21 
22 #include "epid/common-testhelper/epid_gtest-testhelper.h"
23 #include "gtest/gtest.h"
24 
25 #include "epid/common-testhelper/bignum_wrapper-testhelper.h"
26 #include "epid/common-testhelper/errors-testhelper.h"
27 
28 extern "C" {
29 #include "epid/common/math/bignum.h"
30 #include "epid/common/src/memory.h"
31 }
32 
33 namespace {
34 
35 // Use Test Fixture for SetUp and TearDown
36 class BigNumObjTest : public ::testing::Test {
37  public:
38   static const BigNumStr str_0;
39   static const std::vector<unsigned char> vec_0;
40 };
41 
42 const BigNumStr BigNumObjTest::str_0 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
43                                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
44                                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
45 
46 const std::vector<unsigned char> BigNumObjTest::vec_0 = {0, 0, 0, 0,
47                                                          0, 0, 0, 0};
48 
TEST_F(BigNumObjTest,ObjDefaultConstructedIsNotNull)49 TEST_F(BigNumObjTest, ObjDefaultConstructedIsNotNull) {
50   BigNumObj bn;
51   EXPECT_NE(nullptr, (BigNum*)bn);
52 }
53 
TEST_F(BigNumObjTest,ObjConstructedWithSizeIsNotNull)54 TEST_F(BigNumObjTest, ObjConstructedWithSizeIsNotNull) {
55   BigNumObj bn1(1);
56   EXPECT_NE(nullptr, (BigNum*)bn1);
57   BigNumObj bn32(32);
58   EXPECT_NE(nullptr, (BigNum*)bn32);
59 }
60 
TEST_F(BigNumObjTest,AssignmentDoesNotCopyPointer)61 TEST_F(BigNumObjTest, AssignmentDoesNotCopyPointer) {
62   BigNumObj bn1;
63   BigNumObj bn2;
64   EXPECT_NE((BigNum*)bn1, (BigNum*)bn2);
65   bn1 = bn2;
66   EXPECT_NE((BigNum*)bn1, (BigNum*)bn2);
67 }
68 
TEST_F(BigNumObjTest,CopyConstructorDoesNotCopyPointer)69 TEST_F(BigNumObjTest, CopyConstructorDoesNotCopyPointer) {
70   BigNumObj bn1;
71   BigNumObj bn2(bn1);
72   EXPECT_NE((BigNum*)bn1, (BigNum*)bn2);
73 }
74 
TEST_F(BigNumObjTest,ConstructorDoesNotThrow)75 TEST_F(BigNumObjTest, ConstructorDoesNotThrow) {
76   BigNumObj bn1;
77   BigNumObj bn2(32);
78   BigNumObj bn3(32, this->str_0);
79   BigNumObj bn4(32, this->vec_0);
80   BigNumObj bn5(this->str_0);
81   BigNumObj bn6(this->vec_0);
82 
83   EXPECT_NE((BigNum*)bn1, (BigNum*)bn2);
84   EXPECT_NE((BigNum*)bn1, (BigNum*)bn3);
85   EXPECT_NE((BigNum*)bn1, (BigNum*)bn4);
86   EXPECT_NE((BigNum*)bn1, (BigNum*)bn5);
87   EXPECT_NE((BigNum*)bn1, (BigNum*)bn6);
88 
89   EXPECT_NE((BigNum*)bn2, (BigNum*)bn1);
90   EXPECT_NE((BigNum*)bn2, (BigNum*)bn3);
91   EXPECT_NE((BigNum*)bn2, (BigNum*)bn4);
92   EXPECT_NE((BigNum*)bn2, (BigNum*)bn5);
93   EXPECT_NE((BigNum*)bn2, (BigNum*)bn6);
94 
95   EXPECT_NE((BigNum*)bn3, (BigNum*)bn1);
96   EXPECT_NE((BigNum*)bn3, (BigNum*)bn2);
97   EXPECT_NE((BigNum*)bn3, (BigNum*)bn4);
98   EXPECT_NE((BigNum*)bn3, (BigNum*)bn5);
99   EXPECT_NE((BigNum*)bn3, (BigNum*)bn6);
100 
101   EXPECT_NE((BigNum*)bn4, (BigNum*)bn1);
102   EXPECT_NE((BigNum*)bn4, (BigNum*)bn2);
103   EXPECT_NE((BigNum*)bn4, (BigNum*)bn3);
104   EXPECT_NE((BigNum*)bn4, (BigNum*)bn5);
105   EXPECT_NE((BigNum*)bn4, (BigNum*)bn6);
106 
107   EXPECT_NE((BigNum*)bn5, (BigNum*)bn1);
108   EXPECT_NE((BigNum*)bn5, (BigNum*)bn2);
109   EXPECT_NE((BigNum*)bn5, (BigNum*)bn3);
110   EXPECT_NE((BigNum*)bn5, (BigNum*)bn4);
111   EXPECT_NE((BigNum*)bn5, (BigNum*)bn6);
112 
113   EXPECT_NE((BigNum*)bn6, (BigNum*)bn1);
114   EXPECT_NE((BigNum*)bn6, (BigNum*)bn2);
115   EXPECT_NE((BigNum*)bn6, (BigNum*)bn3);
116   EXPECT_NE((BigNum*)bn6, (BigNum*)bn4);
117   EXPECT_NE((BigNum*)bn6, (BigNum*)bn5);
118 }
119 
TEST_F(BigNumObjTest,CanCastConstToConstPointer)120 TEST_F(BigNumObjTest, CanCastConstToConstPointer) {
121   BigNumObj const bn;
122   BigNum const* bn_ptr = bn;
123   (void)bn_ptr;
124 }
125 
TEST_F(BigNumObjTest,CanGetConstPointerFromConst)126 TEST_F(BigNumObjTest, CanGetConstPointerFromConst) {
127   BigNumObj const bn;
128   BigNum const* bn_ptr = bn.getc();
129   (void)bn_ptr;
130 }
131 
132 /*
133 The following tests are expected to result in
134 compile time errors (by design)
135 */
136 /*
137 TEST_F(BigNumObjTest, CannotCastConstToNonConstPointer) {
138   BigNumObj const bn;
139   BigNum * bn_ptr = bn;
140   (void) bn_ptr;
141 }
142 
143 TEST_F(BigNumObjTest, CannotGetNonConstPointerFromConst) {
144   BigNumObj const bn;
145   BigNum * bn_ptr = bn.get();
146   (void) bn_ptr;
147 }
148 */
149 
TEST_F(BigNumObjTest,CanCastNonConstToConstPointer)150 TEST_F(BigNumObjTest, CanCastNonConstToConstPointer) {
151   BigNumObj bn;
152   BigNum const* bn_ptr = bn;
153   (void)bn_ptr;
154 }
155 
TEST_F(BigNumObjTest,CanGetConstPointerFromNonConst)156 TEST_F(BigNumObjTest, CanGetConstPointerFromNonConst) {
157   BigNumObj bn;
158   BigNum const* bn_ptr = bn.getc();
159   (void)bn_ptr;
160 }
161 
TEST_F(BigNumObjTest,CanCastNonConstToNonConstPointer)162 TEST_F(BigNumObjTest, CanCastNonConstToNonConstPointer) {
163   BigNumObj bn;
164   BigNum* bn_ptr = bn;
165   (void)bn_ptr;
166 }
167 
TEST_F(BigNumObjTest,CanGetNonConstPointerFromNonConst)168 TEST_F(BigNumObjTest, CanGetNonConstPointerFromNonConst) {
169   BigNumObj bn;
170   BigNum* bn_ptr = bn.get();
171   (void)bn_ptr;
172 }
173 
174 }  // namespace
175