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
16 #define LOG_TAG "NdkDataConversionTest"
17
18 #include "ndk_data_conversion.h"
19 #include <gtest/gtest.h>
20 #include <memory>
21 #include "token_setproc.h"
22 #include "accesstoken_kit.h"
23 #include "nativetoken_kit.h"
24 #include "logger.h"
25 #include "error_code.h"
26 #include "udmf.h"
27 #include "udmf_capi_common.h"
28 #include "uds.h"
29
30 using namespace testing::ext;
31 using namespace OHOS::Security::AccessToken;
32 using namespace OHOS::UDMF;
33 using namespace OHOS;
34
35 namespace OHOS::Test {
36 class NdkDataConversionTest : public testing::Test {
37 public:
38 static void SetUpTestCase(void);
39
40 static void TearDownTestCase(void);
41
42 void SetUp();
43
44 void TearDown();
45 };
46
SetUpTestCase(void)47 void NdkDataConversionTest::SetUpTestCase(void) {}
48
TearDownTestCase(void)49 void NdkDataConversionTest::TearDownTestCase(void) {}
50
SetUp(void)51 void NdkDataConversionTest::SetUp(void) {}
52
TearDown(void)53 void NdkDataConversionTest::TearDown(void) {}
54
55 /* *
56 * @tc.name: GetNativeUnifiedData_001
57 * @tc.desc: Normal testcase of GetNativeUnifiedData
58 * @tc.type: FUNC
59 */
60 HWTEST_F(NdkDataConversionTest, GetNativeUnifiedData_001, TestSize.Level1)
61 {
62 LOG_INFO(UDMF_TEST, "GetNativeUnifiedData_001 begin.");
63 auto unifiedRecord = std::make_shared<UnifiedRecord>();
64 const std::string uid("typeId");
65 unifiedRecord->SetUid(uid);
66 OH_UdmfData *ndkData = OH_UdmfData_Create();
67 ndkData->unifiedData_->AddRecord(unifiedRecord);
68 auto data = std::make_shared<UnifiedData>();
69
70 Status status = NdkDataConversion::GetNativeUnifiedData(ndkData, data);
71 ASSERT_EQ(E_OK, status);
72 EXPECT_EQ("typeId", data->GetRecordAt(0)->GetUid());
73
74 OH_UdmfData *ndkDataNull = nullptr;
75 status = NdkDataConversion::GetNativeUnifiedData(ndkDataNull, data);
76 ASSERT_EQ(E_INVALID_PARAMETERS, status);
77
78 std::shared_ptr<UnifiedData> dataNull;
79 status = NdkDataConversion::GetNativeUnifiedData(ndkData, dataNull);
80 OH_UdmfData_Destroy(ndkData);
81 ASSERT_EQ(E_INVALID_PARAMETERS, status);
82 LOG_INFO(UDMF_TEST, "GetNativeUnifiedData_001 end.");
83 }
84
85 /* *
86 * @tc.name: GetNativeUnifiedData_002
87 * @tc.desc: Normal testcase of GetNativeUnifiedData
88 * @tc.type: FUNC
89 */
90 HWTEST_F(NdkDataConversionTest, GetNativeUnifiedData_002, TestSize.Level1)
91 {
92 LOG_INFO(UDMF_TEST, "GetNativeUnifiedData_002 begin.");
93 auto plainText = OH_UdsPlainText_Create();
94 OH_UdmfData *fakeNdkData = reinterpret_cast<OH_UdmfData *>(plainText);
95 auto data = std::make_shared<UnifiedData>();
96 Status status = NdkDataConversion::GetNativeUnifiedData(fakeNdkData, data);
97 OH_UdsPlainText_Destroy(plainText);
98 ASSERT_EQ(E_INVALID_PARAMETERS, status);
99 LOG_INFO(UDMF_TEST, "GetNativeUnifiedData_002 end.");
100 }
101
102 /* *
103 * @tc.name: GetNdkUnifiedData_001
104 * @tc.desc: Error testcase of GetNdkUnifiedData
105 * @tc.type: FUNC
106 */
107 HWTEST_F(NdkDataConversionTest, GetNdkUnifiedData_001, TestSize.Level1)
108 {
109 LOG_INFO(UDMF_TEST, "GetNdkUnifiedData_001 begin.");
110 auto unifiedRecord = std::make_shared<UnifiedRecord>();
111 const std::string uid("typeId");
112 unifiedRecord->SetUid(uid);
113 auto data = std::make_shared<UnifiedData>();
114 data->AddRecord(unifiedRecord);
115 OH_UdmfData *ndkData = OH_UdmfData_Create();
116 Status status = NdkDataConversion::GetNdkUnifiedData(data, ndkData);
117 ASSERT_EQ(E_OK, status);
118 EXPECT_EQ("typeId", ndkData->unifiedData_->GetRecordAt(0)->GetUid());
119
120 OH_UdmfData *ndkDataNull = nullptr;
121 status = NdkDataConversion::GetNdkUnifiedData(data, ndkDataNull);
122 ASSERT_EQ(E_INVALID_PARAMETERS, status);
123
124 std::shared_ptr<UnifiedData> dataNull;
125 status = NdkDataConversion::GetNdkUnifiedData(dataNull, ndkData);
126 OH_UdmfData_Destroy(ndkData);
127 ASSERT_EQ(E_INVALID_PARAMETERS, status);
128 LOG_INFO(UDMF_TEST, "GetNdkUnifiedData_001 end.");
129 }
130
131 /* *
132 * @tc.name: GetNdkUnifiedData_002
133 * @tc.desc: Error testcase of GetNdkUnifiedData
134 * @tc.type: FUNC
135 */
136 HWTEST_F(NdkDataConversionTest, GetNdkUnifiedData_002, TestSize.Level1)
137 {
138 LOG_INFO(UDMF_TEST, "GetNdkUnifiedData_002 begin.");
139 auto plainText = OH_UdsPlainText_Create();
140 OH_UdmfData *fakeNdkData = reinterpret_cast<OH_UdmfData *>(plainText);
141 auto data = std::make_shared<UnifiedData>();
142 Status status = NdkDataConversion::GetNdkUnifiedData(data, fakeNdkData);
143 OH_UdsPlainText_Destroy(plainText);
144 ASSERT_EQ(E_INVALID_PARAMETERS, status);
145 LOG_INFO(UDMF_TEST, "GetNdkUnifiedData_002 end.");
146 }
147 }