bignum_wrapper-test.cc 4.7 KB

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