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 | |
Martin Kustermann | c6cae3d | 2015-01-30 11:26:48 +0100 | [diff] [blame] | 6 | import 'dart:typed_data'; |
| 7 | |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 8 | import 'package:protobuf/protobuf.dart'; |
Brian Slesinsky | 5c8bba6 | 2015-07-10 14:28:29 -0700 | [diff] [blame] | 9 | import 'package:test/test.dart'; |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 10 | |
Anton Muhin | a58efc8 | 2013-07-10 22:02:13 +0400 | [diff] [blame] | 11 | import 'test_util.dart'; |
| 12 | |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 13 | void main() { |
| 14 | final throwsInvalidProtocolBufferException = |
Diego Ballesteros Villamizar | 4ce027a | 2019-03-29 12:48:39 +0100 | [diff] [blame] | 15 | throwsA(TypeMatcher<InvalidProtocolBufferException>()); |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 16 | |
Martin Kustermann | c6cae3d | 2015-01-30 11:26:48 +0100 | [diff] [blame] | 17 | group('testCodedBufferReader', () { |
Sigurd Meldgaard | 9e3aa6b | 2019-07-09 12:08:56 +0200 | [diff] [blame] | 18 | final inputBuffer = List<int>.unmodifiable([ |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 19 | 0xb8, 0x06, 0x20, // 103 int32 = 32 |
| 20 | 0xc0, 0x06, 0x40, // 104 int64 = 64 |
| 21 | 0xc8, 0x06, 0x20, // 105 uint32 = 32 |
| 22 | 0xd0, 0x06, 0x40, // 106 uint64 = 64 |
| 23 | 0xd8, 0x06, 0x40, // 107 sint32 = 32 |
| 24 | 0xe0, 0x06, 0x80, 0x01, // 108 sint64 = 64 |
| 25 | 0xed, 0x06, 0x20, 0x00, 0x00, 0x00, // 109 fixed32 = 32 |
| 26 | 0xf1, 0x06, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, |
Jakob Andersen | 506c706 | 2017-07-05 14:40:58 +0200 | [diff] [blame] | 27 | 0x00, 0x00, // 110 fixed64 = 64 |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 28 | 0xfd, 0x06, 0x20, 0x00, 0x00, 0x00, // 111 sfixed32 = 64 |
| 29 | 0x81, 0x07, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
Jakob Andersen | 506c706 | 2017-07-05 14:40:58 +0200 | [diff] [blame] | 30 | 0x00, // 112 sfixed64 = 64 |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 31 | 0x88, 0x07, 0x01, // 113 bool = true |
| 32 | 0x92, 0x07, 0x0f, 0x6f, 0x70, 0x74, 0x69, 0x6f, |
Jakob Andersen | 506c706 | 2017-07-05 14:40:58 +0200 | [diff] [blame] | 33 | 0x6e, 0x61, 0x6c, 0x5f, 0x73, 0x74, 0x72, |
| 34 | 0x69, 0x6e, 0x67, // 114 string 15 optional_string |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 35 | 0x9a, 0x07, 0x0e, 0x6f, 0x70, 0x74, 0x69, 0x6f, |
Jakob Andersen | 506c706 | 2017-07-05 14:40:58 +0200 | [diff] [blame] | 36 | 0x6e, 0x61, 0x6c, 0x5f, 0x62, 0x79, 0x74, |
| 37 | 0x65, 0x73 // 115 bytes 14 optional_bytes |
Sigurd Meldgaard | 9e3aa6b | 2019-07-09 12:08:56 +0200 | [diff] [blame] | 38 | ]); |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 39 | |
Ivan Inozemtsev | cdffe5e | 2020-02-27 14:56:54 +0100 | [diff] [blame] | 40 | void testWithList(List<int> inputBuffer) { |
Sigurd Meldgaard | 9e3aa6b | 2019-07-09 12:08:56 +0200 | [diff] [blame] | 41 | final cis = CodedBufferReader(inputBuffer); |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 42 | |
Martin Kustermann | c6cae3d | 2015-01-30 11:26:48 +0100 | [diff] [blame] | 43 | expect(cis.readTag(), makeTag(103, WIRETYPE_VARINT)); |
| 44 | expect(cis.readInt32(), 32); |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 45 | |
Martin Kustermann | c6cae3d | 2015-01-30 11:26:48 +0100 | [diff] [blame] | 46 | expect(cis.readTag(), makeTag(104, WIRETYPE_VARINT)); |
| 47 | expect(cis.readInt64(), expect64(64)); |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 48 | |
Martin Kustermann | c6cae3d | 2015-01-30 11:26:48 +0100 | [diff] [blame] | 49 | expect(cis.readTag(), makeTag(105, WIRETYPE_VARINT)); |
| 50 | expect(cis.readUint32(), 32); |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 51 | |
Martin Kustermann | c6cae3d | 2015-01-30 11:26:48 +0100 | [diff] [blame] | 52 | expect(cis.readTag(), makeTag(106, WIRETYPE_VARINT)); |
| 53 | expect(cis.readUint64(), expect64(64)); |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 54 | |
Martin Kustermann | c6cae3d | 2015-01-30 11:26:48 +0100 | [diff] [blame] | 55 | expect(cis.readTag(), makeTag(107, WIRETYPE_VARINT)); |
| 56 | expect(cis.readSint32(), 32); |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 57 | |
Martin Kustermann | c6cae3d | 2015-01-30 11:26:48 +0100 | [diff] [blame] | 58 | expect(cis.readTag(), makeTag(108, WIRETYPE_VARINT)); |
| 59 | expect(cis.readSint64(), expect64(64)); |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 60 | |
Martin Kustermann | c6cae3d | 2015-01-30 11:26:48 +0100 | [diff] [blame] | 61 | expect(cis.readTag(), makeTag(109, WIRETYPE_FIXED32)); |
| 62 | expect(cis.readFixed32(), 32); |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 63 | |
Martin Kustermann | c6cae3d | 2015-01-30 11:26:48 +0100 | [diff] [blame] | 64 | expect(cis.readTag(), makeTag(110, WIRETYPE_FIXED64)); |
| 65 | expect(cis.readFixed64(), expect64(64)); |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 66 | |
Martin Kustermann | c6cae3d | 2015-01-30 11:26:48 +0100 | [diff] [blame] | 67 | expect(cis.readTag(), makeTag(111, WIRETYPE_FIXED32)); |
| 68 | expect(cis.readSfixed32(), 32); |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 69 | |
Martin Kustermann | c6cae3d | 2015-01-30 11:26:48 +0100 | [diff] [blame] | 70 | expect(cis.readTag(), makeTag(112, WIRETYPE_FIXED64)); |
| 71 | expect(cis.readSfixed64(), expect64(64)); |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 72 | |
Martin Kustermann | c6cae3d | 2015-01-30 11:26:48 +0100 | [diff] [blame] | 73 | expect(cis.readTag(), makeTag(113, WIRETYPE_VARINT)); |
| 74 | expect(cis.readBool(), isTrue); |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 75 | |
Martin Kustermann | c6cae3d | 2015-01-30 11:26:48 +0100 | [diff] [blame] | 76 | expect(cis.readTag(), makeTag(114, WIRETYPE_LENGTH_DELIMITED)); |
| 77 | expect(cis.readString(), 'optional_string'); |
| 78 | |
| 79 | expect(cis.readTag(), makeTag(115, WIRETYPE_LENGTH_DELIMITED)); |
| 80 | expect(cis.readBytes(), 'optional_bytes'.codeUnits); |
| 81 | } |
| 82 | |
| 83 | test('normal-list', () { |
Sigurd Meldgaard | 9e3aa6b | 2019-07-09 12:08:56 +0200 | [diff] [blame] | 84 | testWithList(Uint8List.fromList(inputBuffer)); |
Martin Kustermann | c6cae3d | 2015-01-30 11:26:48 +0100 | [diff] [blame] | 85 | }); |
| 86 | |
Sigurd Meldgaard | 9e3aa6b | 2019-07-09 12:08:56 +0200 | [diff] [blame] | 87 | test('unmodifiable-uint8-list-view', () { |
| 88 | testWithList(UnmodifiableUint8ListView(Uint8List.fromList(inputBuffer))); |
Martin Kustermann | c6cae3d | 2015-01-30 11:26:48 +0100 | [diff] [blame] | 89 | }); |
| 90 | |
| 91 | test('uint8-list-view', () { |
Sigurd Meldgaard | 9e3aa6b | 2019-07-09 12:08:56 +0200 | [diff] [blame] | 92 | final uint8List = Uint8List(inputBuffer.length + 4); |
Martin Kustermann | c6cae3d | 2015-01-30 11:26:48 +0100 | [diff] [blame] | 93 | uint8List[0] = 0xc0; |
| 94 | uint8List[1] = 0xc8; |
| 95 | uint8List.setRange(2, 2 + inputBuffer.length, inputBuffer); |
| 96 | uint8List[inputBuffer.length + 2] = 0xe0; |
| 97 | uint8List[inputBuffer.length + 3] = 0xed; |
Sigurd Meldgaard | 9e3aa6b | 2019-07-09 12:08:56 +0200 | [diff] [blame] | 98 | final view = Uint8List.view(uint8List.buffer, 2, inputBuffer.length); |
Martin Kustermann | c6cae3d | 2015-01-30 11:26:48 +0100 | [diff] [blame] | 99 | testWithList(view); |
| 100 | }); |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 101 | }); |
| 102 | |
| 103 | test('testReadMaliciouslyLargeBlob', () { |
Ivan Inozemtsev | cdffe5e | 2020-02-27 14:56:54 +0100 | [diff] [blame] | 104 | var output = CodedBufferWriter(); |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 105 | |
Ivan Inozemtsev | cdffe5e | 2020-02-27 14:56:54 +0100 | [diff] [blame] | 106 | var tag = makeTag(1, WIRETYPE_LENGTH_DELIMITED); |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 107 | output.writeInt32NoTag(tag); |
| 108 | output.writeInt32NoTag(0x7FFFFFFF); |
| 109 | // Pad with a few random bytes. |
| 110 | output.writeInt32NoTag(0); |
| 111 | output.writeInt32NoTag(32); |
| 112 | output.writeInt32NoTag(47); |
| 113 | |
Ivan Inozemtsev | cdffe5e | 2020-02-27 14:56:54 +0100 | [diff] [blame] | 114 | var input = CodedBufferReader(output.toBuffer()); |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 115 | expect(input.readTag(), tag); |
| 116 | |
Jakob Andersen | 506c706 | 2017-07-05 14:40:58 +0200 | [diff] [blame] | 117 | expect(() { |
| 118 | input.readBytes(); |
| 119 | }, throwsInvalidProtocolBufferException); |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 120 | }); |
| 121 | |
Jakob Andersen | 5e7bb77 | 2017-08-14 14:00:31 +0200 | [diff] [blame] | 122 | /// Tests that if we read a string that contains invalid UTF-8, no exception |
| 123 | /// is thrown. Instead, the invalid bytes are replaced with the Unicode |
| 124 | /// 'replacement character' U+FFFD. |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 125 | test('testReadInvalidUtf8', () { |
Ivan Inozemtsev | cdffe5e | 2020-02-27 14:56:54 +0100 | [diff] [blame] | 126 | var input = CodedBufferReader([1, 0x80]); |
| 127 | var text = input.readString(); |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 128 | expect(text.codeUnitAt(0), 0xfffd); |
| 129 | }); |
| 130 | |
| 131 | test('testInvalidTag', () { |
| 132 | // Any tag number which corresponds to field number zero is invalid and |
| 133 | // should throw InvalidProtocolBufferException. |
Ivan Inozemtsev | cdffe5e | 2020-02-27 14:56:54 +0100 | [diff] [blame] | 134 | for (var i = 0; i < 8; i++) { |
Jakob Andersen | 506c706 | 2017-07-05 14:40:58 +0200 | [diff] [blame] | 135 | expect(() { |
Diego Ballesteros Villamizar | 4ce027a | 2019-03-29 12:48:39 +0100 | [diff] [blame] | 136 | CodedBufferReader([i]).readTag(); |
Jakob Andersen | 506c706 | 2017-07-05 14:40:58 +0200 | [diff] [blame] | 137 | }, throwsInvalidProtocolBufferException); |
Anton Muhin | d6b1923 | 2013-07-10 20:39:25 +0400 | [diff] [blame] | 138 | } |
| 139 | }); |
| 140 | } |