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 FiniteField 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/errors-testhelper.h"
26 #include "epid/common-testhelper/ffelement_wrapper-testhelper.h"
27 #include "epid/common-testhelper/finite_field_wrapper-testhelper.h"
28 
29 extern "C" {
30 #include "epid/common/math/bignum.h"
31 #include "epid/common/types.h"
32 }
33 
34 namespace {
35 
36 // Use Test Fixture for SetUp and TearDown
37 class FiniteFieldObjTest : public ::testing::Test {
38  public:
39   static const BigNumStr prime_str;
40   static const FpElemStr ground_str;
41 };
42 
43 /// Intel(R) EPID 2.0 parameter p
44 const BigNumStr FiniteFieldObjTest::prime_str = {
45     0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0xF0, 0xCD, 0x46, 0xE5, 0xF2,
46     0x5E, 0xEE, 0x71, 0xA4, 0x9E, 0x0C, 0xDC, 0x65, 0xFB, 0x12, 0x99,
47     0x92, 0x1A, 0xF6, 0x2D, 0x53, 0x6C, 0xD1, 0x0B, 0x50, 0x0D};
48 
49 const FpElemStr FiniteFieldObjTest::ground_str = {
50     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
51     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
52     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
53 };
54 
TEST_F(FiniteFieldObjTest,ObjDefaultConstructedIsNotNull)55 TEST_F(FiniteFieldObjTest, ObjDefaultConstructedIsNotNull) {
56   FiniteFieldObj ff;
57   EXPECT_NE(nullptr, (FiniteField*)ff);
58 }
59 
TEST_F(FiniteFieldObjTest,AssignmentCopiesPointer)60 TEST_F(FiniteFieldObjTest, AssignmentCopiesPointer) {
61   FiniteFieldObj ff1;
62   FiniteFieldObj ff2;
63   EXPECT_NE((FiniteField*)ff1, (FiniteField*)ff2);
64   ff1 = ff2;
65   EXPECT_EQ((FiniteField*)ff1, (FiniteField*)ff2);
66 }
67 
TEST_F(FiniteFieldObjTest,CopyConstructorCopiesPointer)68 TEST_F(FiniteFieldObjTest, CopyConstructorCopiesPointer) {
69   FiniteFieldObj ff1;
70   FiniteFieldObj ff2(ff1);
71   EXPECT_EQ((FiniteField*)ff1, (FiniteField*)ff2);
72 }
73 
TEST_F(FiniteFieldObjTest,ConstructorDoesNotThrow)74 TEST_F(FiniteFieldObjTest, ConstructorDoesNotThrow) {
75   FiniteFieldObj ff1;
76   FiniteFieldObj ff2(this->prime_str);
77   FfElementObj ffe(&ff2, this->ground_str);
78   FiniteFieldObj ff3(ff2, ffe, 2);
79 }
80 
TEST_F(FiniteFieldObjTest,CanCastConstToConstPointer)81 TEST_F(FiniteFieldObjTest, CanCastConstToConstPointer) {
82   FiniteFieldObj const ff;
83   FiniteField const* ff_ptr = ff;
84   (void)ff_ptr;
85 }
86 
TEST_F(FiniteFieldObjTest,CanGetConstPointerFromConst)87 TEST_F(FiniteFieldObjTest, CanGetConstPointerFromConst) {
88   FiniteFieldObj const ff;
89   FiniteField const* ff_ptr = ff.getc();
90   (void)ff_ptr;
91 }
92 
93 /*
94 The following tests are expected to result in
95 compile time errors (by design)
96 */
97 /*
98 TEST_F(FiniteFieldObjTest, CannotCastConstToNonConstPointer) {
99   FiniteFieldObj const ff;
100   FiniteField * ff_ptr = ff;
101   (void) ff_ptr;
102 }
103 
104 TEST_F(FiniteFieldObjTest, CannotGetNonConstPointerFromConst) {
105   FiniteFieldObj const ff;
106   FiniteField * ff_ptr = ff.get();
107   (void) ff_ptr;
108 }
109 */
110 
TEST_F(FiniteFieldObjTest,CanCastNonConstToConstPointer)111 TEST_F(FiniteFieldObjTest, CanCastNonConstToConstPointer) {
112   FiniteFieldObj ff;
113   FiniteField const* ff_ptr = ff;
114   (void)ff_ptr;
115 }
116 
TEST_F(FiniteFieldObjTest,CanGetConstPointerFromNonConst)117 TEST_F(FiniteFieldObjTest, CanGetConstPointerFromNonConst) {
118   FiniteFieldObj ff;
119   FiniteField const* ff_ptr = ff.getc();
120   (void)ff_ptr;
121 }
122 
TEST_F(FiniteFieldObjTest,CanCastNonConstToNonConstPointer)123 TEST_F(FiniteFieldObjTest, CanCastNonConstToNonConstPointer) {
124   FiniteFieldObj ff;
125   FiniteField* ff_ptr = ff;
126   (void)ff_ptr;
127 }
128 
TEST_F(FiniteFieldObjTest,CanGetNonConstPointerFromNonConst)129 TEST_F(FiniteFieldObjTest, CanGetNonConstPointerFromNonConst) {
130   FiniteFieldObj ff;
131   FiniteField* ff_ptr = ff.get();
132   (void)ff_ptr;
133 }
134 
135 }  // namespace
136