-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfigSchema.test.ts
More file actions
1490 lines (1372 loc) · 39.4 KB
/
Copy pathconfigSchema.test.ts
File metadata and controls
1490 lines (1372 loc) · 39.4 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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Unit Tests for Config Schema Validation
*
* Tests the configSchema module functionality including:
* - Empty string handling for optional fields
* - Required field validation
* - Malicious value rejection
* - Length constraints (minLength, maxLength)
* - Format validation (email, hex)
* - Schema validation edge cases
*/
import * as assert from "node:assert";
import {
validateIdentitySchema,
validateIdentitiesSchema,
getSchemaDocumentation,
IDENTITY_SCHEMA,
} from "../identity/configSchema";
/**
* Test empty string handling for required fields
*
* SECURITY: Required fields (id, name, email) must NOT accept empty strings.
* Empty string should be treated as "missing" and trigger a required field error.
*/
function testRequiredFieldsEmptyString(): void {
console.log("Testing required fields with empty string...");
// Test 1: Empty string for 'id' should fail
{
const identity = {
id: "",
name: "Test User",
email: "test@example.com",
};
const result = validateIdentitySchema(identity);
assert.strictEqual(result.valid, false, "Empty id should fail validation");
assert.ok(
result.errors.some(
(e) => e.field === "id" && e.message.includes("Required"),
),
"Error should indicate id is required",
);
}
// Test 2: Empty string for 'name' should fail
{
const identity = {
id: "test",
name: "",
email: "test@example.com",
};
const result = validateIdentitySchema(identity);
assert.strictEqual(
result.valid,
false,
"Empty name should fail validation",
);
assert.ok(
result.errors.some(
(e) => e.field === "name" && e.message.includes("Required"),
),
"Error should indicate name is required",
);
}
// Test 3: Empty string for 'email' should fail
{
const identity = {
id: "test",
name: "Test User",
email: "",
};
const result = validateIdentitySchema(identity);
assert.strictEqual(
result.valid,
false,
"Empty email should fail validation",
);
assert.ok(
result.errors.some(
(e) => e.field === "email" && e.message.includes("Required"),
),
"Error should indicate email is required",
);
}
// Test 4: All required fields empty should fail with multiple errors
{
const identity = {
id: "",
name: "",
email: "",
};
const result = validateIdentitySchema(identity);
assert.strictEqual(
result.valid,
false,
"All empty required fields should fail",
);
assert.strictEqual(
result.errors.length,
3,
"Should have 3 errors for 3 required fields",
);
}
console.log(" Required fields empty string tests passed!");
}
/**
* Test empty string handling for optional fields
*
* Empty string should be treated as "not set" for optional fields.
* This allows default config with "" values to pass validation without errors.
*/
function testOptionalFieldsEmptyString(): void {
console.log("Testing optional fields with empty string...");
// Test 1: Empty string for 'service' should pass (treated as not set)
{
const identity = {
id: "test",
name: "Test User",
email: "test@example.com",
service: "",
};
const result = validateIdentitySchema(identity);
assert.strictEqual(
result.valid,
true,
"Empty service should pass (treated as not set)",
);
assert.strictEqual(result.errors.length, 0, "No errors for empty service");
}
// Test 2: Empty string for 'sshKeyPath' should pass
{
const identity = {
id: "test",
name: "Test User",
email: "test@example.com",
sshKeyPath: "",
};
const result = validateIdentitySchema(identity);
assert.strictEqual(
result.valid,
true,
"Empty sshKeyPath should pass (treated as not set)",
);
assert.strictEqual(
result.errors.length,
0,
"No errors for empty sshKeyPath",
);
}
// Test 3: Empty string for 'sshHost' should pass
{
const identity = {
id: "test",
name: "Test User",
email: "test@example.com",
sshHost: "",
};
const result = validateIdentitySchema(identity);
assert.strictEqual(
result.valid,
true,
"Empty sshHost should pass (treated as not set)",
);
assert.strictEqual(result.errors.length, 0, "No errors for empty sshHost");
}
// Test 4: Empty string for 'gpgKeyId' should pass
{
const identity = {
id: "test",
name: "Test User",
email: "test@example.com",
gpgKeyId: "",
};
const result = validateIdentitySchema(identity);
assert.strictEqual(
result.valid,
true,
"Empty gpgKeyId should pass (treated as not set)",
);
assert.strictEqual(result.errors.length, 0, "No errors for empty gpgKeyId");
}
// Test 5: Empty string for 'description' should pass
{
const identity = {
id: "test",
name: "Test User",
email: "test@example.com",
description: "",
};
const result = validateIdentitySchema(identity);
assert.strictEqual(
result.valid,
true,
"Empty description should pass (treated as not set)",
);
assert.strictEqual(
result.errors.length,
0,
"No errors for empty description",
);
}
// Test 6: Empty string for 'icon' should pass
{
const identity = {
id: "test",
name: "Test User",
email: "test@example.com",
icon: "",
};
const result = validateIdentitySchema(identity);
assert.strictEqual(
result.valid,
true,
"Empty icon should pass (treated as not set)",
);
assert.strictEqual(result.errors.length, 0, "No errors for empty icon");
}
// Test 7: All optional fields empty should pass (default config scenario)
{
const identity = {
id: "example",
name: "Example User",
email: "example@example.com",
service: "",
sshKeyPath: "",
sshHost: "",
gpgKeyId: "",
description: "",
icon: "",
};
const result = validateIdentitySchema(identity);
assert.strictEqual(
result.valid,
true,
"All empty optional fields should pass",
);
assert.strictEqual(
result.errors.length,
0,
"No errors when all optional fields are empty",
);
}
console.log(" Optional fields empty string tests passed!");
}
/**
* Test malicious value rejection
*
* SECURITY: Malicious values (command injection, etc.) must be blocked
* even when the empty string check is in place.
*/
function testMaliciousValueRejection(): void {
console.log("Testing malicious value rejection...");
// Test 1: Command injection in sshKeyPath should fail
{
const identity = {
id: "test",
name: "Test User",
email: "test@example.com",
sshKeyPath: "$(rm -rf /)",
};
const result = validateIdentitySchema(identity);
assert.strictEqual(
result.valid,
false,
"Command injection in sshKeyPath should fail",
);
assert.ok(
result.errors.some((e) => e.field === "sshKeyPath"),
"Error should mention sshKeyPath field",
);
}
// Test 2: Backtick injection in name should fail
{
const identity = {
id: "test",
name: "test`id`",
email: "test@example.com",
};
const result = validateIdentitySchema(identity);
assert.strictEqual(
result.valid,
false,
"Backtick injection in name should fail",
);
assert.ok(
result.errors.some((e) => e.field === "name"),
"Error should mention name field",
);
}
// Test 3: Pipe command in name should fail
{
const identity = {
id: "test",
name: "Test | cat /etc/passwd",
email: "test@example.com",
};
const result = validateIdentitySchema(identity);
assert.strictEqual(result.valid, false, "Pipe command in name should fail");
}
// Test 4: Command substitution in service should fail
{
const identity = {
id: "test",
name: "Test User",
email: "test@example.com",
service: "$(whoami)",
};
const result = validateIdentitySchema(identity);
assert.strictEqual(
result.valid,
false,
"Command substitution in service should fail",
);
}
// Test 5: Ampersand in description should PASS (display-only field, never passed to shell)
// Security analysis: description is only used for UI display, not shell commands
// Therefore, shell metacharacters like & are safe and allowed for usability (e.g., "AT&T")
{
const identity = {
id: "test",
name: "Test User",
email: "test@example.com",
description: "Test & description with ampersand",
};
const result = validateIdentitySchema(identity);
assert.strictEqual(
result.valid,
true,
"Ampersand in description should pass (display-only field)",
);
}
// Test 6: Path traversal in sshKeyPath
// Note: configSchema.ts pattern does NOT detect path traversal (..)
// Path traversal detection is done in validation.ts (validateSshKeyPath)
// This is by design: configSchema handles format validation, validation.ts handles security
{
const identity = {
id: "test",
name: "Test User",
email: "test@example.com",
sshKeyPath: "/home/../etc/passwd",
};
const result = validateIdentitySchema(identity);
// configSchema.ts allows this because pattern only checks for dangerous chars
// The path traversal check is in validation.ts
assert.strictEqual(
result.valid,
true,
"configSchema does not check path traversal (validation.ts does)",
);
}
// Test 7: Relative path in sshKeyPath should fail
{
const identity = {
id: "test",
name: "Test User",
email: "test@example.com",
sshKeyPath: "relative/path/key",
};
const result = validateIdentitySchema(identity);
assert.strictEqual(
result.valid,
false,
"Relative path in sshKeyPath should fail",
);
}
// Test 7.1: Windows absolute path in sshKeyPath should fail (use ~/.ssh/ format)
{
const identity = {
id: "test",
name: "Test User",
email: "test@example.com",
sshKeyPath: String.raw`C:\Users\test\.ssh\id_rsa`,
};
const result = validateIdentitySchema(identity);
assert.strictEqual(
result.valid,
false,
"Windows absolute path in sshKeyPath should fail",
);
}
// Test 7.2: Windows lowercase drive letter should fail (use ~/.ssh/ format)
{
const identity = {
id: "test",
name: "Test User",
email: "test@example.com",
sshKeyPath: String.raw`d:\keys\work_key`,
};
const result = validateIdentitySchema(identity);
assert.strictEqual(
result.valid,
false,
"Windows lowercase drive letter should fail",
);
}
// Test 7.3: Windows forward-slash path should fail (use ~/.ssh/ format)
{
const identity = {
id: "test",
name: "Test User",
email: "test@example.com",
sshKeyPath: "C:/Users/test/.ssh/id_rsa",
};
const result = validateIdentitySchema(identity);
assert.strictEqual(
result.valid,
false,
"Windows forward-slash path in sshKeyPath should fail",
);
}
// Test 8: Invalid sshHost format should fail
{
const identity = {
id: "test",
name: "Test User",
email: "test@example.com",
sshHost: "-invalid-start",
};
const result = validateIdentitySchema(identity);
assert.strictEqual(
result.valid,
false,
"Invalid sshHost format should fail",
);
}
// Test 9: Non-hex gpgKeyId should fail
{
const identity = {
id: "test",
name: "Test User",
email: "test@example.com",
gpgKeyId: "not-hex-value",
};
const result = validateIdentitySchema(identity);
assert.strictEqual(result.valid, false, "Non-hex gpgKeyId should fail");
}
console.log(" Malicious value rejection tests passed!");
}
/**
* Test whitespace handling
*
* Whitespace-only values pass pattern validation but may be treated differently.
* This behavior is outside current scope but worth documenting via tests.
*/
function testWhitespaceHandling(): void {
console.log("Testing whitespace handling...");
// Test 1: Whitespace-only service (passes pattern, may be UX issue but not security)
{
const identity = {
id: "test",
name: "Test User",
email: "test@example.com",
service: " ".repeat(3),
};
const result = validateIdentitySchema(identity);
// Current behavior: whitespace passes pattern validation
// This is noted as potential UX improvement in PRD but not a security issue
assert.strictEqual(
result.valid,
true,
"Whitespace-only service passes current pattern",
);
}
// Test 2: Whitespace-only id should fail (still needs to match pattern)
{
const identity = {
id: " ".repeat(3),
name: "Test User",
email: "test@example.com",
};
const result = validateIdentitySchema(identity);
assert.strictEqual(
result.valid,
false,
"Whitespace-only id should fail pattern check",
);
}
console.log(" Whitespace handling tests passed!");
}
/**
* Test validateIdentitySchema edge cases
*/
function testValidateIdentitySchemaEdgeCases(): void {
console.log("Testing validateIdentitySchema edge cases...");
// Test 1: null input should fail
{
const result = validateIdentitySchema(null);
assert.strictEqual(result.valid, false, "null input should fail");
assert.ok(
result.errors.some((e) => e.field === "root"),
"Error should mention root for null input",
);
}
// Test 2: undefined input should fail
{
const result = validateIdentitySchema(undefined);
assert.strictEqual(result.valid, false, "undefined input should fail");
}
// Test 3: Array input should fail
{
const result = validateIdentitySchema([]);
assert.strictEqual(result.valid, false, "Array input should fail");
}
// Test 4: Primitive input should fail
{
const result = validateIdentitySchema("string");
assert.strictEqual(result.valid, false, "String input should fail");
}
// Test 5: Unknown field should be flagged
{
const identity = {
id: "test",
name: "Test User",
email: "test@example.com",
unknownField: "value",
};
const result = validateIdentitySchema(identity);
assert.strictEqual(result.valid, false, "Unknown field should fail");
assert.ok(
result.errors.some(
(e) => e.field === "unknownField" && e.message.includes("Unknown"),
),
"Error should mention unknown field",
);
}
// Test 6: Too many fields should fail (DoS protection)
{
const identity: Record<string, unknown> = {
id: "test",
name: "Test User",
email: "test@example.com",
};
// Add 101 unknown fields
for (let i = 0; i < 101; i++) {
identity[`field${i}`] = "value";
}
const result = validateIdentitySchema(identity);
assert.strictEqual(result.valid, false, "Too many fields should fail");
assert.ok(
result.errors.some((e) => e.message.includes("too many fields")),
"Error should mention too many fields",
);
}
// Test 7: Valid identity with all fields should pass
{
const identity = {
id: "work",
icon: "👔",
name: "John Doe",
service: "GitHub",
email: "john@company.com",
description: "Work account",
sshKeyPath: "~/.ssh/id_work",
sshHost: "github.com-work",
gpgKeyId: "ABCD1234",
};
const result = validateIdentitySchema(identity);
assert.strictEqual(
result.valid,
true,
"Valid identity with all fields should pass",
);
assert.strictEqual(result.errors.length, 0, "No errors for valid identity");
}
// Test 8: null value in optional field should be treated as not set
{
const identity = {
id: "test",
name: "Test User",
email: "test@example.com",
service: null,
};
const result = validateIdentitySchema(identity);
assert.strictEqual(
result.valid,
true,
"null in optional field should pass (treated as not set)",
);
}
// Test 9: undefined value in optional field should be treated as not set
{
const identity = {
id: "test",
name: "Test User",
email: "test@example.com",
service: undefined,
};
const result = validateIdentitySchema(identity);
assert.strictEqual(
result.valid,
true,
"undefined in optional field should pass (treated as not set)",
);
}
// Test 10: null value in required field should fail
{
const identity = {
id: "test",
name: null,
email: "test@example.com",
};
const result = validateIdentitySchema(identity);
assert.strictEqual(
result.valid,
false,
"null in required field should fail",
);
assert.ok(
result.errors.some(
(e) => e.field === "name" && e.message.includes("Required"),
),
"Error should indicate name is required",
);
}
console.log(" validateIdentitySchema edge cases tests passed!");
}
/**
* Test validateIdentitiesSchema
*/
function testValidateIdentitiesSchema(): void {
console.log("Testing validateIdentitiesSchema...");
// Test 1: Empty array should pass
{
const result = validateIdentitiesSchema([]);
assert.strictEqual(result.valid, true, "Empty array should pass");
}
// Test 2: Non-array input should fail
{
const result = validateIdentitiesSchema({});
assert.strictEqual(result.valid, false, "Non-array input should fail");
}
// Test 3: Array with valid identities should pass
{
const identities = [
{ id: "personal", name: "John", email: "john@example.com" },
{ id: "work", name: "John Work", email: "john@company.com" },
];
const result = validateIdentitiesSchema(identities);
assert.strictEqual(
result.valid,
true,
"Valid identities array should pass",
);
}
// Test 4: Duplicate IDs should fail
{
const identities = [
{ id: "same", name: "John", email: "john@example.com" },
{ id: "same", name: "Jane", email: "jane@example.com" },
];
const result = validateIdentitiesSchema(identities);
assert.strictEqual(result.valid, false, "Duplicate IDs should fail");
assert.ok(
result.errors.some((e) => e.message.includes("Duplicate")),
"Error should mention duplicate",
);
}
// Test 5: Array with one invalid identity should fail
{
const identities = [
{ id: "valid", name: "John", email: "john@example.com" },
{ id: "invalid", name: "Test$(rm -rf /)", email: "test@example.com" },
];
const result = validateIdentitiesSchema(identities);
assert.strictEqual(
result.valid,
false,
"Array with invalid identity should fail",
);
}
// Test 6: Array with empty string in optional field should pass
{
const identities = [
{
id: "test",
name: "Test User",
email: "test@example.com",
service: "",
sshKeyPath: "",
},
];
const result = validateIdentitiesSchema(identities);
assert.strictEqual(
result.valid,
true,
"Array with empty optional fields should pass",
);
}
// Test 7: Array with non-object elements should fail (covers extractIdentityId edge case)
{
const identities = [
"not-an-object",
42,
null,
{ id: "valid", name: "Valid User", email: "valid@example.com" },
];
const result = validateIdentitiesSchema(identities);
assert.strictEqual(
result.valid,
false,
"Array with non-object elements should fail",
);
}
// Test 8: Array with identity missing id (covers checkDuplicateIds undefined-id path)
{
const identities = [
{ name: "No ID User", email: "noid@example.com" },
{ id: "valid", name: "Valid User", email: "valid@example.com" },
];
const result = validateIdentitiesSchema(identities);
// Should fail because first identity is missing required 'id'
assert.strictEqual(result.valid, false, "Identity missing id should fail");
// But should not crash in duplicate ID detection
assert.ok(
result.errors.some((e) => e.message.includes("Required")),
"Error should indicate required field is missing",
);
}
console.log(" validateIdentitiesSchema tests passed!");
}
/**
* Test getSchemaDocumentation
*/
function testGetSchemaDocumentation(): void {
console.log("Testing getSchemaDocumentation...");
const doc = getSchemaDocumentation();
// Test 1: Should return a string
{
assert.strictEqual(typeof doc, "string", "Should return a string");
}
// Test 2: Should include all field names
{
const fieldNames = Object.keys(IDENTITY_SCHEMA);
for (const field of fieldNames) {
assert.ok(
doc.includes(field),
`Documentation should include field: ${field}`,
);
}
}
// Test 3: Should indicate required fields
{
assert.ok(
doc.includes("(required)"),
"Documentation should indicate required fields",
);
}
// Test 4: Should include type information
{
assert.ok(
doc.includes("string"),
"Documentation should include type information",
);
}
console.log(" getSchemaDocumentation tests passed!");
}
/**
* Test IDENTITY_SCHEMA structure
*/
function testIdentitySchemaStructure(): void {
console.log("Testing IDENTITY_SCHEMA structure...");
// Test 1: Required fields should be marked
{
const requiredFields = ["id", "name", "email"];
for (const field of requiredFields) {
assert.ok(IDENTITY_SCHEMA[field], `Schema should have field: ${field}`);
assert.strictEqual(
IDENTITY_SCHEMA[field].required,
true,
`${field} should be marked as required`,
);
}
}
// Test 2: Optional fields should not be marked as required
{
const optionalFields = [
"icon",
"service",
"description",
"sshKeyPath",
"sshHost",
"gpgKeyId",
];
for (const field of optionalFields) {
assert.ok(IDENTITY_SCHEMA[field], `Schema should have field: ${field}`);
assert.ok(
!IDENTITY_SCHEMA[field].required,
`${field} should not be marked as required`,
);
}
}
// Test 3: All fields should have type and description
{
for (const [field, schema] of Object.entries(IDENTITY_SCHEMA)) {
assert.ok(schema.type, `${field} should have type`);
assert.ok(schema.description, `${field} should have description`);
}
}
// Test 4: Fields with patterns should have valid regex
{
for (const [field, schema] of Object.entries(IDENTITY_SCHEMA)) {
if (schema.pattern) {
assert.doesNotThrow(
() => new RegExp(schema.pattern as string),
`${field} pattern should be valid regex`,
);
}
}
}
// Test 5: Fields with patterns should have pre-compiled RegExp
{
for (const [field, schema] of Object.entries(IDENTITY_SCHEMA)) {
if (schema.pattern) {
assert.ok(
schema.compiledPattern instanceof RegExp,
`${field} should have compiledPattern as RegExp`,
);
assert.strictEqual(
schema.compiledPattern?.source,
new RegExp(schema.pattern).source,
`${field} compiledPattern should match pattern source`,
);
}
}
}
// Test 6: Expected fields must have pattern defined
{
const fieldsRequiringPattern = [
"id",
"name",
"service",
"description",
"sshKeyPath",
"sshHost",
"icon",
];
for (const field of fieldsRequiringPattern) {
assert.ok(
IDENTITY_SCHEMA[field].pattern,
`${field} must have a pattern defined for input validation`,
);
}
}
// Test 7: Fields without pattern should not accidentally gain one
{
const fieldsWithoutPattern = ["email", "gpgKeyId"];
for (const field of fieldsWithoutPattern) {
assert.strictEqual(
IDENTITY_SCHEMA[field].pattern,
undefined,
`${field} should use format-based validation, not pattern`,
);
}
}
// Test 8: name pattern should allow semicolons (e.g., "Null;Variant")
{
const namePattern = new RegExp(IDENTITY_SCHEMA["name"].pattern!);
assert.ok(
namePattern.test("Null;Variant"),
"name pattern should allow semicolons for names like Null;Variant",
);
}
// Test 9: name pattern should block shell metacharacters
{
const namePattern = new RegExp(IDENTITY_SCHEMA["name"].pattern!);
const blockedChars = ["`", "$", "(", ")", "{", "}", "|", "&", "<", ">"];
for (const char of blockedChars) {
assert.strictEqual(
namePattern.test(`Test${char}Name`),
false,
`name pattern should block "${char}"`,
);
}
}
console.log(" IDENTITY_SCHEMA structure tests passed!");
}
/**
* Test length constraints (minLength, maxLength)
*/
function testLengthConstraints(): void {
console.log("Testing length constraints...");
// Test 1: gpgKeyId too short (minLength: 8) should fail
{
const identity = {
id: "test",
name: "Test User",
email: "test@example.com",
gpgKeyId: "ABCD", // Only 4 chars, needs 8
};
const result = validateIdentitySchema(identity);
assert.strictEqual(result.valid, false, "gpgKeyId too short should fail");
assert.ok(
result.errors.some(
(e) => e.field === "gpgKeyId" && e.message.includes("at least"),
),
"Error should mention minimum length",
);
}
// Test 2: gpgKeyId at minimum length (8) should pass
{
const identity = {
id: "test",
name: "Test User",
email: "test@example.com",
gpgKeyId: "ABCD1234", // Exactly 8 chars
};
const result = validateIdentitySchema(identity);
assert.strictEqual(
result.valid,
true,
"gpgKeyId at minimum length should pass",
);
}
// Test 3: gpgKeyId too long (maxLength: 40) should fail
{
const identity = {
id: "test",
name: "Test User",
email: "test@example.com",
gpgKeyId: "A".repeat(41), // 41 chars, max is 40
};
const result = validateIdentitySchema(identity);
assert.strictEqual(result.valid, false, "gpgKeyId too long should fail");