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 <string>
19
20 #include "key_utils.h"
21
22 using namespace testing::ext;
23 using namespace std;
24
25 namespace OHOS {
26 namespace Security {
27 namespace CodeSign {
28
29 static const std::string RD_DEVICE_1 = "oemmode=rd efuse_status=0";
30 static const std::string RD_DEVICE_2 = "oemmode=user efuse_status=1";
31 static const std::string NOT_RD_DEVICE = "oemmode=user efuse_status=0";
32 static const std::string DEVICE_MODE_ATTACKED = "oemmode=rd oemmode=user";
33 static const std::string EFUSED_ATTACKED = "efuse_status=1 efuse_status=0";
34 constexpr int32_t NOT_INITIALIZE = 0;
35
36 class KeyEnableUtilsTest : public testing::Test {
37 public:
KeyEnableUtilsTest()38 KeyEnableUtilsTest() {};
~KeyEnableUtilsTest()39 virtual ~KeyEnableUtilsTest() {};
SetUpTestCase()40 static void SetUpTestCase() {};
TearDownTestCase()41 static void TearDownTestCase() {};
SetUp()42 void SetUp() {};
TearDown()43 void TearDown() {};
44 };
45
OverWriteCMDLine(const std::string & content)46 static bool OverWriteCMDLine(const std::string &content)
47 {
48 FILE *file = fopen(PROC_CMDLINE_FILE_PATH.c_str(), "w+");
49 if (file == nullptr) {
50 return false;
51 }
52 size_t result = fwrite(content.c_str(), 1, content.size(), file);
53 if (result != content.size()) {
54 (void)fclose(file);
55 return false;
56 }
57 (void)fclose(file);
58 return true;
59 }
60
61 /**
62 * @tc.name: KeyEnableUtilsTest_0001
63 * @tc.desc: check status of device
64 * @tc.type: Func
65 * @tc.require: issueI8FCGF
66 */
67 HWTEST_F(KeyEnableUtilsTest, KeyEnableUtilsTest_0001, TestSize.Level0)
68 {
69 ASSERT_EQ(OverWriteCMDLine(RD_DEVICE_1), true);
70 EXPECT_EQ(IsRdDevice(), true);
71 g_isRdDevice = NOT_INITIALIZE;
72 ASSERT_EQ(OverWriteCMDLine(RD_DEVICE_2), true);
73 EXPECT_EQ(IsRdDevice(), true);
74 g_isRdDevice = NOT_INITIALIZE;
75 ASSERT_EQ(OverWriteCMDLine(NOT_RD_DEVICE), true);
76 EXPECT_EQ(IsRdDevice(), false);
77 g_isRdDevice = NOT_INITIALIZE;
78 ASSERT_EQ(OverWriteCMDLine(DEVICE_MODE_ATTACKED), true);
79 EXPECT_EQ(IsRdDevice(), false);
80 g_isRdDevice = NOT_INITIALIZE;
81 ASSERT_EQ(OverWriteCMDLine(EFUSED_ATTACKED), true);
82 EXPECT_EQ(IsRdDevice(), false);
83 }
84 } // namespace CodeSign
85 } // namespace Security
86 } // namespace OHOS
87