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 /// GetSigSize unit tests.
17 /*!\file */
18 
19 #include "epid/common-testhelper/epid_gtest-testhelper.h"
20 #include "gtest/gtest.h"
21 
22 extern "C" {
23 #include "epid/member/api.h"
24 }
25 
26 #include "epid/member/tiny/unittests/member-testhelper.h"
27 
28 namespace {
29 
TEST_F(EpidMemberTest,GetSigSizeReturnsSizeofBasicSigGivenNullPointer)30 TEST_F(EpidMemberTest, GetSigSizeReturnsSizeofBasicSigGivenNullPointer) {
31   size_t sig_size_without_sig_rl = sizeof(EpidSignature) - sizeof(NrProof);
32   EXPECT_EQ(sig_size_without_sig_rl, EpidGetSigSize(nullptr));
33 }
34 
TEST_F(EpidMemberTest,GetSigSizeReturnsCorrectValueGivenValidSigRl)35 TEST_F(EpidMemberTest, GetSigSizeReturnsCorrectValueGivenValidSigRl) {
36   SigRl srl = {{{0}}, {{0}}, {{0}}, {{{{0}, {0}}, {{0}, {0}}}}};
37   OctStr32 octstr32_0 = {0x00, 0x00, 0x00, 0x00};
38   OctStr32 octstr32_1 = {0x00, 0x00, 0x00, 0x01};
39   OctStr32 octstr32_2 = {0x00, 0x00, 0x00, 0x02};
40   OctStr32 octstr32_16 = {0x00, 0x00, 0x00, 0x10};
41   OctStr32 octstr32_256 = {0x00, 0x00, 0x01, 0x00};
42   OctStr32 octstr32_65536 = {0x00, 0x01, 0x00, 0x00};
43   OctStr32 octstr32_4294967295 = {0xff, 0xff, 0xff, 0xff};
44 
45   size_t one_entry_size = sizeof(NrProof);
46   size_t sig_size_0_entries = sizeof(EpidSignature) - one_entry_size;
47   size_t sig_size_1_entry = sig_size_0_entries + one_entry_size;
48   size_t sig_size_2_entries = sig_size_0_entries + 2 * one_entry_size;
49   size_t sig_size_16_entries = sig_size_0_entries + 16 * one_entry_size;
50   size_t sig_size_256_entries = sig_size_0_entries + 256 * one_entry_size;
51   size_t sig_size_65536_entries = sig_size_0_entries + 65536 * one_entry_size;
52   // no entries
53   srl.n2 = octstr32_0;
54   EXPECT_EQ(sig_size_0_entries, EpidGetSigSize(&srl));
55   // 1 entry
56   srl.n2 = octstr32_1;
57   EXPECT_EQ(sig_size_1_entry, EpidGetSigSize(&srl));
58   // 2 entries
59   srl.n2 = octstr32_2;
60   EXPECT_EQ(sig_size_2_entries, EpidGetSigSize(&srl));
61   // 16 entries
62   srl.n2 = octstr32_16;
63   EXPECT_EQ(sig_size_16_entries, EpidGetSigSize(&srl));
64   // 256 entries
65   srl.n2 = octstr32_256;
66   EXPECT_EQ(sig_size_256_entries, EpidGetSigSize(&srl));
67   // 65536 entries
68   srl.n2 = octstr32_65536;
69   EXPECT_EQ(sig_size_65536_entries, EpidGetSigSize(&srl));
70   // 4294967295 entries
71   srl.n2 = octstr32_4294967295;
72 #if (SIZE_MAX <= 0xFFFFFFFF)  // When size_t value is 32 bit or lower
73   EXPECT_EQ(sig_size_0_entries, EpidGetSigSize(&srl));
74 #else
75   size_t sig_size_4294967295_entries =
76       sig_size_0_entries + 4294967295 * one_entry_size;
77   EXPECT_EQ(sig_size_4294967295_entries, EpidGetSigSize(&srl));
78 #endif
79 }
80 
TEST_F(EpidMemberTest,GetSigSizeReturnsCorrectValueGivenValidSigRlUsingIKGFData)81 TEST_F(EpidMemberTest,
82        GetSigSizeReturnsCorrectValueGivenValidSigRlUsingIKGFData) {
83   const std::vector<uint8_t> sigrl_bin = {
84 #include "epid/common-testhelper/testdata/ikgf/groupa/sigrl.inc"
85   };
86 
87   SigRl const* sig_rl = reinterpret_cast<const SigRl*>(sigrl_bin.data());
88   size_t sig_size_3_entries =
89       sizeof(EpidSignature) - sizeof(NrProof) + 3 * sizeof(NrProof);
90   // 3 entries
91   EXPECT_EQ(sig_size_3_entries, EpidGetSigSize(sig_rl));
92 }
93 
94 }  // namespace
95