bignum_wrapper-test.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 Bignum C++ wrapper unit tests.
  19. */
  20. #include "epid/common-testhelper/epid_gtest-testhelper.h"
  21. #include "gtest/gtest.h"
  22. #include "epid/common-testhelper/bignum_wrapper-testhelper.h"
  23. #include "epid/common-testhelper/errors-testhelper.h"
  24. extern "C" {
  25. #include "epid/common/math/bignum.h"
  26. #include "epid/common/src/memory.h"
  27. }
  28. namespace {
  29. // Use Test Fixture for SetUp and TearDown
  30. class BigNumObjTest : public ::testing::Test {
  31. public:
  32. static const BigNumStr str_0;
  33. static const std::vector<unsigned char> vec_0;
  34. };
  35. const BigNumStr BigNumObjTest::str_0 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  36. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  37. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  38. const std::vector<unsigned char> BigNumObjTest::vec_0 = {0, 0, 0, 0,
  39. 0, 0, 0, 0};
  40. TEST_F(BigNumObjTest, ObjDefaultConstructedIsNotNull) {
  41. BigNumObj bn;
  42. EXPECT_NE(nullptr, (BigNum*)bn);
  43. }
  44. TEST_F(BigNumObjTest, ObjConstructedWithSizeIsNotNull) {
  45. BigNumObj bn1(1);
  46. EXPECT_NE(nullptr, (BigNum*)bn1);
  47. BigNumObj bn32(32);
  48. EXPECT_NE(nullptr, (BigNum*)bn32);
  49. }
  50. TEST_F(BigNumObjTest, AssignmentDoesNotCopyPointer) {
  51. BigNumObj bn1;
  52. BigNumObj bn2;
  53. EXPECT_NE((BigNum*)bn1, (BigNum*)bn2);
  54. bn1 = bn2;
  55. EXPECT_NE((BigNum*)bn1, (BigNum*)bn2);
  56. }
  57. TEST_F(BigNumObjTest, CopyConstructorDoesNotCopyPointer) {
  58. BigNumObj bn1;
  59. BigNumObj bn2(bn1);
  60. EXPECT_NE((BigNum*)bn1, (BigNum*)bn2);
  61. }
  62. TEST_F(BigNumObjTest, ConstructorDoesNotThrow) {
  63. BigNumObj bn1;
  64. BigNumObj bn2(32);
  65. BigNumObj bn3(32, this->str_0);
  66. BigNumObj bn4(32, this->vec_0);
  67. BigNumObj bn5(this->str_0);
  68. BigNumObj bn6(this->vec_0);
  69. EXPECT_NE((BigNum*)bn1, (BigNum*)bn2);
  70. EXPECT_NE((BigNum*)bn1, (BigNum*)bn3);
  71. EXPECT_NE((BigNum*)bn1, (BigNum*)bn4);
  72. EXPECT_NE((BigNum*)bn1, (BigNum*)bn5);
  73. EXPECT_NE((BigNum*)bn1, (BigNum*)bn6);
  74. EXPECT_NE((BigNum*)bn2, (BigNum*)bn1);
  75. EXPECT_NE((BigNum*)bn2, (BigNum*)bn3);
  76. EXPECT_NE((BigNum*)bn2, (BigNum*)bn4);
  77. EXPECT_NE((BigNum*)bn2, (BigNum*)bn5);
  78. EXPECT_NE((BigNum*)bn2, (BigNum*)bn6);
  79. EXPECT_NE((BigNum*)bn3, (BigNum*)bn1);
  80. EXPECT_NE((BigNum*)bn3, (BigNum*)bn2);
  81. EXPECT_NE((BigNum*)bn3, (BigNum*)bn4);
  82. EXPECT_NE((BigNum*)bn3, (BigNum*)bn5);
  83. EXPECT_NE((BigNum*)bn3, (BigNum*)bn6);
  84. EXPECT_NE((BigNum*)bn4, (BigNum*)bn1);
  85. EXPECT_NE((BigNum*)bn4, (BigNum*)bn2);
  86. EXPECT_NE((BigNum*)bn4, (BigNum*)bn3);
  87. EXPECT_NE((BigNum*)bn4, (BigNum*)bn5);
  88. EXPECT_NE((BigNum*)bn4, (BigNum*)bn6);
  89. EXPECT_NE((BigNum*)bn5, (BigNum*)bn1);
  90. EXPECT_NE((BigNum*)bn5, (BigNum*)bn2);
  91. EXPECT_NE((BigNum*)bn5, (BigNum*)bn3);
  92. EXPECT_NE((BigNum*)bn5, (BigNum*)bn4);
  93. EXPECT_NE((BigNum*)bn5, (BigNum*)bn6);
  94. EXPECT_NE((BigNum*)bn6, (BigNum*)bn1);
  95. EXPECT_NE((BigNum*)bn6, (BigNum*)bn2);
  96. EXPECT_NE((BigNum*)bn6, (BigNum*)bn3);
  97. EXPECT_NE((BigNum*)bn6, (BigNum*)bn4);
  98. EXPECT_NE((BigNum*)bn6, (BigNum*)bn5);
  99. }
  100. TEST_F(BigNumObjTest, CanCastConstToConstPointer) {
  101. BigNumObj const bn;
  102. BigNum const* bn_ptr = bn;
  103. (void)bn_ptr;
  104. }
  105. TEST_F(BigNumObjTest, CanGetConstPointerFromConst) {
  106. BigNumObj const bn;
  107. BigNum const* bn_ptr = bn.getc();
  108. (void)bn_ptr;
  109. }
  110. /*
  111. The following tests are expected to result in
  112. compile time errors (by design)
  113. */
  114. /*
  115. TEST_F(BigNumObjTest, CannotCastConstToNonConstPointer) {
  116. BigNumObj const bn;
  117. BigNum * bn_ptr = bn;
  118. (void) bn_ptr;
  119. }
  120. TEST_F(BigNumObjTest, CannotGetNonConstPointerFromConst) {
  121. BigNumObj const bn;
  122. BigNum * bn_ptr = bn.get();
  123. (void) bn_ptr;
  124. }
  125. */
  126. TEST_F(BigNumObjTest, CanCastNonConstToConstPointer) {
  127. BigNumObj bn;
  128. BigNum const* bn_ptr = bn;
  129. (void)bn_ptr;
  130. }
  131. TEST_F(BigNumObjTest, CanGetConstPointerFromNonConst) {
  132. BigNumObj bn;
  133. BigNum const* bn_ptr = bn.getc();
  134. (void)bn_ptr;
  135. }
  136. TEST_F(BigNumObjTest, CanCastNonConstToNonConstPointer) {
  137. BigNumObj bn;
  138. BigNum* bn_ptr = bn;
  139. (void)bn_ptr;
  140. }
  141. TEST_F(BigNumObjTest, CanGetNonConstPointerFromNonConst) {
  142. BigNumObj bn;
  143. BigNum* bn_ptr = bn.get();
  144. (void)bn_ptr;
  145. }
  146. } // namespace