• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 <gtest/gtest.h>
17 
18 #include <cstring>
19 
20 #include "accesstoken_kit.h"
21 #include "anonymous_string.h"
22 #include "dcamera_utils_tools.h"
23 #include "distributed_camera_errno.h"
24 #include "distributed_hardware_log.h"
25 #include "nativetoken_kit.h"
26 #include "token_setproc.h"
27 
28 using namespace testing::ext;
29 
30 namespace OHOS {
31 namespace DistributedHardware {
32 class DcameraUtilsToolsTest : public testing::Test {
33 public:
34     static void SetUpTestCase(void);
35     static void TearDownTestCase(void);
36     void SetUp();
37     void TearDown();
38 
39     void SetTokenID();
40 };
41 
SetUpTestCase(void)42 void DcameraUtilsToolsTest::SetUpTestCase(void)
43 {
44     DHLOGI("enter");
45 }
46 
TearDownTestCase(void)47 void DcameraUtilsToolsTest::TearDownTestCase(void)
48 {
49     DHLOGI("enter");
50 }
51 
SetUp(void)52 void DcameraUtilsToolsTest::SetUp(void)
53 {
54     DHLOGI("enter");
55 }
56 
TearDown(void)57 void DcameraUtilsToolsTest::TearDown(void)
58 {
59     DHLOGI("enter");
60 }
61 
SetTokenID()62 void DcameraUtilsToolsTest::SetTokenID()
63 {
64     uint64_t tokenId;
65     const char *perms[2];
66     perms[0] = "ohos.permission.DISTRIBUTED_DATASYNC";
67     perms[1] = "ohos.permission.CAMERA";
68     NativeTokenInfoParams infoInstance = {
69         .dcapsNum = 0,
70         .permsNum = 2,
71         .aclsNum = 0,
72         .dcaps = NULL,
73         .perms = perms,
74         .acls = NULL,
75         .processName = "dcamera_client_demo",
76         .aplStr = "system_basic",
77     };
78     tokenId = GetAccessTokenId(&infoInstance);
79     SetSelfTokenID(tokenId);
80     OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
81 }
82 /**
83  * @tc.name: GetLocalDeviceNetworkId_001
84  * @tc.desc: Verify the GetLocalDeviceNetworkId function failed.
85  * @tc.type: FUNC
86  * @tc.require: Issue Number
87  */
88 HWTEST_F(DcameraUtilsToolsTest, GetLocalDeviceNetworkId_001, TestSize.Level1)
89 {
90     std::string networkId;
91     SetTokenID();
92     int32_t ret = GetLocalDeviceNetworkId(networkId);
93     EXPECT_EQ(DCAMERA_OK, ret);
94 }
95 
96 /**
97  * @tc.name: GetNowTimeStampMs_001
98  * @tc.desc: Verify the GetNowTimeStampMs function failed.
99  * @tc.type: FUNC
100  * @tc.require: Issue Number
101  */
102 HWTEST_F(DcameraUtilsToolsTest, GetNowTimeStampMs_001, TestSize.Level1)
103 {
104     int32_t ret = GetNowTimeStampMs();
105     EXPECT_NE(DCAMERA_OK, ret);
106 }
107 
108 /**
109  * @tc.name: GetNowTimeStampUs_001
110  * @tc.desc: Verify the GetNowTimeStampUs function failed.
111  * @tc.type: FUNC
112  * @tc.require: Issue Number
113  */
114 HWTEST_F(DcameraUtilsToolsTest, GetNowTimeStampUs_001, TestSize.Level1)
115 {
116     int32_t ret = GetNowTimeStampUs();
117     EXPECT_NE(DCAMERA_OK, ret);
118 }
119 
120 /**
121  * @tc.name: GetAlignedHeight_001
122  * @tc.desc: Verify the GetAlignedHeight function failed.
123  * @tc.type: FUNC
124  * @tc.require: Issue Number
125  */
126 HWTEST_F(DcameraUtilsToolsTest, GetAlignedHeight_001, TestSize.Level1)
127 {
128     int32_t width = 0;
129     int32_t ret = GetAlignedHeight(width);
130     EXPECT_EQ(DCAMERA_OK, ret);
131 }
132 
133 /**
134  * @tc.name: Base64Encode_001
135  * @tc.desc: Verify the Base64Encode function failed.
136  * @tc.type: FUNC
137  * @tc.require: Issue Number
138  */
139 HWTEST_F(DcameraUtilsToolsTest, Base64Encode_001, TestSize.Level1)
140 {
141     int32_t value = 0;
142     std::string toEncode = "testtest";
143     std::string retsult = Base64Encode(reinterpret_cast<const unsigned char *>(toEncode.c_str()), toEncode.size());
144     EXPECT_NE(0, retsult.size());
145     Base64Decode(retsult);
146     int32_t ret = GetAlignedHeight(value);
147     EXPECT_EQ(DCAMERA_OK, ret);
148 }
149 
150 /**
151  * @tc.name: Base64Encode_002
152  * @tc.desc: Verify the Base64Encode function failed.
153  * @tc.type: FUNC
154  * @tc.require: Issue Number
155  */
156 HWTEST_F(DcameraUtilsToolsTest, Base64Encode_002, TestSize.Level1)
157 {
158     int32_t value = 0;
159     std::string toEncode = "testtest";
160     int32_t len = 0;
161     Base64Encode(reinterpret_cast<const unsigned char *>(toEncode.c_str()), len);
162     std::string toDncode = "";
163     Base64Decode(toDncode);
164     int32_t ret = GetAlignedHeight(value);
165     EXPECT_EQ(DCAMERA_OK, ret);
166 }
167 
168 /**
169  * @tc.name: GetAnonyInt32_001
170  * @tc.desc: Verify the GetAnonyInt32 function failed.
171  * @tc.type: FUNC
172  * @tc.require: Issue Number
173  */
174 HWTEST_F(DcameraUtilsToolsTest, GetAnonyInt32_001, TestSize.Level1)
175 {
176     int32_t value = 0;
177     GetAnonyInt32(value);
178     int32_t ret = GetAlignedHeight(value);
179     EXPECT_EQ(DCAMERA_OK, ret);
180 }
181 
182 /**
183  * @tc.name: IsOverDumpSize_001
184  * @tc.desc: Verify the IsUnderDumpMaxSize function failed.
185  * @tc.type: FUNC
186  * @tc.require: Issue Number
187  */
188 HWTEST_F(DcameraUtilsToolsTest, IsOverDumpSize_001, TestSize.Level1)
189 {
190     std::string fileName = "";
191     uint8_t *buf = nullptr;
192     size_t size = 0;
193     DumpBufferToFile(fileName, buf, size);
194     EXPECT_EQ(DCAMERA_INIT_ERR, IsUnderDumpMaxSize(fileName));
195     fileName = "test";
196     uint8_t str[] = "test";
197     size = strlen(reinterpret_cast<const char*>(str));
198     DumpBufferToFile(fileName, str, size);
199     IsUnderDumpMaxSize(fileName);
200 }
201 
202 /**
203  * @tc.name: IsUnderDumpMaxSize_001
204  * @tc.desc: Verify the IsUnderDumpMaxSize.
205  * @tc.type: FUNC
206  * @tc.require: Issue Number
207  */
208 HWTEST_F(DcameraUtilsToolsTest, IsUnderDumpMaxSize_001, TestSize.Level1)
209 {
210     std::string longFileName(PATH_MAX + 1, 'a');
211     EXPECT_EQ(IsUnderDumpMaxSize(longFileName), DCAMERA_INIT_ERR);
212 }
213 
214 /**
215  * @tc.name: IsUnderDumpMaxSize_002
216  * @tc.desc: Verify the IsUnderDumpMaxSize.
217  * @tc.type: FUNC
218  * @tc.require: Issue Number
219  */
220 HWTEST_F(DcameraUtilsToolsTest, IsUnderDumpMaxSize_002, TestSize.Level1)
221 {
222     EXPECT_EQ(IsUnderDumpMaxSize("/nonexistent/file"), DCAMERA_INIT_ERR);
223 }
224 
225 /**
226  * @tc.name: OpenDumpFile_001
227  * @tc.desc: Verify the IsUnderDumpMaxSize.
228  * @tc.type: FUNC
229  * @tc.require: Issue Number
230  */
231 HWTEST_F(DcameraUtilsToolsTest, OpenDumpFile_001, TestSize.Level1)
232 {
233     FILE *dumpFile = nullptr;
234     void *buffer = nullptr;
235     size_t bufferSize = 0;
236     DumpFileUtil::WriteDumpFile(dumpFile, buffer, bufferSize);
237     const std::string DUMP_SERVER_PARA_TEST = "sys.dcamera.dump.write.enable w";
238     const std::string DUMP_DCAMERA_TEST_FILENAME = "opendumpfile.txt";
239     DumpFileUtil::OpenDumpFile(DUMP_SERVER_PARA_TEST, DUMP_DCAMERA_TEST_FILENAME, &dumpFile);
240     DumpFileUtil::WriteDumpFile(dumpFile, buffer, bufferSize);
241     const std::string DUMP_SERVER_PARA_TEST_1 = "sys.dcamera.dump.write.enable a";
242     const std::string DUMP_DCAMERA_TEST_FILENAME_1 = "opendumpfile1.txt";
243     DumpFileUtil::OpenDumpFile(DUMP_SERVER_PARA_TEST_1, DUMP_DCAMERA_TEST_FILENAME_1, &dumpFile);
244     DumpFileUtil::WriteDumpFile(dumpFile, buffer, bufferSize);
245     DumpFileUtil::CloseDumpFile(&dumpFile);
246     EXPECT_EQ(true, dumpFile == nullptr);
247 }
248 } // namespace DistributedHardware
249 } // namespace OHOS