source: rtos_arduino/trunk/arduino_lib/libraries/pubsubclient-2.6/tests/src/connect_spec.cpp@ 209

Last change on this file since 209 was 209, checked in by ertl-honda, 8 years ago

BlueMix用のフィアルを追加

File size: 8.1 KB
Line 
1#include "PubSubClient.h"
2#include "ShimClient.h"
3#include "Buffer.h"
4#include "BDDTest.h"
5#include "trace.h"
6
7
8byte server[] = { 172, 16, 0, 2 };
9
10void callback(char* topic, byte* payload, unsigned int length) {
11 // handle message arrived
12}
13
14
15int test_connect_fails_no_network() {
16 IT("fails to connect if underlying client doesn't connect");
17 ShimClient shimClient;
18 shimClient.setAllowConnect(false);
19 PubSubClient client(server, 1883, callback, shimClient);
20 int rc = client.connect((char*)"client_test1");
21 IS_FALSE(rc);
22 int state = client.state();
23 IS_TRUE(state == MQTT_CONNECT_FAILED);
24 END_IT
25}
26
27int test_connect_fails_on_no_response() {
28 IT("fails to connect if no response received after 15 seconds");
29 ShimClient shimClient;
30 shimClient.setAllowConnect(true);
31 PubSubClient client(server, 1883, callback, shimClient);
32 int rc = client.connect((char*)"client_test1");
33 IS_FALSE(rc);
34 int state = client.state();
35 IS_TRUE(state == MQTT_CONNECTION_TIMEOUT);
36 END_IT
37}
38
39int test_connect_properly_formatted() {
40 IT("sends a properly formatted connect packet and succeeds");
41 ShimClient shimClient;
42
43 shimClient.setAllowConnect(true);
44 byte expectServer[] = { 172, 16, 0, 2 };
45 shimClient.expectConnect(expectServer,1883);
46 byte connect[] = {0x10,0x18,0x0,0x4,0x4d,0x51,0x54,0x54,0x4,0x2,0x0,0xf,0x0,0xc,0x63,0x6c,0x69,0x65,0x6e,0x74,0x5f,0x74,0x65,0x73,0x74,0x31};
47 byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
48
49 shimClient.expect(connect,26);
50 shimClient.respond(connack,4);
51
52 PubSubClient client(server, 1883, callback, shimClient);
53 int state = client.state();
54 IS_TRUE(state == MQTT_DISCONNECTED);
55
56 int rc = client.connect((char*)"client_test1");
57 IS_TRUE(rc);
58 IS_FALSE(shimClient.error());
59
60 state = client.state();
61 IS_TRUE(state == MQTT_CONNECTED);
62
63 END_IT
64}
65
66int test_connect_properly_formatted_hostname() {
67 IT("accepts a hostname");
68 ShimClient shimClient;
69
70 shimClient.setAllowConnect(true);
71 shimClient.expectConnect((char* const)"localhost",1883);
72 byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
73 shimClient.respond(connack,4);
74
75 PubSubClient client((char* const)"localhost", 1883, callback, shimClient);
76 int rc = client.connect((char*)"client_test1");
77 IS_TRUE(rc);
78 IS_FALSE(shimClient.error());
79
80 END_IT
81}
82
83
84int test_connect_fails_on_bad_rc() {
85 IT("fails to connect if a bad return code is received");
86 ShimClient shimClient;
87 shimClient.setAllowConnect(true);
88 byte connack[] = { 0x20, 0x02, 0x00, 0x01 };
89 shimClient.respond(connack,4);
90
91 PubSubClient client(server, 1883, callback, shimClient);
92 int rc = client.connect((char*)"client_test1");
93 IS_FALSE(rc);
94
95 int state = client.state();
96 IS_TRUE(state == 0x01);
97
98 END_IT
99}
100
101int test_connect_accepts_username_password() {
102 IT("accepts a username and password");
103 ShimClient shimClient;
104 shimClient.setAllowConnect(true);
105
106 byte connect[] = { 0x10,0x24,0x0,0x4,0x4d,0x51,0x54,0x54,0x4,0xc2,0x0,0xf,0x0,0xc,0x63,0x6c,0x69,0x65,0x6e,0x74,0x5f,0x74,0x65,0x73,0x74,0x31,0x0,0x4,0x75,0x73,0x65,0x72,0x0,0x4,0x70,0x61,0x73,0x73};
107 byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
108 shimClient.expect(connect,0x26);
109 shimClient.respond(connack,4);
110
111 PubSubClient client(server, 1883, callback, shimClient);
112 int rc = client.connect((char*)"client_test1",(char*)"user",(char*)"pass");
113 IS_TRUE(rc);
114 IS_FALSE(shimClient.error());
115
116 END_IT
117}
118
119int test_connect_accepts_username_no_password() {
120 IT("accepts a username but no password");
121 ShimClient shimClient;
122 shimClient.setAllowConnect(true);
123
124 byte connect[] = { 0x10,0x1e,0x0,0x4,0x4d,0x51,0x54,0x54,0x4,0x82,0x0,0xf,0x0,0xc,0x63,0x6c,0x69,0x65,0x6e,0x74,0x5f,0x74,0x65,0x73,0x74,0x31,0x0,0x4,0x75,0x73,0x65,0x72};
125 byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
126 shimClient.expect(connect,0x20);
127 shimClient.respond(connack,4);
128
129 PubSubClient client(server, 1883, callback, shimClient);
130 int rc = client.connect((char*)"client_test1",(char*)"user",0);
131 IS_TRUE(rc);
132 IS_FALSE(shimClient.error());
133
134 END_IT
135}
136
137int test_connect_ignores_password_no_username() {
138 IT("ignores a password but no username");
139 ShimClient shimClient;
140 shimClient.setAllowConnect(true);
141
142 byte connect[] = {0x10,0x18,0x0,0x4,0x4d,0x51,0x54,0x54,0x4,0x2,0x0,0xf,0x0,0xc,0x63,0x6c,0x69,0x65,0x6e,0x74,0x5f,0x74,0x65,0x73,0x74,0x31};
143 byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
144 shimClient.expect(connect,26);
145 shimClient.respond(connack,4);
146
147 PubSubClient client(server, 1883, callback, shimClient);
148 int rc = client.connect((char*)"client_test1",0,(char*)"pass");
149 IS_TRUE(rc);
150 IS_FALSE(shimClient.error());
151
152 END_IT
153}
154
155int test_connect_with_will() {
156 IT("accepts a will");
157 ShimClient shimClient;
158 shimClient.setAllowConnect(true);
159
160 byte connect[] = {0x10,0x30,0x0,0x4,0x4d,0x51,0x54,0x54,0x4,0xe,0x0,0xf,0x0,0xc,0x63,0x6c,0x69,0x65,0x6e,0x74,0x5f,0x74,0x65,0x73,0x74,0x31,0x0,0x9,0x77,0x69,0x6c,0x6c,0x54,0x6f,0x70,0x69,0x63,0x0,0xb,0x77,0x69,0x6c,0x6c,0x4d,0x65,0x73,0x73,0x61,0x67,0x65};
161 byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
162 shimClient.expect(connect,0x32);
163 shimClient.respond(connack,4);
164
165 PubSubClient client(server, 1883, callback, shimClient);
166 int rc = client.connect((char*)"client_test1",(char*)"willTopic",1,0,(char*)"willMessage");
167 IS_TRUE(rc);
168 IS_FALSE(shimClient.error());
169
170 END_IT
171}
172
173int test_connect_with_will_username_password() {
174 IT("accepts a will, username and password");
175 ShimClient shimClient;
176 shimClient.setAllowConnect(true);
177
178 byte connect[] = {0x10,0x40,0x0,0x4,0x4d,0x51,0x54,0x54,0x4,0xce,0x0,0xf,0x0,0xc,0x63,0x6c,0x69,0x65,0x6e,0x74,0x5f,0x74,0x65,0x73,0x74,0x31,0x0,0x9,0x77,0x69,0x6c,0x6c,0x54,0x6f,0x70,0x69,0x63,0x0,0xb,0x77,0x69,0x6c,0x6c,0x4d,0x65,0x73,0x73,0x61,0x67,0x65,0x0,0x4,0x75,0x73,0x65,0x72,0x0,0x8,0x70,0x61,0x73,0x73,0x77,0x6f,0x72,0x64};
179 byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
180 shimClient.expect(connect,0x42);
181 shimClient.respond(connack,4);
182
183 PubSubClient client(server, 1883, callback, shimClient);
184 int rc = client.connect((char*)"client_test1",(char*)"user",(char*)"password",(char*)"willTopic",1,0,(char*)"willMessage");
185 IS_TRUE(rc);
186 IS_FALSE(shimClient.error());
187
188 END_IT
189}
190
191int test_connect_disconnect_connect() {
192 IT("connects, disconnects and connects again");
193 ShimClient shimClient;
194
195 shimClient.setAllowConnect(true);
196 byte expectServer[] = { 172, 16, 0, 2 };
197 shimClient.expectConnect(expectServer,1883);
198 byte connect[] = {0x10,0x18,0x0,0x4,0x4d,0x51,0x54,0x54,0x4,0x2,0x0,0xf,0x0,0xc,0x63,0x6c,0x69,0x65,0x6e,0x74,0x5f,0x74,0x65,0x73,0x74,0x31};
199 byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
200
201 shimClient.expect(connect,26);
202 shimClient.respond(connack,4);
203
204 PubSubClient client(server, 1883, callback, shimClient);
205
206 int state = client.state();
207 IS_TRUE(state == MQTT_DISCONNECTED);
208
209 int rc = client.connect((char*)"client_test1");
210 IS_TRUE(rc);
211 IS_FALSE(shimClient.error());
212
213 state = client.state();
214 IS_TRUE(state == MQTT_CONNECTED);
215
216 byte disconnect[] = {0xE0,0x00};
217 shimClient.expect(disconnect,2);
218
219 client.disconnect();
220
221 IS_FALSE(client.connected());
222 IS_FALSE(shimClient.connected());
223 IS_FALSE(shimClient.error());
224
225 state = client.state();
226 IS_TRUE(state == MQTT_DISCONNECTED);
227
228 shimClient.expect(connect,28);
229 shimClient.respond(connack,4);
230 rc = client.connect((char*)"client_test1");
231 IS_TRUE(rc);
232 IS_FALSE(shimClient.error());
233 state = client.state();
234 IS_TRUE(state == MQTT_CONNECTED);
235
236 END_IT
237}
238
239int main()
240{
241 SUITE("Connect");
242 test_connect_fails_no_network();
243 test_connect_fails_on_no_response();
244
245 test_connect_properly_formatted();
246 test_connect_accepts_username_password();
247 test_connect_fails_on_bad_rc();
248 test_connect_properly_formatted_hostname();
249
250 test_connect_accepts_username_no_password();
251 test_connect_ignores_password_no_username();
252 test_connect_with_will();
253 test_connect_with_will_username_password();
254 test_connect_disconnect_connect();
255 FINISH
256}
Note: See TracBrowser for help on using the repository browser.