@@ -48,6 +48,14 @@ describe('ErrorHandler', () => {
4848 const wrapped = ErrorHandler . wrap ( fn )
4949 expect ( ( ) => wrapped ( 'test' ) ) . toThrow ( 'Unknown error occurred' )
5050 } )
51+
52+ it ( 'should create SecureJWTError with error message for Error objects' , ( ) => {
53+ const fn = jest . fn ( ) . mockImplementation ( ( ) => {
54+ throw new Error ( 'Custom error message' )
55+ } )
56+ const wrapped = ErrorHandler . wrap ( fn )
57+ expect ( ( ) => wrapped ( 'test' ) ) . toThrow ( 'Custom error message' )
58+ } )
5159 } )
5260
5361 describe ( 'validateData' , ( ) => {
@@ -454,10 +462,8 @@ describe('ErrorHandler', () => {
454462 expect ( result ) . toBe ( 'test' )
455463 } )
456464
457- it ( 'should not throw for invalid base64 (Buffer.from handles it)' , ( ) => {
458- // Buffer.from doesn't throw for invalid base64, it just produces garbage
459- const result = ErrorHandler . validateBase64Decode ( 'invalid base64!' , 'Invalid base64' )
460- expect ( typeof result ) . toBe ( 'string' )
465+ it ( 'should throw ValidationError for invalid base64' , ( ) => {
466+ expect ( ( ) => ErrorHandler . validateBase64Decode ( 'invalid base64!' , 'Invalid base64' ) ) . toThrow ( ValidationError )
461467 } )
462468 } )
463469
@@ -619,10 +625,8 @@ describe('ErrorHandler', () => {
619625 expect ( result ) . toBe ( original )
620626 } )
621627
622- it ( 'should not throw for invalid base64 (Buffer.from handles it)' , ( ) => {
623- // Buffer.from doesn't throw for invalid base64, it just produces garbage
624- const result = ErrorHandler . validateBase64Decode ( 'invalid base64!' , 'Invalid base64' )
625- expect ( typeof result ) . toBe ( 'string' )
628+ it ( 'should throw ValidationError for invalid base64' , ( ) => {
629+ expect ( ( ) => ErrorHandler . validateBase64Decode ( 'invalid base64!' , 'Invalid base64' ) ) . toThrow ( ValidationError )
626630 } )
627631
628632 it ( 'should throw ValidationError when Buffer.from throws' , ( ) => {
@@ -1229,4 +1233,97 @@ describe('ErrorHandler', () => {
12291233 }
12301234 } )
12311235 } )
1236+
1237+ describe ( 'validateAlgorithm' , ( ) => {
1238+ it ( 'should not throw for valid algorithms' , ( ) => {
1239+ expect ( ( ) => ErrorHandler . validateAlgorithm ( 'aes-256-gcm' ) ) . not . toThrow ( )
1240+ expect ( ( ) => ErrorHandler . validateAlgorithm ( 'chacha20-poly1305' ) ) . not . toThrow ( )
1241+ } )
1242+
1243+ it ( 'should throw ValidationError for non-string algorithm' , ( ) => {
1244+ expect ( ( ) => ErrorHandler . validateAlgorithm ( 123 as any ) ) . toThrow ( ValidationError )
1245+ expect ( ( ) => ErrorHandler . validateAlgorithm ( { } as any ) ) . toThrow ( ValidationError )
1246+ } )
1247+
1248+ it ( 'should throw ValidationError for empty string algorithm' , ( ) => {
1249+ expect ( ( ) => ErrorHandler . validateAlgorithm ( '' ) ) . toThrow ( ValidationError )
1250+ } )
1251+
1252+ it ( 'should throw ValidationError for invalid algorithm' , ( ) => {
1253+ expect ( ( ) => ErrorHandler . validateAlgorithm ( 'invalid-algo' ) ) . toThrow ( ValidationError )
1254+ expect ( ( ) => ErrorHandler . validateAlgorithm ( 'aes-128-gcm' ) ) . toThrow ( ValidationError )
1255+ } )
1256+ } )
1257+
1258+ describe ( 'validateExpiration' , ( ) => {
1259+ it ( 'should not throw for valid expiration' , ( ) => {
1260+ const now = Math . floor ( Date . now ( ) / 1000 )
1261+ const future = now + 3600
1262+ expect ( ( ) => ErrorHandler . validateExpiration ( future , now + 7200 ) ) . not . toThrow ( )
1263+ } )
1264+
1265+ it ( 'should throw ValidationError for expiration too far in future' , ( ) => {
1266+ const now = Math . floor ( Date . now ( ) / 1000 )
1267+ const future = now + 3600
1268+ expect ( ( ) => ErrorHandler . validateExpiration ( future , now + 1800 ) ) . toThrow ( ValidationError )
1269+ } )
1270+ } )
1271+
1272+ describe ( 'validateVersionCompatibility' , ( ) => {
1273+ it ( 'should not throw for matching versions' , ( ) => {
1274+ expect ( ( ) => ErrorHandler . validateVersionCompatibility ( '1.0.0' , '1.0.0' ) ) . not . toThrow ( )
1275+ } )
1276+
1277+ it ( 'should throw VersionMismatchError for different versions' , ( ) => {
1278+ expect ( ( ) => ErrorHandler . validateVersionCompatibility ( '1.0.0' , '2.0.0' ) ) . toThrow ( VersionMismatchError )
1279+ } )
1280+
1281+ it ( 'should throw VersionMismatchError for downgrade attack' , ( ) => {
1282+ expect ( ( ) => ErrorHandler . validateVersionCompatibility ( '1.0.0' , '2.0.0' ) ) . toThrow ( VersionMismatchError )
1283+ } )
1284+
1285+ it ( 'should throw VersionMismatchError for upgrade not supported' , ( ) => {
1286+ expect ( ( ) => ErrorHandler . validateVersionCompatibility ( '2.0.0' , '1.0.0' ) ) . toThrow ( VersionMismatchError )
1287+ } )
1288+ } )
1289+
1290+ describe ( 'validateTokenTimestamps' , ( ) => {
1291+ it ( 'should not throw for matching timestamps' , ( ) => {
1292+ const now = Math . floor ( Date . now ( ) / 1000 )
1293+ expect ( ( ) => ErrorHandler . validateTokenTimestamps ( now + 3600 , now + 3600 , now , now ) ) . not . toThrow ( )
1294+ } )
1295+
1296+ it ( 'should throw ValidationError for mismatched timestamps' , ( ) => {
1297+ const now = Math . floor ( Date . now ( ) / 1000 )
1298+ expect ( ( ) => ErrorHandler . validateTokenTimestamps ( now + 3600 , now + 1800 , now , now ) ) . toThrow ( ValidationError )
1299+ expect ( ( ) => ErrorHandler . validateTokenTimestamps ( now + 3600 , now + 3600 , now , now + 1 ) ) . toThrow ( ValidationError )
1300+ } )
1301+ } )
1302+
1303+ describe ( 'validateJSONParse' , ( ) => {
1304+ it ( 'should parse valid JSON' , ( ) => {
1305+ const result = ErrorHandler . validateJSONParse ( '{"test": "value"}' , 'Error' )
1306+ expect ( result ) . toEqual ( { test : 'value' } )
1307+ } )
1308+
1309+ it ( 'should throw ValidationError for invalid JSON' , ( ) => {
1310+ expect ( ( ) => ErrorHandler . validateJSONParse ( 'invalid json' , 'Custom error' ) ) . toThrow ( ValidationError )
1311+ } )
1312+ } )
1313+
1314+ describe ( 'validateBase64Decode' , ( ) => {
1315+ it ( 'should decode valid base64' , ( ) => {
1316+ const encoded = Buffer . from ( 'test' ) . toString ( 'base64' )
1317+ const result = ErrorHandler . validateBase64Decode ( encoded , 'Error' )
1318+ expect ( result ) . toBe ( 'test' )
1319+ } )
1320+
1321+ it ( 'should throw ValidationError for invalid base64' , ( ) => {
1322+ expect ( ( ) => ErrorHandler . validateBase64Decode ( 'invalid base64!' , 'Custom error' ) ) . toThrow ( ValidationError )
1323+ } )
1324+
1325+ it ( 'should throw ValidationError for malformed base64' , ( ) => {
1326+ expect ( ( ) => ErrorHandler . validateBase64Decode ( 'invalid@#$%' , 'Custom error' ) ) . toThrow ( ValidationError )
1327+ } )
1328+ } )
12321329} )
0 commit comments