1
1
import type { Host , Port } from 'polykey/dist/network/types' ;
2
2
import ErrorPolykey from 'polykey/dist/ErrorPolykey' ;
3
+ import { test } from '@fast-check/jest' ;
3
4
import * as ids from 'polykey/dist/ids' ;
4
5
import * as nodesUtils from 'polykey/dist/nodes/utils' ;
5
6
import * as polykeyErrors from 'polykey/dist/errors' ;
6
7
import * as fc from 'fast-check' ;
7
8
import * as binUtils from '@/utils/utils' ;
9
+ import * as binParsers from '@/utils/parsers' ;
10
+ import path from 'path' ;
8
11
9
- const nonPrintableCharArb = fc
10
- . oneof (
11
- fc . integer ( { min : 0 , max : 0x1f } ) ,
12
- fc . integer ( { min : 0x7f , max : 0x9f } ) ,
13
- )
14
- . map ( ( code ) => String . fromCharCode ( code ) ) ;
12
+ describe ( 'outputFormatters' , ( ) => {
13
+ const nonPrintableCharArb = fc
14
+ . oneof (
15
+ fc . integer ( { min : 0 , max : 0x1f } ) ,
16
+ fc . integer ( { min : 0x7f , max : 0x9f } ) ,
17
+ )
18
+ . map ( ( code ) => String . fromCharCode ( code ) ) ;
15
19
16
- const stringWithNonPrintableCharsArb = fc . stringOf (
17
- fc . oneof ( fc . char ( ) , nonPrintableCharArb ) ,
18
- ) ;
20
+ const stringWithNonPrintableCharsArb = fc . stringOf (
21
+ fc . oneof ( fc . char ( ) , nonPrintableCharArb ) ,
22
+ ) ;
19
23
20
- describe ( 'bin/utils' , ( ) => {
21
24
test ( 'list in human and json format' , ( ) => {
22
25
// List
23
26
expect (
@@ -164,7 +167,7 @@ describe('bin/utils', () => {
164
167
' key9\tvalue\n' ,
165
168
) ;
166
169
} ) ;
167
- test ( 'outputFormatter should encode non-printable characters within a dict' , ( ) => {
170
+ test ( 'should encode non-printable characters within a dict' , ( ) => {
168
171
fc . assert (
169
172
fc . property (
170
173
stringWithNonPrintableCharsArb ,
@@ -174,15 +177,12 @@ describe('bin/utils', () => {
174
177
type : 'dict' ,
175
178
data : { [ key ] : value } ,
176
179
} ) ;
177
-
178
180
const expectedKey = binUtils . encodeEscapedWrapped ( key ) ;
179
-
180
181
// Construct the expected output
181
182
let expectedValue = value ;
182
183
expectedValue = binUtils . encodeEscapedWrapped ( expectedValue ) ;
183
184
expectedValue = expectedValue . replace ( / (?: \r \n | \n ) $ / , '' ) ;
184
185
expectedValue = expectedValue . replace ( / ( \r \n | \n ) / g, '$1\t' ) ;
185
-
186
186
const maxKeyLength = Math . max (
187
187
...Object . keys ( { [ key ] : value } ) . map ( ( k ) => k . length ) ,
188
188
) ;
@@ -342,3 +342,55 @@ describe('bin/utils', () => {
342
342
) ;
343
343
} ) ;
344
344
} ) ;
345
+
346
+ describe ( 'parsers' , ( ) => {
347
+ const vaultNameArb = fc . stringOf (
348
+ fc . char ( ) . filter ( ( c ) => binParsers . vaultNameRegex . test ( c ) ) ,
349
+ { minLength : 1 , maxLength : 100 } ,
350
+ ) ;
351
+ const singleSecretPathArb = fc . stringOf (
352
+ fc . char ( ) . filter ( ( c ) => binParsers . secretPathRegex . test ( c ) ) ,
353
+ { minLength : 1 , maxLength : 25 } ,
354
+ ) ;
355
+ const secretPathArb = fc
356
+ . array ( singleSecretPathArb , { minLength : 1 , maxLength : 5 } )
357
+ . map ( ( segments ) => path . join ( ...segments ) ) ;
358
+ const valueFirstCharArb = fc . char ( ) . filter ( ( c ) => / ^ [ a - z A - Z _ ] $ / . test ( c ) ) ;
359
+ const valueRestCharArb = fc . stringOf (
360
+ fc . char ( ) . filter ( ( c ) => / ^ [ \w ] $ / . test ( c ) ) ,
361
+ { minLength : 1 , maxLength : 100 } ,
362
+ ) ;
363
+ const valueDataArb = fc
364
+ . tuple ( valueFirstCharArb , valueRestCharArb )
365
+ . map ( ( components ) => components . join ( '' ) ) ;
366
+
367
+ test . prop ( [ vaultNameArb ] , { numRuns : 100 } ) (
368
+ 'should parse vault name' ,
369
+ async ( vaultName ) => {
370
+ expect ( binParsers . parseVaultName ( vaultName ) ) . toEqual ( vaultName ) ;
371
+ } ,
372
+ ) ;
373
+ test . prop ( [ vaultNameArb ] , { numRuns : 10 } ) (
374
+ 'should parse secret path with only vault name' ,
375
+ async ( vaultName ) => {
376
+ const result = [ vaultName , undefined , undefined ] ;
377
+ expect ( binParsers . parseSecretPath ( vaultName ) ) . toEqual ( result ) ;
378
+ } ,
379
+ ) ;
380
+ test . prop ( [ vaultNameArb , secretPathArb ] , { numRuns : 100 } ) (
381
+ 'should parse full secret path with vault name' ,
382
+ async ( vaultName , secretPath ) => {
383
+ const query = `${ vaultName } :${ secretPath } ` ;
384
+ const result = [ vaultName , secretPath , undefined ] ;
385
+ expect ( binParsers . parseSecretPath ( query ) ) . toEqual ( result ) ;
386
+ } ,
387
+ ) ;
388
+ test . prop ( [ vaultNameArb , secretPathArb , valueDataArb ] , { numRuns : 100 } ) (
389
+ 'should parse full secret path with vault name and value' ,
390
+ async ( vaultName , secretPath , valueData ) => {
391
+ const query = `${ vaultName } :${ secretPath } =${ valueData } ` ;
392
+ const result = [ vaultName , secretPath , valueData ] ;
393
+ expect ( binParsers . parseSecretPathValue ( query ) ) . toEqual ( result ) ;
394
+ } ,
395
+ ) ;
396
+ } ) ;
0 commit comments