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
16 #define LOG_TAG "KvHiviewReporterTest"
17 #include <gtest/gtest.h>
18
19 #include "include/hisysevent_mock.h"
20 #include "kv_hiview_reporter.h"
21 #include "log_print.h"
22 #include "types.h"
23 #include <unistd.h>
24
25 namespace OHOS::Test {
26 using namespace testing;
27 using namespace testing::ext;
28 using namespace OHOS::DistributedKv;
29
30 static constexpr const char *BASE_DIR = "/data/service/el1/public/database/KvHiviewReporterTest/";
31 static constexpr const char *STOREID = "test_storeId";
32 static constexpr const char *DB_CORRUPTED_POSTFIX = ".corruptedflg";
33
34 class KvHiviewReporterTest : public testing::Test {
35 public:
36 static void SetUpTestCase(void);
37 static void TearDownTestCase(void);
38
39 void SetUp();
40 void TearDown();
41 };
42
SetUpTestCase(void)43 void KvHiviewReporterTest::SetUpTestCase(void)
44 {
45 auto ret = mkdir(BASE_DIR, (S_IRWXU));
46 if (ret != 0) {
47 ZLOGE("Mkdir failed, result:%{public}d, path:%{public}s", ret, BASE_DIR);
48 }
49 }
50
TearDownTestCase(void)51 void KvHiviewReporterTest::TearDownTestCase(void)
52 {
53 auto ret = remove(BASE_DIR);
54 if (ret != 0) {
55 ZLOGE("Remove failed, result:%{public}d, path:%{public}s", ret, BASE_DIR);
56 }
57 }
58
SetUp(void)59 void KvHiviewReporterTest::SetUp(void) { }
60
TearDown(void)61 void KvHiviewReporterTest::TearDown(void) { }
62
63 /**
64 * @tc.name: CorruptedFlagTest001
65 * @tc.desc: Create and delete corrupted flag
66 * @tc.type: FUNC
67 */
68 HWTEST_F(KvHiviewReporterTest, CorruptedFlagTest001, TestSize.Level1)
69 {
70 ZLOGI("CorruptedFlagTest001 begin.");
71 KVDBFaultHiViewReporter::CreateCorruptedFlag(BASE_DIR, STOREID);
72 std::string flagFilename = std::string(BASE_DIR) + std::string(STOREID) + std::string(DB_CORRUPTED_POSTFIX);
73 auto ret = access(flagFilename.c_str(), F_OK);
74 ASSERT_EQ(ret, 0);
75 KVDBFaultHiViewReporter::DeleteCorruptedFlag(BASE_DIR, STOREID);
76 ret = access(flagFilename.c_str(), F_OK);
77 ASSERT_NE(ret, 0);
78 }
79
80 /**
81 * @tc.name: ReportKVFaultEvent001
82 * @tc.desc: Execute the ReportFaultEvent method
83 * @tc.type: FUNC
84 */
85 HWTEST_F(KvHiviewReporterTest, ReportKVFaultEvent001, TestSize.Level1)
86 {
87 ZLOGI("ReportKVFaultEvent001 begin.");
88 HiSysEventMock mock;
89 EXPECT_CALL(mock, HiSysEvent_Write(_, _, _, _, _, _, _)).Times(1);
90 ReportInfo reportInfo;
91 KVDBFaultHiViewReporter::ReportKVFaultEvent(reportInfo);
92 }
93 } // namespace OHOS::Test