-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCBORDecoderTests.swift
380 lines (315 loc) · 17 KB
/
CBORDecoderTests.swift
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
//
// CBORDecoderTests.swift
// CBORSwiftTests
//
// Created by Hassan Shahbazi on 5/4/18.
// Copyright © 2018 Hassan Shahbazi. All rights reserved.
//
import XCTest
@testable import CBORSwift
// All tests are verified using http://cbor.me/ tool
class CBORDecoderTests: XCTestCase {
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func test_1_identifyMajor() {
let major = MajorTypes()
var type:[UInt8] = [0x00, 0x00, 0x00]
XCTAssertNotNil(major.identify(type))
XCTAssertEqual(MajorType.major0, major.identify(type))
type = [0x00, 0x00, 0x01]
XCTAssertNotNil(major.identify(type))
XCTAssertEqual(MajorType.major1, major.identify(type))
type = [0x01, 0x00, 0x01]
XCTAssertNotNil(major.identify(type))
XCTAssertEqual(MajorType.major5, major.identify(type))
}
//MARK:- Unsigned integer decoding
func test_2_decodeSimpleInt() {
var decoded = CBOR.decode([0x00])
XCTAssertNotNil(decoded)
XCTAssertEqual(0, (decoded as? NSNumber)?.intValue)
decoded = CBOR.decode([0x05])
XCTAssertNotNil(decoded)
XCTAssertEqual(5, (decoded as? NSNumber)?.intValue)
decoded = CBOR.decode([0x0A])
XCTAssertNotNil(decoded)
XCTAssertEqual(10, (decoded as? NSNumber)?.intValue)
decoded = CBOR.decode([0x10])
XCTAssertNotNil(decoded)
XCTAssertEqual(16, (decoded as? NSNumber)?.intValue)
decoded = CBOR.decode([0x17])
XCTAssertNotNil(decoded)
XCTAssertEqual(23, (decoded as? NSNumber)?.intValue)
}
func test_3_decodeStage1Int() {
var decoded = CBOR.decode([0x18, 0x18])
XCTAssertNotNil(decoded)
XCTAssertEqual(24, (decoded as? NSNumber)?.intValue)
decoded = CBOR.decode([0x18, 0x19])
XCTAssertNotNil(decoded)
XCTAssertEqual(25, (decoded as? NSNumber)?.intValue)
decoded = CBOR.decode([0x18, 0x7D])
XCTAssertNotNil(decoded)
XCTAssertEqual(125, (decoded as? NSNumber)?.intValue)
decoded = CBOR.decode([0x18, 0xB2])
XCTAssertNotNil(decoded)
XCTAssertEqual(178, (decoded as? NSNumber)?.intValue)
decoded = CBOR.decode([0x18, 0xFF])
XCTAssertNotNil(decoded)
XCTAssertEqual(255, (decoded as? NSNumber)?.intValue)
}
func test_4_decodeStage2Int() {
var decoded = CBOR.decode([0x19, 0x01, 0x00])
XCTAssertNotNil(decoded)
XCTAssertEqual(256, (decoded as? NSNumber)?.intValue)
decoded = CBOR.decode([0x19, 0x01, 0x2C])
XCTAssertNotNil(decoded)
XCTAssertEqual(300, (decoded as? NSNumber)?.intValue)
decoded = CBOR.decode([0x19, 0x04, 0xB0])
XCTAssertNotNil(decoded)
XCTAssertEqual(1200, (decoded as? NSNumber)?.intValue)
decoded = CBOR.decode([0x19, 0x0D, 0x64])
XCTAssertNotNil(decoded)
XCTAssertEqual(3428, (decoded as? NSNumber)?.intValue)
decoded = CBOR.decode([0x19, 0xFF, 0xDC])
XCTAssertNotNil(decoded)
XCTAssertEqual(65500, (decoded as? NSNumber)?.intValue)
}
func test_5_decodeStage3Int() {
var decoded = CBOR.decode([0x1A, 0x00, 0x01, 0x00, 0x00])
XCTAssertNotNil(decoded)
XCTAssertEqual(65536, (decoded as? NSNumber)?.intValue)
decoded = CBOR.decode([0x1A, 0x02, 0x8F, 0x5A, 0xAF])
XCTAssertNotNil(decoded)
XCTAssertEqual(42949295, (decoded as? NSNumber)?.intValue)
}
//MARK:- Negative integer decoding
func test_2_decodeSimpleNeg() {
var decoded = CBOR.decode([0x20])
XCTAssertNotNil(decoded)
XCTAssertEqual(-1, (decoded as? NSNumber)?.intValue)
decoded = CBOR.decode([0x26])
XCTAssertNotNil(decoded)
XCTAssertEqual(-7, (decoded as? NSNumber)?.intValue)
decoded = CBOR.decode([0x2F])
XCTAssertNotNil(decoded)
XCTAssertEqual(-16, (decoded as? NSNumber)?.intValue)
decoded = CBOR.decode([0x37])
XCTAssertNotNil(decoded)
XCTAssertEqual(-24, (decoded as? NSNumber)?.intValue)
}
func test_3_decodeStage1Neg() {
var decoded = CBOR.decode([0x38, 0x18])
XCTAssertNotNil(decoded)
XCTAssertEqual(-25, (decoded as? NSNumber)?.intValue)
decoded = CBOR.decode([0x38, 0x5F])
XCTAssertNotNil(decoded)
XCTAssertEqual(-96, (decoded as? NSNumber)?.intValue)
decoded = CBOR.decode([0x38, 0x9F])
XCTAssertNotNil(decoded)
XCTAssertEqual(-160, (decoded as? NSNumber)?.intValue)
decoded = CBOR.decode([0x38, 0xFF])
XCTAssertNotNil(decoded)
XCTAssertEqual(-256, (decoded as? NSNumber)?.intValue)
}
func test_4_decodeStage2Neg() {
var decoded = CBOR.decode([0x39, 0x01, 0x00])
XCTAssertNotNil(decoded)
XCTAssertEqual(-257, (decoded as? NSNumber)?.intValue)
decoded = CBOR.decode([0x39, 0x01, 0x7D])
XCTAssertNotNil(decoded)
XCTAssertEqual(-382, (decoded as? NSNumber)?.intValue)
decoded = CBOR.decode([0x39, 0x0D, 0x63])
XCTAssertNotNil(decoded)
XCTAssertEqual(-3428, (decoded as? NSNumber)?.intValue)
decoded = CBOR.decode([0x39, 0xFF, 0xDB])
XCTAssertNotNil(decoded)
XCTAssertEqual(-65500, (decoded as? NSNumber)?.intValue)
}
func test_5_decodeStage3Neg() {
var decoded = CBOR.decode([0x3A, 0x00, 0x01, 0x00, 0x00])
XCTAssertNotNil(decoded)
XCTAssertEqual(-65537, (decoded as? NSNumber)?.intValue)
decoded = CBOR.decode([0x3A, 0x02, 0x8F, 0x5A, 0xAE])
XCTAssertNotNil(decoded)
XCTAssertEqual(-42949295, (decoded as? NSNumber)?.intValue)
}
//MARK:- Byte string dencoding
func test_2_decodeByteString() {
var decoded = CBOR.decode([0x42, 0x25, 0x25])
XCTAssertNotNil(decoded)
XCTAssertEqual("2525", decoded as? String)
decoded = CBOR.decode([0x44, 0x25, 0x25, 0x30, 0x15])
XCTAssertNotNil(decoded)
XCTAssertEqual("25253015", decoded as? String)
decoded = CBOR.decode([0x48, 0x33, 0x30, 0x38, 0x32, 0x30, 0x31, 0x39, 0x33])
XCTAssertNotNil(decoded)
XCTAssertEqual("3330383230313933", decoded as? String)
}
//MARK:- Text string decoding
func test_2_decodeTextString() {
var decoded = CBOR.decode([0x65, 0x68, 0x65, 0x6C, 0x6C, 0x6F])
XCTAssertNotNil(decoded)
XCTAssertEqual("hello", decoded as? String)
decoded = CBOR.decode([0x78, 0x18, 0x61, 0x20, 0x73, 0x69, 0x6D, 0x70, 0x6C, 0x65, 0x20, 0x73,
0x74, 0x72, 0x69, 0x6E, 0x67, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x6C,
0x65, 0x6E])
XCTAssertNotNil(decoded)
XCTAssertEqual("a simple string with len", decoded as? String)
decoded = CBOR.decode([0x78, 0x19, 0x61, 0x20, 0x73, 0x69, 0x6D, 0x70, 0x6C, 0x65, 0x20, 0x73,
0x74, 0x72, 0x69, 0x6E, 0x67, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x6C,
0x65, 0x6E, 0x32])
XCTAssertNotNil(decoded)
XCTAssertEqual("a simple string with len2", decoded as? String)
decoded = CBOR.decode([0x78, 0x1C, 0x6C, 0x65, 0x74, 0x27, 0x73, 0x20, 0x64, 0x6F,
0x20, 0x61, 0x20, 0x6D, 0x6F, 0x72, 0x65, 0x20, 0x63, 0x6F,
0x6D, 0x70, 0x6C, 0x65, 0x78, 0x20, 0x74, 0x65, 0x73, 0x74])
XCTAssertNotNil(decoded)
XCTAssertEqual("let's do a more complex test", decoded as? String)
decoded = CBOR.decode([0x79, 0x01, 0x51, 0x61, 0x20, 0x73, 0x69, 0x6D, 0x70,
0x6C, 0x65, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6E, 0x67,
0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x61, 0x20, 0x6C,
0x61, 0x72, 0x67, 0x65, 0x20, 0x6C, 0x65, 0x6E, 0x67,
0x74, 0x68, 0x2C, 0x20, 0x74, 0x72, 0x79, 0x69, 0x6E,
0x67, 0x20, 0x74, 0x6F, 0x20, 0x74, 0x65, 0x73, 0x74,
0x20, 0x61, 0x6C, 0x6C, 0x20, 0x70, 0x6F, 0x73, 0x73,
0x69, 0x62, 0x6C, 0x65, 0x20, 0x73, 0x69, 0x74, 0x75,
0x61, 0x74, 0x69, 0x6F, 0x6E, 0x73, 0x2E, 0x20, 0x54,
0x68, 0x75, 0x73, 0x2C, 0x20, 0x49, 0x20, 0x68, 0x61,
0x76, 0x65, 0x20, 0x74, 0x6F, 0x20, 0x64, 0x6F, 0x75,
0x62, 0x6C, 0x65, 0x20, 0x74, 0x68, 0x69, 0x73, 0x20,
0x73, 0x74, 0x72, 0x69, 0x6E, 0x67, 0x2E, 0x20, 0x61,
0x20, 0x73, 0x69, 0x6D, 0x70, 0x6C, 0x65, 0x20, 0x73,
0x74, 0x72, 0x69, 0x6E, 0x67, 0x20, 0x77, 0x69, 0x74,
0x68, 0x20, 0x61, 0x20, 0x6C, 0x61, 0x72, 0x67, 0x65,
0x20, 0x6C, 0x65, 0x6E, 0x67, 0x74, 0x68, 0x2C, 0x20,
0x74, 0x72, 0x79, 0x69, 0x6E, 0x67, 0x20, 0x74, 0x6F,
0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x61, 0x6C, 0x6C,
0x20, 0x70, 0x6F, 0x73, 0x73, 0x69, 0x62, 0x6C, 0x65,
0x20, 0x73, 0x69, 0x74, 0x75, 0x61, 0x74, 0x69, 0x6F,
0x6E, 0x73, 0x2E, 0x20, 0x54, 0x68, 0x75, 0x73, 0x2C,
0x20, 0x49, 0x20, 0x68, 0x61, 0x76, 0x65, 0x20, 0x74,
0x6F, 0x20, 0x64, 0x6F, 0x75, 0x62, 0x6C, 0x65, 0x20,
0x74, 0x68, 0x69, 0x73, 0x20, 0x73, 0x74, 0x72, 0x69,
0x6E, 0x67, 0x2E, 0x20, 0x61, 0x20, 0x73, 0x69, 0x6D,
0x70, 0x6C, 0x65, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6E,
0x67, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x61, 0x20,
0x6C, 0x61, 0x72, 0x67, 0x65, 0x20, 0x6C, 0x65, 0x6E,
0x67, 0x74, 0x68, 0x2C, 0x20, 0x74, 0x72, 0x79, 0x69,
0x6E, 0x67, 0x20, 0x74, 0x6F, 0x20, 0x74, 0x65, 0x73,
0x74, 0x20, 0x61, 0x6C, 0x6C, 0x20, 0x70, 0x6F, 0x73,
0x73, 0x69, 0x62, 0x6C, 0x65, 0x20, 0x73, 0x69, 0x74,
0x75, 0x61, 0x74, 0x69, 0x6F, 0x6E, 0x73, 0x2E, 0x20,
0x54, 0x68, 0x75, 0x73, 0x2C, 0x20, 0x49, 0x20, 0x68,
0x61, 0x76, 0x65, 0x20, 0x74, 0x6F, 0x20, 0x64, 0x6F,
0x75, 0x62, 0x6C, 0x65, 0x20, 0x74, 0x68, 0x69, 0x73,
0x20, 0x73, 0x74, 0x72, 0x69, 0x6E, 0x67])
XCTAssertNotNil(decoded)
XCTAssertEqual("a simple string with a large length, trying to test all possible situations. Thus, I have to double this string. a simple string with a large length, trying to test all possible situations. Thus, I have to double this string. a simple string with a large length, trying to test all possible situations. Thus, I have to double this string", decoded as? String)
}
//MARK:- Array decoding
func test_2_decodeStringArray() {
var decoded = CBOR.decode([0x81, 0x65, 0x68, 0x65, 0x6C, 0x6C, 0x6F])
XCTAssertNotNil(decoded)
XCTAssertEqual(["hello"], decoded as? NSArray)
decoded = CBOR.decode([0x83, 0x65, 0x68, 0x65, 0x6C, 0x6C, 0x6F,
0x62, 0x6D, 0x79,
0x66, 0x66, 0x72, 0x69, 0x65, 0x6E, 0x64])
XCTAssertNotNil(decoded)
XCTAssertEqual(["hello", "my", "friend"], decoded as? NSArray)
var array = [String]()
for _ in 0..<8 {
array.append("hello")
array.append("my")
array.append("friend")
}
var hexArray:[UInt8] = [0x98, 0x18]
for _ in 0..<8 {
hexArray.append(contentsOf: [0x65, 0x68, 0x65, 0x6C, 0x6C, 0x6F])
hexArray.append(contentsOf: [0x62, 0x6D, 0x79])
hexArray.append(contentsOf: [0x66, 0x66, 0x72, 0x69, 0x65, 0x6E, 0x64])
}
decoded = CBOR.decode(hexArray)
XCTAssertNotNil(decoded)
XCTAssertEqual(array, (decoded as? NSArray) as? [String])
}
func test_3_decodeNumberArray() {
var decoded = CBOR.decode([0x81, 0x0A])
XCTAssertNotNil(decoded)
XCTAssertEqual([10], decoded as? NSArray)
decoded = CBOR.decode([0x83, 0x0A, 0x0F, 0x28])
XCTAssertNotNil(decoded)
XCTAssertEqual([10, 15, -9], decoded as? NSArray)
}
func test_3_decodeArrayArray() {
var decoded = CBOR.decode([0x81, 0x81, 0x0A])
XCTAssertNotNil(decoded)
XCTAssertEqual([[10]], decoded as? NSArray)
decoded = CBOR.decode([0x82, 0x81, 0x0A, 0x82, 0x28, 0x00])
XCTAssertNotNil(decoded)
XCTAssertEqual([[10], [-9, 0]], decoded as? NSArray)
}
//MARK:- Map decoding
func test_2_decodeMap() {
var decoded = CBOR.decode([0xA1, 0x65, 0x68, 0x65, 0x6C, 0x6C, 0x6F,
0x65, 0x77, 0x6F, 0x72, 0x6C, 0x64])
XCTAssertNotNil(decoded)
XCTAssertEqual(["hello": "world"], decoded as? NSDictionary)
decoded = CBOR.decode([0xA2, 0x64, 0x6E, 0x61, 0x6D, 0x65,
0x66, 0x68, 0x61, 0x73, 0x73, 0x61, 0x6E,
0x64, 0x73, 0x75, 0x72, 0x65,
0x68, 0x73, 0x68, 0x61, 0x68, 0x62, 0x61, 0x7A, 0x69])
XCTAssertNotNil(decoded)
XCTAssertEqual(["sure":"shahbazi", "name":"hassan"], decoded as? NSDictionary)
decoded = CBOR.decode([0xA4, 0x05, 0x66, 0x6E, 0x75, 0x6D, 0x62, 0x65, 0x72,
0x12, 0x18, 0x62,
0x65, 0x69, 0x74, 0x65, 0x6D, 0x30,
0x18, 0x7F,
0x65, 0x69, 0x74, 0x65, 0x6D, 0x31,
0x66, 0x73, 0x74, 0x72, 0x69, 0x6E, 0x67])
XCTAssertNotNil(decoded)
XCTAssertEqual(["item0": 127, "item1": "string", 5: "number", 18: 98], decoded as? NSDictionary)
}
//MARK:- Tagged Value decoding
func test_2_decodeTaggedValue() {
var decoded = CBOR.decode([0xC5, 0x0A])
XCTAssertNotNil(decoded)
XCTAssertEqual(5, (decoded as? NSTag)?.tagValue())
XCTAssertEqual(NSNumber(value: 10), (decoded as? NSTag)?.objectValue())
decoded = CBOR.decode([0xD9, 0x02, 0x6D, 0x18, 0xE9])
XCTAssertNotNil(decoded)
XCTAssertEqual(621, (decoded as? NSTag)?.tagValue())
XCTAssertEqual(NSNumber(value: 233), (decoded as? NSTag)?.objectValue())
decoded = CBOR.decode([0xDA, 0x00, 0x01, 0x22, 0x3c,
0xA2, 0x64, 0x6E, 0x61, 0x6D, 0x65,
0x66, 0x68, 0x61, 0x73, 0x73, 0x61, 0x6E,
0x64, 0x73, 0x75, 0x72, 0x65,
0x68, 0x73, 0x68, 0x61, 0x68, 0x62, 0x61, 0x7A, 0x69])
XCTAssertNotNil(decoded)
XCTAssertEqual(74300, (decoded as? NSTag)?.tagValue())
XCTAssertEqual(["sure":"shahbazi", "name":"hassan"] as NSDictionary, (decoded as? NSTag)?.objectValue())
decoded = CBOR.decode([0xD9, 0x03, 0x99,
0x58, 0x20,
0x68, 0x71, 0x34, 0x96, 0x82, 0x22, 0xEC, 0x17, 0x20, 0x2E,
0x42, 0x50, 0x5F, 0x8E, 0xD2, 0xB1, 0x6A, 0xE2, 0x2F, 0x16,
0xBB, 0x05, 0xB8, 0x8C, 0x25, 0xDB, 0x9E, 0x60, 0x26, 0x45, 0xF1, 0x41])
XCTAssertNotNil(decoded)
XCTAssertEqual(921, (decoded as? NSTag)?.tagValue())
XCTAssertEqual("687134968222EC17202E42505F8ED2B16AE22F16BB05B88C25DB9E602645F141".lowercased() as NSString, (decoded as? NSTag)?.objectValue())
}
//MARK:- Simple Value encoding
func test_2_decodeSimpleBool() {
var decoded = CBOR.decode([0xF4])
XCTAssertNotNil(decoded)
XCTAssertEqual(false, decoded as? NSNumber)
decoded = CBOR.decode([0xF5])
XCTAssertNotNil(decoded)
XCTAssertEqual(true, decoded as? NSNumber)
}
}