1 /*
2 * Copyright (c) 2021-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 <future>
17
18 #include <gtest/gtest.h>
19
20 #include "uds_client.h"
21
22 namespace OHOS {
23 namespace MMI {
24 namespace {
25 using namespace testing::ext;
26 } // namespace
27
28 class UDSClientTest : public testing::Test {
29 public:
SetUpTestCase(void)30 static void SetUpTestCase(void) {}
TearDownTestCase(void)31 static void TearDownTestCase(void) {}
32 };
33
34 class UDSClientUnitTest : public UDSClient {
35 public:
SetFd(int32_t fd)36 void SetFd(int32_t fd)
37 {
38 fd_ = fd;
39 }
Socket()40 int32_t Socket()
41 {
42 return fd_;
43 }
44 };
45
46 HWTEST_F(UDSClientTest, ConnectTo_01, TestSize.Level1)
47 {
48 UDSClientUnitTest udsClient;
49 int32_t retResult = udsClient.ConnectTo();
50 ASSERT_EQ(RET_ERR, retResult);
51 }
52
53 HWTEST_F(UDSClientTest, ConnectTo_02, TestSize.Level1)
54 {
55 UDSClientUnitTest udsClient;
56 udsClient.SetFd(0);
57 int32_t retResult = udsClient.ConnectTo();
58 ASSERT_EQ(RET_OK, retResult);
59 }
60
61 HWTEST_F(UDSClientTest, SendMsg_001, TestSize.Level1)
62 {
63 const char *buf = nullptr;
64 size_t size = 0;
65
66 UDSClientUnitTest udsClientUt;
67 auto retResult = udsClientUt.SendMsg(buf, size);
68 ASSERT_FALSE(retResult);
69 }
70
71 HWTEST_F(UDSClientTest, SendMsg_002, TestSize.Level1)
72 {
73 const char *buf = "1234#";
74 size_t size = 5;
75
76 UDSClientUnitTest udsClientUt;
77 auto retResult = udsClientUt.SendMsg(buf, size);
78 ASSERT_FALSE(retResult);
79 }
80
81 HWTEST_F(UDSClientTest, SendMsg_type2_001, TestSize.Level1)
82 {
83 NetPacket pkt(MmiMessageId::INVALID);
84
85 UDSClientUnitTest udsClientUt;
86 auto retResult = udsClientUt.SendMsg(pkt);
87 ASSERT_FALSE(retResult);
88 }
89
90 HWTEST_F(UDSClientTest, SendMsg_type2_002, TestSize.Level1)
91 {
92 NetPacket pkt(static_cast<MmiMessageId>(222));
93
94 UDSClientUnitTest udsClientUt;
95 auto retResult = udsClientUt.SendMsg(pkt);
96 ASSERT_FALSE(retResult);
97 }
98
99 HWTEST_F(UDSClientTest, Stop_001, TestSize.Level1)
100 {
101 UDSClientUnitTest udsClientUt;
102 ASSERT_NO_FATAL_FAILURE(udsClientUt.Stop());
103 }
104
105 HWTEST_F(UDSClientTest, SendMsg_003, TestSize.Level1)
106 {
107 const char *buf = "1234#";
108 size_t size = 1025 * 8;
109
110 UDSClientUnitTest udsClientUt;
111 udsClientUt.SetFd(0);
112 auto retResult = udsClientUt.SendMsg(buf, size);
113 ASSERT_FALSE(retResult);
114 }
115
116 HWTEST_F(UDSClientTest, SendMsg_004, TestSize.Level1)
117 {
118 const char *buf = "1234#";
119 size_t size = 0;
120
121 UDSClientUnitTest udsClientUt;
122 udsClientUt.SetFd(0);
123 auto retResult = udsClientUt.SendMsg(buf, size);
124 ASSERT_FALSE(retResult);
125 }
126
127 HWTEST_F(UDSClientTest, SendMsg_005, TestSize.Level1)
128 {
129 const char *buf = "1234#";
130 size_t size = 8;
131
132 UDSClientUnitTest udsClientUt;
133 udsClientUt.SetFd(0);
134 auto retResult = udsClientUt.SendMsg(buf, size);
135 ASSERT_FALSE(retResult);
136 }
137
StartClientTest(const UDSClient & udsClient,NetPacket & netPacket)138 void StartClientTest(const UDSClient &udsClient, NetPacket &netPacket) {}
139
140 HWTEST_F(UDSClientTest, StartClient_001, TestSize.Level1)
141 {
142 UDSClientUnitTest udsClientUt;
143 udsClientUt.isRunning_ = true;
144 udsClientUt.isConnected_ = false;
145 MsgClientFunCallback fun = StartClientTest;
146 ASSERT_NO_FATAL_FAILURE(udsClientUt.StartClient(fun));
147 }
148
149 HWTEST_F(UDSClientTest, StartClient_002, TestSize.Level1)
150 {
151 UDSClientUnitTest udsClientUt;
152 udsClientUt.isRunning_ = false;
153 udsClientUt.isConnected_ = true;
154 MsgClientFunCallback fun = StartClientTest;
155 ASSERT_NO_FATAL_FAILURE(udsClientUt.StartClient(fun));
156 }
157 } // namespace MMI
158 } // namespace OHOS
159