@@ -78,6 +78,46 @@ describe('dag-cbor', () => {
78
78
assert . throws ( ( ) => encode ( objWithUndefined ) , / \W u n d e f i n e d \W .* n o t s u p p o r t e d / )
79
79
} )
80
80
81
+ test ( 'error on decoding undefined' , ( ) => {
82
+ // encoded forms from the encode() test above
83
+ assert . throws ( ( ) => decode ( bytes . fromHex ( 'f7' ) ) , / \W u n d e f i n e d \W .* n o t s u p p o r t e d / )
84
+ assert . throws ( ( ) => decode ( bytes . fromHex ( 'a2616161616162f7' ) ) , / \W u n d e f i n e d \W .* n o t s u p p o r t e d / )
85
+ } )
86
+
87
+ test ( 'error on encoding IEEE 754 specials' , ( ) => {
88
+ for ( const special of [ NaN , Infinity , - Infinity ] ) {
89
+ assert . throws ( ( ) => encode ( special ) , new RegExp ( `\\W${ String ( special ) } \\W.*not supported` ) )
90
+ const objWithSpecial = { a : 'a' , b : special }
91
+ assert . throws ( ( ) => encode ( objWithSpecial ) , new RegExp ( `\\W${ String ( special ) } \\W.*not supported` ) )
92
+ const arrWithSpecial = [ 1 , 1.1 , - 1 , - 1.1 , Number . MAX_SAFE_INTEGER , special , Number . MIN_SAFE_INTEGER ]
93
+ assert . throws ( ( ) => encode ( arrWithSpecial ) , new RegExp ( `\\W${ String ( special ) } \\W.*not supported` ) )
94
+ }
95
+ } )
96
+
97
+ test ( 'error on decoding IEEE 754 specials' , ( ) => {
98
+ // encoded forms of each of the previous encode() tests
99
+ const cases = [
100
+ [ 'NaN' , 'f97e00' ] ,
101
+ [ 'NaN' , 'f97ff8' ] ,
102
+ [ 'NaN' , 'fa7ff80000' ] ,
103
+ [ 'NaN' , 'fb7ff8000000000000' ] ,
104
+ [ 'NaN' , 'a2616161616162fb7ff8000000000000' ] ,
105
+ [ 'NaN' , '8701fb3ff199999999999a20fbbff199999999999a1b001ffffffffffffffb7ff80000000000003b001ffffffffffffe' ] ,
106
+ [ 'Infinity' , 'f97c00' ] ,
107
+ [ 'Infinity' , 'fb7ff0000000000000' ] ,
108
+ [ 'Infinity' , 'a2616161616162fb7ff0000000000000' ] ,
109
+ [ 'Infinity' , '8701fb3ff199999999999a20fbbff199999999999a1b001ffffffffffffffb7ff00000000000003b001ffffffffffffe' ] ,
110
+ [ '-Infinity' , 'f9fc00' ] ,
111
+ [ '-Infinity' , 'fbfff0000000000000' ] ,
112
+ [ '-Infinity' , 'a2616161616162fbfff0000000000000' ] ,
113
+ [ '-Infinity' , '8701fb3ff199999999999a20fbbff199999999999a1b001ffffffffffffffbfff00000000000003b001ffffffffffffe' ]
114
+ ]
115
+ for ( const [ typ , hex ] of cases ) {
116
+ const byts = bytes . fromHex ( hex )
117
+ assert . throws ( ( ) => decode ( byts ) , new RegExp ( `\\W${ typ . replace ( / ^ - / , '' ) } \\W.*not supported` ) )
118
+ }
119
+ } )
120
+
81
121
test ( 'fuzz serialize and deserialize with garbage' , ( ) => {
82
122
for ( let ii = 0 ; ii < 1000 ; ii ++ ) {
83
123
const original = garbage ( 100 )
0 commit comments