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 "ecmascript/ohos/tests/mock/mock_aot_runtime_info.h"
17 
18 namespace panda::test {
MockAotRuntimeInfo()19 MockAotRuntimeInfo::MockAotRuntimeInfo()
20 {}
~MockAotRuntimeInfo()21 MockAotRuntimeInfo::~MockAotRuntimeInfo()
22 {}
GetRuntimeBuildId(char *buildId, int length) const23 bool MockAotRuntimeInfo::GetRuntimeBuildId(char *buildId, int length) const
24 {
25     char tmp[NAME_MAX] = "abcd1234567890";
26     if (strcpy_s(buildId, length, tmp) != 0) {
27         return false;
28     }
29     return true;
30 }
31 
GetMicrosecondsTimeStamp(char *timestamp, size_t length) const32 bool MockAotRuntimeInfo::GetMicrosecondsTimeStamp(char *timestamp, size_t length) const
33 {
34     char tmp[ecmascript::ohos::AotRuntimeInfo::TIME_STAMP_SIZE] = "1970-01-01 00:00:00";
35     if (strcpy_s(timestamp, length, tmp) != 0) {
36         return false;
37     }
38     return true;
39 }
40 
GetCrashSandBoxRealPath(char *realOutPath, size_t length) const41 bool MockAotRuntimeInfo::GetCrashSandBoxRealPath(char *realOutPath, size_t length) const
42 {
43     if (strcpy_s(realOutPath, length, SANBOX_DIR) != 0) {
44         return false;
45     }
46     if (strcat_s(realOutPath, length, "/") != 0) {
47         return false;
48     }
49     if (strcat_s(realOutPath, length, AOT_RUNTIME_INFO) != 0) {
50         return false;
51     }
52     return true;
53 }
54 
BuildRuntimeInfoPart(char *runtimeInfoPart, const char *soBuildId, const char *timestamp, ecmascript::ohos::RuntimeInfoType type) const55 bool MockAotRuntimeInfo::BuildRuntimeInfoPart(char *runtimeInfoPart, const char *soBuildId, const char *timestamp,
56     ecmascript::ohos::RuntimeInfoType type) const
57 {
58     return ecmascript::ohos::AotRuntimeInfo::BuildRuntimeInfoPart(runtimeInfoPart, soBuildId, timestamp, type);
59 }
60 
SetRuntimeInfo(const char *realOutPath, char lines[][BUFFER_SIZE], int length) const61 void MockAotRuntimeInfo::SetRuntimeInfo(const char *realOutPath, char lines[][BUFFER_SIZE], int length) const
62     {
63         int fd = open(realOutPath,  O_WRONLY | O_CREAT | O_TRUNC, 0666);
64         if (fd == -1) {
65             return;
66         }
67         for (int i = 0; i < length && lines[i] != NULL; i++) {
68             if (lines[i][0] != '\0') {
69                 write(fd, lines[i], strlen(lines[i]));
70                 if (IsRuntimeInfoCrashTest()) {
71                     char buffer[BUFFER_SIZE];
72                     memset_s(buffer, BUFFER_SIZE, 'a', BUFFER_SIZE);
73                     write(fd, buffer, strlen(buffer));
74                     continue;
75                 }
76                 write(fd, "\n", 1);
77             }
78         }
79         close(fd);
80     }
81 };
82