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 ComputePreSig unit tests.
20 */
21 #include <algorithm>
22 #include <cstring>
23 #include <limits>
24 #include <vector>
25
26 #include "epid/common-testhelper/epid_gtest-testhelper.h"
27 #include "gtest/gtest.h"
28
29 extern "C" {
30 #include "epid/member/api.h"
31 }
32
33 #include "epid/common-testhelper/errors-testhelper.h"
34 #include "epid/common-testhelper/prng-testhelper.h"
35 #include "epid/member/unittests/member-testhelper.h"
36
37 /// Count of elements in array
38 #define COUNT_OF(A) (sizeof(A) / sizeof((A)[0]))
39
40 namespace {
41
42 ///////////////////////////////////////////////////////////////////////
43 // EpidAddPreSigs
TEST_F(EpidMemberTest,AddPreSigsFailsGivenNullPointer)44 TEST_F(EpidMemberTest, AddPreSigsFailsGivenNullPointer) {
45 Prng my_prng;
46 MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
47 this->kMemberPrecomp, &Prng::Generate, &my_prng);
48
49 EXPECT_EQ(kEpidBadArgErr, EpidAddPreSigs(nullptr, 1));
50 }
51
TEST_F(EpidMemberTest,AddPreSigsFailsGivenHugeNumberOfPreSigs)52 TEST_F(EpidMemberTest, AddPreSigsFailsGivenHugeNumberOfPreSigs) {
53 Prng my_prng;
54 MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
55 this->kMemberPrecomp, &Prng::Generate, &my_prng);
56
57 // number_presigs = 0x80..01 of size equal to sizeof(size_t)
58 EXPECT_NE(kEpidNoErr, EpidAddPreSigs(member, (SIZE_MAX >> 1) + 2));
59 }
60
TEST_F(EpidMemberTest,AddPreSigsComputesSpecifiedNumberOfPresigsIfInputPresigsNull)61 TEST_F(EpidMemberTest,
62 AddPreSigsComputesSpecifiedNumberOfPresigsIfInputPresigsNull) {
63 Prng my_prng;
64 MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
65 this->kMemberPrecomp, &Prng::Generate, &my_prng);
66
67 ASSERT_EQ(kEpidNoErr, EpidAddPreSigs(member, 2));
68 ASSERT_EQ(kEpidNoErr, EpidAddPreSigs(member, 1));
69 // request to generate 0 pre-computed signatures do nothing
70 ASSERT_EQ(kEpidNoErr, EpidAddPreSigs(member, 0));
71 EXPECT_EQ((size_t)3, EpidGetNumPreSigs(member));
72 }
73
TEST_F(EpidMemberTest,AddPreSigsAddsCorrectNumberOfPresigsGivenValidInput)74 TEST_F(EpidMemberTest, AddPreSigsAddsCorrectNumberOfPresigsGivenValidInput) {
75 Prng my_prng;
76 MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
77 this->kMemberPrecomp, &Prng::Generate, &my_prng);
78
79 const size_t presigs1_added = 2;
80 const size_t presigs2_added = 3;
81
82 // add
83 ASSERT_EQ(kEpidNoErr, EpidAddPreSigs(member, presigs1_added));
84 // extend
85 ASSERT_EQ(kEpidNoErr, EpidAddPreSigs(member, presigs2_added));
86 // add empty pre-computed signatures array does not affect internal pool
87 ASSERT_EQ(kEpidNoErr, EpidAddPreSigs(member, 0));
88 EXPECT_EQ(presigs1_added + presigs2_added, EpidGetNumPreSigs(member));
89 }
90
91 ///////////////////////////////////////////////////////////////////////
92 // EpidGetNumPreSigs
TEST_F(EpidMemberTest,GetNumPreSigsReturnsZeroGivenNullptr)93 TEST_F(EpidMemberTest, GetNumPreSigsReturnsZeroGivenNullptr) {
94 EXPECT_EQ((size_t)0, EpidGetNumPreSigs(nullptr));
95 }
96
TEST_F(EpidMemberTest,NumPreSigsForNewleyCreatedContextIsZero)97 TEST_F(EpidMemberTest, NumPreSigsForNewleyCreatedContextIsZero) {
98 Prng my_prng;
99 MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
100 this->kMemberPrecomp, &Prng::Generate, &my_prng);
101
102 EXPECT_EQ((size_t)0, EpidGetNumPreSigs(member));
103 }
104
TEST_F(EpidMemberTest,GetNumPreSigsReturnsNumberOfAddedPresigs)105 TEST_F(EpidMemberTest, GetNumPreSigsReturnsNumberOfAddedPresigs) {
106 Prng my_prng;
107 MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
108 this->kMemberPrecomp, &Prng::Generate, &my_prng);
109
110 const size_t presigs_added = 5;
111
112 THROW_ON_EPIDERR(EpidAddPreSigs(member, presigs_added));
113 EXPECT_EQ(presigs_added, EpidGetNumPreSigs(member));
114 }
115
116 } // namespace
117