presig-test.cc 4.0 KB

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