|
| 1 | +/******************************************************************************* |
| 2 | + * |
| 3 | + * Copyright (c) 2023 GARDENA GmbH |
| 4 | + * |
| 5 | + * All rights reserved. This program and the accompanying materials |
| 6 | + * are made available under the terms of the Eclipse Public License v2.0 |
| 7 | + * and Eclipse Distribution License v1.0 which accompany this distribution. |
| 8 | + * |
| 9 | + * The Eclipse Public License is available at |
| 10 | + * http://www.eclipse.org/legal/epl-v20.html |
| 11 | + * The Eclipse Distribution License is available at |
| 12 | + * http://www.eclipse.org/org/documents/edl-v10.php. |
| 13 | + * |
| 14 | + * Contributors: |
| 15 | + * Lukas Woodtli, GARDENA GmbH - Please refer to git log |
| 16 | + * |
| 17 | + *******************************************************************************/ |
| 18 | + |
| 19 | +#include <stdbool.h> |
| 20 | +#include <stdint.h> |
| 21 | +#include <string.h> |
| 22 | + |
| 23 | +#include "liblwm2m.h" |
| 24 | + |
| 25 | +#include "er-coap-13/er-coap-13.h" |
| 26 | +#include "registration.h" |
| 27 | + |
| 28 | +// stub function |
| 29 | +void *lwm2m_connect_server(uint16_t secObjInstID, void *userData) { |
| 30 | + (void)userData; |
| 31 | + return (void *)(uintptr_t)secObjInstID; |
| 32 | +} |
| 33 | + |
| 34 | +void lwm2m_close_connection(void *sessionH, void *userData) { |
| 35 | + (void)sessionH; |
| 36 | + (void)userData; |
| 37 | + return; |
| 38 | +} |
| 39 | + |
| 40 | +static void init_test_packet(coap_packet_t *message, const char *registration_message, uint8_t *payload, |
| 41 | + const size_t payload_len) { |
| 42 | + |
| 43 | + memset(message, 0, sizeof(coap_packet_t)); |
| 44 | + message->code = COAP_POST; |
| 45 | + coap_set_header_uri_query(message, "lwm2m=1.1&ep=abc"); |
| 46 | + |
| 47 | + memcpy(payload, registration_message, payload_len); |
| 48 | + message->payload = payload; |
| 49 | + message->payload_len = payload_len; |
| 50 | + |
| 51 | + message->content_type = (coap_content_type_t)LWM2M_CONTENT_LINK; |
| 52 | +} |
| 53 | + |
| 54 | +void init_uri(lwm2m_uri_t *const uri) { |
| 55 | + memset(uri, 0, sizeof(*uri)); |
| 56 | + uri->objectId = LWM2M_MAX_ID; |
| 57 | +} |
| 58 | + |
| 59 | +static int call_test_function(const char *reg_msg, const size_t reg_msg_len) { |
| 60 | + int ret; |
| 61 | + |
| 62 | + uint8_t user_data[1] = {0}; |
| 63 | + lwm2m_context_t *context = lwm2m_init(user_data); |
| 64 | + |
| 65 | + lwm2m_uri_t uri; |
| 66 | + init_uri(&uri); |
| 67 | + coap_packet_t message; |
| 68 | + |
| 69 | + uint8_t payload[reg_msg_len == 0 ? 1 : reg_msg_len]; |
| 70 | + init_test_packet(&message, reg_msg, payload, reg_msg_len); |
| 71 | + |
| 72 | + coap_packet_t response; |
| 73 | + memset(&response, 0, sizeof(coap_packet_t)); |
| 74 | + |
| 75 | + ret = registration_handleRequest(context, &uri, NULL, &message, &response); |
| 76 | + |
| 77 | + free_multi_option(message.uri_query); |
| 78 | + free_multi_option(response.location_path); |
| 79 | + |
| 80 | + lwm2m_close(context); |
| 81 | + |
| 82 | + return ret; |
| 83 | +} |
| 84 | + |
| 85 | +int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
| 86 | + |
| 87 | + call_test_function((const char *)data, size); |
| 88 | + |
| 89 | + return 0; |
| 90 | +} |
0 commit comments