Coverage Report

Created: 2025-12-24 07:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openweave-core/src/test-apps/ToolCommon.h
Line
Count
Source
1
/*
2
 *
3
 *    Copyright (c) 2013-2017 Nest Labs, Inc.
4
 *    All rights reserved.
5
 *
6
 *    Licensed under the Apache License, Version 2.0 (the "License");
7
 *    you may not use this file except in compliance with the License.
8
 *    You may obtain a copy of the License at
9
 *
10
 *        http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 *    Unless required by applicable law or agreed to in writing, software
13
 *    distributed under the License is distributed on an "AS IS" BASIS,
14
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 *    See the License for the specific language governing permissions and
16
 *    limitations under the License.
17
 */
18
19
/**
20
 *    @file
21
 *      This file defines constants, globals and interfaces common to
22
 *      and used by all Weave test applications and tools.
23
 *
24
 *      NOTE: These do not comprise a public part of the Weave API and
25
 *            are subject to change without notice.
26
 *
27
 */
28
29
#ifndef TOOLCOMMON_H_
30
#define TOOLCOMMON_H_
31
32
#ifndef __STDC_LIMIT_MACROS
33
#define __STDC_LIMIT_MACROS
34
#endif
35
#include <stdint.h>
36
37
#include <stdio.h>
38
#include <stdlib.h>
39
#include <limits.h>
40
#include <time.h>
41
#include <sys/time.h>
42
#include <stdio.h>
43
#include <ctype.h>
44
#include <errno.h>
45
#include <pthread.h>
46
47
#include <nlfaultinjection.hpp>
48
49
#include "ToolCommonOptions.h"
50
#include "CASEOptions.h"
51
#include "TAKEOptions.h"
52
#include "KeyExportOptions.h"
53
#include "DeviceDescOptions.h"
54
55
#include <Weave/WeaveVersion.h>
56
#include <SystemLayer/SystemLayer.h>
57
#include <InetLayer/InetLayer.h>
58
#include <Weave/Core/WeaveCore.h>
59
#include <Weave/Support/CodeUtils.h>
60
#include <Weave/Support/ErrorStr.h>
61
#include <Weave/Core/WeaveStats.h>
62
63
#if CONFIG_BLE_PLATFORM_BLUEZ
64
#include <BleLayer/BleApplicationDelegate.h>
65
#include <PlatformLayer/Ble/Bluez/BluezBlePlatformDelegate.h>
66
#include <PlatformLayer/Ble/Bluez/WoBluezLayer.h>
67
#endif // CONFIG_BLE_PLATFORM_BLUEZ
68
69
70
using namespace nl::Inet;
71
using namespace nl::Weave;
72
using namespace nl::Weave::Profiles;
73
74
#ifndef nlDEFINE_ALIGNED_VAR
75
#define nlDEFINE_ALIGNED_VAR(varName, bytes, alignment_type) \
76
  alignment_type varName[(((bytes)+(sizeof(alignment_type)-1))/sizeof(alignment_type))]
77
#endif
78
79
#define WEAVE_TOOL_COPYRIGHT "Copyright (c) 2018 Google LLC.\nCopyright (c) 2013-2018 Nest Labs, Inc.\nAll rights reserved.\n"
80
81
extern System::Layer SystemLayer;
82
83
extern InetLayer Inet;
84
extern BleLayer Ble;
85
86
extern uint16_t sTestDefaultUDPSessionKeyId;
87
extern uint16_t sTestDefaultTCPSessionKeyId;
88
extern uint16_t sTestDefaultSessionKeyId;
89
90
extern bool Done;
91
extern bool gSigusr1Received;
92
93
struct TestNodeCert
94
{
95
    uint64_t NodeId;
96
    const uint8_t *Cert;
97
    uint16_t CertLength;
98
    const uint8_t *PrivateKey;
99
    uint16_t PrivateKeyLength;
100
};
101
102
extern TestNodeCert TestNodeCerts[];
103
104
struct TestCACert
105
{
106
    uint64_t CAId;
107
    const uint8_t *Cert;
108
    uint16_t CertLength;
109
};
110
111
extern TestCACert TestCACerts[];
112
extern uint64_t TestMockRoot_CAId;
113
extern uint64_t TestMockServiceEndpointCA_CAId;
114
115
extern uint64_t TestDevice1_NodeId;
116
extern uint8_t TestDevice1_Cert[];
117
extern uint16_t TestDevice1_CertLength;
118
extern uint8_t TestDevice1_PrivateKey[];
119
extern uint16_t TestDevice1_PrivateKeyLength;
120
extern uint64_t TestDevice2_NodeId;
121
extern uint8_t TestDevice2_Cert[];
122
extern uint16_t TestDevice2_CertLength;
123
extern uint8_t TestDevice2_PrivateKey[];
124
extern uint16_t TestDevice2_PrivateKeyLength;
125
126
extern bool sSuppressAccessControls;
127
128
extern void InitToolCommon();
129
extern void UseStdoutLineBuffering();
130
extern void SetSIGUSR1Handler(void);
131
extern void InitSystemLayer();
132
extern void ShutdownSystemLayer();
133
typedef void (*SignalHandler)(int signum);
134
extern void SetSignalHandler(SignalHandler handler);
135
extern void DoneOnHandleSIGUSR1(int);
136
extern void InitNetwork();
137
extern void PrintNodeConfig();
138
extern void ServiceEvents(::timeval& aSleepTime);
139
extern void InitWeaveStack(bool listen, bool initExchangeMgr);
140
extern void ShutdownNetwork();
141
extern void ShutdownWeaveStack();
142
extern bool ParseCASEConfig(const char *str, uint32_t& output);
143
extern bool ParseAllowedCASEConfigs(const char *strConst, uint8_t& output);
144
extern void DumpMemory(const uint8_t *mem, uint32_t len, const char *prefix, uint32_t rowWidth = 16);
145
extern void DumpMemoryCStyle(const uint8_t *mem, uint32_t len, const char *prefix, uint32_t rowWidth = 16);
146
extern bool IsZeroBytes(const uint8_t *buf, uint32_t len);
147
extern void PrintMACAddress(const uint8_t *buf, uint32_t len);
148
extern void PrintAddresses();
149
extern uint8_t *ReadFileArg(const char *fileName, uint32_t& len, uint32_t maxLen = UINT32_MAX);
150
extern void HandleMessageReceiveError(WeaveMessageLayer *msgLayer, WEAVE_ERROR err, const IPPacketInfo *pktInfo);
151
extern void HandleAcceptConnectionError(WeaveMessageLayer *msgLayer, WEAVE_ERROR err);
152
extern bool GetTestNodeCert(uint64_t nodeId, const uint8_t *& cert, uint16_t& certLen);
153
extern bool GetTestNodePrivateKey(uint64_t nodeId, const uint8_t *& key, uint16_t& keyLen);
154
extern bool GetTestCACert(uint64_t caId, const uint8_t *& cert, uint16_t& certLen);
155
extern bool GetTestCAPrivateKey(uint64_t caId, const uint8_t *& key, uint16_t& keyLen);
156
157
#if CONFIG_BLE_PLATFORM_BLUEZ
158
void *WeaveBleIOLoop(void *arg);
159
nl::Ble::Platform::BlueZ::BluezBlePlatformDelegate *getBluezPlatformDelegate();
160
nl::Ble::Platform::BlueZ::BluezBleApplicationDelegate *getBluezApplicationDelegate();
161
#endif // CONFIG_BLE_PLATFORM_BLUEZ
162
163
inline static void ServiceNetwork(struct ::timeval aSleepTime)
164
0
{
165
0
    ServiceEvents(aSleepTime);
166
0
}
Unexecuted instantiation: FuzzPASEKeyConfirm.cpp:ServiceNetwork(timeval)
Unexecuted instantiation: PASEEngineTest.cpp:ServiceNetwork(timeval)
Unexecuted instantiation: ToolCommon.cpp:ServiceNetwork(timeval)
Unexecuted instantiation: TestPersistedStorageImplementation.cpp:ServiceNetwork(timeval)
Unexecuted instantiation: CASEOptions.cpp:ServiceNetwork(timeval)
Unexecuted instantiation: KeyExportOptions.cpp:ServiceNetwork(timeval)
Unexecuted instantiation: TAKEOptions.cpp:ServiceNetwork(timeval)
Unexecuted instantiation: DeviceDescOptions.cpp:ServiceNetwork(timeval)
Unexecuted instantiation: Certs.cpp:ServiceNetwork(timeval)
Unexecuted instantiation: TestGroupKeyStore.cpp:ServiceNetwork(timeval)
Unexecuted instantiation: FuzzPASEResponderStep2.cpp:ServiceNetwork(timeval)
Unexecuted instantiation: FuzzPASEInitiatorStep2.cpp:ServiceNetwork(timeval)
Unexecuted instantiation: FuzzCertificateConversion.cpp:ServiceNetwork(timeval)
Unexecuted instantiation: FuzzPASEResponderStep1.cpp:ServiceNetwork(timeval)
Unexecuted instantiation: FuzzPASEInitiatorStep1.cpp:ServiceNetwork(timeval)
167
168
extern void ServiceNetworkUntil(const bool *aDone, const uint32_t *aIntervalMs = NULL);
169
170
extern void PrintStatsCounters(nl::Weave::System::Stats::count_t *counters, const char *aPrefix);
171
extern bool ProcessStats(nl::Weave::System::Stats::Snapshot &aBefore, nl::Weave::System::Stats::Snapshot &aAfter, bool aPrint, const char *aPrefix);
172
extern void PrintFaultInjectionCounters(void);
173
extern void SetupFaultInjectionContext(int argc, char *argv[]);
174
extern void SetupFaultInjectionContext(int argc, char *argv[], int32_t (*aNumEventsAvailable)(void), void (*aInjectAsyncEvents)(int32_t index));
175
#define TOOL_COMMON_FIRST_APP_ASYNC_EVENT 1
176
extern void (*gAsyncEventCb)(uint16_t numArgs, int32_t argument);
177
178
#if WEAVE_CONFIG_ENABLE_TUNNELING
179
180
#if !WEAVE_TUNNEL_CONFIG_WILL_OVERRIDE_ADDR_ROUTING_FUNCS
181
extern INET_ERROR InterfaceAddAddress(InterfaceId tunIf, IPAddress ipAddr, uint8_t prefixLen);
182
extern INET_ERROR InterfaceRemoveAddress(InterfaceId tunIf, IPAddress ipAddr, uint8_t prefixLen);
183
extern INET_ERROR SetRouteToTunnelInterface(InterfaceId tunIf, IPPrefix ipPrefix, TunEndPoint::RouteOp routeAddDel);
184
#endif // WEAVE_TUNNEL_CONFIG_WILL_OVERRIDE_ADDR_ROUTING_FUNCS
185
186
#endif // WEAVE_CONFIG_ENABLE_TUNNELING
187
using nl::ErrorStr;
188
189
inline uint64_t Now()
190
0
{
191
0
  struct timeval now;
192
0
  gettimeofday(&now, NULL);
193
0
  return ((uint64_t)now.tv_sec * 1000000) + (uint64_t)now.tv_usec;
194
0
}
195
inline uint64_t NowMs()
196
0
{
197
0
    return Now()/1000;
198
0
}
199
200
#define FAIL_ERROR(ERR, MSG) \
201
0
  do { \
202
0
    if ((ERR) != WEAVE_NO_ERROR) \
203
0
    { \
204
0
      fprintf(stderr, "%s: %s\n", (MSG), ErrorStr(ERR)); \
205
0
      exit(-1); \
206
0
    } \
207
0
  } while (0)
208
209
#endif /* TOOLCOMMON_H_ */