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 FfElement 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 }
32
33 namespace {
34
35 // Use Test Fixture for SetUp and TearDown
36 class FfElementObjTest : public ::testing::Test {
37 public:
38 static FiniteFieldObj ff;
39 static const BigNumStr prime_str;
40
41 static const FpElemStr ff_str_1;
42 static const FpElemStr ff_str_2;
43 static const Fq2ElemStr ff_2_str;
44 };
45
46 /// Intel(R) EPID 2.0 parameter p
47 const BigNumStr FfElementObjTest::prime_str = {
48 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0xF0, 0xCD, 0x46, 0xE5, 0xF2,
49 0x5E, 0xEE, 0x71, 0xA4, 0x9E, 0x0C, 0xDC, 0x65, 0xFB, 0x12, 0x99,
50 0x92, 0x1A, 0xF6, 0x2D, 0x53, 0x6C, 0xD1, 0x0B, 0x50, 0x0D};
51
52 const FpElemStr FfElementObjTest::ff_str_1 = {
53 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
54 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
55 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
56 };
57
58 const FpElemStr FfElementObjTest::ff_str_2 = {
59 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
60 0x00, 0x00, 0xDC, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA4, 0x00, 0x00,
61 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
62 };
63
64 const Fq2ElemStr FfElementObjTest::ff_2_str = {
65 // 1
66 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
67 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
68 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
69 // 2
70 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
71 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
72 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20};
73
74 FiniteFieldObj FfElementObjTest::ff(prime_str);
75
TEST_F(FfElementObjTest,ObjDefaultConstructedIsNotNull)76 TEST_F(FfElementObjTest, ObjDefaultConstructedIsNotNull) {
77 FfElementObj ffe(&ff);
78 EXPECT_NE(nullptr, (FfElement*)ffe);
79 }
80
TEST_F(FfElementObjTest,AssignmentDoesNotCopyPointer)81 TEST_F(FfElementObjTest, AssignmentDoesNotCopyPointer) {
82 FfElementObj ffe1(&ff, ff_str_1);
83 FfElementObj ffe2(&ff, ff_str_2);
84 EXPECT_NE((FfElement*)ffe1, (FfElement*)ffe2);
85 ffe1 = ffe2;
86 EXPECT_NE((FfElement*)ffe1, (FfElement*)ffe2);
87 }
88
TEST_F(FfElementObjTest,CopyConstructorDoesNotCopyPointer)89 TEST_F(FfElementObjTest, CopyConstructorDoesNotCopyPointer) {
90 FfElementObj ffe1(&ff, ff_str_1);
91 FfElementObj ffe2(ffe1);
92 EXPECT_NE((FfElement*)ffe1, (FfElement*)ffe2);
93 }
94
TEST_F(FfElementObjTest,CanConstructBinomialElement)95 TEST_F(FfElementObjTest, CanConstructBinomialElement) {
96 FfElementObj ffe1(&ff, ff_str_1);
97 FiniteFieldObj ff2(ff, ffe1, 2);
98 FfElementObj ff2_e1(&ff2, ff_2_str);
99 EXPECT_NE(nullptr, (FfElement*)ff2_e1);
100 }
101
TEST_F(FfElementObjTest,CanCastConstToConstPointer)102 TEST_F(FfElementObjTest, CanCastConstToConstPointer) {
103 FfElementObj const ffe(&ff);
104 FfElement const* ffe_ptr = ffe;
105 (void)ffe_ptr;
106 }
107
TEST_F(FfElementObjTest,CanGetConstPointerFromConst)108 TEST_F(FfElementObjTest, CanGetConstPointerFromConst) {
109 FfElementObj const ffe(&ff);
110 FfElement const* ffe_ptr = ffe.getc();
111 (void)ffe_ptr;
112 }
113
114 /*
115 The following tests are expected to result in
116 compile time errors (by design)
117 */
118 /*
119 TEST_F(FfElementObjTest, CannotCastConstToNonConstPointer) {
120 FfElementObj const ffe(&ff);
121 FfElement * ffe_ptr = ffe;
122 (void) ffe_ptr;
123 }
124
125 TEST_F(FfElementObjTest, CannotGetNonConstPointerFromConst) {
126 FfElementObj const ffe(&ff);
127 FfElement * ffe_ptr = ffe.get();
128 (void) ffe_ptr;
129 }
130 */
131
TEST_F(FfElementObjTest,CanCastNonConstToConstPointer)132 TEST_F(FfElementObjTest, CanCastNonConstToConstPointer) {
133 FfElementObj ffe(&ff);
134 FfElement const* ffe_ptr = ffe;
135 (void)ffe_ptr;
136 }
137
TEST_F(FfElementObjTest,CanGetConstPointerFromNonConst)138 TEST_F(FfElementObjTest, CanGetConstPointerFromNonConst) {
139 FfElementObj ffe(&ff);
140 FfElement const* ffe_ptr = ffe.getc();
141 (void)ffe_ptr;
142 }
143
TEST_F(FfElementObjTest,CanCastNonConstToNonConstPointer)144 TEST_F(FfElementObjTest, CanCastNonConstToNonConstPointer) {
145 FfElementObj ffe(&ff);
146 FfElement* ffe_ptr = ffe;
147 (void)ffe_ptr;
148 }
149
TEST_F(FfElementObjTest,CanGetNonConstPointerFromNonConst)150 TEST_F(FfElementObjTest, CanGetNonConstPointerFromNonConst) {
151 FfElementObj ffe(&ff);
152 FfElement* ffe_ptr = ffe.get();
153 (void)ffe_ptr;
154 }
155
156 } // namespace
157