octstrconvert-test.cc 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 OctStr2Bnu unit tests.
  19. *
  20. * OctStr2Bnu is an internal function used in the IPP implementation of the
  21. * math libraries. These tests can be omitted if you do not use this function.
  22. */
  23. #include "epid/common-testhelper/epid_gtest-testhelper.h"
  24. #include "epid/common/stdtypes.h"
  25. #include "gtest/gtest.h"
  26. extern "C" {
  27. #include "epid/common/math/src/bignum-internal.h"
  28. }
  29. namespace {
  30. const uint8_t bnstr1[] = {0x01, 0x02, 0x03, 0x04};
  31. const uint8_t bnstr2[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
  32. uint32_t bnustr1[sizeof(bnstr1) / sizeof(uint32_t)] = {0x01020304};
  33. uint32_t bnustr2[sizeof(bnstr2) / sizeof(uint32_t)] = {0x05060708, 0x01020304};
  34. TEST(OctStr2Bnu, octstr2bnuFailsGivenNullBnu) {
  35. int len = OctStr2Bnu(nullptr, bnstr1, sizeof(bnstr1) / sizeof(uint8_t));
  36. EXPECT_EQ(-1, len);
  37. }
  38. TEST(OctStr2Bnu, octstr2bnuFailsGivenNullOctstr) {
  39. uint32_t bnustr_res[sizeof(bnstr1) / sizeof(uint32_t)] = {0};
  40. int len = OctStr2Bnu(bnustr_res, nullptr, sizeof(bnstr1) / sizeof(uint8_t));
  41. EXPECT_EQ(-1, len);
  42. }
  43. TEST(OctStr2Bnu, octstr2bnuFailsGivenInvalidOctsrtLen) {
  44. uint32_t bnustr_res[sizeof(bnstr1) / sizeof(uint32_t)] = {0};
  45. int len = OctStr2Bnu(bnustr_res, bnstr1, -1);
  46. EXPECT_EQ(-1, len);
  47. len = OctStr2Bnu(bnustr_res, bnstr1, 0);
  48. EXPECT_EQ(-1, len);
  49. len = OctStr2Bnu(bnustr_res, bnstr1, 3);
  50. EXPECT_EQ(-1, len);
  51. len = OctStr2Bnu(bnustr_res, bnstr1, 5);
  52. EXPECT_EQ(-1, len);
  53. }
  54. TEST(OctStr2Bnu, octstr2bnuWorksGivenOctstr1) {
  55. uint32_t bnustr_res[sizeof(bnstr1) / sizeof(uint32_t)] = {0};
  56. int len = OctStr2Bnu(bnustr_res, bnstr1, sizeof(bnstr1) / sizeof(uint8_t));
  57. EXPECT_EQ(1, len);
  58. EXPECT_EQ(0,
  59. memcmp(bnustr1, bnustr_res, sizeof(bnustr_res) / sizeof(uint32_t)))
  60. << "OctStr2Bnu: bnu string result does not match with predefined value\n";
  61. }
  62. TEST(OctStr2Bnu, octstr2bnuWorksGivenOctstr2) {
  63. uint32_t bnustr_res[sizeof(bnstr2) / sizeof(uint32_t)] = {0};
  64. int len = OctStr2Bnu(bnustr_res, bnstr2, sizeof(bnstr2) / sizeof(uint8_t));
  65. EXPECT_EQ(2, len);
  66. EXPECT_EQ(0,
  67. memcmp(bnustr2, bnustr_res, sizeof(bnustr_res) / sizeof(uint32_t)))
  68. << "OctStr2Bnu: bnu string result does not match with predefined value\n";
  69. }
  70. } // namespace