context-test.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 Context unit tests.
  17. /*! \file */
  18. #include "epid/common-testhelper/epid_gtest-testhelper.h"
  19. #include "gtest/gtest.h"
  20. #include "epid/common-testhelper/epid2params_wrapper-testhelper.h"
  21. #include "epid/common-testhelper/errors-testhelper.h"
  22. #include "epid/common-testhelper/mem_params-testhelper.h"
  23. #include "epid/common-testhelper/prng-testhelper.h"
  24. #include "epid/member/tpm2/unittests/tpm2-testhelper.h"
  25. extern "C" {
  26. #include "epid/common/src/epid2params.h"
  27. #include "epid/member/tpm2/context.h"
  28. #include "epid/member/tpm2/load_external.h"
  29. }
  30. namespace {
  31. //////////////////////////////////////////////////////////////////////////
  32. // Tpm2CreateContext Tests
  33. TEST_F(EpidTpm2Test, CreateFailsGivenNullParameters) {
  34. Tpm2Ctx* ctx = nullptr;
  35. Prng my_prng;
  36. BitSupplier rnd_func = NULL;
  37. void* rnd_param = NULL;
  38. const FpElemStr* f = NULL;
  39. MemberParams mem_params = {0};
  40. Epid2ParamsObj epid_params;
  41. SetMemberParams(&Prng::Generate, &my_prng, nullptr, &mem_params);
  42. EXPECT_EQ(kEpidBadArgErr, Tpm2CreateContext(nullptr, epid_params, &rnd_func,
  43. &rnd_param, &f, &ctx));
  44. EXPECT_EQ(kEpidBadArgErr, Tpm2CreateContext(&mem_params, nullptr, &rnd_func,
  45. &rnd_param, &f, &ctx));
  46. EXPECT_EQ(kEpidBadArgErr, Tpm2CreateContext(&mem_params, epid_params, nullptr,
  47. &rnd_param, &f, &ctx));
  48. EXPECT_EQ(kEpidBadArgErr, Tpm2CreateContext(&mem_params, epid_params,
  49. &rnd_func, nullptr, &f, &ctx));
  50. EXPECT_EQ(kEpidBadArgErr,
  51. Tpm2CreateContext(&mem_params, epid_params, &rnd_func, &rnd_param,
  52. nullptr, &ctx));
  53. EXPECT_EQ(kEpidBadArgErr,
  54. Tpm2CreateContext(&mem_params, epid_params, &rnd_func, &rnd_param,
  55. &f, nullptr));
  56. }
  57. TEST_F(EpidTpm2Test, CreateSucceedsGivenValidParameters) {
  58. Tpm2Ctx* ctx = nullptr;
  59. Prng my_prng;
  60. BitSupplier rnd_func = NULL;
  61. void* rnd_param = NULL;
  62. const FpElemStr* f = NULL;
  63. MemberParams mem_params = {0};
  64. Epid2ParamsObj epid_params;
  65. SetMemberParams(&Prng::Generate, &my_prng, nullptr, &mem_params);
  66. EXPECT_EQ(kEpidNoErr, Tpm2CreateContext(&mem_params, epid_params, &rnd_func,
  67. &rnd_param, &f, &ctx));
  68. Tpm2DeleteContext(&ctx);
  69. }
  70. //////////////////////////////////////////////////////////////////////////
  71. // Tpm2DeleteContext Tests
  72. TEST_F(EpidTpm2Test, DeleteWorksGivenNullTpm2Ctx) {
  73. Tpm2DeleteContext(nullptr);
  74. Tpm2Ctx* ctx = nullptr;
  75. Tpm2DeleteContext(&ctx);
  76. }
  77. TEST_F(EpidTpm2Test, DeleteNullsTpm2Ctx) {
  78. Tpm2Ctx* ctx = nullptr;
  79. Prng my_prng;
  80. BitSupplier rnd_func = NULL;
  81. void* rnd_param = NULL;
  82. const FpElemStr* f = NULL;
  83. MemberParams mem_params = {0};
  84. Epid2ParamsObj epid_params;
  85. SetMemberParams(&Prng::Generate, &my_prng, nullptr, &mem_params);
  86. Tpm2CreateContext(&mem_params, epid_params, &rnd_func, &rnd_param, &f, &ctx);
  87. Tpm2DeleteContext(&ctx);
  88. EXPECT_EQ(nullptr, ctx);
  89. }
  90. TEST_F(EpidTpm2Test, PROTECTED_SampleTest) { SUCCEED(); }
  91. TEST_F(EpidTpm2Test, PROTECTED_EPS1_SampleTest) { SUCCEED(); }
  92. TEST_F(EpidTpm2Test, PROTECTED_EPSOther_SampleTest) { SUCCEED(); }
  93. } // namespace