1 | #include "PubSubClient.h"
|
---|
2 | #include "ShimClient.h"
|
---|
3 | #include "Buffer.h"
|
---|
4 | #include "BDDTest.h"
|
---|
5 | #include "trace.h"
|
---|
6 |
|
---|
7 |
|
---|
8 | byte server[] = { 172, 16, 0, 2 };
|
---|
9 |
|
---|
10 | void callback(char* topic, byte* payload, unsigned int length) {
|
---|
11 | // handle message arrived
|
---|
12 | }
|
---|
13 |
|
---|
14 | int test_publish() {
|
---|
15 | IT("publishes a null-terminated string");
|
---|
16 | ShimClient shimClient;
|
---|
17 | shimClient.setAllowConnect(true);
|
---|
18 |
|
---|
19 | byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
|
---|
20 | shimClient.respond(connack,4);
|
---|
21 |
|
---|
22 | PubSubClient client(server, 1883, callback, shimClient);
|
---|
23 | int rc = client.connect((char*)"client_test1");
|
---|
24 | IS_TRUE(rc);
|
---|
25 |
|
---|
26 | byte publish[] = {0x30,0xe,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64};
|
---|
27 | shimClient.expect(publish,16);
|
---|
28 |
|
---|
29 | rc = client.publish((char*)"topic",(char*)"payload");
|
---|
30 | IS_TRUE(rc);
|
---|
31 |
|
---|
32 | IS_FALSE(shimClient.error());
|
---|
33 |
|
---|
34 | END_IT
|
---|
35 | }
|
---|
36 |
|
---|
37 |
|
---|
38 | int test_publish_bytes() {
|
---|
39 | IT("publishes a byte array");
|
---|
40 | ShimClient shimClient;
|
---|
41 | shimClient.setAllowConnect(true);
|
---|
42 |
|
---|
43 | byte payload[] = { 0x01,0x02,0x03,0x0,0x05 };
|
---|
44 | int length = 5;
|
---|
45 |
|
---|
46 | byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
|
---|
47 | shimClient.respond(connack,4);
|
---|
48 |
|
---|
49 | PubSubClient client(server, 1883, callback, shimClient);
|
---|
50 | int rc = client.connect((char*)"client_test1");
|
---|
51 | IS_TRUE(rc);
|
---|
52 |
|
---|
53 | byte publish[] = {0x30,0xc,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x1,0x2,0x3,0x0,0x5};
|
---|
54 | shimClient.expect(publish,14);
|
---|
55 |
|
---|
56 | rc = client.publish((char*)"topic",payload,length);
|
---|
57 | IS_TRUE(rc);
|
---|
58 |
|
---|
59 | IS_FALSE(shimClient.error());
|
---|
60 |
|
---|
61 | END_IT
|
---|
62 | }
|
---|
63 |
|
---|
64 |
|
---|
65 | int test_publish_retained() {
|
---|
66 | IT("publishes retained - 1");
|
---|
67 | ShimClient shimClient;
|
---|
68 | shimClient.setAllowConnect(true);
|
---|
69 |
|
---|
70 | byte payload[] = { 0x01,0x02,0x03,0x0,0x05 };
|
---|
71 | int length = 5;
|
---|
72 |
|
---|
73 | byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
|
---|
74 | shimClient.respond(connack,4);
|
---|
75 |
|
---|
76 | PubSubClient client(server, 1883, callback, shimClient);
|
---|
77 | int rc = client.connect((char*)"client_test1");
|
---|
78 | IS_TRUE(rc);
|
---|
79 |
|
---|
80 | byte publish[] = {0x31,0xc,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x1,0x2,0x3,0x0,0x5};
|
---|
81 | shimClient.expect(publish,14);
|
---|
82 |
|
---|
83 | rc = client.publish((char*)"topic",payload,length,true);
|
---|
84 | IS_TRUE(rc);
|
---|
85 |
|
---|
86 | IS_FALSE(shimClient.error());
|
---|
87 |
|
---|
88 | END_IT
|
---|
89 | }
|
---|
90 |
|
---|
91 | int test_publish_retained_2() {
|
---|
92 | IT("publishes retained - 2");
|
---|
93 | ShimClient shimClient;
|
---|
94 | shimClient.setAllowConnect(true);
|
---|
95 |
|
---|
96 | byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
|
---|
97 | shimClient.respond(connack,4);
|
---|
98 |
|
---|
99 | PubSubClient client(server, 1883, callback, shimClient);
|
---|
100 | int rc = client.connect((char*)"client_test1");
|
---|
101 | IS_TRUE(rc);
|
---|
102 |
|
---|
103 | byte publish[] = {0x31,0xc,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,'A','B','C','D','E'};
|
---|
104 | shimClient.expect(publish,14);
|
---|
105 |
|
---|
106 | rc = client.publish((char*)"topic",(char*)"ABCDE",true);
|
---|
107 | IS_TRUE(rc);
|
---|
108 |
|
---|
109 | IS_FALSE(shimClient.error());
|
---|
110 |
|
---|
111 | END_IT
|
---|
112 | }
|
---|
113 |
|
---|
114 | int test_publish_not_connected() {
|
---|
115 | IT("publish fails when not connected");
|
---|
116 | ShimClient shimClient;
|
---|
117 |
|
---|
118 | PubSubClient client(server, 1883, callback, shimClient);
|
---|
119 |
|
---|
120 | int rc = client.publish((char*)"topic",(char*)"payload");
|
---|
121 | IS_FALSE(rc);
|
---|
122 |
|
---|
123 | IS_FALSE(shimClient.error());
|
---|
124 |
|
---|
125 | END_IT
|
---|
126 | }
|
---|
127 |
|
---|
128 | int test_publish_too_long() {
|
---|
129 | IT("publish fails when topic/payload are too long");
|
---|
130 | ShimClient shimClient;
|
---|
131 | shimClient.setAllowConnect(true);
|
---|
132 |
|
---|
133 | byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
|
---|
134 | shimClient.respond(connack,4);
|
---|
135 |
|
---|
136 | PubSubClient client(server, 1883, callback, shimClient);
|
---|
137 | int rc = client.connect((char*)"client_test1");
|
---|
138 | IS_TRUE(rc);
|
---|
139 |
|
---|
140 | // 0 1 2 3 4 5 6 7 8 9 0 1 2
|
---|
141 | rc = client.publish((char*)"topic",(char*)"123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890");
|
---|
142 | IS_FALSE(rc);
|
---|
143 |
|
---|
144 | IS_FALSE(shimClient.error());
|
---|
145 |
|
---|
146 | END_IT
|
---|
147 | }
|
---|
148 |
|
---|
149 | int test_publish_P() {
|
---|
150 | IT("publishes using PROGMEM");
|
---|
151 | ShimClient shimClient;
|
---|
152 | shimClient.setAllowConnect(true);
|
---|
153 |
|
---|
154 | byte payload[] = { 0x01,0x02,0x03,0x0,0x05 };
|
---|
155 | int length = 5;
|
---|
156 |
|
---|
157 | byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
|
---|
158 | shimClient.respond(connack,4);
|
---|
159 |
|
---|
160 | PubSubClient client(server, 1883, callback, shimClient);
|
---|
161 | int rc = client.connect((char*)"client_test1");
|
---|
162 | IS_TRUE(rc);
|
---|
163 |
|
---|
164 | byte publish[] = {0x31,0xc,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x1,0x2,0x3,0x0,0x5};
|
---|
165 | shimClient.expect(publish,14);
|
---|
166 |
|
---|
167 | rc = client.publish_P((char*)"topic",payload,length,true);
|
---|
168 | IS_TRUE(rc);
|
---|
169 |
|
---|
170 | IS_FALSE(shimClient.error());
|
---|
171 |
|
---|
172 | END_IT
|
---|
173 | }
|
---|
174 |
|
---|
175 |
|
---|
176 |
|
---|
177 |
|
---|
178 | int main()
|
---|
179 | {
|
---|
180 | SUITE("Publish");
|
---|
181 | test_publish();
|
---|
182 | test_publish_bytes();
|
---|
183 | test_publish_retained();
|
---|
184 | test_publish_retained_2();
|
---|
185 | test_publish_not_connected();
|
---|
186 | test_publish_too_long();
|
---|
187 | test_publish_P();
|
---|
188 |
|
---|
189 | FINISH
|
---|
190 | }
|
---|