blob: 7a30bee0513815ebf44d1509591fdd7832d35f23 [file] [log] [blame]
Anton Muhind6b19232013-07-10 20:39:25 +04001#!/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
6library coded_buffer_reader_tests;
7
Martin Kustermannc6cae3d2015-01-30 11:26:48 +01008import 'dart:typed_data';
9
Anton Muhind6b19232013-07-10 20:39:25 +040010import 'package:protobuf/protobuf.dart';
Brian Slesinsky5c8bba62015-07-10 14:28:29 -070011import 'package:test/test.dart';
Anton Muhind6b19232013-07-10 20:39:25 +040012
Anton Muhina58efc82013-07-10 22:02:13 +040013import 'test_util.dart';
14
Anton Muhind6b19232013-07-10 20:39:25 +040015void main() {
16 final throwsInvalidProtocolBufferException =
17 throwsA(new isInstanceOf<InvalidProtocolBufferException>());
18
Martin Kustermannc6cae3d2015-01-30 11:26:48 +010019 group('testCodedBufferReader', () {
Anton Muhind6b19232013-07-10 20:39:25 +040020 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 Andersen506c7062017-07-05 14:40:58 +020029 0x00, 0x00, // 110 fixed64 = 64
Anton Muhind6b19232013-07-10 20:39:25 +040030 0xfd, 0x06, 0x20, 0x00, 0x00, 0x00, // 111 sfixed32 = 64
31 0x81, 0x07, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Jakob Andersen506c7062017-07-05 14:40:58 +020032 0x00, // 112 sfixed64 = 64
Anton Muhind6b19232013-07-10 20:39:25 +040033 0x88, 0x07, 0x01, // 113 bool = true
34 0x92, 0x07, 0x0f, 0x6f, 0x70, 0x74, 0x69, 0x6f,
Jakob Andersen506c7062017-07-05 14:40:58 +020035 0x6e, 0x61, 0x6c, 0x5f, 0x73, 0x74, 0x72,
36 0x69, 0x6e, 0x67, // 114 string 15 optional_string
Anton Muhind6b19232013-07-10 20:39:25 +040037 0x9a, 0x07, 0x0e, 0x6f, 0x70, 0x74, 0x69, 0x6f,
Jakob Andersen506c7062017-07-05 14:40:58 +020038 0x6e, 0x61, 0x6c, 0x5f, 0x62, 0x79, 0x74,
39 0x65, 0x73 // 115 bytes 14 optional_bytes
Anton Muhind6b19232013-07-10 20:39:25 +040040 ];
Anton Muhind6b19232013-07-10 20:39:25 +040041
Martin Kustermannc6cae3d2015-01-30 11:26:48 +010042 testWithList(List<int> inputBuffer) {
43 CodedBufferReader cis = new CodedBufferReader(inputBuffer);
Anton Muhind6b19232013-07-10 20:39:25 +040044
Martin Kustermannc6cae3d2015-01-30 11:26:48 +010045 expect(cis.readTag(), makeTag(103, WIRETYPE_VARINT));
46 expect(cis.readInt32(), 32);
Anton Muhind6b19232013-07-10 20:39:25 +040047
Martin Kustermannc6cae3d2015-01-30 11:26:48 +010048 expect(cis.readTag(), makeTag(104, WIRETYPE_VARINT));
49 expect(cis.readInt64(), expect64(64));
Anton Muhind6b19232013-07-10 20:39:25 +040050
Martin Kustermannc6cae3d2015-01-30 11:26:48 +010051 expect(cis.readTag(), makeTag(105, WIRETYPE_VARINT));
52 expect(cis.readUint32(), 32);
Anton Muhind6b19232013-07-10 20:39:25 +040053
Martin Kustermannc6cae3d2015-01-30 11:26:48 +010054 expect(cis.readTag(), makeTag(106, WIRETYPE_VARINT));
55 expect(cis.readUint64(), expect64(64));
Anton Muhind6b19232013-07-10 20:39:25 +040056
Martin Kustermannc6cae3d2015-01-30 11:26:48 +010057 expect(cis.readTag(), makeTag(107, WIRETYPE_VARINT));
58 expect(cis.readSint32(), 32);
Anton Muhind6b19232013-07-10 20:39:25 +040059
Martin Kustermannc6cae3d2015-01-30 11:26:48 +010060 expect(cis.readTag(), makeTag(108, WIRETYPE_VARINT));
61 expect(cis.readSint64(), expect64(64));
Anton Muhind6b19232013-07-10 20:39:25 +040062
Martin Kustermannc6cae3d2015-01-30 11:26:48 +010063 expect(cis.readTag(), makeTag(109, WIRETYPE_FIXED32));
64 expect(cis.readFixed32(), 32);
Anton Muhind6b19232013-07-10 20:39:25 +040065
Martin Kustermannc6cae3d2015-01-30 11:26:48 +010066 expect(cis.readTag(), makeTag(110, WIRETYPE_FIXED64));
67 expect(cis.readFixed64(), expect64(64));
Anton Muhind6b19232013-07-10 20:39:25 +040068
Martin Kustermannc6cae3d2015-01-30 11:26:48 +010069 expect(cis.readTag(), makeTag(111, WIRETYPE_FIXED32));
70 expect(cis.readSfixed32(), 32);
Anton Muhind6b19232013-07-10 20:39:25 +040071
Martin Kustermannc6cae3d2015-01-30 11:26:48 +010072 expect(cis.readTag(), makeTag(112, WIRETYPE_FIXED64));
73 expect(cis.readSfixed64(), expect64(64));
Anton Muhind6b19232013-07-10 20:39:25 +040074
Martin Kustermannc6cae3d2015-01-30 11:26:48 +010075 expect(cis.readTag(), makeTag(113, WIRETYPE_VARINT));
76 expect(cis.readBool(), isTrue);
Anton Muhind6b19232013-07-10 20:39:25 +040077
Martin Kustermannc6cae3d2015-01-30 11:26:48 +010078 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 Muhind6b19232013-07-10 20:39:25 +0400104 });
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 Andersen506c7062017-07-05 14:40:58 +0200120 expect(() {
121 input.readBytes();
122 }, throwsInvalidProtocolBufferException);
Anton Muhind6b19232013-07-10 20:39:25 +0400123 });
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 Andersen506c7062017-07-05 14:40:58 +0200140 expect(() {
141 new CodedBufferReader([i]).readTag();
142 }, throwsInvalidProtocolBufferException);
Anton Muhind6b19232013-07-10 20:39:25 +0400143 }
144 });
145}