@@ -11,9 +11,8 @@ import * as fs from 'fs';
11
11
12
12
import MemoryMap from 'nrf-intel-hex' ;
13
13
14
- import { bytesToStr , strToBytes } from '../common' ;
14
+ import { strToBytes } from '../common' ;
15
15
import {
16
- addIntelHexFile ,
17
16
addIntelHexFiles ,
18
17
calculateFileSize ,
19
18
getIntelHexFiles ,
@@ -286,12 +285,10 @@ describe('Writing files to the filesystem.', () => {
286
285
} ,
287
286
} ;
288
287
289
- it ( 'Can generate a full chunk that also uses the next one.' , ( ) => {
290
- const fwWithFsOther = addIntelHexFile (
291
- uPyHexFile ,
292
- fullChunkPlus . fileName ,
293
- strToBytes ( fullChunkPlus . fileStr )
294
- ) ;
288
+ it ( 'Can generate a full chunk that also consumes the next one.' , ( ) => {
289
+ const fwWithFsOther = addIntelHexFiles ( uPyHexFile , {
290
+ [ fullChunkPlus . fileName ] : strToBytes ( fullChunkPlus . fileStr ) ,
291
+ } ) ;
295
292
296
293
const opMap = MemoryMap . fromHex ( fwWithFsOther ) ;
297
294
const readFileData = opMap
@@ -301,11 +298,9 @@ describe('Writing files to the filesystem.', () => {
301
298
} ) ;
302
299
303
300
it ( 'Correctly generate an almost full chunk (not using last byte).' , ( ) => {
304
- const fwWithFsOther = addIntelHexFile (
305
- uPyHexFile ,
306
- fullChunkMinus . fileName ,
307
- strToBytes ( fullChunkMinus . fileStr )
308
- ) ;
301
+ const fwWithFsOther = addIntelHexFiles ( uPyHexFile , {
302
+ [ fullChunkMinus . fileName ] : strToBytes ( fullChunkMinus . fileStr ) ,
303
+ } ) ;
309
304
310
305
const opMap = MemoryMap . fromHex ( fwWithFsOther ) ;
311
306
const readFileData = opMap
@@ -314,12 +309,10 @@ describe('Writing files to the filesystem.', () => {
314
309
expect ( readFileData ) . toEqual ( fullChunkMinus . bytes ( ) ) ;
315
310
} ) ;
316
311
317
- it ( 'Correctlty generate just over a full chunk.' , ( ) => {
318
- const fwWithFsOther = addIntelHexFile (
319
- uPyHexFile ,
320
- twoChunks . fileName ,
321
- strToBytes ( twoChunks . fileStr )
322
- ) ;
312
+ it ( 'Correctly generate just over a full chunk.' , ( ) => {
313
+ const fwWithFsOther = addIntelHexFiles ( uPyHexFile , {
314
+ [ twoChunks . fileName ] : strToBytes ( twoChunks . fileStr ) ,
315
+ } ) ;
323
316
324
317
const opMap = MemoryMap . fromHex ( fwWithFsOther ) ;
325
318
const readFileData = opMap
@@ -329,31 +322,25 @@ describe('Writing files to the filesystem.', () => {
329
322
} ) ;
330
323
331
324
it ( 'Empty file name throws an error.' , ( ) => {
332
- const failCase = ( ) => addIntelHexFile ( uPyHexFile , '' , randContent ) ;
325
+ const failCase = ( ) => addIntelHexFiles ( uPyHexFile , { '' : randContent } ) ;
333
326
334
- expect ( failCase ) . toThrow ( Error ) ;
327
+ expect ( failCase ) . toThrow ( 'File has to have a file name' ) ;
335
328
} ) ;
336
329
337
330
it ( 'Empty file data throw an error.' , ( ) => {
338
- const failCase = ( ) => {
339
- const hexWithFs = addIntelHexFile (
340
- uPyHexFile ,
341
- 'my_file.txt' ,
342
- new Uint8Array ( 0 )
343
- ) ;
344
- } ;
345
- expect ( failCase ) . toThrow ( Error ) ;
331
+ const failCase = ( ) =>
332
+ addIntelHexFiles ( uPyHexFile , { 'my_file.txt' : new Uint8Array ( 0 ) } ) ;
333
+
334
+ expect ( failCase ) . toThrow ( 'has to contain data' ) ;
346
335
} ) ;
347
336
348
337
it ( 'Large file that does not fit throws error.' , ( ) => {
349
338
const failCase = ( ) => {
350
- const hexWithFs = addIntelHexFile (
351
- uPyHexFile ,
352
- 'my_file.txt' ,
353
- new Uint8Array ( 50 * 1024 ) . fill ( 0x55 )
354
- ) ;
339
+ addIntelHexFiles ( uPyHexFile , {
340
+ 'my_file.txt' : new Uint8Array ( 50 * 1024 ) . fill ( 0x55 ) ,
341
+ } ) ;
355
342
} ;
356
- expect ( failCase ) . toThrow ( Error ) ;
343
+ expect ( failCase ) . toThrow ( 'Not enough space' ) ;
357
344
} ) ;
358
345
359
346
it ( 'Add files until no more fit.' , ( ) => {
@@ -362,36 +349,49 @@ describe('Writing files to the filesystem.', () => {
362
349
// Use 4 KB blocks per file (each chunk is 128 B)
363
350
const fakeBigFileData = new Uint8Array ( 4000 ) . fill ( 0x55 ) ;
364
351
const fakeSingleChunkData = new Uint8Array ( [ 0x55 , 0x55 ] ) ;
352
+
365
353
const addLargeFiles = ( ) => {
366
354
// At 4Kbs we only fit 7 files
367
355
for ( let i = 0 ; i < 15 ; i ++ ) {
368
- hexWithFs = addIntelHexFile (
369
- hexWithFs ,
370
- 'file_' + i + '.txt' ,
371
- fakeBigFileData
372
- ) ;
356
+ hexWithFs = addIntelHexFiles ( hexWithFs , {
357
+ [ 'file_' + i + '.txt' ] : fakeBigFileData ,
358
+ } ) as string ;
373
359
}
374
360
} ;
375
361
const completeFsFilling = ( ) => {
376
362
// At a maximum of 4 Kbs left, it would fit 32 chunks max
377
363
for ( let i = 100 ; i < 132 ; i ++ ) {
378
- hexWithFs = addIntelHexFile (
379
- hexWithFs ,
380
- 'file_' + i + '.txt' ,
381
- fakeSingleChunkData
382
- ) ;
364
+ hexWithFs = addIntelHexFiles ( hexWithFs , {
365
+ [ 'file_' + i + '.txt' ] : fakeSingleChunkData ,
366
+ } ) as string ;
383
367
}
384
368
} ;
385
- expect ( addLargeFiles ) . toThrow ( Error ) ;
386
- expect ( completeFsFilling ) . toThrow ( Error ) ;
369
+
370
+ expect ( addLargeFiles ) . toThrow ( 'Not enough space' ) ;
371
+ expect ( completeFsFilling ) . toThrow ( 'no storage space left' ) ;
372
+ } ) ;
373
+
374
+ it ( 'Add a group of files that do not fit.' , ( ) => {
375
+ // The MicroPython hex has about 29 KBs
376
+ const hexWithFs = uPyHexFile ;
377
+ // Use 4 KB blocks per file (each chunk is 128 B)
378
+ const fakeBigFileData = new Uint8Array ( 4000 ) . fill ( 0x55 ) ;
379
+ const tooManyBigFiles : { [ filename : string ] : Uint8Array } = { } ;
380
+ for ( let i = 0 ; i < 8 ; i ++ ) {
381
+ tooManyBigFiles [ 'file_' + i + '.txt' ] = fakeBigFileData ;
382
+ }
383
+
384
+ const addingAllFiles = ( ) => addIntelHexFiles ( uPyHexFile , tooManyBigFiles ) ;
385
+
386
+ expect ( addingAllFiles ) . toThrow ( 'Not enough space' ) ;
387
387
} ) ;
388
388
389
389
it ( 'Max filename length works.' , ( ) => {
390
390
const maxLength = 120 ;
391
391
const largeName = 'a' . repeat ( maxLength ) ;
392
392
393
393
const workingCase = ( ) =>
394
- addIntelHexFile ( uPyHexFile , largeName , randContent ) ;
394
+ addIntelHexFiles ( uPyHexFile , { [ largeName ] : randContent } ) ;
395
395
396
396
expect ( workingCase ) . not . toThrow ( Error ) ;
397
397
} ) ;
@@ -400,20 +400,20 @@ describe('Writing files to the filesystem.', () => {
400
400
const maxLength = 120 ;
401
401
const largeName = 'a' . repeat ( maxLength + 1 ) ;
402
402
403
- const failCase = ( ) => addIntelHexFile ( uPyHexFile , largeName , randContent ) ;
403
+ const failCase = ( ) =>
404
+ addIntelHexFiles ( uPyHexFile , { [ largeName ] : randContent } ) ;
404
405
405
- expect ( failCase ) . toThrow ( Error ) ;
406
+ expect ( failCase ) . toThrow ( 'File name' ) ;
406
407
} ) ;
407
408
408
409
it ( 'Adding files to non-MicroPython hex fails.' , ( ) => {
409
410
const failCase = ( ) =>
410
- addIntelHexFile ( makecodeHexFile , 'a.py' , randContent ) ;
411
+ addIntelHexFiles ( makecodeHexFile , { 'a.py' : randContent } ) ;
411
412
412
- expect ( failCase ) . toThrow ( Error ) ;
413
+ expect ( failCase ) . toThrow ( 'Could not find valid MicroPython UICR' ) ;
413
414
} ) ;
414
415
415
416
// TODO: Hex file with persistent page marker doesn't get two markers
416
- // TODO: Hex file with injection string (:::...) still works
417
417
} ) ;
418
418
419
419
describe ( 'Reading files from the filesystem.' , ( ) => {
@@ -704,7 +704,7 @@ describe('Reading files from the filesystem.', () => {
704
704
705
705
const failCase = ( ) => getIntelHexFiles ( fullUpyFsMemMap . asHexString ( ) ) ;
706
706
707
- expect ( failCase ) . toThrow ( Error ) ;
707
+ expect ( failCase ) . toThrow ( 'Found multiple files named' ) ;
708
708
} ) ;
709
709
710
710
it ( 'Reading files from empty MicroPython hex returns empty list.' , ( ) => {
@@ -716,7 +716,7 @@ describe('Reading files from the filesystem.', () => {
716
716
it ( 'Reading files from non-MicroPython hex fails.' , ( ) => {
717
717
const failCase = ( ) => getIntelHexFiles ( makecodeHexFile ) ;
718
718
719
- expect ( failCase ) . toThrow ( Error ) ;
719
+ expect ( failCase ) . toThrow ( 'Could not find valid MicroPython UICR' ) ;
720
720
} ) ;
721
721
722
722
// TODO: Read tests with a file that has chunks in non-continuous order
0 commit comments