Coverage Report

Created: 2025-11-12 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openthread/tests/fuzz/fuzz_trel.cpp
Line
Count
Source
1
/*
2
 *  Copyright (c) 2025, The OpenThread Authors.
3
 *  All rights reserved.
4
 *
5
 *  Redistribution and use in source and binary forms, with or without
6
 *  modification, are permitted provided that the following conditions are met:
7
 *  1. Redistributions of source code must retain the above copyright
8
 *     notice, this list of conditions and the following disclaimer.
9
 *  2. Redistributions in binary form must reproduce the above copyright
10
 *     notice, this list of conditions and the following disclaimer in the
11
 *     documentation and/or other materials provided with the distribution.
12
 *  3. Neither the name of the copyright holder nor the
13
 *     names of its contributors may be used to endorse or promote products
14
 *     derived from this software without specific prior written permission.
15
 *
16
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17
 *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
 *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20
 *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21
 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22
 *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23
 *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24
 *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25
 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26
 *  POSSIBILITY OF SUCH DAMAGE.
27
 */
28
29
#include <stdarg.h>
30
#include <stdio.h>
31
#include <stdlib.h>
32
#include <string.h>
33
34
#include <openthread/platform/trel.h>
35
36
#include "platform/nexus_core.hpp"
37
#include "platform/nexus_node.hpp"
38
39
namespace ot {
40
namespace Nexus {
41
42
class FuzzDataProvider
43
{
44
public:
45
    FuzzDataProvider(const uint8_t *aData, size_t aSize)
46
24.3k
        : mData(aData)
47
24.3k
        , mSize(aSize)
48
24.3k
    {
49
24.3k
    }
50
51
    void ConsumeData(void *aBuf, size_t aLength)
52
55.6k
    {
53
55.6k
        assert(aLength <= mSize);
54
55.6k
        memcpy(aBuf, mData, aLength);
55
55.6k
        mData += aLength;
56
55.6k
        mSize -= aLength;
57
55.6k
    }
58
59
    uint8_t *ConsumeRemainingBytes(void)
60
18.9k
    {
61
18.9k
        uint8_t *buf = static_cast<uint8_t *>(malloc(mSize));
62
18.9k
        memcpy(buf, mData, mSize);
63
18.9k
        mSize = 0;
64
18.9k
        return buf;
65
18.9k
    }
66
67
13.1k
    size_t RemainingBytes(void) { return mSize; }
68
69
private:
70
    const uint8_t *mData;
71
    size_t         mSize;
72
};
73
74
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
75
11.2k
{
76
11.2k
    const uint16_t kMaxMessageSize = 2048;
77
78
11.2k
    FuzzDataProvider fdp(data, size);
79
80
11.2k
    unsigned int seed;
81
11.2k
    uint8_t     *buffer;
82
11.2k
    uint16_t     bufferLength;
83
11.2k
    otSockAddr   senderAddr;
84
85
11.2k
    if (size < sizeof(seed) + sizeof(senderAddr))
86
11
    {
87
11
        return 0;
88
11
    }
89
90
11.2k
    if (size > sizeof(seed) + sizeof(senderAddr) + kMaxMessageSize)
91
23
    {
92
23
        return 0;
93
23
    }
94
95
11.2k
    fdp.ConsumeData(&seed, sizeof(seed));
96
11.2k
    srand(seed);
97
98
11.2k
    Core nexus;
99
100
11.2k
    Node &node = nexus.CreateNode();
101
102
11.2k
    node.GetInstance().SetLogLevel(kLogLevelInfo);
103
104
11.2k
    node.GetInstance().Get<BorderRouter::InfraIf>().Init(/* aInfraIfIndex */ 1, /* aInfraIfIsRunning */ true);
105
11.2k
    node.GetInstance().Get<BorderRouter::RoutingManager>().SetEnabled(true);
106
11.2k
    node.GetInstance().Get<Srp::Server>().SetAutoEnableMode(true);
107
11.2k
    node.GetInstance().Get<BorderRouter::RoutingManager>().SetDhcp6PdEnabled(true);
108
11.2k
    node.GetInstance().Get<BorderRouter::RoutingManager>().SetNat64PrefixManagerEnabled(true);
109
11.2k
    node.GetInstance().Get<Nat64::Translator>().SetEnabled(true);
110
111
11.2k
    Log("---------------------------------------------------------------------------------------");
112
11.2k
    Log("Form network");
113
114
11.2k
    node.Form();
115
11.2k
    nexus.AdvanceTime(60 * 1000);
116
11.2k
    VerifyOrQuit(node.Get<Mle::Mle>().IsLeader());
117
11.2k
    VerifyOrQuit(node.Get<Srp::Server>().GetState() == Srp::Server::kStateRunning);
118
119
11.2k
    Log("---------------------------------------------------------------------------------------");
120
11.2k
    Log("Fuzz");
121
122
11.2k
    fdp.ConsumeData(&senderAddr, sizeof(senderAddr));
123
11.2k
    bufferLength = fdp.RemainingBytes();
124
11.2k
    buffer       = fdp.ConsumeRemainingBytes();
125
126
11.2k
    otPlatTrelHandleReceived(&node.GetInstance(), buffer, bufferLength, &senderAddr);
127
128
11.2k
    nexus.AdvanceTime(10 * 1000);
129
130
11.2k
    free(buffer);
131
132
11.2k
    return 0;
133
11.2k
}
134
135
} // namespace Nexus
136
} // namespace ot