getrandom-test.cc 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. /// TPM GetRandom unit tests.
  17. /*! \file */
  18. #include <vector>
  19. #include "epid/common-testhelper/epid_gtest-testhelper.h"
  20. #include "gtest/gtest.h"
  21. #include "epid/common-testhelper/epid2params_wrapper-testhelper.h"
  22. #include "epid/common-testhelper/prng-testhelper.h"
  23. #include "epid/member/tpm2/unittests/tpm2-testhelper.h"
  24. extern "C" {
  25. #include "epid/member/tpm2/context.h"
  26. #include "epid/member/tpm2/getrandom.h"
  27. }
  28. namespace {
  29. //////////////////////////////////////////////////////////////////////////
  30. // Tpm2GetRandom Tests
  31. TEST_F(EpidTpm2Test, GetRandomFailsGivenNullParameters) {
  32. uint8_t output[48] = {0};
  33. Prng my_prng;
  34. my_prng.set_seed(0x1234);
  35. Epid2ParamsObj epid2params;
  36. Tpm2CtxObj tpm(&Prng::Generate, &my_prng, nullptr, epid2params);
  37. EXPECT_EQ(kEpidBadArgErr, Tpm2GetRandom(nullptr, 48 * 8, output));
  38. EXPECT_EQ(kEpidBadArgErr, Tpm2GetRandom(tpm, 48 * 8, nullptr));
  39. }
  40. TEST_F(EpidTpm2Test, GetRandomReturnsDifferentBufs) {
  41. Prng my_prng;
  42. Epid2ParamsObj epid2params;
  43. Tpm2CtxObj tpm(&Prng::Generate, &my_prng, nullptr, epid2params);
  44. int length = 10;
  45. std::vector<uint8_t> buf1(length, (uint8_t)0);
  46. std::vector<uint8_t> buf2(length, (uint8_t)0);
  47. EXPECT_EQ(kEpidNoErr, Tpm2GetRandom(tpm, length * CHAR_BIT, buf1.data()));
  48. EXPECT_EQ(kEpidNoErr, Tpm2GetRandom(tpm, length * CHAR_BIT, buf2.data()));
  49. EXPECT_NE(buf1, buf2);
  50. }
  51. TEST_F(EpidTpm2Test, GetRandomCanGenerateLongStream) {
  52. Prng my_prng;
  53. Epid2ParamsObj epid2params;
  54. Tpm2CtxObj tpm(&Prng::Generate, &my_prng, nullptr, epid2params);
  55. int length = 1000;
  56. std::vector<uint8_t> zeros(length, (uint8_t)0);
  57. std::vector<uint8_t> buf = zeros;
  58. EXPECT_EQ(kEpidNoErr, Tpm2GetRandom(tpm, length * CHAR_BIT, buf.data()));
  59. EXPECT_NE(buf, zeros);
  60. }
  61. } // namespace