Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 1 | #!/usr/bin/env dart |
| 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 |
| 4 | // BSD-style license that can be found in the LICENSE file. |
| 5 | |
| 6 | library coded_buffer_reader_tests; |
| 7 | |
Martin Kustermann | c6cae3d | 2015-01-30 11:26:48 +0100 | [diff] [blame] | 8 | import 'dart:typed_data'; |
| 9 | |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 10 | import 'package:protobuf/protobuf.dart'; |
Brian Slesinsky | 5c8bba6 | 2015-07-10 14:28:29 -0700 | [diff] [blame] | 11 | import 'package:test/test.dart'; |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 12 | |
Anton Muhin | a58efc8 | 2013-07-10 22:02:13 +0400 | [diff] [blame] | 13 | import 'test_util.dart'; |
| 14 | |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 15 | void main() { |
| 16 | final throwsInvalidProtocolBufferException = |
| 17 | throwsA(new isInstanceOf<InvalidProtocolBufferException>()); |
| 18 | |
Martin Kustermann | c6cae3d | 2015-01-30 11:26:48 +0100 | [diff] [blame] | 19 | group('testCodedBufferReader', () { |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 20 | List<int> inputBuffer = <int>[ |
| 21 | 0xb8, 0x06, 0x20, // 103 int32 = 32 |
| 22 | 0xc0, 0x06, 0x40, // 104 int64 = 64 |
| 23 | 0xc8, 0x06, 0x20, // 105 uint32 = 32 |
| 24 | 0xd0, 0x06, 0x40, // 106 uint64 = 64 |
| 25 | 0xd8, 0x06, 0x40, // 107 sint32 = 32 |
| 26 | 0xe0, 0x06, 0x80, 0x01, // 108 sint64 = 64 |
| 27 | 0xed, 0x06, 0x20, 0x00, 0x00, 0x00, // 109 fixed32 = 32 |
| 28 | 0xf1, 0x06, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, |
Jakob Andersen | 506c706 | 2017-07-05 14:40:58 +0200 | [diff] [blame^] | 29 | 0x00, 0x00, // 110 fixed64 = 64 |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 30 | 0xfd, 0x06, 0x20, 0x00, 0x00, 0x00, // 111 sfixed32 = 64 |
| 31 | 0x81, 0x07, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
Jakob Andersen | 506c706 | 2017-07-05 14:40:58 +0200 | [diff] [blame^] | 32 | 0x00, // 112 sfixed64 = 64 |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 33 | 0x88, 0x07, 0x01, // 113 bool = true |
| 34 | 0x92, 0x07, 0x0f, 0x6f, 0x70, 0x74, 0x69, 0x6f, |
Jakob Andersen | 506c706 | 2017-07-05 14:40:58 +0200 | [diff] [blame^] | 35 | 0x6e, 0x61, 0x6c, 0x5f, 0x73, 0x74, 0x72, |
| 36 | 0x69, 0x6e, 0x67, // 114 string 15 optional_string |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 37 | 0x9a, 0x07, 0x0e, 0x6f, 0x70, 0x74, 0x69, 0x6f, |
Jakob Andersen | 506c706 | 2017-07-05 14:40:58 +0200 | [diff] [blame^] | 38 | 0x6e, 0x61, 0x6c, 0x5f, 0x62, 0x79, 0x74, |
| 39 | 0x65, 0x73 // 115 bytes 14 optional_bytes |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 40 | ]; |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 41 | |
Martin Kustermann | c6cae3d | 2015-01-30 11:26:48 +0100 | [diff] [blame] | 42 | testWithList(List<int> inputBuffer) { |
| 43 | CodedBufferReader cis = new CodedBufferReader(inputBuffer); |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 44 | |
Martin Kustermann | c6cae3d | 2015-01-30 11:26:48 +0100 | [diff] [blame] | 45 | expect(cis.readTag(), makeTag(103, WIRETYPE_VARINT)); |
| 46 | expect(cis.readInt32(), 32); |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 47 | |
Martin Kustermann | c6cae3d | 2015-01-30 11:26:48 +0100 | [diff] [blame] | 48 | expect(cis.readTag(), makeTag(104, WIRETYPE_VARINT)); |
| 49 | expect(cis.readInt64(), expect64(64)); |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 50 | |
Martin Kustermann | c6cae3d | 2015-01-30 11:26:48 +0100 | [diff] [blame] | 51 | expect(cis.readTag(), makeTag(105, WIRETYPE_VARINT)); |
| 52 | expect(cis.readUint32(), 32); |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 53 | |
Martin Kustermann | c6cae3d | 2015-01-30 11:26:48 +0100 | [diff] [blame] | 54 | expect(cis.readTag(), makeTag(106, WIRETYPE_VARINT)); |
| 55 | expect(cis.readUint64(), expect64(64)); |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 56 | |
Martin Kustermann | c6cae3d | 2015-01-30 11:26:48 +0100 | [diff] [blame] | 57 | expect(cis.readTag(), makeTag(107, WIRETYPE_VARINT)); |
| 58 | expect(cis.readSint32(), 32); |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 59 | |
Martin Kustermann | c6cae3d | 2015-01-30 11:26:48 +0100 | [diff] [blame] | 60 | expect(cis.readTag(), makeTag(108, WIRETYPE_VARINT)); |
| 61 | expect(cis.readSint64(), expect64(64)); |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 62 | |
Martin Kustermann | c6cae3d | 2015-01-30 11:26:48 +0100 | [diff] [blame] | 63 | expect(cis.readTag(), makeTag(109, WIRETYPE_FIXED32)); |
| 64 | expect(cis.readFixed32(), 32); |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 65 | |
Martin Kustermann | c6cae3d | 2015-01-30 11:26:48 +0100 | [diff] [blame] | 66 | expect(cis.readTag(), makeTag(110, WIRETYPE_FIXED64)); |
| 67 | expect(cis.readFixed64(), expect64(64)); |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 68 | |
Martin Kustermann | c6cae3d | 2015-01-30 11:26:48 +0100 | [diff] [blame] | 69 | expect(cis.readTag(), makeTag(111, WIRETYPE_FIXED32)); |
| 70 | expect(cis.readSfixed32(), 32); |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 71 | |
Martin Kustermann | c6cae3d | 2015-01-30 11:26:48 +0100 | [diff] [blame] | 72 | expect(cis.readTag(), makeTag(112, WIRETYPE_FIXED64)); |
| 73 | expect(cis.readSfixed64(), expect64(64)); |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 74 | |
Martin Kustermann | c6cae3d | 2015-01-30 11:26:48 +0100 | [diff] [blame] | 75 | expect(cis.readTag(), makeTag(113, WIRETYPE_VARINT)); |
| 76 | expect(cis.readBool(), isTrue); |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 77 | |
Martin Kustermann | c6cae3d | 2015-01-30 11:26:48 +0100 | [diff] [blame] | 78 | expect(cis.readTag(), makeTag(114, WIRETYPE_LENGTH_DELIMITED)); |
| 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 | }); |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 104 | }); |
| 105 | |
| 106 | test('testReadMaliciouslyLargeBlob', () { |
| 107 | CodedBufferWriter output = new CodedBufferWriter(); |
| 108 | |
| 109 | int tag = makeTag(1, WIRETYPE_LENGTH_DELIMITED); |
| 110 | output.writeInt32NoTag(tag); |
| 111 | output.writeInt32NoTag(0x7FFFFFFF); |
| 112 | // Pad with a few random bytes. |
| 113 | output.writeInt32NoTag(0); |
| 114 | output.writeInt32NoTag(32); |
| 115 | output.writeInt32NoTag(47); |
| 116 | |
| 117 | CodedBufferReader input = new CodedBufferReader(output.toBuffer()); |
| 118 | expect(input.readTag(), tag); |
| 119 | |
Jakob Andersen | 506c706 | 2017-07-05 14:40:58 +0200 | [diff] [blame^] | 120 | expect(() { |
| 121 | input.readBytes(); |
| 122 | }, throwsInvalidProtocolBufferException); |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 123 | }); |
| 124 | |
| 125 | /** |
| 126 | * Tests that if we read a string that contains invalid UTF-8, no exception |
| 127 | * is thrown. Instead, the invalid bytes are replaced with the Unicode |
| 128 | * 'replacement character' U+FFFD. |
| 129 | */ |
| 130 | test('testReadInvalidUtf8', () { |
| 131 | CodedBufferReader input = new CodedBufferReader([1, 0x80]); |
| 132 | String text = input.readString(); |
| 133 | expect(text.codeUnitAt(0), 0xfffd); |
| 134 | }); |
| 135 | |
| 136 | test('testInvalidTag', () { |
| 137 | // Any tag number which corresponds to field number zero is invalid and |
| 138 | // should throw InvalidProtocolBufferException. |
| 139 | for (int i = 0; i < 8; i++) { |
Jakob Andersen | 506c706 | 2017-07-05 14:40:58 +0200 | [diff] [blame^] | 140 | expect(() { |
| 141 | new CodedBufferReader([i]).readTag(); |
| 142 | }, throwsInvalidProtocolBufferException); |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 143 | } |
| 144 | }); |
| 145 | } |