Coverage Report

Created: 2026-01-17 06:14

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
27.4k
        : mData(aData)
47
27.4k
        , mSize(aSize)
48
27.4k
    {
49
27.4k
    }
50
51
    void ConsumeData(void *aBuf, size_t aLength)
52
60.6k
    {
53
60.6k
        assert(aLength <= mSize);
54
60.6k
        memcpy(aBuf, mData, aLength);
55
60.6k
        mData += aLength;
56
60.6k
        mSize -= aLength;
57
60.6k
    }
58
59
    uint8_t *ConsumeRemainingBytes(void)
60
21.9k
    {
61
21.9k
        uint8_t *buf = static_cast<uint8_t *>(malloc(mSize));
62
21.9k
        memcpy(buf, mData, mSize);
63
21.9k
        mSize = 0;
64
21.9k
        return buf;
65
21.9k
    }
66
67
14.9k
    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
14.3k
{
76
14.3k
    const uint16_t kMaxMessageSize = 2048;
77
78
14.3k
    FuzzDataProvider fdp(data, size);
79
80
14.3k
    unsigned int seed;
81
14.3k
    uint8_t     *buffer;
82
14.3k
    uint16_t     bufferLength;
83
14.3k
    otSockAddr   senderAddr;
84
85
14.3k
    if (size < sizeof(seed) + sizeof(senderAddr))
86
11
    {
87
11
        return 0;
88
11
    }
89
90
14.3k
    if (size > sizeof(seed) + sizeof(senderAddr) + kMaxMessageSize)
91
22
    {
92
22
        return 0;
93
22
    }
94
95
14.3k
    fdp.ConsumeData(&seed, sizeof(seed));
96
14.3k
    srand(seed);
97
98
14.3k
    Core nexus;
99
100
14.3k
    Node &node = nexus.CreateNode();
101
102
14.3k
    node.GetInstance().SetLogLevel(kLogLevelInfo);
103
104
14.3k
    node.GetInstance().Get<BorderRouter::InfraIf>().Init(/* aInfraIfIndex */ 1, /* aInfraIfIsRunning */ true);
105
14.3k
    SuccessOrQuit(node.GetInstance().Get<BorderRouter::RoutingManager>().SetEnabled(true));
106
14.3k
    node.GetInstance().Get<Srp::Server>().SetAutoEnableMode(true);
107
14.3k
    node.GetInstance().Get<BorderRouter::RoutingManager>().SetDhcp6PdEnabled(true);
108
14.3k
    node.GetInstance().Get<BorderRouter::RoutingManager>().SetNat64PrefixManagerEnabled(true);
109
14.3k
    node.GetInstance().Get<Nat64::Translator>().SetEnabled(true);
110
111
14.3k
    Log("---------------------------------------------------------------------------------------");
112
14.3k
    Log("Form network");
113
114
14.3k
    node.Form();
115
14.3k
    nexus.AdvanceTime(60 * 1000);
116
14.3k
    VerifyOrQuit(node.Get<Mle::Mle>().IsLeader());
117
14.3k
    VerifyOrQuit(node.Get<Srp::Server>().GetState() == Srp::Server::kStateRunning);
118
119
14.3k
    Log("---------------------------------------------------------------------------------------");
120
14.3k
    Log("Fuzz");
121
122
14.3k
    fdp.ConsumeData(&senderAddr, sizeof(senderAddr));
123
14.3k
    bufferLength = fdp.RemainingBytes();
124
14.3k
    buffer       = fdp.ConsumeRemainingBytes();
125
126
14.3k
    otPlatTrelHandleReceived(&node.GetInstance(), buffer, bufferLength, &senderAddr);
127
128
14.3k
    nexus.AdvanceTime(10 * 1000);
129
130
14.3k
    free(buffer);
131
132
14.3k
    return 0;
133
14.3k
}
134
135
} // namespace Nexus
136
} // namespace ot