-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeviceutils_mock.cpp
More file actions
476 lines (440 loc) · 14.5 KB
/
deviceutils_mock.cpp
File metadata and controls
476 lines (440 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*
* Copyright 2023 Comcast Cable Communications Management, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "deviceutils_mock.h"
#include <iostream>
using namespace std;
// Define the global mock pointer (can be set by test suites)
DeviceUtilsMock *g_DeviceUtilsMock = nullptr;
extern "C" int v_secure_system(const char *mode, ...)
{
if (!g_DeviceUtilsMock)
{
cout << "v_secure_system g_DeviceUtilsMock object is NULL" << endl;
return NULL; // Return error code instead of NULL
}
printf("Inside Mock Function v_secure_system\n");
return g_DeviceUtilsMock->v_secure_system(mode, NULL, NULL);
}
//extern "C" FILE* v_secure_popen(const char *mode, const char *cmd, const char *opt )
extern "C" FILE* v_secure_popen(const char *mode, ...)
{
if (!g_DeviceUtilsMock)
{
cout << "v_secure_popen g_DeviceUtilsMock object is NULL" << endl;
return NULL;
}
printf("Inside Mock Function v_secure_popen\n");
return g_DeviceUtilsMock->v_secure_popen(mode, NULL, NULL);
}
/*extern "C" FILE* v_secure_popen(const char *mode, const char *cmd)
{
if (!g_DeviceUtilsMock)
{
cout << "v_secure_popen g_DeviceUtilsMock object is NULL" << endl;
return NULL;
}
printf("Inside Mock Function v_secure_popen\n");
return g_DeviceUtilsMock->v_secure_popen(mode, cmd);
}*/
extern "C" void getDeviceTypeRFC(char *deviceType, size_t size)
{
if (g_DeviceUtilsMock)
{
g_DeviceUtilsMock->getDeviceTypeRFC(deviceType, size);
}
else
{
cout << "getDeviceTypeRFC g_DeviceUtilsMock object is NULL" << endl;
if (deviceType != nullptr && size > 0)
{
const char *defaultType = "unknown";
size_t i = 0;
/* Copy up to size - 1 characters from defaultType, then NUL-terminate */
while (i + 1 < size && defaultType[i] != '\0')
{
deviceType[i] = defaultType[i];
++i;
}
deviceType[i] = '\0';
}
}
}
extern "C" int v_secure_pclose(FILE *fp)
{
if (!g_DeviceUtilsMock)
{
cout << "v_secure_pclose g_DeviceUtilsMock object is NULL" << endl;
return 0;
}
printf("Inside Mock Function v_secure_pclose\n");
return g_DeviceUtilsMock->v_secure_pclose(fp);
}
extern "C" void* doCurlInit()
{
if (!g_DeviceUtilsMock)
{
cout << "doCurlInit g_DeviceUtilsMock object is NULL" << endl;
return 0;
}
printf("Inside Mock Function doCurlInit\n");
return g_DeviceUtilsMock->doCurlInit();
}
extern "C" int getJsonRpcData(void *Curl_req, FileDwnl_t *req_data, char token_header, int httpCode )
{
if (!g_DeviceUtilsMock)
{
cout << "getJsonRpcData g_DeviceUtilsMock object is NULL" << endl;
return 0;
}
printf("Inside Mock Function getJsonRpcData\n");
return g_DeviceUtilsMock->getJsonRpcData(Curl_req, req_data,token_header, httpCode );
}
extern "C" void doStopDownload(void *curl)
{
if (!g_DeviceUtilsMock)
{
cout << "doStopDownload g_DeviceUtilsMock object is NULL" << endl;
return;
}
printf("Inside Mock Function doStopDownload\n");
return g_DeviceUtilsMock->doStopDownload(curl);
}
extern "C" int getDevicePropertyData(const char *model, char *data, int size)
{
if (!g_DeviceUtilsMock)
{
cout << "g_DeviceUtilsMock object is NULL" << endl;
return -1;
}
printf("Inside Mock Function getDevicePropertyData\n");
if (0 == (strncmp(model, "CPU_ARCH", 8))) {
snprintf(data, size, "%s", "X86");
} else if (0 == (strncmp(model, "DEVICE_NAME", 11))) {
snprintf(data, size, "%s", "PLATCO");
} else if (0 == (strncmp(model, "PDRI_ENABLED", 12))){
snprintf(data, size, "%s", "true");
} else if (0 == (strncmp(model, "STAGE2LOCKFILE", 13))) {
snprintf(data, size, "%s", "/tmp/stage2");
} else if (0 == (strncmp(model, "ESTB_INTERFACE", 14))) {
snprintf(data, size, "%s", "eth1");
}
return g_DeviceUtilsMock->getDevicePropertyData(model, data, size);
}
extern "C" int read_RFCProperty(char* type, const char* key, char *out_value, size_t datasize)
{
if (!g_DeviceUtilsMock)
{
cout << "g_DeviceUtilsMock object is NULL" << endl;
return -1;
}
printf("Inside Mock Function read_RFCProperty\n");
if (0 == (strncmp(type, "OsClass", 7))) {
snprintf(out_value, datasize, "%s", "true");
} else if (0 == (strncmp(type, "SerialNumber", 12))) {
snprintf(out_value, datasize, "%s", "123456789012345");
} else if (0 == (strncmp(type, "PDRI_ENABLED", 12))){
snprintf(out_value, datasize, "%s", "true");
} else if (0 == (strncmp(type, "AccountID", 9))) {
snprintf(out_value, datasize, "%s", "123456789");
} else {
snprintf(out_value, datasize, "%s", "default.com");
}
return g_DeviceUtilsMock->read_RFCProperty(type, key, out_value, datasize);
}
extern "C" int write_RFCProperty(char* type, const char* key, const char *value, RFCVALDATATYPE datatype)
{
if (!g_DeviceUtilsMock)
{
cout << "write_RFCProperty g_DeviceUtilsMock object is NULL" << endl;
return -1;
}
printf("Inside Mock Function write_RFCProperty\n");
return g_DeviceUtilsMock->write_RFCProperty(type, key, value, datatype);
}
extern "C" int filePresentCheck(const char *filename)
{
if (!g_DeviceUtilsMock)
{
cout << "filePresentCheck g_DeviceUtilsMock object is NULL" << endl;
return 0;
}
printf("Inside Mock Function filePresentCheck\n");
return g_DeviceUtilsMock->filePresentCheck(filename);
}
extern "C" int getFileSize(const char *file)
{
if (!g_DeviceUtilsMock)
{
cout << "getFileSize g_DeviceUtilsMock object is NULL" << endl;
return 0;
}
printf("Inside Mock Function getFileSize\n");
return g_DeviceUtilsMock->getFileSize(file);
}
extern "C" bool isInStateRed()
{
if (!g_DeviceUtilsMock)
{
cout << "isInStateRed g_DeviceUtilsMock object is NULL" << endl;
return false;
}
printf("Inside Mock Function isInStateRed\n");
return g_DeviceUtilsMock->isInStateRed();
}
extern "C" bool isDebugServicesEnabled(void) {
if (!g_DeviceUtilsMock) {
cout << "isDebugServicesEnabled g_DeviceUtilsMock object is NULL" << endl;
return false;
}
return g_DeviceUtilsMock->isDebugServicesEnabled();
}
extern "C" size_t GetHwMacAddress( char *iface, char *pMac, size_t szBufSize )
{
if (!g_DeviceUtilsMock)
{
cout << "GetHwMacAddress g_DeviceUtilsMock object is NULL" << endl;
return 0;
}
printf("Inside Mock Function GetHwMacAddress\n");
return g_DeviceUtilsMock->GetHwMacAddress(iface, pMac, szBufSize);
}
extern "C" size_t GetModelNum( char *pModelNum, size_t szBufSize )
{
if (!g_DeviceUtilsMock)
{
cout << "GetBuildType g_DeviceStatusMock object is NULL" << endl;
return 0;
}
printf("Inside Mock Function GetModelNum\n");
snprintf(pModelNum, szBufSize, "%s", "12345");
return g_DeviceUtilsMock->GetModelNum(pModelNum, szBufSize);
}
#ifdef DEVICE_API
extern "C" void t2CountNotify(char *marker)
{
if (!g_DeviceUtilsMock)
{
cout << "t2CountNotify g_DeviceUtilsMock object is NULL" << endl;
return ;
}
printf("Inside Mock Function t2CountNotify\n");
return g_DeviceUtilsMock->t2CountNotify(marker);
}
extern "C" void t2ValNotify(char *marker, char *val)
{
if (!g_DeviceUtilsMock)
{
cout << "t2ValNotify g_DeviceUtilsMock object is NULL" << endl;
return ;
}
printf("Inside Mock Function t2ValNotify\n");
return g_DeviceUtilsMock->t2ValNotify(marker, val);
}
extern "C" size_t GetPDRIFileNameUsingMFR(char *pPDRIFilename, size_t szBufSize)
{
if (!g_DeviceUtilsMock) {
cout << "GetPDRIFileNameUsingMFR g_IarmInterfaceMock object is NULL" << endl;
return 0;
}
printf("Inside Mock Function GetPDRIFileNameUsingMFR\n");
// Give a fake file name for tests unless you want to do more in your mock object
const char *mockPDRI = "mock-PDRI-image.bin";
size_t len = strlen(mockPDRI);
if (pPDRIFilename && szBufSize > len) {
strncpy(pPDRIFilename, mockPDRI, szBufSize);
pPDRIFilename[szBufSize - 1] = '\0';
return len;
}
return g_DeviceUtilsMock->GetPDRIFileNameUsingMFR(pPDRIFilename, szBufSize);
}
// ========== Mocks for external functions (moved to common_utilities) ==========
/*
extern "C" size_t GetTimezone(char *pTimezone, const char *arch, size_t szBufSize)
{
// Simple stub for external function
if (pTimezone != NULL && szBufSize > 0) {
snprintf(pTimezone, szBufSize, "UTC");
return strlen(pTimezone);
}
return 0;
}
extern "C" size_t GetCapabilities(char *pCapabilities, size_t szBufSize)
{
// Simple stub for external function
if (pCapabilities != NULL && szBufSize > 0) {
snprintf(pCapabilities, szBufSize, "HDR");
return strlen(pCapabilities);
}
return 0;
}
extern "C" size_t GetUTCTime(char *pUTCTime, size_t szBufSize)
{
// Simple stub for external function
if (pUTCTime != NULL && szBufSize > 0) {
snprintf(pUTCTime, szBufSize, "12345");
return strlen(pUTCTime);
}
return 0;
}
extern "C" size_t GetFirmwareVersion(char *pFWVersion, size_t szBufSize)
{
// Simple stub for external function
if (pFWVersion != NULL && szBufSize > 0) {
snprintf(pFWVersion, szBufSize, "1.0.0");
return strlen(pFWVersion);
}
return 0;
}
extern "C" size_t GetMFRName(char *pMFRName, size_t szBufSize)
{
// Simple stub for external function
if (pMFRName != NULL && szBufSize > 0) {
snprintf(pMFRName, szBufSize, "Comcast");
return strlen(pMFRName);
}
return 0;
}
extern "C" size_t GetFileContents(char **ppFileContents, const char *pFileName)
{
// Simple stub for external function
if (ppFileContents != NULL && pFileName != NULL) {
*ppFileContents = (char *)malloc(20);
if (*ppFileContents) {
snprintf(*ppFileContents, 20, "File contents");
return strlen(*ppFileContents);
}
}
return 0;
}
*/
// Forward declaration for stripinvalidchar (defined later in this file)
extern "C" size_t stripinvalidchar(char *pIn, size_t szIn);
extern "C" size_t GetBuildType(char *pBuildType, size_t szBufSize, BUILDTYPE *peBuildTypeOut)
{
// Mock for external function from common_utilities
// Reads from /tmp/device_gtest.prop to match test expectations
// Possible values: "dev" (eDEV), "vbn" (eVBN), "prod" (ePROD), "qa" (eQA)
FILE *fp;
char *pTmp;
size_t i = 0;
BUILDTYPE eBuildType = eUNKNOWN;
char buf[150];
if (pBuildType != NULL && szBufSize > 0) {
*pBuildType = 0;
if ((fp = fopen("/tmp/device_gtest.prop", "r")) != NULL) {
while (fgets(buf, sizeof(buf), fp) != NULL) {
pTmp = strstr(buf, "BUILD_TYPE=");
if (pTmp && pTmp == buf) {
pTmp = strchr(pTmp, '=');
if (pTmp != NULL) {
++pTmp;
i = snprintf(pBuildType, szBufSize, "%s", pTmp);
// Strip invalid characters (newline, etc.)
i = stripinvalidchar(pBuildType, i);
// Convert to lowercase
char *pLower = pBuildType;
while (*pLower) {
*pLower = tolower(*pLower);
++pLower;
}
}
}
}
fclose(fp);
}
// If file not found or no BUILD_TYPE, default to "prod"
if (*pBuildType == 0) {
i = snprintf(pBuildType, szBufSize, "prod");
eBuildType = ePROD;
} else {
// Determine eBuildType from string
if (strstr(pBuildType, "vbn") != NULL) {
eBuildType = eVBN;
} else if (strstr(pBuildType, "prod") != NULL) {
eBuildType = ePROD;
} else if (strstr(pBuildType, "qa") != NULL) {
eBuildType = eQA;
} else if (strstr(pBuildType, "dev") != NULL) {
eBuildType = eDEV;
}
}
if (peBuildTypeOut) {
*peBuildTypeOut = eBuildType;
}
return i;
}
return 0;
}
/*
extern "C" int stripinvalidchar(char *pStr, int len)
{
// Mock for external function from common_utilities
// Returns the length after stripping invalid characters
// For testing purposes, just return the original length
if (pStr != NULL && len > 0) {
return len;
}
return 0;
}
*/
extern "C" int makeHttpHttps(char *pStr, int len)
{
// Mock for external function from common_utilities
// This function converts http:// URLs to https://
// For testing purposes, just return success
if (pStr != NULL && len > 0) {
return 1; // Return success
}
return 0;
}
extern "C" void swLog(const char *file, const char *func, int line, int level, const char *format, ...)
{
// Mock for external function from common_utilities (libfwutils)
// This is a logging function - for testing purposes, we can just ignore it
// or optionally print to stdout for debugging
return;
}
extern "C" int allocDowndLoadDataMem(void *ptr, int size)
{
// Mock for external function from common_utilities (libdwnutils)
// This function allocates memory for download data
// For testing purposes, just return success
return 0;
}
extern "C" size_t stripinvalidchar(char *pIn, size_t szIn)
{
// Mock for external function from common_utilities (libfwutils)
// This function truncates a string when a space or control character is encountered
size_t i = 0;
if (pIn != NULL)
{
while (*pIn && szIn)
{
if (isspace(*pIn) || iscntrl(*pIn))
{
*pIn = 0;
break;
}
++pIn;
--szIn;
++i;
}
}
return i;
}
#endif