/src/open62541/tests/fuzz/fuzz_binary_message.cc
Line | Count | Source |
1 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
2 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
3 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
4 | | * |
5 | | * Copyright 2019 (c) fortiss (Author: Stefan Profanter) |
6 | | */ |
7 | | |
8 | | #include <open62541/plugin/log_stdout.h> |
9 | | #include <open62541/server_config_default.h> |
10 | | #include <open62541/types.h> |
11 | | |
12 | | #include "ua_server_internal.h" |
13 | | #include "testing_networklayers.h" |
14 | | |
15 | | #define RECEIVE_BUFFER_SIZE 65535 |
16 | | |
17 | | /* |
18 | | ** Main entry point. The fuzzer invokes this function with each |
19 | | ** fuzzed input. |
20 | | */ |
21 | | extern "C" int |
22 | 286 | LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
23 | 286 | if(size <= 4) |
24 | 4 | return 0; |
25 | | |
26 | | /* less debug output */ |
27 | 282 | UA_ServerConfig initialConfig; |
28 | 282 | memset(&initialConfig, 0, sizeof(UA_ServerConfig)); |
29 | 282 | UA_StatusCode retval = UA_ServerConfig_setDefault(&initialConfig); |
30 | 282 | initialConfig.allowEmptyVariables = UA_RULEHANDLING_ACCEPT; |
31 | 282 | if(retval != UA_STATUSCODE_GOOD) { |
32 | 0 | UA_ServerConfig_clean(&initialConfig); |
33 | 0 | UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, |
34 | 0 | "Could not generate the server config"); |
35 | 0 | return 0; |
36 | 0 | } |
37 | | |
38 | 282 | UA_Server *server = UA_Server_newWithConfig(&initialConfig); |
39 | 282 | if(!server) { |
40 | 0 | UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, |
41 | 0 | "Could not create server instance using UA_Server_new"); |
42 | 0 | return 0; |
43 | 0 | } |
44 | | |
45 | | // we need to copy the message because it will be freed in the processing function |
46 | 282 | UA_ByteString msg = UA_BYTESTRING_NULL; |
47 | 282 | retval = UA_ByteString_allocBuffer(&msg, size); |
48 | 282 | if(retval != UA_STATUSCODE_GOOD) { |
49 | 0 | UA_Server_delete(server); |
50 | 0 | UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, |
51 | 0 | "Could not allocate message buffer"); |
52 | 0 | return 0; |
53 | 0 | } |
54 | 282 | memcpy(msg.data, data, size); |
55 | | |
56 | | /* Get the binary server components */ |
57 | 282 | UA_String binStr = UA_STRING((char*)(uintptr_t)"binary"); |
58 | 282 | UA_Driver *bpm = NULL; |
59 | 1.69k | for(UA_Driver *drv = server->drivers; drv; drv = drv->next) { |
60 | 1.41k | if(UA_String_equal(&binStr, &drv->name)) |
61 | 564 | bpm = drv; |
62 | 1.41k | } |
63 | 282 | UA_assert(bpm != NULL); |
64 | | |
65 | 282 | void *ctx = NULL; |
66 | 282 | UA_ConnectionManager *cm = TestConnectionManager_new("tcp", NULL); |
67 | 282 | serverNetworkCallback(cm, 0, bpm, |
68 | 282 | &ctx, UA_CONNECTIONSTATE_ESTABLISHED, |
69 | 282 | &UA_KEYVALUEMAP_NULL, msg); |
70 | 282 | cm->eventSource.free(&cm->eventSource); |
71 | | |
72 | | // if we got an invalid chunk, the message is not deleted, so delete it here |
73 | 282 | UA_ByteString_clear(&msg); |
74 | 282 | UA_Server_delete(server); |
75 | 282 | return 0; |
76 | 282 | } |