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/tiny/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,DISABLED_AddPreSigsFailsGivenNullPointer)44 TEST_F(EpidMemberTest, DISABLED_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,DISABLED_AddPreSigsFailsGivenHugeNumberOfPreSigs)52 TEST_F(EpidMemberTest, DISABLED_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,DISABLED_AddPreSigsComputesSpecifiedNumberOfPresigsIfInputPresigsNull)61 TEST_F(EpidMemberTest,
62        DISABLED_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,DISABLED_AddPreSigsAddsCorrectNumberOfPresigsGivenValidInput)74 TEST_F(EpidMemberTest,
75        DISABLED_AddPreSigsAddsCorrectNumberOfPresigsGivenValidInput) {
76   Prng my_prng;
77   MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
78                       this->kMemberPrecomp, &Prng::Generate, &my_prng);
79 
80   const size_t presigs1_added = 2;
81   const size_t presigs2_added = 3;
82 
83   // add
84   ASSERT_EQ(kEpidNoErr, EpidAddPreSigs(member, presigs1_added));
85   // extend
86   ASSERT_EQ(kEpidNoErr, EpidAddPreSigs(member, presigs2_added));
87   // add empty pre-computed signatures array does not affect internal pool
88   ASSERT_EQ(kEpidNoErr, EpidAddPreSigs(member, 0));
89   EXPECT_EQ(presigs1_added + presigs2_added, EpidGetNumPreSigs(member));
90 }
91 
92 ///////////////////////////////////////////////////////////////////////
93 // EpidGetNumPreSigs
TEST_F(EpidMemberTest,DISABLED_GetNumPreSigsReturnsZeroGivenNullptr)94 TEST_F(EpidMemberTest, DISABLED_GetNumPreSigsReturnsZeroGivenNullptr) {
95   EXPECT_EQ((size_t)0, EpidGetNumPreSigs(nullptr));
96 }
97 
TEST_F(EpidMemberTest,DISABLED_NumPreSigsForNewleyCreatedContextIsZero)98 TEST_F(EpidMemberTest, DISABLED_NumPreSigsForNewleyCreatedContextIsZero) {
99   Prng my_prng;
100   MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
101                       this->kMemberPrecomp, &Prng::Generate, &my_prng);
102 
103   EXPECT_EQ((size_t)0, EpidGetNumPreSigs(member));
104 }
105 
TEST_F(EpidMemberTest,DISABLED_GetNumPreSigsReturnsNumberOfAddedPresigs)106 TEST_F(EpidMemberTest, DISABLED_GetNumPreSigsReturnsNumberOfAddedPresigs) {
107   Prng my_prng;
108   MemberCtxObj member(this->kGroupPublicKey, this->kMemberPrivateKey,
109                       this->kMemberPrecomp, &Prng::Generate, &my_prng);
110 
111   const size_t presigs_added = 5;
112 
113   THROW_ON_EPIDERR(EpidAddPreSigs(member, presigs_added));
114   EXPECT_EQ(presigs_added, EpidGetNumPreSigs(member));
115 }
116 
117 }  // namespace
118