1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 17 #include <cstdio> 18 #include "gtest/gtest.h" 19 20 #include "devattest_interface.h" 21 22 #define ACTS_DEVATTEST_INIT (-2) 23 #define ACTS_DEVATTEST_SUCCESS 0 24 25 using namespace std; 26 using namespace testing::ext; 27 28 class DeviceAttestQuerryTest : public testing::Test { 29 public: AttestStatusNumberValid(int32_t attestStatusNumber)30 bool AttestStatusNumberValid(int32_t attestStatusNumber) 31 { 32 if (attestStatusNumber < ACTS_DEVATTEST_INIT || attestStatusNumber > ACTS_DEVATTEST_SUCCESS) { 33 return false; 34 } 35 return true; 36 } 37 AttestStatusValid(AttestResultInfo attestResultInfo)38 ::testing::AssertionResult AttestStatusValid(AttestResultInfo attestResultInfo) 39 { 40 bool result = true; 41 string failString; 42 if (!AttestStatusNumberValid(attestResultInfo.authResult)) { 43 failString += string(" authResult is ") + to_string(attestResultInfo.authResult); 44 result = false; 45 } 46 if (!AttestStatusNumberValid(attestResultInfo.softwareResult)) { 47 failString += string(" softwareResult is ") + to_string(attestResultInfo.softwareResult); 48 result = false; 49 } 50 if (!AttestStatusNumberValid(attestResultInfo.softwareResultDetail[SOFTWARE_RESULT_VERSIONID])) { 51 failString += string(" versionResult is ") 52 + to_string(attestResultInfo.softwareResultDetail[SOFTWARE_RESULT_VERSIONID]); 53 result = false; 54 } 55 if (!AttestStatusNumberValid(attestResultInfo.softwareResultDetail[SOFTWARE_RESULT_PATCHLEVEL])) { 56 failString += string(" patchResult is ") 57 + to_string(attestResultInfo.softwareResultDetail[SOFTWARE_RESULT_PATCHLEVEL]); 58 result = false; 59 } 60 if (!AttestStatusNumberValid(attestResultInfo.softwareResultDetail[SOFTWARE_RESULT_ROOTHASH])) { 61 failString += string(" roothashResult is ") 62 + to_string(attestResultInfo.softwareResultDetail[SOFTWARE_RESULT_ROOTHASH]); 63 result = false; 64 } 65 if (!AttestStatusNumberValid(attestResultInfo.softwareResultDetail[SOFTWARE_RESULT_PCID])) { 66 failString += string(" pcidResult is ") 67 + to_string(attestResultInfo.softwareResultDetail[SOFTWARE_RESULT_PCID]); 68 result = false; 69 } 70 if (!AttestStatusNumberValid(attestResultInfo.softwareResultDetail[SOFTWARE_RESULT_RESERVE])) { 71 failString += string(" reserveResult is ") 72 + to_string(attestResultInfo.softwareResultDetail[SOFTWARE_RESULT_RESERVE]); 73 result = false; 74 } 75 if (attestResultInfo.authResult == ACTS_DEVATTEST_SUCCESS) { 76 if (attestResultInfo.ticketLength <= 0) { 77 failString += string(" ticketLength is ") 78 + to_string(attestResultInfo.ticketLength); 79 result = false; 80 } 81 if (attestResultInfo.ticket == nullptr || strcmp(attestResultInfo.ticket, "") == 0) { 82 failString += string(" ticket is empty"); 83 result = false; 84 } 85 } 86 if (result) { 87 return ::testing::AssertionSuccess(); 88 } else { 89 return ::testing::AssertionFailure() << failString.c_str(); 90 } 91 } 92 protected: SetUpTestCase(void)93 static void SetUpTestCase(void) {} TearDownTestCase(void)94 static void TearDownTestCase(void) {} SetUp()95 virtual void SetUp() {} TearDown()96 virtual void TearDown() {} 97 }; 98 99 /** 100 * @tc.number : Sub_Device_Attest_Test_0200 101 * @tc.name : HiLog::Warn parameter legal test (Cortex-A, C) 102 * @tc.desc : [C- SOFTWARE -0200] 103 */ 104 HWTEST_F(DeviceAttestQuerryTest, subDeviceAttestTest0200, Function | MediumTest | Level1) 105 { 106 int32_t ret = ACTS_DEVATTEST_SUCCESS; 107 AttestResultInfo attestResultInfo = { 0 }; 108 attestResultInfo.ticket = nullptr; 109 printf("[CLIENT MAIN] query.\n"); 110 ret = GetAttestStatus(&attestResultInfo); 111 EXPECT_EQ(ret, ACTS_DEVATTEST_SUCCESS); 112 bool resultBool = AttestStatusValid(attestResultInfo); 113 EXPECT_EQ(resultBool, true); 114 } 115 116 /** 117 * @tc.number : Sub_Device_Attest_Test_0300 118 * @tc.name : HiLog::Warn parameter legal test (Cortex-A, C) 119 * @tc.desc : [C- SOFTWARE -0200] 120 */ 121 HWTEST_F(DeviceAttestQuerryTest, subDeviceAttestTest0300, Function | MediumTest | Level1) 122 { 123 int32_t ret = ACTS_DEVATTEST_SUCCESS; 124 ret = StartDevAttestTask(); 125 printf("[CLIENT MAIN] StartDevAttestTask ret:%d.\n", ret); 126 EXPECT_EQ(ret, ACTS_DEVATTEST_SUCCESS); 127 AttestResultInfo attestResultInfo = { 0 }; 128 attestResultInfo.ticket = nullptr; 129 printf("[CLIENT MAIN] query.\n"); 130 ret = GetAttestStatus(&attestResultInfo); 131 printf("[CLIENT MAIN] wrong. ret:%d\n", ret); 132 EXPECT_EQ(ret, ACTS_DEVATTEST_SUCCESS); 133 } 134