Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
coap_test.c
00001 #include <stdio.h> 00002 #include <string.h> 00003 #include "minunit.h" 00004 #include "../src/coap.h" 00005 00006 int tests_run = 0; 00007 00008 00009 void coap_pretty_print(coap_pdu *pdu); 00010 void hex_dump(char* bytes, size_t len); 00011 00012 static char * test_math() { 00013 mu_assert("[ERROR] 2+2 != 4", 2+2 == 4); 00014 return 0; 00015 } 00016 00017 static char * test_msg_empty_con_getters() { 00018 uint8_t ref_bin[] = {64,0,0,0}; 00019 coap_pdu msg_ref = {ref_bin, 4, 4}; 00020 00021 mu_assert("[ERROR] Empty CON failed validation.", 00022 coap_validate_pkt(&msg_ref) == CE_NONE); 00023 00024 mu_assert("[ERROR] Empty CON version decoded wrong.", 00025 coap_get_version(&msg_ref) == COAP_V1); 00026 00027 mu_assert("[ERROR] Empty CON type decoded wrong.", 00028 coap_get_type(&msg_ref) == CT_CON); 00029 00030 mu_assert("[ERROR] Empty CON tkl decoded wrong.", 00031 coap_get_tkl(&msg_ref) == 0); 00032 00033 mu_assert("[ERROR] Empty CON token decoded wrong.", 00034 coap_get_token(&msg_ref) == 0); 00035 00036 mu_assert("[ERROR] Empty CON code decoded wrong.", 00037 coap_get_code(&msg_ref) == CC_EMPTY); 00038 00039 mu_assert("[ERROR] Empty CON code class decoded wrong.", 00040 coap_get_code_class(&msg_ref) == 0); 00041 00042 mu_assert("[ERROR] Empty CON code detail decoded wrong.", 00043 coap_get_code_detail(&msg_ref) == 0); 00044 00045 return 0; 00046 } 00047 00048 static char * test_msg_empty_con_getters_with_token() { 00049 uint8_t ref_bin[] = {66,0,0,0,37,42}; 00050 coap_pdu msg_ref = {ref_bin, 6, 6}; 00051 00052 mu_assert("[ERROR] Empty CON with token failed validation.", 00053 coap_validate_pkt(&msg_ref) == CE_NONE); 00054 00055 mu_assert("[ERROR] Empty CON with token version decoded wrong.", 00056 coap_get_version(&msg_ref) == COAP_V1); 00057 00058 mu_assert("[ERROR] Empty CON with token type decoded wrong.", 00059 coap_get_type(&msg_ref) == CT_CON); 00060 00061 mu_assert("[ERROR] Empty CON with token tkl decoded wrong.", 00062 coap_get_tkl(&msg_ref) == 2); 00063 00064 mu_assert("[ERROR] Empty CON with token token decoded wrong.", 00065 coap_get_token(&msg_ref) == 0x2A25); 00066 00067 mu_assert("[ERROR] Empty CON with token code decoded wrong.", 00068 coap_get_code(&msg_ref) == CC_EMPTY); 00069 00070 mu_assert("[ERROR] Empty CON with token code class decoded wrong.", 00071 coap_get_code_class(&msg_ref) == 0); 00072 00073 mu_assert("[ERROR] Empty CON with token code detail decoded wrong.", 00074 coap_get_code_detail(&msg_ref) == 0); 00075 00076 return 0; 00077 } 00078 00079 static char * test_msg_empty_con_setters() { 00080 uint8_t ref_bin[] = {64,0,0,0}; 00081 uint8_t test_bin[4]; 00082 coap_pdu msg_ref = {ref_bin, 4, 4}; 00083 coap_pdu msg_test = {test_bin, 0, 4}; 00084 00085 mu_assert("[ERROR] Empty CON failed to init.", 00086 coap_init_pdu(&msg_test) == CE_NONE); 00087 00088 mu_assert("[ERROR] Empty CON failed to set version.", 00089 coap_set_version(&msg_test, COAP_V1) >= 0); 00090 00091 mu_assert("[ERROR] Empty CON failed to set type.", 00092 coap_set_type(&msg_test, CT_CON) >= 0); 00093 00094 mu_assert("[ERROR] Empty CON failed to set code.", 00095 coap_set_code(&msg_test, CC_EMPTY) >= 0); 00096 00097 mu_assert("[ERROR] Empty CON failed to set message ID.", 00098 coap_set_mid(&msg_test, 0) >= 0); 00099 00100 mu_assert("[ERROR] Empty CON failed to set token.", 00101 coap_set_token(&msg_test, 0, 0) == CE_NONE); 00102 00103 mu_assert("[ERROR] Empty CON length set wrong.", 00104 msg_test.len == msg_ref.len); 00105 00106 mu_assert("[ERROR] Empty CON failed to encode.", 00107 memcmp(msg_test.buf, msg_ref.buf, msg_ref.len) == 0); 00108 00109 return 0; 00110 } 00111 00112 static char * test_msg_get_con_getters() { 00113 uint8_t ref_bin[] = {0x40,0x01,0x00,0x37,0xb2,0x31,0x61,0x04,0x74,0x65, 00114 0x6d,0x70,0x4d,0x1b,0x61,0x33,0x32,0x63,0x38,0x35, 00115 0x62,0x61,0x39,0x64,0x64,0x61,0x34,0x35,0x38,0x32, 00116 0x33,0x62,0x65,0x34,0x31,0x36,0x32,0x34,0x36,0x63, 00117 0x66,0x38,0x62,0x34,0x33,0x33,0x62,0x61,0x61,0x30, 00118 0x36,0x38,0x64,0x37}; 00119 coap_pdu msg_ref = {ref_bin, 54, 54}; 00120 coap_option option; 00121 00122 mu_assert("[ERROR] GET CON failed validation.", 00123 coap_validate_pkt(&msg_ref) == CE_NONE); 00124 00125 mu_assert("[ERROR] GET CON version decoded wrong.", 00126 coap_get_version(&msg_ref) == COAP_V1); 00127 00128 mu_assert("[ERROR] GET CON type decoded wrong.", 00129 coap_get_type(&msg_ref) == CT_CON); 00130 00131 mu_assert("[ERROR] GET CON code decoded wrong.", 00132 coap_get_code(&msg_ref) == CC_GET); 00133 00134 mu_assert("[ERROR] GET CON code class decoded wrong.", 00135 coap_get_code_class(&msg_ref) == 0); 00136 00137 mu_assert("[ERROR] GET CON code detail decoded wrong.", 00138 coap_get_code_detail(&msg_ref) == 1); 00139 00140 option = coap_get_option(&msg_ref, NULL); 00141 mu_assert("[ERROR] GET CON option zero length was wrong.", 00142 option.len == 2); 00143 mu_assert("[ERROR] GET CON option zero number was wrong.", 00144 option.num == CON_URI_PATH); 00145 mu_assert("[ERROR] GET CON option zero value was wrong.", 00146 memcmp(option.val, ref_bin+5, option.len) == 0); 00147 00148 option = coap_get_option(&msg_ref, &option); 00149 mu_assert("[ERROR] GET CON option one length was wrong.", 00150 option.len == 4); 00151 mu_assert("[ERROR] GET CON option one number was wrong.", 00152 option.num == CON_URI_PATH); 00153 mu_assert("[ERROR] GET CON option one value was wrong.", 00154 memcmp(option.val, ref_bin+8, option.len) == 0); 00155 00156 option = coap_get_option(&msg_ref, &option); 00157 mu_assert("[ERROR] GET CON option two length was wrong.", 00158 option.len == 40); 00159 mu_assert("[ERROR] GET CON option two number was wrong.", 00160 option.num == CON_URI_QUERY); 00161 mu_assert("[ERROR] GET CON option two value was wrong.", 00162 memcmp(option.val, ref_bin+14, option.len) == 0); 00163 00164 option = coap_get_option_by_num(&msg_ref, CON_URI_QUERY, 0); 00165 mu_assert("[ERROR] GET CON option by num length was wrong.", 00166 option.len == 40); 00167 mu_assert("[ERROR] GET CON option by num number was wrong.", 00168 option.num == CON_URI_QUERY); 00169 mu_assert("[ERROR] GET CON option by num value was wrong.", 00170 memcmp(option.val, ref_bin+14, option.len) == 0); 00171 00172 option = coap_get_option_by_num(&msg_ref, CON_ETAG, 0); 00173 mu_assert("[ERROR] GET CON non-option by num length not zero.", 00174 option.len == 0); 00175 mu_assert("[ERROR] GET CON non-option by num number not null.", 00176 option.num == 0); 00177 mu_assert("[ERROR] GET CON non-option by num value not null.", 00178 option.val == NULL); 00179 00180 return 0; 00181 } 00182 00183 static char * test_msg_get_con_setters() { 00184 uint8_t ref_bin[] = {0x40,0x01,0x00,0x37,0xb2,0x31,0x61,0x04,0x74,0x65, 00185 0x6d,0x70,0x4d,0x1b,0x61,0x33,0x32,0x63,0x38,0x35, 00186 0x62,0x61,0x39,0x64,0x64,0x61,0x34,0x35,0x38,0x32, 00187 0x33,0x62,0x65,0x34,0x31,0x36,0x32,0x34,0x36,0x63, 00188 0x66,0x38,0x62,0x34,0x33,0x33,0x62,0x61,0x61,0x30, 00189 0x36,0x38,0x64,0x37}; 00190 coap_pdu msg_ref = {ref_bin, 54, 54}; 00191 00192 uint8_t test_bin[54]; 00193 coap_pdu msg_test = {test_bin, 0,54}; 00194 00195 mu_assert("[ERROR] Empty CON failed to init.", 00196 coap_init_pdu(&msg_test) == CE_NONE); 00197 00198 mu_assert("[ERROR] GET CON failed to set version.", 00199 coap_set_version(&msg_test, COAP_V1) >= 0); 00200 00201 mu_assert("[ERROR] GET CON failed to set type.", 00202 coap_set_type(&msg_test, CT_CON) >= 0); 00203 00204 mu_assert("[ERROR] GET CON failed to set code.", 00205 coap_set_code(&msg_test, CC_GET) >= 0); 00206 00207 mu_assert("[ERROR] GET CON failed to set message ID.", 00208 coap_set_mid(&msg_test, 0x37) >= 0); 00209 00210 mu_assert("[ERROR] GET CON failed to set token.", 00211 coap_set_token(&msg_test, 0, 0) >= 0); 00212 00213 mu_assert("[ERROR] GET CON failed to add first path option.", 00214 coap_add_option(&msg_test, CON_URI_PATH, ref_bin+5, 2) == CE_NONE); 00215 00216 mu_assert("[ERROR] GET CON failed to add second path option.", 00217 coap_add_option(&msg_test, CON_URI_PATH, ref_bin+8, 4) == CE_NONE); 00218 00219 mu_assert("[ERROR] GET CON failed to add query option.", 00220 coap_add_option(&msg_test, CON_URI_QUERY, ref_bin+14, 40) == CE_NONE); 00221 00222 mu_assert("[ERROR] GET CON length set wrong.", 00223 msg_test.len == 54); 00224 00225 mu_assert("[ERROR] GET CON failed to encode.", 00226 memcmp(msg_ref.buf, msg_test.buf, 54) == 0); 00227 00228 return 0; 00229 } 00230 00231 static char * test_msg_get_con_setters_out_order() { 00232 uint8_t ref_bin[] = {0x44,0x01,0x00,0x37,0xff,0xff,0xff,0xff,0xb2,0x31, 00233 0x61,0x04,0x74,0x65,0x6d,0x70,0x4d,0x1b,0x61,0x33, 00234 0x32,0x63,0x38,0x35,0x62,0x61,0x39,0x64,0x64,0x61, 00235 0x34,0x35,0x38,0x32,0x33,0x62,0x65,0x34,0x31,0x36, 00236 0x32,0x34,0x36,0x63,0x66,0x38,0x62,0x34,0x33,0x33, 00237 0x62,0x61,0x61,0x30,0x36,0x38,0x64,0x37}; 00238 coap_pdu msg_ref = {ref_bin, 58, 58}; 00239 00240 uint8_t test_bin[64]; 00241 coap_pdu msg_test = {test_bin, 0, 64}; 00242 00243 //memset(test_bin, 0, 64); 00244 00245 mu_assert("[ERROR] Empty CON failed to init.", 00246 coap_init_pdu(&msg_test) == CE_NONE); 00247 00248 mu_assert("[ERROR] GET CON failed to set version. (Out of Order)", 00249 coap_set_version(&msg_test, COAP_V1) >= 0); 00250 00251 mu_assert("[ERROR] GET CON failed to set type. (Out of Order)", 00252 coap_set_type(&msg_test, CT_CON) >= 0); 00253 00254 mu_assert("[ERROR] GET CON failed to set code. (Out of Order)", 00255 coap_set_code(&msg_test, CC_GET) >= 0); 00256 00257 mu_assert("[ERROR] GET CON failed to set message ID. (Out of Order)", 00258 coap_set_mid(&msg_test, 0x37) >= 0); 00259 00260 mu_assert("[ERROR] GET CON failed to set token. (Out of Order)", 00261 coap_set_token(&msg_test, 0xFFFFFFFF, 4) >= 0); 00262 00263 mu_assert("[ERROR] GET CON failed to add first path option. (Out of Order)", 00264 coap_add_option(&msg_test, CON_URI_PATH, ref_bin+9, 2) == CE_NONE); 00265 00266 mu_assert("[ERROR] GET CON failed to add second path option. (Out of Order)", 00267 coap_add_option(&msg_test, CON_URI_PATH, ref_bin+12, 4) == CE_NONE); 00268 00269 mu_assert("[ERROR] GET CON failed to add query option. (Out of Order)", 00270 coap_add_option(&msg_test, CON_URI_QUERY, ref_bin+18, 40) == CE_NONE); 00271 00272 mu_assert("[ERROR] GET CON length set wrong. (Out of Order)", 00273 msg_test.len == 58); 00274 00275 mu_assert("[ERROR] GET CON failed to encode. (Out of Order)", 00276 memcmp(msg_test.buf, msg_ref.buf, 58) == 0); 00277 00278 return 0; 00279 } 00280 00281 static char * test_msg_post_con_setters() { 00282 uint8_t ref_bin[] = {0x40,0x02,0x00,0x37,0xb2,0x31,0x61,0x04,0x74,0x65, 00283 0x6d,0x70,0x4d,0x1b,0x61,0x33,0x32,0x63,0x38,0x35, 00284 0x62,0x61,0x39,0x64,0x64,0x61,0x34,0x35,0x38,0x32, 00285 0x33,0x62,0x65,0x34,0x31,0x36,0x32,0x34,0x36,0x63, 00286 0x66,0x38,0x62,0x34,0x33,0x33,0x62,0x61,0x61,0x30, 00287 0x36,0x38,0x64,0x37,0xFF,0x39,0x39}; 00288 coap_pdu msg_ref = {ref_bin, 57, 57}; 00289 00290 uint8_t test_bin[57]; 00291 coap_pdu msg_test = {test_bin, 0, 57}; 00292 00293 mu_assert("[ERROR] Empty CON failed to init.", 00294 coap_init_pdu(&msg_test) == CE_NONE); 00295 00296 mu_assert("[ERROR] POST CON failed to set version.", 00297 coap_set_version(&msg_test, COAP_V1) >= 0); 00298 00299 mu_assert("[ERROR] POST CON failed to set type.", 00300 coap_set_type(&msg_test, CT_CON) >= 0); 00301 00302 mu_assert("[ERROR] POST CON failed to set code.", 00303 coap_set_code(&msg_test, CC_POST) >= 0); 00304 00305 mu_assert("[ERROR] POST CON failed to set message ID.", 00306 coap_set_mid(&msg_test, 0x37) >= 0); 00307 00308 mu_assert("[ERROR] POST CON failed to set token.", 00309 coap_set_token(&msg_test, 0, 0) >= 0); 00310 00311 mu_assert("[ERROR] POST CON failed to add first path option.", 00312 coap_add_option(&msg_test, CON_URI_PATH, ref_bin+5, 2) == CE_NONE); 00313 00314 mu_assert("[ERROR] POST CON failed to add second path option.", 00315 coap_add_option(&msg_test, CON_URI_PATH, ref_bin+8, 4) == CE_NONE); 00316 00317 mu_assert("[ERROR] POST CON failed to add query option.", 00318 coap_add_option(&msg_test, CON_URI_QUERY, ref_bin+14, 40) == CE_NONE); 00319 00320 mu_assert("[ERROR] POST CON failed to add payload.", 00321 coap_set_payload(&msg_test, ref_bin+55, 2) == CE_NONE); 00322 00323 mu_assert("[ERROR] POST CON length set wrong.", 00324 msg_test.len == 57); 00325 00326 mu_assert("[ERROR] POST CON failed to encode.", 00327 memcmp(msg_ref.buf, msg_test.buf, 57) == 0); 00328 00329 return 0; 00330 } 00331 00332 static char * test_msg_content_ack_getters() { 00333 uint8_t ref_bin[] = {0x61,0x45,0xEE,0xCC,0xA2,0xFF,0x35,0x36}; 00334 coap_pdu msg_ref = {ref_bin, 8, 8}; 00335 00336 coap_payload payload; 00337 00338 mu_assert("[ERROR] CONTENT ACK failed validation.", 00339 coap_validate_pkt(&msg_ref) == CE_NONE); 00340 00341 mu_assert("[ERROR] CONTENT ACK version decoded wrong.", 00342 coap_get_version(&msg_ref) == COAP_V1); 00343 00344 mu_assert("[ERROR] CONTENT ACK type decoded wrong.", 00345 coap_get_type(&msg_ref) == CT_ACK); 00346 00347 mu_assert("[ERROR] CONTENT ACK code decoded wrong.", 00348 coap_get_code(&msg_ref) == CC_CONTENT); 00349 00350 mu_assert("[ERROR] CONTENT ACK code class decoded wrong.", 00351 coap_get_code_class(&msg_ref) == 2); 00352 00353 mu_assert("[ERROR] CONTENT ACK code detail decoded wrong.", 00354 coap_get_code_detail(&msg_ref) == 5); 00355 00356 payload = coap_get_payload(&msg_ref); 00357 mu_assert("[ERROR] CONTENT ACK payload length was wrong.", 00358 payload.len == 2); 00359 mu_assert("[ERROR] CONTENT ACK payload value was wrong.", 00360 memcmp(payload.val, ref_bin+6, 2) == 0); 00361 00362 return 0; 00363 } 00364 00365 static char * test_msg_pl_from_get_opt_no_opt() { 00366 uint8_t ref_bin[] = {0x62, 0x45, 0xee, 0x8b, 0x0e, 0x50, 0xff, 0x53, 00367 0x68, 0x6f, 0x6f, 0x70}; 00368 coap_pdu msg_ref = {ref_bin, 12, 12}; 00369 00370 coap_option payload; 00371 00372 mu_assert("[ERROR] PAYLOAD NO OPT failed validation.", 00373 coap_validate_pkt(&msg_ref) == CE_NONE); 00374 00375 payload = coap_get_option(&msg_ref, 0); 00376 mu_assert("[ERROR] PAYLOAD NO OPT didn't find payload.", 00377 payload.num == 0); 00378 mu_assert("[ERROR] PAYLOAD NO OPT payload length was wrong.", 00379 payload.len == 5); 00380 mu_assert("[ERROR] PAYLOAD NO OPT payload value was wrong.", 00381 memcmp(payload.val, ref_bin+7, 5) == 0); 00382 00383 return 0; 00384 } 00385 00386 // Helpers 00387 void hex_dump(char* bytes, size_t len) 00388 { 00389 size_t i, j; 00390 for (i = 0; i < len; i+=16){ 00391 printf(" 0x%.3zx ", i); 00392 for (j = 0; j < 16; j++){ 00393 if (i+j < len) 00394 printf("%.2hhx ", bytes[i+j]); 00395 else 00396 printf("%s ", "--"); 00397 } 00398 printf(" %.*s\n", (int)(16 > len-i ? len-i : 16), bytes+i); 00399 } 00400 } 00401 00402 void coap_pretty_print(coap_pdu *pdu) 00403 { 00404 if (coap_validate_pkt(pdu) == 0){ 00405 printf("Found Valid Coap Packet\n"); 00406 } 00407 00408 hex_dump((char*)pdu->buf, pdu->len); 00409 } 00410 00411 static char * all_tests() { 00412 // Make Sure the Tests Are Working 00413 mu_run_test(test_math); 00414 00415 // Actually Run the Real Tests 00416 mu_run_test(test_msg_empty_con_getters); 00417 mu_run_test(test_msg_empty_con_getters_with_token); 00418 mu_run_test(test_msg_empty_con_setters); 00419 mu_run_test(test_msg_get_con_getters); 00420 mu_run_test(test_msg_get_con_setters); 00421 mu_run_test(test_msg_content_ack_getters); 00422 mu_run_test(test_msg_get_con_setters_out_order); 00423 mu_run_test(test_msg_post_con_setters); 00424 mu_run_test(test_msg_pl_from_get_opt_no_opt); 00425 return 0; 00426 } 00427 00428 int main(int argc, char **argv) { 00429 char *result = all_tests(); 00430 if (result != 0) { 00431 printf("%s\n", result); 00432 } 00433 else { 00434 printf("ALL TESTS PASSED\n"); 00435 } 00436 printf("Tests run: %d\n", tests_run); 00437 00438 return result != 0; 00439 } 00440
Generated on Tue Jul 12 2022 18:09:07 by
1.7.2