forked from helium/console-decoders
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-decoder.js
More file actions
executable file
·386 lines (322 loc) · 11.9 KB
/
Copy pathtest-decoder.js
File metadata and controls
executable file
·386 lines (322 loc) · 11.9 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
#!/usr/bin/env node
const fs = require('node:fs');
const path = require('node:path');
const { execSync } = require('node:child_process');
// ANSI color codes for better output
const colors = {
reset: '\x1b[0m',
green: '\x1b[32m',
red: '\x1b[31m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
cyan: '\x1b[36m',
bold: '\x1b[1m'
};
class DecoderTester {
constructor() {
this.logFile = 'decoder-test-results.log';
}
showHelp() {
console.log(`
${colors.bold}LoRaWAN Decoder Testing Tool${colors.reset}
${colors.cyan}USAGE:${colors.reset}
node test-decoder.js <decoder-name> --data <base64-payload> [options]
node test-decoder.js <decoder-name> --file <json-file-path> [options]
${colors.cyan}EXAMPLES:${colors.reset}
node test-decoder.js Dragino/LHT65 --data "AXQBSwqJAkA2Ffi0Cg///NA="
node test-decoder.js RAK/RAK2171 --file "/path/to/uplink.json"
node test-decoder.js Dragino/LHT65 --data "AXQBSwqJAkA2Ffi0Cg///NA=" --port 2
${colors.cyan}OPTIONS:${colors.reset}
--data <payload> Test with raw base64 payload data
--file <path> Test with JSON file containing uplink data
--port <number> Specify fPort (default: 1)
--decoder <file> Specify decoder file (default: decoder.js)
--help, -h Show this help message
--list List all available decoders
--no-log Don't write results to log file
--verbose, -v Show detailed output
${colors.cyan}DECODER NAMING:${colors.reset}
Use the directory path relative to current directory:
- Dragino/LHT65
- RAK/RAK2171
- Browan/TBHV100
${colors.cyan}LOG FILE:${colors.reset}
Results are logged to: ${this.logFile}
Most recent tests appear at the top.
${colors.cyan}NOTES:${colors.reset}
- JSON files should contain 'data' field with base64 payload
- fPort will be extracted from JSON file if available
- Use --list to see all available decoders
`);
}
listDecoders() {
console.log(`\n${colors.bold}Available Decoders:${colors.reset}\n`);
const directories = fs.readdirSync('.', { withFileTypes: true })
.filter(dirent => dirent.isDirectory() && !dirent.name.startsWith('.'))
.map(dirent => dirent.name);
for (const dir of directories) {
this.findDecodersInDirectory(dir, '');
}
}
findDecodersInDirectory(dirPath, prefix) {
try {
const fullPath = path.join('.', dirPath);
const items = fs.readdirSync(fullPath, { withFileTypes: true });
// Check for decoder files in current directory
const decoderFiles = items
.filter(item => item.isFile() && item.name.endsWith('.js'))
.map(item => item.name);
if (decoderFiles.length > 0) {
console.log(`${colors.green}${prefix}${dirPath}/${colors.reset}`);
decoderFiles.forEach(file => {
console.log(` ${colors.cyan}├── ${file}${colors.reset}`);
});
}
// Recursively check subdirectories
const subDirs = items.filter(item => item.isDirectory());
for (const subDir of subDirs) {
this.findDecodersInDirectory(path.join(dirPath, subDir.name), prefix + ' ');
}
} catch (error) {
// Skip directories we can't read
}
}
base64ToBytes(base64String) {
const buffer = Buffer.from(base64String, 'base64');
return Array.from(buffer);
}
loadDecoder(decoderPath, decoderFile = 'decoder.js') {
const fullPath = path.join(decoderPath, decoderFile);
if (!fs.existsSync(fullPath)) {
throw new Error(`Decoder file not found: ${fullPath}`);
}
const decoderCode = fs.readFileSync(fullPath, 'utf8');
// Create a safe execution context
const context = {
console: console,
Buffer: Buffer,
Date: Date,
Math: Math,
JSON: JSON,
String: String,
Number: Number,
Array: Array,
Object: Object
};
try {
// Execute the decoder code in our context
const wrappedCode = `
${decoderCode}
// Export the Decoder function
if (typeof Decoder === 'function') {
context.DecoderFunction = Decoder;
} else {
throw new Error('No Decoder function found in decoder file');
}
`;
eval(wrappedCode);
return context.DecoderFunction;
} catch (error) {
throw new Error(`Failed to load decoder: ${error.message}`);
}
}
parseJsonFile(filePath) {
if (!fs.existsSync(filePath)) {
throw new Error(`File not found: ${filePath}`);
}
const fileContent = fs.readFileSync(filePath, 'utf8');
const jsonData = JSON.parse(fileContent);
if (!jsonData.data) {
throw new Error('JSON file must contain a "data" field with base64 payload');
}
return {
data: jsonData.data,
fPort: jsonData.fPort || 1,
uplinkInfo: jsonData
};
}
runTest(decoderName, options) {
const startTime = new Date();
let testResult = {
timestamp: startTime.toISOString(),
decoderName: decoderName,
success: false,
error: null,
input: {},
output: null,
executionTime: 0
};
try {
// Load the decoder
const decoderFunction = this.loadDecoder(decoderName, options.decoder);
// Prepare test data
let payload, fPort, uplinkInfo = null;
if (options.file) {
const fileData = this.parseJsonFile(options.file);
payload = fileData.data;
fPort = fileData.fPort;
uplinkInfo = fileData.uplinkInfo;
testResult.input.source = 'file';
testResult.input.filePath = options.file;
} else {
payload = options.data;
fPort = options.port;
testResult.input.source = 'direct';
}
testResult.input.payload = payload;
testResult.input.fPort = fPort;
// Convert base64 to bytes
const bytes = this.base64ToBytes(payload);
if (options.verbose) {
console.log(`\n${colors.cyan}Testing decoder:${colors.reset} ${decoderName}`);
console.log(`${colors.cyan}Payload:${colors.reset} ${payload}`);
console.log(`${colors.cyan}Bytes:${colors.reset} [${bytes.join(', ')}]`);
console.log(`${colors.cyan}fPort:${colors.reset} ${fPort}`);
console.log(`${colors.cyan}Decoder file:${colors.reset} ${options.decoder}`);
}
// Run the decoder
const execStart = Date.now();
let result;
// Try different function signatures
try {
result = decoderFunction(bytes, fPort, uplinkInfo);
} catch (error) {
// Try with just bytes and port
result = decoderFunction(bytes, fPort);
}
const execEnd = Date.now();
testResult.executionTime = execEnd - execStart;
testResult.output = result;
testResult.success = true;
// Display results
console.log(`\n${colors.green}✓ Decoder test successful!${colors.reset}`);
console.log(`${colors.bold}Decoded Result:${colors.reset}`);
console.log(JSON.stringify(result, null, 2));
console.log(`\n${colors.yellow}Execution time: ${testResult.executionTime}ms${colors.reset}`);
} catch (error) {
testResult.error = error.message;
console.log(`\n${colors.red}✗ Decoder test failed!${colors.reset}`);
console.log(`${colors.red}Error: ${error.message}${colors.reset}`);
if (options.verbose) {
console.log(`${colors.red}Stack trace:${colors.reset}`);
console.log(error.stack);
}
}
// Log results if not disabled
if (!options.noLog) {
this.logResults(testResult);
}
return testResult;
}
logResults(testResult) {
const separator = '='.repeat(80);
const logEntry = `
${separator}
TEST EXECUTION - ${testResult.timestamp}
${separator}
Decoder: ${testResult.decoderName}
Status: ${testResult.success ? 'SUCCESS' : 'FAILED'}
Execution Time: ${testResult.executionTime}ms
INPUT:
Source: ${testResult.input.source}
${testResult.input.filePath ? `File: ${testResult.input.filePath}` : ''}
Payload: ${testResult.input.payload}
fPort: ${testResult.input.fPort}
OUTPUT:
${testResult.success ? JSON.stringify(testResult.output, null, 2) : `ERROR: ${testResult.error}`}
${separator}
`;
// Prepend to log file (newest entries at top)
let existingLog = '';
if (fs.existsSync(this.logFile)) {
existingLog = fs.readFileSync(this.logFile, 'utf8');
}
fs.writeFileSync(this.logFile, logEntry + existingLog);
console.log(`\n${colors.blue}Results logged to: ${this.logFile}${colors.reset}`);
}
}
// Main execution
function main() {
const tester = new DecoderTester();
const args = process.argv.slice(2);
if (args.length === 0 || args.includes('--help') || args.includes('-h')) {
tester.showHelp();
return;
}
if (args.includes('--list')) {
tester.listDecoders();
return;
}
// Parse arguments
const options = {
data: null,
file: null,
port: 1,
decoder: 'decoder.js',
verbose: false,
noLog: false
};
let decoderName = null;
for (let i = 0; i < args.length; i++) {
const arg = args[i];
const nextArg = args[i + 1];
switch (arg) {
case '--data':
options.data = nextArg;
i++;
break;
case '--file':
options.file = nextArg;
i++;
break;
case '--port':
options.port = parseInt(nextArg) || 1;
i++;
break;
case '--decoder':
options.decoder = nextArg;
i++;
break;
case '--verbose':
case '-v':
options.verbose = true;
break;
case '--no-log':
options.noLog = true;
break;
default:
if (!arg.startsWith('--') && !decoderName) {
decoderName = arg;
}
break;
}
}
// Validate arguments
if (!decoderName) {
console.log(`${colors.red}Error: Decoder name is required${colors.reset}`);
console.log('Use --help for usage information');
process.exit(1);
}
if (!options.data && !options.file) {
console.log(`${colors.red}Error: Either --data or --file must be specified${colors.reset}`);
console.log('Use --help for usage information');
process.exit(1);
}
if (options.data && options.file) {
console.log(`${colors.red}Error: Cannot specify both --data and --file${colors.reset}`);
console.log('Use --help for usage information');
process.exit(1);
}
// Run the test
try {
tester.runTest(decoderName, options);
} catch (error) {
console.log(`${colors.red}Fatal error: ${error.message}${colors.reset}`);
process.exit(1);
}
}
// Run if called directly
if (require.main === module) {
main();
}
module.exports = DecoderTester;