ffelement_wrapper-test.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*############################################################################
  2. # Copyright 2016 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 FfElement C++ wrapper unit tests.
  19. */
  20. #include "gtest/gtest.h"
  21. #include "epid/common-testhelper/errors-testhelper.h"
  22. #include "epid/common-testhelper/ffelement_wrapper-testhelper.h"
  23. #include "epid/common-testhelper/finite_field_wrapper-testhelper.h"
  24. extern "C" {
  25. #include "epid/common/math/bignum.h"
  26. }
  27. namespace {
  28. // Use Test Fixture for SetUp and TearDown
  29. class FfElementObjTest : public ::testing::Test {
  30. public:
  31. static FiniteFieldObj ff;
  32. static const BigNumStr prime_str;
  33. static const FpElemStr ff_str_1;
  34. static const FpElemStr ff_str_2;
  35. static const Fq2ElemStr ff_2_str;
  36. };
  37. /// Intel(R) EPID 2.0 parameter p
  38. const BigNumStr FfElementObjTest::prime_str = {
  39. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0xF0, 0xCD, 0x46, 0xE5, 0xF2,
  40. 0x5E, 0xEE, 0x71, 0xA4, 0x9E, 0x0C, 0xDC, 0x65, 0xFB, 0x12, 0x99,
  41. 0x92, 0x1A, 0xF6, 0x2D, 0x53, 0x6C, 0xD1, 0x0B, 0x50, 0x0D};
  42. const FpElemStr FfElementObjTest::ff_str_1 = {
  43. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  44. 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
  45. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  46. };
  47. const FpElemStr FfElementObjTest::ff_str_2 = {
  48. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49. 0x00, 0x00, 0xDC, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA4, 0x00, 0x00,
  50. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  51. };
  52. const Fq2ElemStr FfElementObjTest::ff_2_str = {
  53. // 1
  54. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  55. 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  56. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
  57. // 2
  58. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  59. 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  60. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20};
  61. FiniteFieldObj FfElementObjTest::ff(prime_str);
  62. TEST_F(FfElementObjTest, ObjDefaultConstructedIsNotNull) {
  63. FfElementObj ffe(&ff);
  64. EXPECT_NE(nullptr, (FfElement*)ffe);
  65. }
  66. TEST_F(FfElementObjTest, AssignmentDoesNotCopyPointer) {
  67. FfElementObj ffe1(&ff, ff_str_1);
  68. FfElementObj ffe2(&ff, ff_str_2);
  69. EXPECT_NE((FfElement*)ffe1, (FfElement*)ffe2);
  70. ffe1 = ffe2;
  71. EXPECT_NE((FfElement*)ffe1, (FfElement*)ffe2);
  72. }
  73. TEST_F(FfElementObjTest, CopyConstructorDoesNotCopyPointer) {
  74. FfElementObj ffe1(&ff, ff_str_1);
  75. FfElementObj ffe2(ffe1);
  76. EXPECT_NE((FfElement*)ffe1, (FfElement*)ffe2);
  77. }
  78. TEST_F(FfElementObjTest, CanConstructBinomialElement) {
  79. FfElementObj ffe1(&ff, ff_str_1);
  80. FiniteFieldObj ff2(ff, ffe1, 2);
  81. FfElementObj ff2_e1(&ff2, ff_2_str);
  82. EXPECT_NE(nullptr, (FfElement*)ff2_e1);
  83. }
  84. TEST_F(FfElementObjTest, CanCastConstToConstPointer) {
  85. FfElementObj const ffe(&ff);
  86. FfElement const* ffe_ptr = ffe;
  87. (void)ffe_ptr;
  88. }
  89. TEST_F(FfElementObjTest, CanGetConstPointerFromConst) {
  90. FfElementObj const ffe(&ff);
  91. FfElement const* ffe_ptr = ffe.getc();
  92. (void)ffe_ptr;
  93. }
  94. /*
  95. The following tests are expected to result in
  96. compile time errors (by design)
  97. */
  98. /*
  99. TEST_F(FfElementObjTest, CannotCastConstToNonConstPointer) {
  100. FfElementObj const ffe(&ff);
  101. FfElement * ffe_ptr = ffe;
  102. (void) ffe_ptr;
  103. }
  104. TEST_F(FfElementObjTest, CannotGetNonConstPointerFromConst) {
  105. FfElementObj const ffe(&ff);
  106. FfElement * ffe_ptr = ffe.get();
  107. (void) ffe_ptr;
  108. }
  109. */
  110. TEST_F(FfElementObjTest, CanCastNonConstToConstPointer) {
  111. FfElementObj ffe(&ff);
  112. FfElement const* ffe_ptr = ffe;
  113. (void)ffe_ptr;
  114. }
  115. TEST_F(FfElementObjTest, CanGetConstPointerFromNonConst) {
  116. FfElementObj ffe(&ff);
  117. FfElement const* ffe_ptr = ffe.getc();
  118. (void)ffe_ptr;
  119. }
  120. TEST_F(FfElementObjTest, CanCastNonConstToNonConstPointer) {
  121. FfElementObj ffe(&ff);
  122. FfElement* ffe_ptr = ffe;
  123. (void)ffe_ptr;
  124. }
  125. TEST_F(FfElementObjTest, CanGetNonConstPointerFromNonConst) {
  126. FfElementObj ffe(&ff);
  127. FfElement* ffe_ptr = ffe.get();
  128. (void)ffe_ptr;
  129. }
  130. } // namespace