| OLD | NEW |
| 1 #!/usr/bin/env dart | 1 #!/usr/bin/env dart |
| 2 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 2 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 3 // for details. All rights reserved. Use of this source code is governed by a | 3 // for details. All rights reserved. Use of this source code is governed by a |
| 4 // BSD-style license that can be found in the LICENSE file. | 4 // BSD-style license that can be found in the LICENSE file. |
| 5 | 5 |
| 6 library coded_buffer_reader_tests; | 6 library coded_buffer_reader_tests; |
| 7 | 7 |
| 8 import 'dart:typed_data'; |
| 9 |
| 8 import 'package:protobuf/protobuf.dart'; | 10 import 'package:protobuf/protobuf.dart'; |
| 9 import 'package:unittest/unittest.dart'; | 11 import 'package:unittest/unittest.dart'; |
| 10 | 12 |
| 11 import 'test_util.dart'; | 13 import 'test_util.dart'; |
| 12 | 14 |
| 13 void main() { | 15 void main() { |
| 14 final throwsInvalidProtocolBufferException = | 16 final throwsInvalidProtocolBufferException = |
| 15 throwsA(new isInstanceOf<InvalidProtocolBufferException>()); | 17 throwsA(new isInstanceOf<InvalidProtocolBufferException>()); |
| 16 | 18 |
| 17 test('testCodedBufferReader', () { | 19 group('testCodedBufferReader', () { |
| 18 List<int> inputBuffer = <int>[ | 20 List<int> inputBuffer = <int>[ |
| 19 0xb8, 0x06, 0x20, // 103 int32 = 32 | 21 0xb8, 0x06, 0x20, // 103 int32 = 32 |
| 20 0xc0, 0x06, 0x40, // 104 int64 = 64 | 22 0xc0, 0x06, 0x40, // 104 int64 = 64 |
| 21 0xc8, 0x06, 0x20, // 105 uint32 = 32 | 23 0xc8, 0x06, 0x20, // 105 uint32 = 32 |
| 22 0xd0, 0x06, 0x40, // 106 uint64 = 64 | 24 0xd0, 0x06, 0x40, // 106 uint64 = 64 |
| 23 0xd8, 0x06, 0x40, // 107 sint32 = 32 | 25 0xd8, 0x06, 0x40, // 107 sint32 = 32 |
| 24 0xe0, 0x06, 0x80, 0x01, // 108 sint64 = 64 | 26 0xe0, 0x06, 0x80, 0x01, // 108 sint64 = 64 |
| 25 0xed, 0x06, 0x20, 0x00, 0x00, 0x00, // 109 fixed32 = 32 | 27 0xed, 0x06, 0x20, 0x00, 0x00, 0x00, // 109 fixed32 = 32 |
| 26 0xf1, 0x06, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, | 28 0xf1, 0x06, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 27 0x00, 0x00, // 110 fixed64 = 64 | 29 0x00, 0x00, // 110 fixed64 = 64 |
| 28 0xfd, 0x06, 0x20, 0x00, 0x00, 0x00, // 111 sfixed32 = 64 | 30 0xfd, 0x06, 0x20, 0x00, 0x00, 0x00, // 111 sfixed32 = 64 |
| 29 0x81, 0x07, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 31 0x81, 0x07, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 30 0x00, // 112 sfixed64 = 64 | 32 0x00, // 112 sfixed64 = 64 |
| 31 0x88, 0x07, 0x01, // 113 bool = true | 33 0x88, 0x07, 0x01, // 113 bool = true |
| 32 0x92, 0x07, 0x0f, 0x6f, 0x70, 0x74, 0x69, 0x6f, | 34 0x92, 0x07, 0x0f, 0x6f, 0x70, 0x74, 0x69, 0x6f, |
| 33 0x6e, 0x61, 0x6c, 0x5f, 0x73, 0x74, 0x72, | 35 0x6e, 0x61, 0x6c, 0x5f, 0x73, 0x74, 0x72, |
| 34 0x69, 0x6e, 0x67, // 114 string 15 optional_string | 36 0x69, 0x6e, 0x67, // 114 string 15 optional_string |
| 35 0x9a, 0x07, 0x0e, 0x6f, 0x70, 0x74, 0x69, 0x6f, | 37 0x9a, 0x07, 0x0e, 0x6f, 0x70, 0x74, 0x69, 0x6f, |
| 36 0x6e, 0x61, 0x6c, 0x5f, 0x62, 0x79, 0x74, | 38 0x6e, 0x61, 0x6c, 0x5f, 0x62, 0x79, 0x74, |
| 37 0x65, 0x73 // 115 bytes 14 optional_bytes | 39 0x65, 0x73 // 115 bytes 14 optional_bytes |
| 38 ]; | 40 ]; |
| 39 CodedBufferReader cis = new CodedBufferReader(inputBuffer); | |
| 40 | 41 |
| 41 expect(cis.readTag(), makeTag(103, WIRETYPE_VARINT)); | 42 testWithList(List<int> inputBuffer) { |
| 42 expect(cis.readInt32(), 32); | 43 CodedBufferReader cis = new CodedBufferReader(inputBuffer); |
| 43 | 44 |
| 44 expect(cis.readTag(), makeTag(104, WIRETYPE_VARINT)); | 45 expect(cis.readTag(), makeTag(103, WIRETYPE_VARINT)); |
| 45 expect(cis.readInt64(), expect64(64)); | 46 expect(cis.readInt32(), 32); |
| 46 | 47 |
| 47 expect(cis.readTag(), makeTag(105, WIRETYPE_VARINT)); | 48 expect(cis.readTag(), makeTag(104, WIRETYPE_VARINT)); |
| 48 expect(cis.readUint32(), 32); | 49 expect(cis.readInt64(), expect64(64)); |
| 49 | 50 |
| 50 expect(cis.readTag(), makeTag(106, WIRETYPE_VARINT)); | 51 expect(cis.readTag(), makeTag(105, WIRETYPE_VARINT)); |
| 51 expect(cis.readUint64(), expect64(64)); | 52 expect(cis.readUint32(), 32); |
| 52 | 53 |
| 53 expect(cis.readTag(), makeTag(107, WIRETYPE_VARINT)); | 54 expect(cis.readTag(), makeTag(106, WIRETYPE_VARINT)); |
| 54 expect(cis.readSint32(), 32); | 55 expect(cis.readUint64(), expect64(64)); |
| 55 | 56 |
| 56 expect(cis.readTag(), makeTag(108, WIRETYPE_VARINT)); | 57 expect(cis.readTag(), makeTag(107, WIRETYPE_VARINT)); |
| 57 expect(cis.readSint64(), expect64(64)); | 58 expect(cis.readSint32(), 32); |
| 58 | 59 |
| 59 expect(cis.readTag(), makeTag(109, WIRETYPE_FIXED32)); | 60 expect(cis.readTag(), makeTag(108, WIRETYPE_VARINT)); |
| 60 expect(cis.readFixed32(), 32); | 61 expect(cis.readSint64(), expect64(64)); |
| 61 | 62 |
| 62 expect(cis.readTag(), makeTag(110, WIRETYPE_FIXED64)); | 63 expect(cis.readTag(), makeTag(109, WIRETYPE_FIXED32)); |
| 63 expect(cis.readFixed64(), expect64(64)); | 64 expect(cis.readFixed32(), 32); |
| 64 | 65 |
| 65 expect(cis.readTag(), makeTag(111, WIRETYPE_FIXED32)); | 66 expect(cis.readTag(), makeTag(110, WIRETYPE_FIXED64)); |
| 66 expect(cis.readSfixed32(), 32); | 67 expect(cis.readFixed64(), expect64(64)); |
| 67 | 68 |
| 68 expect(cis.readTag(), makeTag(112, WIRETYPE_FIXED64)); | 69 expect(cis.readTag(), makeTag(111, WIRETYPE_FIXED32)); |
| 69 expect(cis.readSfixed64(), expect64(64)); | 70 expect(cis.readSfixed32(), 32); |
| 70 | 71 |
| 71 expect(cis.readTag(), makeTag(113, WIRETYPE_VARINT)); | 72 expect(cis.readTag(), makeTag(112, WIRETYPE_FIXED64)); |
| 72 expect(cis.readBool(), isTrue); | 73 expect(cis.readSfixed64(), expect64(64)); |
| 73 | 74 |
| 74 expect(cis.readTag(), makeTag(114, WIRETYPE_LENGTH_DELIMITED)); | 75 expect(cis.readTag(), makeTag(113, WIRETYPE_VARINT)); |
| 75 expect(cis.readString(), 'optional_string'); | 76 expect(cis.readBool(), isTrue); |
| 76 | 77 |
| 77 expect(cis.readTag(), makeTag(115, WIRETYPE_LENGTH_DELIMITED)); | 78 expect(cis.readTag(), makeTag(114, WIRETYPE_LENGTH_DELIMITED)); |
| 78 expect(cis.readBytes(), 'optional_bytes'.codeUnits); | 79 expect(cis.readString(), 'optional_string'); |
| 80 |
| 81 expect(cis.readTag(), makeTag(115, WIRETYPE_LENGTH_DELIMITED)); |
| 82 expect(cis.readBytes(), 'optional_bytes'.codeUnits); |
| 83 } |
| 84 |
| 85 test('normal-list', () { |
| 86 testWithList(inputBuffer); |
| 87 }); |
| 88 |
| 89 test('uint8-list', () { |
| 90 var uint8List = new Uint8List.fromList(inputBuffer); |
| 91 testWithList(uint8List); |
| 92 }); |
| 93 |
| 94 test('uint8-list-view', () { |
| 95 var uint8List = new Uint8List(inputBuffer.length + 4); |
| 96 uint8List[0] = 0xc0; |
| 97 uint8List[1] = 0xc8; |
| 98 uint8List.setRange(2, 2 + inputBuffer.length, inputBuffer); |
| 99 uint8List[inputBuffer.length + 2] = 0xe0; |
| 100 uint8List[inputBuffer.length + 3] = 0xed; |
| 101 var view = new Uint8List.view(uint8List.buffer, 2, inputBuffer.length); |
| 102 testWithList(view); |
| 103 }); |
| 79 }); | 104 }); |
| 80 | 105 |
| 81 test('testReadMaliciouslyLargeBlob', () { | 106 test('testReadMaliciouslyLargeBlob', () { |
| 82 CodedBufferWriter output = new CodedBufferWriter(); | 107 CodedBufferWriter output = new CodedBufferWriter(); |
| 83 | 108 |
| 84 int tag = makeTag(1, WIRETYPE_LENGTH_DELIMITED); | 109 int tag = makeTag(1, WIRETYPE_LENGTH_DELIMITED); |
| 85 output.writeInt32NoTag(tag); | 110 output.writeInt32NoTag(tag); |
| 86 output.writeInt32NoTag(0x7FFFFFFF); | 111 output.writeInt32NoTag(0x7FFFFFFF); |
| 87 // Pad with a few random bytes. | 112 // Pad with a few random bytes. |
| 88 output.writeInt32NoTag(0); | 113 output.writeInt32NoTag(0); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 109 | 134 |
| 110 test('testInvalidTag', () { | 135 test('testInvalidTag', () { |
| 111 // Any tag number which corresponds to field number zero is invalid and | 136 // Any tag number which corresponds to field number zero is invalid and |
| 112 // should throw InvalidProtocolBufferException. | 137 // should throw InvalidProtocolBufferException. |
| 113 for (int i = 0; i < 8; i++) { | 138 for (int i = 0; i < 8; i++) { |
| 114 expect(() { new CodedBufferReader([i]).readTag(); }, | 139 expect(() { new CodedBufferReader([i]).readTag(); }, |
| 115 throwsInvalidProtocolBufferException); | 140 throwsInvalidProtocolBufferException); |
| 116 } | 141 } |
| 117 }); | 142 }); |
| 118 } | 143 } |
| OLD | NEW |