1 /* 2 * Copyright (c) 2022 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 <cstdint> 17 #include <gtest/gtest.h> 18 #include <variant> 19 #include <vector> 20 21 #include "change_notification.h" 22 #include "iremote_broker.h" 23 #include "iremote_object.h" 24 #include "iremote_proxy.h" 25 #include "iremote_stub.h" 26 #include "itypes_util.h" 27 #include "types.h" 28 29 using namespace testing::ext; 30 using namespace OHOS::DistributedKv; 31 using namespace OHOS; 32 using var_t = std::variant<std::monostate, uint32_t, std::string, int32_t, uint64_t>; 33 class TypesUtilTest : public testing::Test { 34 public: 35 class ITestRemoteObject : public IRemoteBroker { 36 public: 37 DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.ITestRemoteObject"); 38 }; 39 class TestRemoteObjectStub : public IRemoteStub<ITestRemoteObject> { 40 public: 41 }; 42 class TestRemoteObjectProxy : public IRemoteProxy<ITestRemoteObject> { 43 public: TestRemoteObjectProxy(const sptr<IRemoteObject> & impl)44 explicit TestRemoteObjectProxy(const sptr<IRemoteObject> &impl) 45 : IRemoteProxy<ITestRemoteObject>(impl) 46 {} 47 ~TestRemoteObjectProxy() = default; 48 private: 49 static inline BrokerDelegator<TestRemoteObjectProxy> delegator_; 50 }; 51 class TestRemoteObjectClient : public TestRemoteObjectStub { 52 public: TestRemoteObjectClient()53 TestRemoteObjectClient() {} ~TestRemoteObjectClient()54 virtual ~TestRemoteObjectClient() {} 55 }; SetUpTestCase(void)56 static void SetUpTestCase(void) {}; TearDownTestCase(void)57 static void TearDownTestCase(void) {}; SetUp()58 void SetUp() {}; TearDown()59 void TearDown() {}; 60 }; 61 62 HWTEST_F(TypesUtilTest, DeviceInfo, TestSize.Level0) 63 { 64 MessageParcel parcel; 65 DeviceInfo clientDev; 66 clientDev.deviceId = "123"; 67 clientDev.deviceName = "rk3568"; 68 clientDev.deviceType = "phone"; 69 ASSERT_TRUE(ITypesUtil::Marshal(parcel, clientDev)); 70 DeviceInfo serverDev; 71 ASSERT_TRUE(ITypesUtil::Unmarshal(parcel, serverDev)); 72 ASSERT_EQ(clientDev.deviceId, serverDev.deviceId); 73 ASSERT_EQ(clientDev.deviceName, serverDev.deviceName); 74 ASSERT_EQ(clientDev.deviceType, serverDev.deviceType); 75 } 76 77 HWTEST_F(TypesUtilTest, Entry, TestSize.Level0) 78 { 79 MessageParcel parcel; 80 Entry entryIn; 81 entryIn.key = "student_name_mali"; 82 entryIn.value = "age:20"; 83 ASSERT_TRUE(ITypesUtil::Marshal(parcel, entryIn)); 84 Entry entryOut; 85 ASSERT_TRUE(ITypesUtil::Unmarshal(parcel, entryOut)); 86 EXPECT_EQ(entryOut.key.ToString(), std::string("student_name_mali")); 87 EXPECT_EQ(entryOut.value.ToString(), std::string("age:20")); 88 } 89 90 HWTEST_F(TypesUtilTest, ChangeNotification, TestSize.Level1) 91 { 92 Entry insert, update, del; 93 insert.key = "insert"; 94 update.key = "update"; 95 del.key = "delete"; 96 insert.value = "insert_value"; 97 update.value = "update_value"; 98 del.value = "delete_value"; 99 std::vector<Entry> inserts, updates, deleteds; 100 inserts.push_back(insert); 101 updates.push_back(update); 102 deleteds.push_back(del); 103 104 ChangeNotification changeIn(std::move(inserts), std::move(updates), std::move(deleteds), std::string(), false); 105 MessageParcel parcel; 106 ASSERT_TRUE(ITypesUtil::Marshal(parcel, changeIn)); 107 ChangeNotification changeOut({}, {}, {}, "", false); 108 ASSERT_TRUE(ITypesUtil::Unmarshal(parcel, changeOut)); 109 ASSERT_EQ(changeOut.GetInsertEntries().size(), 1UL); 110 EXPECT_EQ(changeOut.GetInsertEntries().front().key.ToString(), std::string("insert")); 111 EXPECT_EQ(changeOut.GetInsertEntries().front().value.ToString(), std::string("insert_value")); 112 ASSERT_EQ(changeOut.GetUpdateEntries().size(), 1UL); 113 EXPECT_EQ(changeOut.GetUpdateEntries().front().key.ToString(), std::string("update")); 114 EXPECT_EQ(changeOut.GetUpdateEntries().front().value.ToString(), std::string("update_value")); 115 ASSERT_EQ(changeOut.GetDeleteEntries().size(), 1UL); 116 EXPECT_EQ(changeOut.GetDeleteEntries().front().key.ToString(), std::string("delete")); 117 EXPECT_EQ(changeOut.GetDeleteEntries().front().value.ToString(), std::string("delete_value")); 118 EXPECT_EQ(changeOut.IsClear(), false); 119 } 120 121 122 HWTEST_F(TypesUtilTest, Multiple, TestSize.Level1) 123 { 124 uint32_t input1 = 10; 125 int32_t input2 = -10; 126 std::string input3 = "i test"; 127 Blob input4 = "input 4"; 128 Entry input5; 129 input5.key = "my test"; 130 input5.value = "test value"; 131 DeviceInfo input6 = {.deviceId = "mock deviceId", .deviceName = "mock phone", .deviceType = "0"}; 132 sptr<ITestRemoteObject> input7 = new TestRemoteObjectClient(); 133 MessageParcel parcel; 134 ASSERT_TRUE(ITypesUtil::Marshal(parcel, input1, input2, input3, input4, input5, input6, input7->AsObject())); 135 uint32_t output1 = 0; 136 int32_t output2 = 0; 137 std::string output3 = ""; 138 Blob output4; 139 Entry output5; 140 DeviceInfo output6; 141 sptr<IRemoteObject> output7; 142 ASSERT_TRUE(ITypesUtil::Unmarshal(parcel, output1, output2, output3, output4, output5, output6, output7)); 143 ASSERT_EQ(output1, input1); 144 ASSERT_EQ(output2, input2); 145 ASSERT_EQ(output3, input3); 146 ASSERT_EQ(output4, input4); 147 ASSERT_EQ(output5.key, input5.key); 148 ASSERT_EQ(output5.value, input5.value); 149 ASSERT_EQ(output6.deviceId, input6.deviceId); 150 ASSERT_EQ(output6.deviceName, input6.deviceName); 151 ASSERT_EQ(output6.deviceType, input6.deviceType); 152 ASSERT_EQ(output7, input7->AsObject()); 153 } 154 155 HWTEST_F(TypesUtilTest, Variant, TestSize.Level0) 156 { 157 MessageParcel parcelNull; 158 var_t valueNullIn; 159 ASSERT_TRUE(ITypesUtil::Marshal(parcelNull, valueNullIn)); 160 var_t valueNullOut; 161 ASSERT_TRUE(ITypesUtil::Unmarshal(parcelNull, valueNullOut)); 162 ASSERT_EQ(valueNullOut.index(), 0); 163 164 MessageParcel parcelUint; 165 var_t valueUintIn; 166 valueUintIn.emplace<1>(100); 167 ASSERT_TRUE(ITypesUtil::Marshal(parcelUint, valueUintIn)); 168 var_t valueUintOut; 169 ASSERT_TRUE(ITypesUtil::Unmarshal(parcelUint, valueUintOut)); 170 ASSERT_EQ(valueUintOut.index(), 1); 171 ASSERT_EQ(std::get<uint32_t>(valueUintOut), 100); 172 173 MessageParcel parcelString; 174 var_t valueStringIn; 175 valueStringIn.emplace<2>("valueString"); 176 ASSERT_TRUE(ITypesUtil::Marshal(parcelString, valueStringIn)); 177 var_t valueStringOut; 178 ASSERT_TRUE(ITypesUtil::Unmarshal(parcelString, valueStringOut)); 179 ASSERT_EQ(valueStringOut.index(), 2); 180 ASSERT_EQ(std::get<std::string>(valueStringOut), "valueString"); 181 182 MessageParcel parcelInt; 183 var_t valueIntIn; 184 valueIntIn.emplace<3>(101); 185 ASSERT_TRUE(ITypesUtil::Marshal(parcelInt, valueIntIn)); 186 var_t valueIntOut; 187 ASSERT_TRUE(ITypesUtil::Unmarshal(parcelInt, valueIntOut)); 188 ASSERT_EQ(valueIntOut.index(), 3); 189 ASSERT_EQ(std::get<int32_t>(valueIntOut), 101); 190 191 MessageParcel parcelUint64; 192 var_t valueUint64In; 193 valueUint64In.emplace<4>(110); 194 ASSERT_TRUE(ITypesUtil::Marshal(parcelUint64, valueUint64In)); 195 var_t valueUint64Out; 196 ASSERT_TRUE(ITypesUtil::Unmarshal(parcelUint64, valueUint64Out)); 197 ASSERT_EQ(valueUint64Out.index(), 4); 198 ASSERT_EQ(std::get<uint64_t>(valueUint64Out), 110); 199 } 200 201