• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 <fcntl.h>
18 #include <gtest/gtest.h>
19 #include <gtest/hwext/gtest-multithread.h>
20 #include <string>
21 
22 #include "access_token_setter.h"
23 #include "byte_buffer.h"
24 #include "code_sign_utils.h"
25 #include "local_code_sign_kit.h"
26 #include "log.h"
27 
28 using namespace OHOS::Security::CodeSign;
29 using namespace std;
30 using namespace testing::ext;
31 using namespace testing::mt;
32 
33 namespace OHOS {
34 namespace Security {
35 namespace CodeSign {
36 static constexpr uint32_t MULTI_THREAD_NUM = 10;
37 static constexpr int64_t BUFFER_SIZE = 1024;
38 static const std::string AN_BASE_PATH = "/data/local/ark-cache/tmp/multi_thread/";
39 static const std::string ORIGIN_AN_FILE = AN_BASE_PATH + "demo.an";
40 static const char *VALID_CALLER = "installs";
41 
GetFileSize(int32_t fd)42 uint64_t GetFileSize(int32_t fd)
43 {
44     struct stat st;
45     if (fstat(fd, &st) != 0) {
46         LOG_ERROR(LABEL, "Stat file failed, errno = <%{public}d, %{public}s>",
47             errno, strerror(errno));
48         return 0;
49     }
50     return st.st_size;
51 }
52 
DupFile(const std::string & path)53 static bool DupFile(const std::string &path)
54 {
55     int32_t fin = open(ORIGIN_AN_FILE.c_str(), O_RDONLY);
56     if (fin < 0) {
57         return false;
58     }
59     uint32_t fileSize = GetFileSize(fin);
60     int32_t fout = open(path.c_str(), O_CREAT | O_RDWR | O_TRUNC, 0777);
61     if (fout < 0) {
62         close(fin);
63         return false;
64     }
65     ssize_t curSize = 0;
66     char buffer[BUFFER_SIZE];
67     bool ret = true;
68     while (curSize < fileSize) {
69         ssize_t len = read(fin, buffer, BUFFER_SIZE);
70         if (len <= 0) {
71             break;
72         }
73         curSize += len;
74         write(fout, buffer, len);
75     }
76     close(fin);
77     close(fout);
78     return ret;
79 }
80 
LocalCodeSignAndEnforce()81 void LocalCodeSignAndEnforce()
82 {
83     ByteBuffer sig;
84     uint64_t selfTokenId = NativeTokenSet(VALID_CALLER);
85     int ret = LocalCodeSignKit::SignLocalCode(ORIGIN_AN_FILE, sig);
86     std::thread::id thisId = std::this_thread::get_id();
87     std::ostringstream oss;
88     oss << thisId;
89     std::string thisIdStr = oss.str();
90     std::string tmpFileName = AN_BASE_PATH + thisIdStr + ".an";
91     EXPECT_EQ(DupFile(tmpFileName), true);
92     NativeTokenReset(selfTokenId);
93     EXPECT_EQ(ret, CS_SUCCESS);
94     ret = CodeSignUtils::EnforceCodeSignForFile(tmpFileName, sig);
95     EXPECT_EQ(ret, CS_SUCCESS);
96 }
97 
98 class MultiThreadLocalSignTest : public testing::Test {
99 public:
MultiThreadLocalSignTest()100     MultiThreadLocalSignTest() {};
~MultiThreadLocalSignTest()101     virtual ~MultiThreadLocalSignTest() {};
SetUpTestCase()102     static void SetUpTestCase() {};
TearDownTestCase()103     static void TearDownTestCase() {};
SetUp()104     void SetUp() {};
TearDown()105     void TearDown() {};
106 };
107 
108 /**
109  * @tc.name: MultiThreadLocalSignTest_0001
110  * @tc.desc: sign AN files and enforce using multi threads
111  * @tc.type: Func
112  * @tc.require:
113  */
114 HWMTEST_F(MultiThreadLocalSignTest, MultiThreadLocalSignTest_0001, TestSize.Level1, MULTI_THREAD_NUM)
115 {
116     LocalCodeSignAndEnforce();
117 }
118 } // namespace CodeSign
119 } // namespace Security
120 } // namespace OHOS