@@ -62,52 +62,101 @@ - (NSData*)bytes_with_sentinel:(int32_t)unused, ... {
62
62
#define bytes (...) [self bytes_with_sentinel: 0 , __VA_ARGS__, 256 ]
63
63
64
64
- (void )testDecodeZigZag {
65
- XCTAssertEqual (0 , GPBDecodeZigZag32 (0 ));
66
- XCTAssertEqual (-1 , GPBDecodeZigZag32 (1 ));
67
- XCTAssertEqual (1 , GPBDecodeZigZag32 (2 ));
68
- XCTAssertEqual (-2 , GPBDecodeZigZag32 (3 ));
69
- XCTAssertEqual ((int32_t )0x3FFFFFFF , GPBDecodeZigZag32 (0x7FFFFFFE ));
70
- XCTAssertEqual ((int32_t )0xC0000000 , GPBDecodeZigZag32 (0x7FFFFFFF ));
71
- XCTAssertEqual ((int32_t )0x7FFFFFFF , GPBDecodeZigZag32 (0xFFFFFFFE ));
72
- XCTAssertEqual ((int32_t )0x80000000 , GPBDecodeZigZag32 (0xFFFFFFFF ));
73
-
74
- XCTAssertEqual ((int64_t )0 , GPBDecodeZigZag64 (0 ));
75
- XCTAssertEqual ((int64_t )-1 , GPBDecodeZigZag64 (1 ));
76
- XCTAssertEqual ((int64_t )1 , GPBDecodeZigZag64 (2 ));
77
- XCTAssertEqual ((int64_t )-2 , GPBDecodeZigZag64 (3 ));
78
- XCTAssertEqual ((int64_t )0x000000003FFFFFFFL ,
79
- GPBDecodeZigZag64 (0x000000007FFFFFFEL ));
80
- XCTAssertEqual ((int64_t )0xFFFFFFFFC0000000L ,
81
- GPBDecodeZigZag64 (0x000000007FFFFFFFL ));
82
- XCTAssertEqual ((int64_t )0x000000007FFFFFFFL ,
83
- GPBDecodeZigZag64 (0x00000000FFFFFFFEL ));
84
- XCTAssertEqual ((int64_t )0xFFFFFFFF80000000L ,
85
- GPBDecodeZigZag64 (0x00000000FFFFFFFFL ));
86
- XCTAssertEqual ((int64_t )0x7FFFFFFFFFFFFFFFL ,
87
- GPBDecodeZigZag64 (0xFFFFFFFFFFFFFFFEL ));
88
- XCTAssertEqual ((int64_t )0x8000000000000000L ,
89
- GPBDecodeZigZag64 (0xFFFFFFFFFFFFFFFFL ));
65
+ [self assertReadZigZag32: bytes (0x0 ) value: 0 ];
66
+ [self assertReadZigZag32: bytes (0x1 ) value: -1 ];
67
+ [self assertReadZigZag32: bytes (0x2 ) value: 1 ];
68
+ [self assertReadZigZag32: bytes (0x3 ) value: -2 ];
69
+
70
+ [self assertReadZigZag32: bytes (0xFE , 0xFF , 0xFF , 0xFF , 0x07 ) value: (int32_t )0x3FFFFFFF ];
71
+ [self assertReadZigZag32: bytes (0xFF , 0xFF , 0xFF , 0xFF , 0x07 ) value: (int32_t )0xC0000000 ];
72
+ [self assertReadZigZag32: bytes (0xFE , 0xFF , 0xFF , 0xFF , 0x0F ) value: (int32_t )0x7FFFFFFF ];
73
+ [self assertReadZigZag32: bytes (0xFF , 0xFF , 0xFF , 0xFF , 0x0F ) value: (int32_t )0x80000000 ];
74
+
75
+ [self assertReadZigZag64: bytes (0x0 ) value: 0 ];
76
+ [self assertReadZigZag64: bytes (0x1 ) value: -1 ];
77
+ [self assertReadZigZag64: bytes (0x2 ) value: 1 ];
78
+ [self assertReadZigZag64: bytes (0x3 ) value: -2 ];
79
+
80
+ [self assertReadZigZag64: bytes (0xFE , 0xFF , 0xFF , 0xFF , 0x07 ) value: (int32_t )0x3FFFFFFF ];
81
+ [self assertReadZigZag64: bytes (0xFF , 0xFF , 0xFF , 0xFF , 0x07 ) value: (int32_t )0xC0000000 ];
82
+ [self assertReadZigZag64: bytes (0xFE , 0xFF , 0xFF , 0xFF , 0x0F ) value: (int32_t )0x7FFFFFFF ];
83
+ [self assertReadZigZag64: bytes (0xFF , 0xFF , 0xFF , 0xFF , 0x0F ) value: (int32_t )0x80000000 ];
84
+
85
+ [self assertReadZigZag64: bytes (0xFE , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0x01 ) value: 0x7FFFFFFFFFFFFFFFL ];
86
+ [self assertReadZigZag64: bytes (0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0x01 ) value: 0x8000000000000000L ];
90
87
}
91
88
92
89
- (void )assertReadVarint : (NSData *)data value : (int64_t )value {
93
- {
90
+ if (value <= INT32_MAX && value >= INT32_MIN) {
91
+ {
92
+ GPBCodedInputStream* input = [GPBCodedInputStream streamWithData: data];
93
+ XCTAssertEqual ((int32_t )value, [input readInt32 ]);
94
+ }
95
+ {
96
+ GPBCodedInputStream* input = [GPBCodedInputStream streamWithData: data];
97
+ XCTAssertEqual ((int32_t )value, [input readEnum ]);
98
+ }
99
+ }
100
+ if (value <= UINT32_MAX && value >= 0 ) {
94
101
GPBCodedInputStream* input = [GPBCodedInputStream streamWithData: data];
95
- XCTAssertEqual ((int32_t )value, [input readInt32 ]);
102
+ XCTAssertEqual ((uint32_t )value, [input readUInt32 ]);
96
103
}
97
104
{
98
105
GPBCodedInputStream* input = [GPBCodedInputStream streamWithData: data];
99
106
XCTAssertEqual (value, [input readInt64 ]);
100
107
}
108
+ if (value >= 0 ) {
109
+ GPBCodedInputStream* input = [GPBCodedInputStream streamWithData: data];
110
+ XCTAssertEqual ((uint64_t )value, [input readUInt64 ]);
111
+ }
101
112
}
102
113
103
114
- (void )assertReadLittleEndian32 : (NSData *)data value : (int32_t )value {
104
- GPBCodedInputStream* input = [GPBCodedInputStream streamWithData: data];
105
- XCTAssertEqual (value, [input readSFixed32 ]);
115
+ {
116
+ GPBCodedInputStream* input = [GPBCodedInputStream streamWithData: data];
117
+ XCTAssertEqual (value, [input readSFixed32 ]);
118
+ }
119
+ {
120
+ GPBCodedInputStream* input = [GPBCodedInputStream streamWithData: data];
121
+ XCTAssertEqual (GPBConvertInt32ToFloat (value), [input readFloat ]);
122
+ }
123
+ {
124
+ GPBCodedInputStream* input = [GPBCodedInputStream streamWithData: data];
125
+ XCTAssertEqual ((uint32_t )value, [input readFixed32 ]);
126
+ }
127
+ {
128
+ GPBCodedInputStream* input = [GPBCodedInputStream streamWithData: data];
129
+ XCTAssertEqual (value, [input readSFixed32 ]);
130
+ }
106
131
}
107
132
108
133
- (void )assertReadLittleEndian64 : (NSData *)data value : (int64_t )value {
134
+ {
135
+ GPBCodedInputStream* input = [GPBCodedInputStream streamWithData: data];
136
+ XCTAssertEqual (value, [input readSFixed64 ]);
137
+ }
138
+ {
139
+ GPBCodedInputStream* input = [GPBCodedInputStream streamWithData: data];
140
+ XCTAssertEqual (GPBConvertInt64ToDouble (value), [input readDouble ]);
141
+ }
142
+ {
143
+ GPBCodedInputStream* input = [GPBCodedInputStream streamWithData: data];
144
+ XCTAssertEqual ((uint64_t )value, [input readFixed64 ]);
145
+ }
146
+ {
147
+ GPBCodedInputStream* input = [GPBCodedInputStream streamWithData: data];
148
+ XCTAssertEqual (value, [input readSFixed64 ]);
149
+ }
150
+ }
151
+
152
+ - (void )assertReadZigZag32 : (NSData *)data value : (int64_t )value {
153
+ GPBCodedInputStream* input = [GPBCodedInputStream streamWithData: data];
154
+ XCTAssertEqual ((int32_t )value, [input readSInt32 ]);
155
+ }
156
+
157
+ - (void )assertReadZigZag64 : (NSData *)data value : (int64_t )value {
109
158
GPBCodedInputStream* input = [GPBCodedInputStream streamWithData: data];
110
- XCTAssertEqual (value, [input readSFixed64 ]);
159
+ XCTAssertEqual (value, [input readSInt64 ]);
111
160
}
112
161
113
162
- (void )assertReadVarintFailure : (NSData *)data {
@@ -128,12 +177,28 @@ - (void)testBytes {
128
177
XCTAssertEqual (((uint8_t *)data.bytes )[1 ], (uint8_t )0x74 );
129
178
}
130
179
180
+ - (void )testReadBool {
181
+ {
182
+ GPBCodedInputStream* input = [GPBCodedInputStream streamWithData: bytes (0x00 )];
183
+ XCTAssertEqual (NO , [input readBool ]);
184
+ }
185
+ {
186
+ GPBCodedInputStream* input = [GPBCodedInputStream streamWithData: bytes (0x01 )];
187
+ XCTAssertEqual (YES , [input readBool ]);
188
+ }
189
+ }
190
+
131
191
- (void )testReadVarint {
132
192
[self assertReadVarint: bytes (0x00 ) value: 0 ];
133
193
[self assertReadVarint: bytes (0x01 ) value: 1 ];
134
194
[self assertReadVarint: bytes (0x7f ) value: 127 ];
135
195
// 14882
136
196
[self assertReadVarint: bytes (0xa2 , 0x74 ) value: (0x22 << 0 ) | (0x74 << 7 )];
197
+ // 1904930
198
+ [self assertReadVarint: bytes (0xa2 , 0xa2 , 0x74 ) value: (0x22 << 0 ) | (0x22 << 7 ) | (0x74 << 14 )];
199
+ // 243831074
200
+ [self assertReadVarint: bytes (0xa2 , 0xa2 , 0xa2 , 0x74 )
201
+ value: (0x22 << 0 ) | (0x22 << 7 ) | (0x22 << 14 ) | (0x74 << 21 )];
137
202
// 2961488830
138
203
[self assertReadVarint: bytes (0xbe , 0xf7 , 0x92 , 0x84 , 0x0b )
139
204
value: (0x3e << 0 ) | (0x77 << 7 ) | (0x12 << 14 ) |
@@ -163,6 +228,45 @@ - (void)testReadVarint {
163
228
[self assertReadVarintFailure: bytes (0x80 )];
164
229
}
165
230
231
+ - (void )testReadVarint32FromVarint64 {
232
+ {
233
+ // Turn on lower 31 bits of the upper half on a 64 bit varint.
234
+ NSData * data = bytes (0x80 , 0x80 , 0x80 , 0x80 , 0xF0 , 0xFF , 0xFF , 0xFF , 0xFF , 0x7E );
235
+
236
+ int32_t value32 = 0x0 ;
237
+ GPBCodedInputStream* input32 = [GPBCodedInputStream streamWithData: data];
238
+ XCTAssertEqual (value32, [input32 readInt32 ]);
239
+
240
+ int64_t value64 = INT64_MAX & 0xFFFFFFFF00000000 ;
241
+ GPBCodedInputStream* input64 = [GPBCodedInputStream streamWithData: data];
242
+ XCTAssertEqual (value64, [input64 readInt64 ]);
243
+ }
244
+ {
245
+ // Turn on lower 31 bits and lower 31 bits on upper half on a 64 bit varint.
246
+ NSData * data = bytes (0xFF , 0xFF , 0xFF , 0xFF , 0xF7 , 0xFF , 0xFF , 0xFF , 0xFF , 0x7E );
247
+
248
+ int32_t value32 = INT32_MAX;
249
+ GPBCodedInputStream* input32 = [GPBCodedInputStream streamWithData: data];
250
+ XCTAssertEqual (value32, [input32 readInt32 ]);
251
+
252
+ int64_t value64 = INT64_MAX & 0xFFFFFFFF7FFFFFFF ;
253
+ GPBCodedInputStream* input64 = [GPBCodedInputStream streamWithData: data];
254
+ XCTAssertEqual (value64, [input64 readInt64 ]);
255
+ }
256
+ {
257
+ // Turn on bits 32 and 64 bit on a 64 bit varint.
258
+ NSData * data = bytes (0x80 , 0x80 , 0x80 , 0x80 , 0x88 , 0x80 , 0x80 , 0x80 , 0x80 , 0x01 );
259
+
260
+ int32_t value32 = INT32_MIN;
261
+ GPBCodedInputStream* input32 = [GPBCodedInputStream streamWithData: data];
262
+ XCTAssertEqual (value32, [input32 readInt32 ]);
263
+
264
+ int64_t value64 = INT64_MIN | (0x01L << 31 );
265
+ GPBCodedInputStream* input64 = [GPBCodedInputStream streamWithData: data];
266
+ XCTAssertEqual (value64, [input64 readInt64 ]);
267
+ }
268
+ }
269
+
166
270
- (void )testReadLittleEndian {
167
271
[self assertReadLittleEndian32: bytes (0x78 , 0x56 , 0x34 , 0x12 )
168
272
value: 0x12345678 ];
@@ -265,6 +369,27 @@ - (void)testReadMaliciouslyLargeBlob {
265
369
XCTAssertThrows ([input readBytes ]);
266
370
}
267
371
372
+ - (void )testReadEmptyString {
373
+ NSData *data = bytes (0x00 );
374
+ GPBCodedInputStream* input = [GPBCodedInputStream streamWithData: data];
375
+ XCTAssertEqualObjects (@" " , [input readString ]);
376
+ }
377
+
378
+ - (void )testInvalidGroupEndTagThrows {
379
+ NSData *data = bytes (0x0B , 0x1A , 0x02 , 0x4B , 0x50 , 0x14 );
380
+ GPBCodedInputStream* input = [GPBCodedInputStream streamWithData: data];
381
+ XCTAssertThrowsSpecificNamed ([input skipMessage ],
382
+ NSException ,
383
+ GPBCodedInputStreamException,
384
+ @" should throw a GPBCodedInputStreamException exception " );
385
+ }
386
+
387
+ - (void )testBytesWithNegativeSize {
388
+ NSData *data = bytes (0xFF , 0xFF , 0xFF , 0xFF , 0x0F );
389
+ GPBCodedInputStream *input = [GPBCodedInputStream streamWithData: data];
390
+ XCTAssertNil ([input readBytes ]);
391
+ }
392
+
268
393
// Verifies fix for b/10315336.
269
394
// Note: Now that there isn't a custom string class under the hood, this test
270
395
// isn't as critical, but it does cover bad input and if a custom class is added
0 commit comments