1/* 2* Copyright (c) 2024 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#include <cstdlib> 17#include <gtest/gtest.h> 18#include "fs_hvb.h" 19#include "init_utils.h" 20#include "securec.h" 21 22using namespace std; 23using namespace testing::ext; 24 25namespace init_ut { 26class FsHvbUnitTest : public testing::Test { 27public: 28 static void SetUpTestCase(void) {}; 29 static void TearDownTestCase(void) {}; 30 void SetUp(void) {}; 31 void TearDown(void) {}; 32}; 33 34void CreateTestFile(const char *fileName, const char *data) 35{ 36 CheckAndCreateDir(fileName); 37 printf("PrepareParamTestData for %s\n", fileName); 38 FILE *tmpFile = fopen(fileName, "a+"); 39 if (tmpFile != nullptr) { 40 fprintf(tmpFile, "%s", data); 41 (void)fflush(tmpFile); 42 fclose(tmpFile); 43 } 44} 45 46HWTEST_F(FsHvbUnitTest, Init_FsHvbInit_001, TestSize.Level0) 47{ 48 const char *cmdLine; 49 int ret = FsHvbInit(); 50 EXPECT_EQ(ret, HVB_ERROR_INVALID_ARGUMENT); 51 52 setenv("VERIFY_VALUE", "PartFail", 1); 53 ret = FsHvbInit(); 54 EXPECT_EQ(ret, HVB_ERROR_UNSUPPORTED_VERSION); 55 56 setenv("VERIFY_VALUE", "Succeed", 1); 57 ret = FsHvbInit(); 58 EXPECT_EQ(ret, -1); 59 60 cmdLine = "ohos.boot.hvb.hash_algo=test "; // HVB_CMDLINE_HASH_ALG 61 CreateTestFile(BOOT_CMD_LINE, cmdLine); 62 ret = FsHvbInit(); 63 EXPECT_EQ(ret, -1); 64 65 cmdLine = "ohos.boot.hvb.digest=1 "; // HVB_CMDLINE_CERT_DIGEST 66 CreateTestFile(BOOT_CMD_LINE, cmdLine); 67 ret = FsHvbInit(); 68 EXPECT_EQ(ret, -1); 69 remove(BOOT_CMD_LINE); 70 71 cmdLine = "ohos.boot.hvb.hash_algo=test "; 72 CreateTestFile(BOOT_CMD_LINE, cmdLine); 73 cmdLine = "ohos.boot.hvb.digest=1234 "; 74 CreateTestFile(BOOT_CMD_LINE, cmdLine); 75 ret = FsHvbInit(); 76 EXPECT_EQ(ret, -1); 77 remove(BOOT_CMD_LINE); 78 79 setenv("HASH_VALUE", "InitFail", 1); 80 cmdLine = "ohos.boot.hvb.hash_algo=sha256 "; 81 CreateTestFile(BOOT_CMD_LINE, cmdLine); 82 cmdLine = "ohos.boot.hvb.digest=12 "; 83 CreateTestFile(BOOT_CMD_LINE, cmdLine); 84 ret = FsHvbInit(); 85 EXPECT_EQ(ret, -1); 86 87 setenv("HASH_VALUE", "UpdateFail", 1); 88 ret = FsHvbInit(); 89 EXPECT_EQ(ret, -1); 90 91 setenv("HASH_VALUE", "FinalFail", 1); 92 ret = FsHvbInit(); 93 EXPECT_EQ(ret, -1); 94 95 setenv("HASH_VALUE", "AllSucceed", 1); 96 ret = FsHvbInit(); 97 EXPECT_EQ(ret, -1); 98 99 remove(BOOT_CMD_LINE); 100 unsetenv("VERIFY_VALUE"); 101 unsetenv("HASH_VALUE"); 102} 103 104HWTEST_F(FsHvbUnitTest, Init_FsHvbSetupHashtree_001, TestSize.Level0) 105{ 106 FstabItem fsItem; 107 108 int ret = FsHvbSetupHashtree(nullptr); 109 EXPECT_EQ(ret, -1); 110 111 ret = FsHvbSetupHashtree(&fsItem); 112 EXPECT_EQ(ret, -1); 113} 114 115HWTEST_F(FsHvbUnitTest, Init_FsHvbFinal_001, TestSize.Level0) 116{ 117 int ret = FsHvbFinal(); 118 EXPECT_EQ(ret, 0); 119} 120 121HWTEST_F(FsHvbUnitTest, Init_FsHvbGetValueFromCmdLine_001, TestSize.Level0) 122{ 123 const char *cmdLine; 124 char hashAlg[32] = { 0 }; 125 126 int ret = FsHvbGetValueFromCmdLine(nullptr, sizeof(hashAlg), "ohos.boot.hvb.hash_algo"); 127 EXPECT_EQ(ret, -1); 128 ret = FsHvbGetValueFromCmdLine(&hashAlg[0], sizeof(hashAlg), nullptr); 129 EXPECT_EQ(ret, -1); 130 131 cmdLine = "ohos.boot.hvb.hash_algo=sha256 "; 132 CreateTestFile(BOOT_CMD_LINE, cmdLine); 133 ret = FsHvbGetValueFromCmdLine(&hashAlg[0], sizeof(hashAlg), "ohos.boot.hvb.hash_algo"); 134 EXPECT_EQ(ret, 0); 135 EXPECT_EQ(strcmp(hashAlg, "sha256"), 0); 136 137 remove(BOOT_CMD_LINE); 138} 139 140HWTEST_F(FsHvbUnitTest, Init_FsHvbConstructVerityTarget_001, TestSize.Level0) 141{ 142 DmVerityTarget target; 143 const char *devName = "test"; 144 struct hvb_cert cert; 145 146 int ret = FsHvbConstructVerityTarget(nullptr, devName, &cert); 147 EXPECT_EQ(ret, -1); 148 ret = FsHvbConstructVerityTarget(&target, nullptr, &cert); 149 EXPECT_EQ(ret, -1); 150 ret = FsHvbConstructVerityTarget(&target, devName, nullptr); 151 EXPECT_EQ(ret, -1); 152 153 cert.image_len = 512; 154 cert.data_block_size = 0; 155 ret = FsHvbConstructVerityTarget(&target, devName, &cert); 156 EXPECT_EQ(ret, -1); 157 cert.data_block_size = 8; 158 cert.hash_block_size = 0; 159 ret = FsHvbConstructVerityTarget(&target, devName, &cert); 160 EXPECT_EQ(ret, -1); 161 162 cert.hash_block_size = 8; 163 cert.hashtree_offset = 16; 164 cert.hash_algo = 2; 165 ret = FsHvbConstructVerityTarget(&target, devName, &cert); 166 EXPECT_EQ(ret, -1); 167 168 cert.hash_algo = 0; 169 cert.hash_payload.digest = (uint8_t *)"5"; 170 cert.digest_size = 1; 171 cert.hash_payload.salt = (uint8_t *)"7"; 172 cert.salt_size = 1; 173 cert.fec_size = 1; 174 cert.fec_num_roots = 1; 175 cert.fec_offset = 8; 176 ret = FsHvbConstructVerityTarget(&target, devName, &cert); 177 EXPECT_EQ(ret, 0); 178 179 cert.hash_algo = 1; 180 ret = FsHvbConstructVerityTarget(&target, devName, &cert); 181 EXPECT_EQ(ret, 0); 182} 183 184HWTEST_F(FsHvbUnitTest, Init_FsHvbDestoryVerityTarget_001, TestSize.Level0) 185{ 186 DmVerityTarget target; 187 188 target.paras = (char *)calloc(1, 10); 189 FsHvbDestoryVerityTarget(&target); 190} 191}