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 <memory> 17 18 #include <securec.h> 19 #include "gtest/gtest.h" 20 #define private public 21 #include "remove_box.h" 22 #include "match_box.h" 23 #include "bundle_icon_box.h" 24 #include "batch_remove_box.h" 25 #include "notification_sync_box.h" 26 #include "request_box.h" 27 #include "response_box.h" 28 #include "state_box.h" 29 #include "ans_log_wrapper.h" 30 31 using namespace testing::ext; 32 33 namespace OHOS { 34 namespace Notification { 35 class TlvBoxTest : public testing::Test { 36 public: SetUp()37 void SetUp() override {}; TearDown()38 void TearDown() override {}; 39 }; 40 41 /** 42 * @tc.name : Tlv box for batch remove. 43 * @tc.number : TlvBoxTest_0100 44 * @tc.desc : test tlv serialization and deserialization. 45 */ 46 HWTEST_F(TlvBoxTest, TlvBoxTest_0100, Function | SmallTest | Level1) 47 { 48 auto data = std::make_shared<BatchRemoveNotificationBox>(); 49 data->SetNotificationHashCodes("123"); 50 data->SetNotificationSlotTypes("321"); 51 data->Serialize(); 52 int len = data->GetByteLength(); 53 unsigned char* cached = new unsigned char[len]; 54 errno_t err = memcpy_s(cached, len, data->GetByteBuffer(), len); 55 if (err != EOK) { 56 delete[] cached; 57 EXPECT_EQ((int)err, (int)EOK); 58 } 59 bool result = TlvBox::CheckMessageCRC((const unsigned char*)cached, len); 60 EXPECT_EQ(result, true); 61 std::shared_ptr<TlvBox> box = std::make_shared<TlvBox>(); 62 result = box->Parse((const unsigned char*)cached, len - sizeof(uint32_t)); 63 EXPECT_EQ(result, true); 64 delete[] cached; 65 66 BatchRemoveNotificationBox bacthBox = BatchRemoveNotificationBox(box); 67 bacthBox.box_ = nullptr; 68 EXPECT_EQ(bacthBox.SetNotificationHashCodes("123"), false); 69 EXPECT_EQ(bacthBox.SetNotificationSlotTypes("321"), false); 70 EXPECT_EQ(bacthBox.Serialize(), false); 71 } 72 73 /** 74 * @tc.name : Tlv box for remove. 75 * @tc.number : TlvBoxTest_0101 76 * @tc.desc : test tlv serialization and deserialization. 77 */ 78 HWTEST_F(TlvBoxTest, TlvBoxTest_0101, Function | SmallTest | Level1) 79 { 80 auto data = std::make_shared<NotificationRemoveBox>(); 81 data->SetNotificationHashCode("123"); 82 data->SetNotificationSlotType(1); 83 data->Serialize(); 84 int len = data->GetByteLength(); 85 unsigned char* cached = new unsigned char[len]; 86 errno_t err = memcpy_s(cached, len, data->GetByteBuffer(), len); 87 if (err != EOK) { 88 delete[] cached; 89 EXPECT_EQ((int)err, (int)EOK); 90 } 91 bool result = TlvBox::CheckMessageCRC((const unsigned char*)cached, len); 92 EXPECT_EQ(result, true); 93 std::shared_ptr<TlvBox> box = std::make_shared<TlvBox>(); 94 result = box->Parse((const unsigned char*)cached, len - sizeof(uint32_t)); 95 EXPECT_EQ(result, true); 96 delete[] cached; 97 98 NotificationRemoveBox removeBox = NotificationRemoveBox(box); 99 removeBox.box_ = nullptr; 100 EXPECT_EQ(removeBox.SetNotificationHashCode("123"), false); 101 EXPECT_EQ(removeBox.SetNotificationSlotType(1), false); 102 } 103 104 /** 105 * @tc.name : Tlv box for remove. 106 * @tc.number : TlvBoxTest_0111 107 * @tc.desc : test tlv serialization and deserialization. 108 */ 109 HWTEST_F(TlvBoxTest, TlvBoxTest_0111, Function | SmallTest | Level1) 110 { 111 auto data = std::make_shared<NotificationRemoveBox>(); 112 data->SetNotificationHashCode("123"); 113 data->SetNotificationSlotType(1); 114 data->Serialize(); 115 int len = data->GetByteLength(); 116 unsigned char* cached = new unsigned char[len]; 117 errno_t err = memcpy_s(cached, len, data->GetByteBuffer(), len); 118 if (err != EOK) { 119 delete[] cached; 120 EXPECT_EQ((int)err, (int)EOK); 121 } 122 bool result = TlvBox::CheckMessageCRC((const unsigned char*)cached, len); 123 EXPECT_EQ(result, true); 124 std::shared_ptr<TlvBox> box = std::make_shared<TlvBox>(); 125 result = box->Parse((const unsigned char*)cached, len - sizeof(uint32_t)); 126 EXPECT_EQ(result, true); 127 delete[] cached; 128 } 129 130 /** 131 * @tc.name : Tlv box for bundle icon. 132 * @tc.number : TlvBoxTest_0102 133 * @tc.desc : test tlv serialization and deserialization. 134 */ 135 HWTEST_F(TlvBoxTest, TlvBoxTest_BundleIcon_0100, Function | SmallTest | Level1) 136 { 137 auto data = std::make_shared<BundleIconBox>(); 138 data->SetMessageType(BUNDLE_ICON_SYNC); 139 data->SetIconSyncType(IconSyncType::REQUEST_BUNDLE_ICON); 140 data->SetBundleList({"ohom.example.test"}); 141 data->SetLocalDeviceId("local_device"); 142 data->Serialize(); 143 int len = data->GetByteLength(); 144 unsigned char* cached = new unsigned char[len]; 145 errno_t err = memcpy_s(cached, len, data->GetByteBuffer(), len); 146 if (err != EOK) { 147 delete[] cached; 148 EXPECT_EQ((int)err, (int)EOK); 149 } 150 bool result = TlvBox::CheckMessageCRC((const unsigned char*)cached, len); 151 EXPECT_EQ(result, true); 152 std::shared_ptr<TlvBox> box = std::make_shared<TlvBox>(); 153 result = box->Parse((const unsigned char*)cached, len - sizeof(uint32_t)); 154 EXPECT_EQ(result, true); 155 delete[] cached; 156 157 int32_t intData; 158 BundleIconBox iconBox = BundleIconBox(box); 159 EXPECT_EQ(iconBox.box_->GetMessageType(intData), true); 160 EXPECT_EQ(intData, BUNDLE_ICON_SYNC); 161 EXPECT_EQ(iconBox.GetIconSyncType(intData), true); 162 EXPECT_EQ(intData, IconSyncType::REQUEST_BUNDLE_ICON); 163 std::string stringData; 164 EXPECT_EQ(iconBox.GetLocalDeviceId(stringData), true); 165 EXPECT_EQ(stringData, "local_device"); 166 std::vector<std::string> bundleList; 167 EXPECT_EQ(iconBox.GetBundleList(bundleList), true); 168 EXPECT_EQ(bundleList.empty(), false); 169 iconBox.box_ = nullptr; 170 EXPECT_EQ(iconBox.SetMessageType(BUNDLE_ICON_SYNC), false); 171 EXPECT_EQ(iconBox.SetIconSyncType(IconSyncType::REQUEST_BUNDLE_ICON), false); 172 EXPECT_EQ(iconBox.SetBundleList({"ohom.example.test"}), false); 173 EXPECT_EQ(iconBox.SetLocalDeviceId("local_device"), false); 174 EXPECT_EQ(iconBox.SetDataLength(1), false); 175 std::unordered_map<std::string, std::string> bundles; 176 bundles.insert({"123", "abc"}); 177 EXPECT_EQ(iconBox.SetBundlesIcon(bundles), false); 178 179 std::unordered_map<std::string, std::string> receiveBundles; 180 EXPECT_EQ(iconBox.GetIconSyncType(intData), false); 181 EXPECT_EQ(iconBox.GetLocalDeviceId(stringData), false); 182 EXPECT_EQ(iconBox.GetBundleList(bundleList), false); 183 EXPECT_EQ(iconBox.GetDataLength(intData), false); 184 EXPECT_EQ(iconBox.GetBundlesIcon(receiveBundles), false); 185 } 186 187 /** 188 * @tc.name : Tlv box for bundle icon. 189 * @tc.number : TlvBoxTest_0103 190 * @tc.desc : test tlv serialization and deserialization. 191 */ 192 HWTEST_F(TlvBoxTest, TlvBoxTest_BundleIcon_0101, Function | SmallTest | Level1) 193 { 194 auto data = std::make_shared<BundleIconBox>(); 195 std::unordered_map<std::string, std::string> bundles; 196 bundles.insert({"123", "abc"}); 197 data->SetBundlesIcon(bundles); 198 data->Serialize(); 199 int len = data->GetByteLength(); 200 unsigned char* cached = new unsigned char[len]; 201 errno_t err = memcpy_s(cached, len, data->GetByteBuffer(), len); 202 if (err != EOK) { 203 delete[] cached; 204 EXPECT_EQ((int)err, (int)EOK); 205 } 206 bool result = TlvBox::CheckMessageCRC((const unsigned char*)cached, len); 207 EXPECT_EQ(result, true); 208 std::shared_ptr<TlvBox> box = std::make_shared<TlvBox>(); 209 result = box->Parse((const unsigned char*)cached, len - sizeof(uint32_t)); 210 EXPECT_EQ(result, true); 211 delete[] cached; 212 213 bundles.clear(); 214 BundleIconBox iconBox = BundleIconBox(box); 215 EXPECT_EQ(iconBox.GetBundlesIcon(bundles), true); 216 EXPECT_EQ(bundles.empty(), false); 217 } 218 219 /** 220 * @tc.name : Tlv box for bundle icon. 221 * @tc.number : TlvBoxTest_0103 222 * @tc.desc : test tlv serialization and deserialization. 223 */ 224 HWTEST_F(TlvBoxTest, TlvBoxTest_BundleIcon_0102, Function | SmallTest | Level1) 225 { 226 auto data = std::make_shared<BundleIconBox>(); 227 std::vector<std::pair<std::string, std::string>> bundles; 228 bundles.push_back(std::make_pair("com.oh.test", "test")); 229 data->SetBundlesInfo(bundles); 230 data->Serialize(); 231 int len = data->GetByteLength(); 232 unsigned char* cached = new unsigned char[len]; 233 errno_t err = memcpy_s(cached, len, data->GetByteBuffer(), len); 234 if (err != EOK) { 235 delete[] cached; 236 EXPECT_EQ((int)err, (int)EOK); 237 } 238 bool result = TlvBox::CheckMessageCRC((const unsigned char*)cached, len); 239 EXPECT_EQ(result, true); 240 std::shared_ptr<TlvBox> box = std::make_shared<TlvBox>(); 241 result = box->Parse((const unsigned char*)cached, len - sizeof(uint32_t)); 242 EXPECT_EQ(result, true); 243 delete[] cached; 244 245 std::vector<std::string> bundle; 246 std::vector<std::string> labels; 247 BundleIconBox iconBox = BundleIconBox(box); 248 EXPECT_EQ(iconBox.GetBundlesInfo(bundle, labels), true); 249 EXPECT_EQ(bundles.empty(), false); 250 } 251 } // namespace Notification 252 } // namespace OHOS 253