onetimepad-test.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*############################################################################
  2. # Copyright 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. /// Unit tests for OneTimePad.
  17. /*! \file */
  18. #include <gtest/gtest.h>
  19. #include "epid/member/tiny/math/unittests/onetimepad.h"
  20. namespace {
  21. TEST(OneTimePadTest, GenerateFailsWhenDefaultConstructedWithoutInit) {
  22. OneTimePad otp;
  23. EXPECT_EQ(0u, otp.BitsConsumed());
  24. std::vector<unsigned int> actual({0, 0});
  25. EXPECT_NE(0, OneTimePad::Generate(actual.data(), 8, &otp));
  26. }
  27. TEST(OneTimePadTest, GeneratesCorrectDataWhenConstructedWithUint8s) {
  28. std::vector<unsigned int> actual1({0, 0});
  29. std::vector<unsigned int> actual2({0, 0});
  30. const std::vector<unsigned int> expected1({0x07050301, 0});
  31. const std::vector<unsigned int> expected2({0x0e0c0a09, 0});
  32. OneTimePad otp({0x01, 0x03, 0x05, 0x07, 0x09, 0x0a, 0x0c, 0x0e});
  33. EXPECT_EQ(0u, otp.BitsConsumed());
  34. EXPECT_EQ(0, OneTimePad::Generate(actual1.data(), 32, &otp));
  35. EXPECT_EQ(expected1, actual1);
  36. EXPECT_EQ(32u, otp.BitsConsumed());
  37. EXPECT_EQ(0, OneTimePad::Generate(actual2.data(), 32, &otp));
  38. EXPECT_EQ(expected2, actual2);
  39. EXPECT_EQ(64u, otp.BitsConsumed());
  40. }
  41. TEST(OneTimePadTest, GeneratesCorrectDataWhenInitilizedWithUint8s) {
  42. std::vector<unsigned int> actual1({0, 0});
  43. std::vector<unsigned int> actual2({0, 0});
  44. const std::vector<unsigned int> expected1({0x07050301, 0});
  45. const std::vector<unsigned int> expected2({0x0e0c0a09, 0});
  46. OneTimePad otp;
  47. otp.InitUint8({0x01, 0x03, 0x05, 0x07, 0x09, 0x0a, 0x0c, 0x0e});
  48. EXPECT_EQ(0u, otp.BitsConsumed());
  49. EXPECT_EQ(0, OneTimePad::Generate(actual1.data(), 32, &otp));
  50. EXPECT_EQ(expected1, actual1);
  51. EXPECT_EQ(32u, otp.BitsConsumed());
  52. EXPECT_EQ(0, OneTimePad::Generate(actual2.data(), 32, &otp));
  53. EXPECT_EQ(expected2, actual2);
  54. EXPECT_EQ(64u, otp.BitsConsumed());
  55. }
  56. TEST(OneTimePadTest, GeneratesCorrectDataWhenInitilizedWithUint32s) {
  57. std::vector<uint32_t> actual({0x00});
  58. const std::vector<uint32_t> expected({0x01});
  59. std::vector<uint32_t> actual2({0x00});
  60. const std::vector<uint32_t> expected2({0x01020304});
  61. OneTimePad otp({0x01, 0x03, 0x05, 0x07, 0x09, 0x0a, 0x0c, 0x0e});
  62. otp.InitUint32({0x01, 0x01020304});
  63. EXPECT_EQ(0u, otp.BitsConsumed());
  64. EXPECT_EQ(0, OneTimePad::Generate(actual.data(), 32, &otp));
  65. EXPECT_EQ(32u, otp.BitsConsumed());
  66. EXPECT_EQ(expected, actual);
  67. EXPECT_EQ(0, OneTimePad::Generate(actual2.data(), 32, &otp));
  68. EXPECT_EQ(expected2, actual2);
  69. EXPECT_EQ(64u, otp.BitsConsumed());
  70. }
  71. TEST(OneTimePadTest, GeneratesSingleBytesCorrectly) {
  72. OneTimePad otp({0x01, 0x03, 0x05, 0x07, 0x09, 0x0a, 0x0c, 0x0e});
  73. std::vector<uint8_t> expected1({0x01, 0x00, 0x00, 0x00});
  74. std::vector<uint8_t> expected2({0x03, 0x00, 0x00, 0x00});
  75. std::vector<uint8_t> expected3({0x05, 0x00, 0x00, 0x00});
  76. std::vector<uint8_t> actual({0, 0, 0, 0});
  77. EXPECT_EQ(0, OneTimePad::Generate((uint32_t*)actual.data(), 8, &otp));
  78. EXPECT_EQ(8u, otp.BitsConsumed());
  79. EXPECT_EQ(expected1, actual);
  80. EXPECT_EQ(0, OneTimePad::Generate((uint32_t*)actual.data(), 8, &otp));
  81. EXPECT_EQ(16u, otp.BitsConsumed());
  82. EXPECT_EQ(expected2, actual);
  83. EXPECT_EQ(0, OneTimePad::Generate((uint32_t*)actual.data(), 8, &otp));
  84. EXPECT_EQ(24u, otp.BitsConsumed());
  85. EXPECT_EQ(expected3, actual);
  86. }
  87. TEST(OneTimePadTest, GenerateRejectsNullPtr) {
  88. OneTimePad otp(8);
  89. EXPECT_NE(0, OneTimePad::Generate(nullptr, 32, &otp));
  90. }
  91. TEST(OneTimePadTest, GenerateRejectsNegativeBits) {
  92. OneTimePad otp(8);
  93. std::vector<unsigned int> actual({0, 0});
  94. EXPECT_NE(0, OneTimePad::Generate(actual.data(), -32, &otp));
  95. }
  96. TEST(OneTimePadTest, GenerateRejectsZeroBits) {
  97. OneTimePad otp(8);
  98. std::vector<unsigned int> actual({0, 0});
  99. EXPECT_NE(0, OneTimePad::Generate(actual.data(), 0, &otp));
  100. }
  101. TEST(OneTimePadTest, GenerateRejectsTooLargeRequest) {
  102. OneTimePad otp(8);
  103. std::vector<unsigned int> actual({0, 0});
  104. EXPECT_EQ(0, OneTimePad::Generate(actual.data(), 32, &otp));
  105. EXPECT_EQ(0, OneTimePad::Generate(actual.data(), 32, &otp));
  106. EXPECT_NE(0, OneTimePad::Generate(actual.data(), 32, &otp));
  107. }
  108. TEST(OneTimePadTest, GenerateRejectsUnsupportedBitRequest) {
  109. OneTimePad otp(8);
  110. std::vector<unsigned int> actual({0, 0});
  111. EXPECT_NE(0, OneTimePad::Generate(actual.data(), 31, &otp));
  112. }
  113. TEST(OneTimePadTest, GenerateCoddlesDevelopersWhoDoNotCheckReturnValues) {
  114. OneTimePad otp(4);
  115. uint32_t word;
  116. EXPECT_EQ(0, OneTimePad::Generate(&word, 32, &otp));
  117. EXPECT_NE(0, OneTimePad::Generate(&word, 32, &otp));
  118. EXPECT_THROW(OneTimePad::Generate(&word, 32, &otp), std::runtime_error);
  119. }
  120. } // namespace