1 /*
2 * Copyright (c) 2025 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 #include "hnp_installer_test.h"
16 #include <gtest/gtest.h>
17
18 #include <cstdio>
19 #include <cstdlib>
20 #include <cstring>
21 #include <sys/stat.h>
22 #include <cerrno>
23 #include "hnp_installer.h"
24
25
26 using namespace testing;
27 using namespace testing::ext;
28
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34 #ifdef __cplusplus
35 }
36 #endif
37
38 namespace OHOS {
39 class InitFirststageTest : public testing::Test {
40 public:
41 static void SetUpTestCase();
42 static void TearDownTestCase();
43 void SetUp();
44 void TearDown();
45 };
46
SetUpTestCase()47 void InitFirststageTest::SetUpTestCase()
48 {
49 GTEST_LOG_(INFO) << "Hnp_Installer_TEST SetUpTestCase";
50 }
51
TearDownTestCase()52 void InitFirststageTest::TearDownTestCase()
53 {
54 GTEST_LOG_(INFO) << "Hnp_Installer_TEST TearDownTestCase";
55 }
56
SetUp()57 void InitFirststageTest::SetUp()
58 {
59 GTEST_LOG_(INFO) << "Hnp_Installer_TEST SetUp";
60 }
61
TearDown()62 void InitFirststageTest::TearDown()
63 {
64 GTEST_LOG_(INFO) << "Hnp_Installer_TEST TearDown";
65 }
66
MakeDirRecursive(const std::string & path,mode_t mode)67 static void MakeDirRecursive(const std::string &path, mode_t mode)
68 {
69 size_t size = path.size();
70 if (size == 0) {
71 return;
72 }
73
74 size_t index = 0;
75 do {
76 size_t pathIndex = path.find_first_of('/', index);
77 index = pathIndex == std::string::npos ? size : pathIndex + 1;
78 std::string dir = path.substr(0, index);
79 if (access(dir.c_str(), F_OK) < 0) {
80 int ret = mkdir(dir.c_str(), mode);
81 if (ret != 0) {
82 return;
83 }
84 }
85 } while (index < size);
86 }
87
CreateTestFile(const char * fileName,const char * data)88 static void CreateTestFile(const char *fileName, const char *data)
89 {
90 FILE *tmpFile = fopen(fileName, "wr");
91 if (tmpFile != nullptr) {
92 fprintf(tmpFile, "%s", data);
93 (void)fflush(tmpFile);
94 fclose(tmpFile);
95 }
96 }
97
98 HWTEST_F(InitFirststageTest, hnp_clearSoftLink_001, TestSize.Level0)
99 {
100 GTEST_LOG_(INFO) << "hnp_clearSoftLink_001 start";
101
102 const std::string binDir = HNP_DEFAULT_INSTALL_ROOT_PATH"/100/hnppublic/bin";
103 const std::string fileDir = HNP_DEFAULT_INSTALL_ROOT_PATH"/100/hnppublic/test.org/bin";
104
105 MakeDirRecursive(binDir, 0711);
106 MakeDirRecursive(fileDir, 0711);
107
108 const std::string lnkFile = HNP_DEFAULT_INSTALL_ROOT_PATH"/100/hnppublic/bin/test";
109 const std::string sourceFile = "../test.org/bin/test";
110
111 CreateTestFile(HNP_DEFAULT_INSTALL_ROOT_PATH"/100/hnppublic/test.org/bin/test", "test");
112 symlink(sourceFile.c_str(), lnkFile.c_str());
113
114 ClearSoftLink(100);
115 int ret = access(lnkFile.c_str(), F_OK);
116 EXPECT_EQ(ret, 0);
117
118 remove(HNP_DEFAULT_INSTALL_ROOT_PATH"/100/hnppublic/test.org/bin/test");
119 ClearSoftLink(100);
120
121 ret = access(lnkFile.c_str(), F_OK);
122 EXPECT_NE(ret, 0);
123 }
124
125 }