• 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 #include "extension_util.h"
16 #include <gtest/gtest.h>
17 
18 using namespace testing::ext;
19 using namespace OHOS::CloudData;
20 
21 namespace OHOS::Test {
22 class ExtensionUtilTest : public testing::Test {};
23 
24 /**
25 * @tc.name: Convert001
26 * @tc.desc: Check that the character string does not contain null characters.
27 * @tc.type: FUNC
28 */
29 HWTEST_F(ExtensionUtilTest, Convert001, TestSize.Level1)
30 {
31     DBAsset dbAsset;
32     dbAsset.version = 1;
33     dbAsset.status = DBAsset::STATUS_NORMAL;
34     dbAsset.expiresTime = 1234567890;
35     dbAsset.id = "12345";
36     dbAsset.name = "test_asset";
37     dbAsset.createTime = "2025-07-03T00:00:00Z";
38     dbAsset.modifyTime = "2025-07-04T00:00:00Z";
39     dbAsset.size = "1024";
40     dbAsset.hash = "abc123";
41     dbAsset.uri = "http://example.com/path/to/file";
42     dbAsset.path = "/path/to/file";
43 
44     auto [asset, assetLen] = ExtensionUtil::Convert(dbAsset);
45     EXPECT_NE(asset, nullptr);
46     EXPECT_GT(assetLen, 0);
47     delete asset;
48 }
49 
50 /**
51 * @tc.name: Convert002
52 * @tc.desc: Check whether the path contains null characters.
53 * @tc.type: FUNC
54 */
55 HWTEST_F(ExtensionUtilTest, Convert002, TestSize.Level1)
56 {
57     const char data[] = "/path\0/to/file";
58     std::string path(data, 13);
59     DBAsset dbAsset;
60     dbAsset.version = 1;
61     dbAsset.status = DBAsset::STATUS_NORMAL;
62     dbAsset.expiresTime = 1234567890;
63     dbAsset.id = "12345";
64     dbAsset.name = "test_asset";
65     dbAsset.createTime = "2025-07-03T00:00:00Z";
66     dbAsset.modifyTime = "2025-07-04T00:00:00Z";
67     dbAsset.size = "1024";
68     dbAsset.hash = "abc123";
69     dbAsset.uri = "http://example.com/path/to/file";
70     dbAsset.path = path;
71 
72     auto [asset, assetLen] = ExtensionUtil::Convert(dbAsset);
73     EXPECT_EQ(asset, nullptr);
74     EXPECT_EQ(assetLen, 0);
75 }
76 
77 /**
78 * @tc.name: Convert003
79 * @tc.desc: Check whether the URI contains null characters.
80 * @tc.type: FUNC
81 */
82 HWTEST_F(ExtensionUtilTest, Convert003, TestSize.Level1)
83 {
84     const char data[] = "http://example.com/path\0to/file";
85     std::string uri(data, 32);
86     DBAsset dbAsset;
87     dbAsset.version = 1;
88     dbAsset.status = DBAsset::STATUS_NORMAL;
89     dbAsset.expiresTime = 1234567890;
90     dbAsset.id = "12345";
91     dbAsset.name = "test_asset";
92     dbAsset.createTime = "2025-07-03T00:00:00Z";
93     dbAsset.modifyTime = "2025-07-04T00:00:00Z";
94     dbAsset.size = "1024";
95     dbAsset.hash = "abc123";
96     dbAsset.uri = uri;
97     dbAsset.path = "/path/to/file";
98 
99     auto [asset, assetLen] = ExtensionUtil::Convert(dbAsset);
100     EXPECT_EQ(asset, nullptr);
101     EXPECT_EQ(assetLen, 0);
102 }
103 
104 /**
105 * @tc.name: Convert004
106 * @tc.desc: Check whether the path and URI contain null characters.
107 * @tc.type: FUNC
108 */
109 HWTEST_F(ExtensionUtilTest, Convert004, TestSize.Level1)
110 {
111     const char data1[] = "http://example.com/path\0to/file";
112     std::string uri(data1, 32);
113     const char data2[] = "/path\0/to/file";
114     std::string path(data2, 13);
115     DBAsset dbAsset;
116     dbAsset.version = 1;
117     dbAsset.status = DBAsset::STATUS_NORMAL;
118     dbAsset.expiresTime = 1234567890;
119     dbAsset.id = "12345";
120     dbAsset.name = "test_asset";
121     dbAsset.createTime = "2025-07-03T00:00:00Z";
122     dbAsset.modifyTime = "2025-07-04T00:00:00Z";
123     dbAsset.size = "1024";
124     dbAsset.hash = "abc123";
125     dbAsset.uri = uri;
126     dbAsset.path = path;
127 
128     auto [asset, assetLen] = ExtensionUtil::Convert(dbAsset);
129     EXPECT_EQ(asset, nullptr);
130     EXPECT_EQ(assetLen, 0);
131 }
132 
133 /**
134 * @tc.name: ConvertVBuckets001
135 * @tc.desc: Test that convert DBVBuckets to empty.
136 * @tc.type: FUNC
137 */
138 HWTEST_F(ExtensionUtilTest, ConvertVBuckets001, TestSize.Level1)
139 {
140     DBVBuckets buckets;
141     auto [result, size] = ExtensionUtil::Convert(std::move(buckets));
142     EXPECT_NE(result, nullptr);
143     EXPECT_EQ(size, 0);
144 }
145 
146 /**
147 * @tc.name: ConvertVBuckets002
148 * @tc.desc: Test that convert function DBVBucket is DBAsset type.
149 * @tc.type: FUNC
150 */
151 HWTEST_F(ExtensionUtilTest, ConvertVBuckets002, TestSize.Level1)
152 {
153     DBVBuckets buckets;
154     DBVBucket bucket;
155     DBAsset dbAsset;
156     bucket["dbAsset"] = dbAsset;
157     buckets.push_back(bucket);
158     auto [result, size] = ExtensionUtil::Convert(std::move(buckets));
159     EXPECT_NE(result, nullptr);
160     EXPECT_GT(size, 0);
161 }
162 
163 /**
164 * @tc.name: ConvertVBuckets003
165 * @tc.desc: Test that convert function DBVBucket is DBAssets type.
166 * @tc.type: FUNC
167 */
168 HWTEST_F(ExtensionUtilTest, ConvertVBuckets003, TestSize.Level1)
169 {
170     DBVBuckets buckets;
171     DBVBucket bucket;
172     DBAssets dbAssets;
173     bucket["dbAsset"] = dbAssets;
174     buckets.push_back(bucket);
175     auto [result, size] = ExtensionUtil::Convert(std::move(buckets));
176     EXPECT_NE(result, nullptr);
177     EXPECT_GT(size, 0);
178 }
179 
180 /**
181 * @tc.name: ConvertVBuckets004
182 * @tc.desc: Test that convert function DBVBucket is str type.
183 * @tc.type: FUNC
184 */
185 HWTEST_F(ExtensionUtilTest, ConvertVBuckets004, TestSize.Level1)
186 {
187     DBVBuckets buckets;
188     DBVBucket bucket;
189     std::string key = "123";
190     bucket["dbAsset"] = key;
191     buckets.push_back(bucket);
192     auto [result, size] = ExtensionUtil::Convert(std::move(buckets));
193     EXPECT_NE(result, nullptr);
194     EXPECT_GT(size, 0);
195 }
196 } // namespace OHOS::Test