static void test_coap_parse_illegal_token_length(void) { uint8_t pdu[] = { 0x59, 0x69, 0x12, 0x34, 't', 'o', 'k', 'e', 'n', '1', '2', '3', '4' }; struct sol_coap_packet *pkt; struct sol_buffer *buf; size_t offset; pkt = sol_coap_packet_new(NULL); ASSERT(pkt); ASSERT(!sol_coap_packet_get_payload(pkt, &buf, &offset)); ASSERT(!sol_buffer_remove_data(buf, 0, offset)); ASSERT(!sol_buffer_insert_bytes(buf, 0, pdu, sizeof(pdu))); ASSERT(coap_packet_parse(pkt)); pdu[0] = 0x5f; ASSERT(!sol_buffer_remove_data(buf, 0, offset)); ASSERT(!sol_buffer_insert_bytes(buf, 0, pdu, sizeof(pdu))); ASSERT(coap_packet_parse(pkt)); sol_coap_packet_unref(pkt); }
static void test_coap_find_options(void) { uint8_t pdu[] = { 0x55, 0xA5, 0x12, 0x34, 't', 'o', 'k', 'e', 'n', 0x00, 0xC1, 0x00, 0xff, 'p', 'a', 'y', 'l', 'o', 'a', 'd', 0x00 }; struct sol_coap_packet *pkt; struct sol_str_slice options[16]; int count = 16; pkt = sol_coap_packet_new(NULL); ASSERT(pkt); memcpy(pkt->buf, pdu, sizeof(pdu)); pkt->payload.size = sizeof(pdu); ASSERT(!coap_packet_parse(pkt)); count = sol_coap_find_options(pkt, SOL_COAP_OPTION_CONTENT_FORMAT, options, count); ASSERT_INT_EQ(count, 1); ASSERT_INT_EQ(options[0].len, 1); ASSERT_INT_EQ((uint8_t)options[0].data[0], 0); count = sol_coap_find_options(pkt, SOL_COAP_OPTION_IF_MATCH, options, count); ASSERT_INT_EQ(count, 0); sol_coap_packet_unref(pkt); }
static void test_coap_find_options(void) { uint8_t pdu[] = { 0x55, 0xA5, 0x12, 0x34, 't', 'o', 'k', 'e', 'n', 0x00, 0xC1, 0x00, 0xff, 'p', 'a', 'y', 'l', 'o', 'a', 'd', 0x00 }; struct sol_str_slice options[16]; struct sol_coap_packet *pkt; struct sol_buffer *buf; int count = 16; size_t offset; pkt = sol_coap_packet_new(NULL); ASSERT(pkt); ASSERT(!sol_coap_packet_get_payload(pkt, &buf, &offset)); ASSERT(!sol_buffer_remove_data(buf, 0, offset)); ASSERT(!sol_buffer_insert_bytes(buf, 0, pdu, sizeof(pdu))); ASSERT(!coap_packet_parse(pkt)); count = sol_coap_find_options(pkt, SOL_COAP_OPTION_CONTENT_FORMAT, options, count); ASSERT_INT_EQ(count, 1); ASSERT_INT_EQ(options[0].len, 1); ASSERT_INT_EQ((uint8_t)options[0].data[0], 0); count = sol_coap_find_options(pkt, SOL_COAP_OPTION_IF_MATCH, options, count); ASSERT_INT_EQ(count, 0); sol_coap_packet_unref(pkt); }
static void test_coap_parse_empty_pdu(void) { uint8_t pdu[] = { 0x40, 0x01, 0, 0 }; struct sol_coap_packet *pkt; struct sol_buffer *buf; size_t offset; uint8_t ver, type, code; uint16_t id; pkt = sol_coap_packet_new(NULL); ASSERT(pkt); ASSERT(!sol_coap_packet_get_payload(pkt, &buf, &offset)); ASSERT(!sol_buffer_remove_data(buf, 0, offset)); ASSERT(!sol_buffer_insert_bytes(buf, 0, pdu, sizeof(pdu))); ASSERT(!coap_packet_parse(pkt)); sol_coap_header_get_version(pkt, &ver); sol_coap_header_get_type(pkt, &type); sol_coap_header_get_code(pkt, &code); sol_coap_header_get_id(pkt, &id); ASSERT_INT_EQ(ver, 1); ASSERT_INT_EQ(type, SOL_COAP_MESSAGE_TYPE_CON); ASSERT_INT_EQ(code, SOL_COAP_METHOD_GET); ASSERT_INT_EQ(id, 0); sol_coap_packet_unref(pkt); }
static int light_method_get(struct sol_coap_server *server, const struct sol_coap_resource *resource, struct sol_coap_packet *req, const struct sol_network_link_addr *cliaddr, void *data) { struct sol_coap_packet *resp; struct sol_buffer *buf; int r; resp = sol_coap_packet_new(req); if (!resp) { SOL_WRN("Could not build response packet"); return -1; } r = sol_coap_header_set_type(resp, SOL_COAP_TYPE_ACK); SOL_INT_CHECK_GOTO(r, < 0, err); r = sol_coap_header_set_code(resp, SOL_COAP_RSPCODE_CONTENT); SOL_INT_CHECK_GOTO(r, < 0, err); r = sol_coap_packet_get_payload(resp, &buf, NULL); SOL_INT_CHECK_GOTO(r, < 0, err); r = light_resource_to_rep(resource, get_scrolllock_led(), buf); SOL_INT_CHECK_GOTO(r, < 0, err); return sol_coap_send_packet(server, resp, cliaddr); err: sol_coap_packet_unref(resp); return r; }
static void disable_observing(struct sol_coap_packet *req, struct sol_coap_server *server, struct sol_str_slice path[], const struct sol_network_link_addr *cliaddr) { struct sol_coap_packet *pkt = sol_coap_packet_new(req); uint8_t observe = 1; int i, r; if (!pkt) return; r = sol_coap_header_set_code(pkt, SOL_COAP_METHOD_GET); SOL_INT_CHECK_GOTO(r, < 0, err); r = sol_coap_header_set_type(pkt, SOL_COAP_TYPE_CON); SOL_INT_CHECK_GOTO(r, < 0, err); r = sol_coap_add_option(pkt, SOL_COAP_OPTION_OBSERVE, &observe, sizeof(observe)); SOL_INT_CHECK_GOTO(r, < 0, err); for (i = 0; path[i].data; i++) sol_coap_add_option(pkt, SOL_COAP_OPTION_URI_PATH, path[i].data, path[i].len); sol_coap_send_packet(server, pkt, cliaddr); SOL_INF("Disabled observing"); return; err: sol_coap_packet_unref(pkt); }
static void test_coap_parse_simple_pdu(void) { uint8_t pdu[] = { 0x55, 0xA5, 0x12, 0x34, 't', 'o', 'k', 'e', 'n', 0x00, 0xc1, 0x00, 0xff, 'p', 'a', 'y', 'l', 'o', 'a', 'd', 0x00 }; struct sol_str_slice options[16]; struct sol_coap_packet *pkt; struct sol_buffer *buf; uint8_t *token; int count = 16; size_t offset; uint8_t tkl, code, ver, type; uint16_t id; pkt = sol_coap_packet_new(NULL); ASSERT(pkt); ASSERT(!sol_coap_packet_get_payload(pkt, &buf, &offset)); ASSERT(!sol_buffer_remove_data(buf, 0, offset)); ASSERT(!sol_buffer_insert_bytes(buf, 0, pdu, sizeof(pdu))); ASSERT(!coap_packet_parse(pkt)); sol_coap_header_get_version(pkt, &ver); sol_coap_header_get_type(pkt, &type); ASSERT_INT_EQ(ver, 1); ASSERT_INT_EQ(type, SOL_COAP_MESSAGE_TYPE_NON_CON); token = sol_coap_header_get_token(pkt, &tkl); ASSERT(token); ASSERT_INT_EQ(tkl, 5); ASSERT(!strcmp((char *)token, "token")); sol_coap_header_get_code(pkt, &code); sol_coap_header_get_id(pkt, &id); ASSERT_INT_EQ(code, SOL_COAP_RESPONSE_CODE_PROXYING_NOT_SUPPORTED); ASSERT_INT_EQ(id, 0x1234); count = sol_coap_find_options(pkt, SOL_COAP_OPTION_CONTENT_FORMAT, options, count); ASSERT_INT_EQ(count, 1); ASSERT_INT_EQ(options[0].len, 1); ASSERT_INT_EQ((uint8_t)options[0].data[0], 0); /* Not existent. */ count = sol_coap_find_options(pkt, SOL_COAP_OPTION_ETAG, options, count); ASSERT_INT_EQ(count, 0); ASSERT(!sol_coap_packet_get_payload(pkt, &buf, &offset)); ASSERT_INT_EQ(offset + sizeof("payload"), buf->used); ASSERT(!strcmp((char *)buf->data + offset, "payload")); sol_coap_packet_unref(pkt); }
static void test_coap_parse_options_that_exceed_pdu(void) { uint8_t pdu[] = { 0x55, 0x73, 0x12, 0x34, 't', 'o', 'k', 'e', 'n', 0x00, 0xc1, 0x00, 0xae, 0xf0, 0x03 }; struct sol_coap_packet *pkt; pkt = sol_coap_packet_new(NULL); ASSERT(pkt); memcpy(pkt->buf, pdu, sizeof(pdu)); pkt->payload.size = sizeof(pdu); ASSERT(coap_packet_parse(pkt)); sol_coap_packet_unref(pkt); }
static void test_coap_parse_without_options_with_payload(void) { uint8_t pdu[] = { 0x50, 0x73, 0x12, 0x34, 0xff, 'p', 'a', 'y', 'l', 'o', 'a', 'd' }; struct sol_coap_packet *pkt; pkt = sol_coap_packet_new(NULL); ASSERT(pkt); memcpy(pkt->buf, pdu, sizeof(pdu)); pkt->payload.size = sizeof(pdu); ASSERT(!coap_packet_parse(pkt)); sol_coap_packet_unref(pkt); }
static void test_coap_parse_simple_pdu(void) { uint8_t pdu[] = { 0x55, 0xA5, 0x12, 0x34, 't', 'o', 'k', 'e', 'n', 0x00, 0xc1, 0x00, 0xff, 'p', 'a', 'y', 'l', 'o', 'a', 'd', 0x00 }; struct sol_coap_packet *pkt; struct sol_str_slice options[16]; uint8_t *payload, *token; uint16_t len; int count = 16; uint8_t tkl; pkt = sol_coap_packet_new(NULL); ASSERT(pkt); memcpy(pkt->buf, pdu, sizeof(pdu)); pkt->payload.size = sizeof(pdu); ASSERT(!coap_packet_parse(pkt)); ASSERT_INT_EQ(sol_coap_header_get_ver(pkt), 1); ASSERT_INT_EQ(sol_coap_header_get_type(pkt), SOL_COAP_TYPE_NONCON); token = sol_coap_header_get_token(pkt, &tkl); ASSERT(token); ASSERT_INT_EQ(tkl, 5); ASSERT(!strcmp((char *)token, "token")); ASSERT_INT_EQ(sol_coap_header_get_code(pkt), SOL_COAP_RSPCODE_PROXYING_NOT_SUPPORTED); ASSERT_INT_EQ(sol_coap_header_get_id(pkt), 0x1234); count = sol_coap_find_options(pkt, SOL_COAP_OPTION_CONTENT_FORMAT, options, count); ASSERT_INT_EQ(count, 1); ASSERT_INT_EQ(options[0].len, 1); ASSERT_INT_EQ((uint8_t)options[0].data[0], 0); /* Not existent. */ count = sol_coap_find_options(pkt, SOL_COAP_OPTION_ETAG, options, count); ASSERT_INT_EQ(count, 0); ASSERT(!sol_coap_packet_get_payload(pkt, &payload, &len)); ASSERT_INT_EQ(len, sizeof("payload")); ASSERT(!strcmp((char *)payload, "payload")); sol_coap_packet_unref(pkt); }
static int light_method_put(struct sol_coap_server *server, const struct sol_coap_resource *resource, struct sol_coap_packet *req, const struct sol_network_link_addr *cliaddr, void *data) { sol_coap_responsecode_t code = SOL_COAP_RSPCODE_CONTENT; struct sol_coap_packet *resp; struct sol_buffer *buf; char *sub = NULL; size_t offset; bool value; int r; sol_coap_packet_get_payload(req, &buf, &offset); if (buf) sub = strstr((char *)sol_buffer_at(buf, offset), "state\":"); if (!sub) { code = SOL_COAP_RSPCODE_BAD_REQUEST; goto done; } value = !memcmp(sub + strlen("state\":"), "true", sizeof("true") - 1); SOL_INF("Changing light state to %s", value ? "on" : "off"); set_scrolllock_led(value); done: resp = sol_coap_packet_new(req); if (!resp) { SOL_WRN("Could not build response packet"); return -1; } r = sol_coap_header_set_type(resp, SOL_COAP_TYPE_ACK); SOL_INT_CHECK_GOTO(r, < 0, err); r = sol_coap_header_set_code(resp, code); SOL_INT_CHECK_GOTO(r, < 0, err); return sol_coap_send_packet(server, resp, cliaddr); err: sol_coap_packet_unref(resp); return r; }
static void test_coap_parse_options_that_exceed_pdu(void) { uint8_t pdu[] = { 0x55, 0x73, 0x12, 0x34, 't', 'o', 'k', 'e', 'n', 0x00, 0xc1, 0x00, 0xae, 0xf0, 0x03 }; struct sol_coap_packet *pkt; struct sol_buffer *buf; size_t offset; pkt = sol_coap_packet_new(NULL); ASSERT(pkt); ASSERT(!sol_coap_packet_get_payload(pkt, &buf, &offset)); ASSERT(!sol_buffer_remove_data(buf, 0, offset)); ASSERT(!sol_buffer_insert_bytes(buf, 0, pdu, sizeof(pdu))); ASSERT(coap_packet_parse(pkt)); sol_coap_packet_unref(pkt); }
static void test_coap_parse_without_options_with_payload(void) { uint8_t pdu[] = { 0x50, 0x73, 0x12, 0x34, 0xff, 'p', 'a', 'y', 'l', 'o', 'a', 'd' }; struct sol_coap_packet *pkt; struct sol_buffer *buf; size_t offset; pkt = sol_coap_packet_new(NULL); ASSERT(pkt); ASSERT(!sol_coap_packet_get_payload(pkt, &buf, &offset)); ASSERT(!sol_buffer_remove_data(buf, 0, offset)); ASSERT(!sol_buffer_insert_bytes(buf, 0, pdu, sizeof(pdu))); ASSERT(!coap_packet_parse(pkt)); sol_coap_packet_unref(pkt); }
static void test_coap_parse_illegal_token_length(void) { uint8_t pdu[] = { 0x59, 0x69, 0x12, 0x34, 't', 'o', 'k', 'e', 'n', '1', '2', '3', '4' }; struct sol_coap_packet *pkt; pkt = sol_coap_packet_new(NULL); ASSERT(pkt); memcpy(pkt->buf, pdu, sizeof(pdu)); pkt->payload.size = sizeof(pdu); ASSERT(coap_packet_parse(pkt)); pdu[0] = 0x5f; memcpy(pkt->buf, pdu, sizeof(pdu)); ASSERT(coap_packet_parse(pkt)); sol_coap_packet_unref(pkt); }
static void test_coap_parse_empty_pdu(void) { uint8_t pdu[] = { 0x40, 0x01, 0, 0 }; struct sol_coap_packet *pkt; pkt = sol_coap_packet_new(NULL); ASSERT(pkt); memcpy(pkt->buf, pdu, sizeof(pdu)); pkt->payload.size = sizeof(pdu); ASSERT(!coap_packet_parse(pkt)); ASSERT_INT_EQ(sol_coap_header_get_ver(pkt), 1); ASSERT_INT_EQ(sol_coap_header_get_type(pkt), SOL_COAP_TYPE_CON); ASSERT_INT_EQ(sol_coap_header_get_code(pkt), SOL_COAP_METHOD_GET); ASSERT_INT_EQ(sol_coap_header_get_id(pkt), 0); sol_coap_packet_unref(pkt); }
static void test_coap_token_simple(void) { uint8_t token[] = { 't', 'o', 'k', 'e', 'n' }; uint8_t token_length; uint8_t *token_ptr; struct sol_coap_packet *pkt; pkt = sol_coap_packet_new(NULL); ASSERT(pkt); ASSERT(!sol_coap_header_set_token(pkt, token, sizeof(token))); token_ptr = sol_coap_header_get_token(pkt, &token_length); ASSERT(token_ptr); ASSERT_INT_EQ(token_length, sizeof(token)); ASSERT(streqn((char *)token_ptr, (char *)token, sizeof(token))); ASSERT(!coap_packet_parse(pkt)); sol_coap_packet_unref(pkt); }
static void test_coap_payload_simple(void) { uint8_t pdu[] = { 0x50, 0x73, 0x12, 0x34, 0xff, 'p', 'a', 'y', 'l', 'o', 'a', 'd', 0x00 }; struct sol_coap_packet *pkt; struct sol_buffer *buf; size_t offset; pkt = sol_coap_packet_new(NULL); ASSERT(pkt); ASSERT(!sol_coap_packet_get_payload(pkt, &buf, &offset)); ASSERT(!sol_buffer_remove_data(buf, 0, offset)); ASSERT(!sol_buffer_insert_bytes(buf, 0, pdu, sizeof(pdu))); ASSERT_INT_EQ(buf->used - offset, sizeof("payload")); ASSERT(streq((char *)buf->data + offset, "payload")); ASSERT(!coap_packet_parse(pkt)); sol_coap_packet_unref(pkt); }
static int light_method_put(const struct sol_coap_resource *resource, struct sol_coap_packet *req, const struct sol_network_link_addr *cliaddr, void *data) { struct sol_coap_server *server = (void *)data; struct sol_coap_packet *resp; char *sub = NULL; uint8_t *p; uint16_t len; bool value; sol_coap_responsecode_t code = SOL_COAP_RSPCODE_CONTENT; sol_coap_packet_get_payload(req, &p, &len); if (p) sub = strstr((char *)p, "state\":"); if (!sub) { code = SOL_COAP_RSPCODE_BAD_REQUEST; goto done; } value = !memcmp(sub + strlen("state\":"), "true", sizeof("true") - 1); SOL_INF("Changing light state to %s", value ? "on" : "off"); set_scrolllock_led(value); done: resp = sol_coap_packet_new(req); if (!resp) { SOL_WRN("Could not build response packet"); return -1; } sol_coap_header_set_type(resp, SOL_COAP_TYPE_ACK); sol_coap_header_set_code(resp, code); return sol_coap_send_packet(server, resp, cliaddr); }
static int light_method_get(const struct sol_coap_resource *resource, struct sol_coap_packet *req, const struct sol_network_link_addr *cliaddr, void *data) { struct sol_coap_server *server = (void *)data; struct sol_coap_packet *resp; uint8_t *payload; uint16_t len; resp = sol_coap_packet_new(req); if (!resp) { SOL_WRN("Could not build response packet"); return -1; } sol_coap_header_set_type(resp, SOL_COAP_TYPE_ACK); sol_coap_header_set_code(resp, SOL_COAP_RSPCODE_CONTENT); sol_coap_packet_get_payload(resp, &payload, &len); len = light_resource_to_rep(resource, get_scrolllock_led(), (char *)payload, len); sol_coap_packet_set_payload_used(resp, len); return sol_coap_send_packet(server, resp, cliaddr); }
static void test_coap_payload_simple(void) { uint8_t pdu[] = { 0x50, 0x73, 0x12, 0x34, 0xff, 'p', 'a', 'y', 'l', 'o', 'a', 'd', 0x00 }; uint16_t payload_length; uint8_t *payload_ptr; struct sol_coap_packet *pkt; pkt = sol_coap_packet_new(NULL); ASSERT(pkt); memcpy(pkt->buf, pdu, sizeof(pdu)); pkt->payload.size = sizeof(pdu); ASSERT(!sol_coap_packet_get_payload(pkt, &payload_ptr, &payload_length)); ASSERT(payload_ptr); ASSERT_INT_EQ(payload_length, sizeof("payload")); ASSERT(streq((char *)payload_ptr, "payload")); ASSERT(!coap_packet_parse(pkt)); sol_coap_packet_unref(pkt); }
static int bootstrap_request(void *data, struct sol_coap_server *coap, const struct sol_coap_resource *resource, struct sol_coap_packet *req, const struct sol_network_link_addr *cliaddr) { struct sol_lwm2m_bootstrap_client_info *bs_cinfo; struct sol_lwm2m_bootstrap_server *server = data; struct sol_coap_packet *response; struct sol_str_slice client_name = SOL_STR_SLICE_EMPTY; int r; size_t i; bool know_client = false; SOL_DBG("Client Bootstrap Request received"); response = sol_coap_packet_new(req); SOL_NULL_CHECK(response, -ENOMEM); r = extract_bootstrap_client_info(req, &client_name); SOL_INT_CHECK_GOTO(r, < 0, err_exit); for (i = 0; server->known_clients[i]; i++) { if (sol_str_slice_str_eq(client_name, server->known_clients[i])) know_client = true; } if (!know_client) { SOL_WRN("Client %.*s bootstrap request received, but this Bootstrap Server" " doesn't have Bootstrap Information for this client.", SOL_STR_SLICE_PRINT(client_name)); goto err_exit; } r = new_bootstrap_client_info(&bs_cinfo, cliaddr, client_name); SOL_INT_CHECK_GOTO(r, < 0, err_exit); r = sol_ptr_vector_append(&server->clients, bs_cinfo); SOL_INT_CHECK_GOTO(r, < 0, err_exit_del_client); r = sol_coap_header_set_code(response, SOL_COAP_RESPONSE_CODE_CHANGED); SOL_INT_CHECK_GOTO(r, < 0, err_exit_del_client_list); SOL_DBG("Client %s bootstrap request received." " Bootstrap Process will start now.", bs_cinfo->name); r = sol_coap_send_packet(coap, response, cliaddr); dispatch_bootstrap_event_to_server(server, bs_cinfo); return r; err_exit_del_client_list: sol_ptr_vector_remove(&server->clients, bs_cinfo); err_exit_del_client: bootstrap_client_info_del(bs_cinfo); err_exit: sol_coap_header_set_code(response, SOL_COAP_RESPONSE_CODE_BAD_REQUEST); sol_coap_send_packet(coap, response, cliaddr); return r; }