1 /*
2 * Copyright (c) 2021 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 #include <openssl/sha.h>
18 #include "reporter.h"
19
20 using namespace testing::ext;
21 using namespace OHOS::DistributedDataDfx;
22
23 class DistributedataDfxMSTTest : public testing::Test {
24 public:
25 static void SetUpTestCase(void);
26
27 static void TearDownTestCase(void);
28
29 void SetUp();
30
31 void TearDown();
32 };
33
SetUpTestCase()34 void DistributedataDfxMSTTest::SetUpTestCase()
35 {
36 size_t max = 12;
37 size_t min = 5;
38 Reporter::GetInstance()->SetThreadPool(std::make_shared<OHOS::ExecutorPool>(max, min));
39 }
40
TearDownTestCase()41 void DistributedataDfxMSTTest::TearDownTestCase()
42 {
43 Reporter::GetInstance()->SetThreadPool(nullptr);
44 }
45
SetUp()46 void DistributedataDfxMSTTest::SetUp() {}
47
TearDown()48 void DistributedataDfxMSTTest::TearDown() {}
49
50 /**
51 * @tc.name: Dfx001
52 * @tc.desc: send data to 1 device, then check reporter message.
53 * @tc.type: send data
54 * @tc.require: AR000CQE1L
55 * @tc.author: hongbo
56 */
57 HWTEST_F(DistributedataDfxMSTTest, Dfx001, TestSize.Level0)
58 {
59 /**
60 * @tc.steps: step1. getcommunicationFault instance
61 * @tc.expected: step1. Expect instance is not null.
62 */
63 auto comFault = Reporter::GetInstance()->CommunicationFault();
64 EXPECT_NE(nullptr, comFault);
65 struct CommFaultMsg msg{.userId = "user001", .appId = "myApp", .storeId = "storeTest"};
66 msg.deviceId.push_back("device001");
67 msg.errorCode.push_back(001);
68 msg.deviceId.push_back("device002");
69 msg.errorCode.push_back(002);
70
71 auto repStatus = comFault->Report(msg);
72 EXPECT_TRUE(repStatus == ReportStatus::SUCCESS);
73
74 std::string myuid = "203230afadj020003";
75 std::vector<uint8_t> value(myuid.begin(), myuid.end());
76 std::vector<uint8_t> result;
77
78 SHA256_CTX *context = new (std::nothrow) SHA256_CTX;
79 if (context == nullptr || !SHA256_Init(context)) {
80 delete context;
81 return;
82 }
83
84 if (!SHA256_Update(context, value.data(), value.size())) {
85 delete context;
86 return;
87 }
88 result.resize(SHA256_DIGEST_LENGTH);
89 SHA256_Final(result.data(), context);
90 delete context;
91 }
92