• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <gtest/gtest.h>
16 #include <unistd.h>
17 
18 #include "file_util.h"
19 #include "gwpasan_collector.h"
20 
21 using namespace testing::ext;
22 namespace OHOS {
23 namespace HiviewDFX {
24 class AsanUnittest : public testing::Test {
25 public:
SetUp()26     void SetUp()
27     {
28         chmod("/data/log/faultlog/", 0777); // 0777: add other user write permission
29         chmod("/data/log/faultlog/faultlogger/", 0777); // 0777: add other user write permission
30         sleep(1);
31     };
TearDown()32     void TearDown()
33     {
34         chmod("/data/log/faultlog/", 0770); // 0770: restore permission
35         chmod("/data/log/faultlog/faultlogger/", 0770); // 0770: restore permission
36     };
37 
ClearAllLogs(const std::string & path)38     static void ClearAllLogs(const std::string& path)
39     {
40         DIR* dir = opendir(path.c_str());
41         struct dirent* entry;
42         while ((entry = readdir(dir)) != nullptr) {
43             std::string full_path = path + "/" + entry->d_name;
44             remove(full_path.c_str());
45         }
46         closedir(dir);
47     }
48 
hasSanitizerLogs(std::string path,std::string type)49     static bool hasSanitizerLogs(std::string path, std::string type)
50     {
51         std::vector<std::string> files;
52         FileUtil::GetDirFiles(path, files, false);
53         bool hasLogs = false;
54         for (const auto& file : files) {
55             if (file.find(type) != std::string::npos) {
56                 hasLogs = true;
57                 break;
58             }
59         }
60         return hasLogs;
61     }
62 };
63 
64 /**
65  * @tc.name: AsanTest001
66  * @tc.desc: Test calling WriteSanitizerLog Func
67  * @tc.type: FUNC
68  */
69 HWTEST_F(AsanUnittest, WriteSanitizerLogTest001, testing::ext::TestSize.Level1)
70 {
71     ClearAllLogs("/data/log/faultlog/faultlogger/");
72     char path[] = "faultlogger";
73     char gwpAsanBuf[] = "Test GWP-ASAN, End GWP-ASan report";
74     WriteSanitizerLog(gwpAsanBuf, strlen(gwpAsanBuf), path);
75     char cfiBuf[] = "Test CFI, End CFI report";
76     WriteSanitizerLog(cfiBuf, strlen(cfiBuf), path);
77     char ubsanBuf[] = "Test UBSAN, End Ubsan report";
78     WriteSanitizerLog(ubsanBuf, strlen(ubsanBuf), path);
79     char tsanBuf[] = "Test TSAN, End Tsan report";
80     WriteSanitizerLog(tsanBuf, strlen(tsanBuf), path);
81     char hwasanBuf[] = "Test HWASAN, End Hwasan report";
82     WriteSanitizerLog(hwasanBuf, strlen(hwasanBuf), path);
83     char asanBuf[] = "Test ASAN, End Asan report";
84     WriteSanitizerLog(asanBuf, strlen(asanBuf), path);
85     ASSERT_TRUE(true);
86 }
87 
88 /**
89  * @tc.name: AsanTest002
90  * @tc.desc: Test calling WriteSanitizerLog Func
91  * @tc.type: FUNC
92  */
93 HWTEST_F(AsanUnittest, WriteSanitizerLogTest002, testing::ext::TestSize.Level1)
94 {
95     ClearAllLogs("/data/log/faultlog/faultlogger/");
96     char* buf = nullptr;
97     size_t sz = 10;
98     char path[] = "faultlogger";
99     WriteSanitizerLog(buf, sz, path);
100     bool result = hasSanitizerLogs("/data/log/faultlog/faultlogger/", "asan");
101     ASSERT_FALSE(result);
102 }
103 
104 /**
105  * @tc.name: AsanTest003
106  * @tc.desc: Test calling WriteSanitizerLog Func
107  * @tc.type: FUNC
108  */
109 HWTEST_F(AsanUnittest, WriteSanitizerLogTest003, testing::ext::TestSize.Level1)
110 {
111     ClearAllLogs("/data/log/faultlog/faultlogger/");
112     char path[] = "/data/sanitizer.log";
113     char hwasanBuf[] = "Test HWASAN, End Hwasan report";
114     WriteSanitizerLog(hwasanBuf, strlen(hwasanBuf), path);
115     bool result = hasSanitizerLogs("/data/log/faultlog/faultlogger/", "hwasan");
116     ASSERT_FALSE(result);
117 }
118 } // namespace HiviewDFX
119 } // namespace OHOS
120