• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <gtest/gtest.h>
17 #include "data_usage_dfx.h"
18 #include <cstdio>
19 #include <fcntl.h>
20 
21 using namespace testing::ext;
22 
23 namespace OHOS {
24 namespace Security {
25 namespace AccessToken {
26 namespace {
27 constexpr const char* INVALID_FILE = "/data/123456/xyz";
28 constexpr const char* ACCESSTOKEN_DATABASE_NAME = "access_token.db";
29 constexpr const char* TEST_FILE_PATH = "/data/test/dfx_test_file.txt";
30 constexpr const char* TEST_TXT = "1234567890abcdefghij";
31 constexpr int32_t TEST_SIZE = 20;
32 } // namespace
33 class DfxTest : public testing::Test {
34 public:
35     static void SetUpTestCase(void);
36     static void TearDownTestCase(void);
37     void SetUp();
38     void TearDown();
39 };
40 
SetUpTestCase()41 void DfxTest::SetUpTestCase() {}
TearDownTestCase()42 void DfxTest::TearDownTestCase() {}
SetUp()43 void DfxTest::SetUp() {}
TearDown()44 void DfxTest::TearDown() {}
45 
46 /**
47  * @tc.name: GetUserDataRemainSizeTest001
48  * @tc.desc: Test GetUserDataRemainSize function
49  * @tc.type: FUNC
50  * @tc.require: Issue Number
51  */
52 HWTEST_F(DfxTest, GetUserDataRemainSizeTest001, TestSize.Level1)
53 {
54     // expect size > 0
55     uint64_t size = GetUserDataRemainSize();
56     EXPECT_GT(size, 0);
57 }
58 
59 /**
60  * @tc.name: GetFileSizeTest001
61  * @tc.desc: Test GetFileSize function
62  * @tc.type: FUNC
63  * @tc.require: Issue Number
64  */
65 HWTEST_F(DfxTest, GetFileSizeTest001, TestSize.Level1)
66 {
67     FILE* file = fopen(TEST_FILE_PATH, "w");
68     size_t written = fwrite(TEST_TXT, sizeof(char), TEST_SIZE, file);
69     EXPECT_EQ(written, TEST_SIZE);
70     fclose(file);
71 
72     // test file size is 20
73     uint64_t testFileSize = GetFileSize(TEST_FILE_PATH);
74     EXPECT_EQ(testFileSize, TEST_SIZE);
75 
76     remove(TEST_FILE_PATH);
77 
78     // invalid file size is 0
79     uint64_t invalidSize = GetFileSize(INVALID_FILE);
80     EXPECT_EQ(invalidSize, 0);
81 }
82 
83 /**
84  * @tc.name: GetDatabaseFileSizeTest001
85  * @tc.desc: Test GetDatabaseFileSize function
86  * @tc.type: FUNC
87  * @tc.require: Issue Number
88  */
89 HWTEST_F(DfxTest, GetDatabaseFileSizeTest001, TestSize.Level1)
90 {
91     std::vector<std::string> filePath;
92     std::vector<uint64_t> fileSize;
93     GetDatabaseFileSize(ACCESSTOKEN_DATABASE_NAME, filePath, fileSize);
94     EXPECT_GT(filePath.size(), 0);
95     EXPECT_GT(fileSize.size(), 0);
96     EXPECT_EQ(filePath.size(), fileSize.size());
97 
98     std::vector<std::string> invalidFilePath;
99     std::vector<uint64_t> invalidFileSize;
100     GetDatabaseFileSize(INVALID_FILE, invalidFilePath, invalidFileSize);
101     EXPECT_EQ(invalidFilePath.size(), 0);
102     EXPECT_EQ(invalidFileSize.size(), 0);
103 }
104 } // namespace AccessToken
105 } // namespace Security
106 } // namespace OHOS
107