/src/open62541/tests/fuzz/fuzz_src_ua_util.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 | | |
9 | | #include "custom_memory_manager.h" |
10 | | |
11 | | #include <open62541/util.h> |
12 | | |
13 | | |
14 | 1.01k | static int tortureParseEndpointUrl(const uint8_t *data, size_t size) { |
15 | 1.01k | const UA_String endpointUrl = { |
16 | 1.01k | size, (UA_Byte* )(void*)data |
17 | 1.01k | }; |
18 | | |
19 | 1.01k | UA_String hostname; |
20 | 1.01k | UA_UInt16 port; |
21 | 1.01k | UA_String path; |
22 | 1.01k | UA_parseEndpointUrl(&endpointUrl, &hostname, &port, &path); |
23 | 1.01k | return 0; |
24 | 1.01k | } |
25 | | |
26 | 761 | static int tortureParseEndpointUrlEthernet(const uint8_t *data, size_t size) { |
27 | 761 | const UA_String endpointUrl = { |
28 | 761 | size, (UA_Byte* )(void*)data |
29 | 761 | }; |
30 | | |
31 | 761 | UA_String target; |
32 | 761 | UA_UInt16 vid; |
33 | 761 | UA_Byte prid; |
34 | 761 | UA_parseEndpointUrlEthernet(&endpointUrl, &target, &vid, &prid); |
35 | 761 | return 0; |
36 | 761 | } |
37 | | |
38 | | /* |
39 | | ** Main entry point. The fuzzer invokes this function with each |
40 | | ** fuzzed input. |
41 | | */ |
42 | 1.78k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
43 | | |
44 | 1.78k | if (!UA_memoryManager_setLimitFromLast4Bytes(data, size)) |
45 | 4 | return 0; |
46 | 1.77k | size -= 4; |
47 | | |
48 | 1.77k | if (size == 0) |
49 | 2 | return 0; |
50 | | |
51 | | // use first byte to decide which function should be fuzzed |
52 | | |
53 | 1.77k | const uint8_t select = data[0]; |
54 | | |
55 | 1.77k | const uint8_t *newData = &data[1]; |
56 | 1.77k | size_t newSize = size-1; |
57 | | |
58 | 1.77k | switch(select) { |
59 | 1.01k | case 0: |
60 | 1.01k | return tortureParseEndpointUrl(newData, newSize); |
61 | 761 | case 1: |
62 | 761 | return tortureParseEndpointUrlEthernet(newData, newSize); |
63 | 5 | default: |
64 | 5 | return 0; |
65 | 1.77k | } |
66 | | |
67 | 1.77k | } |