octstrconvert-test.cc 2.9 KB

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