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