ffelement_wrapper-test.cc 4.7 KB

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