Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(79)

Side by Side Diff: tests/lib/convert/base64_test.dart

Issue 1370073002: Add Base64 codec to dart:convert. (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Fix for dart2js Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « sdk/lib/convert/convert_sources.gypi ('k') | tests/lib/lib.status » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 import 'dart:convert';
6 import "dart:typed_data";
7 import "package:expect/expect.dart";
8
9 main() {
10 for (var list in [[],
11 [0x00],
12 [0xff, 0x00],
13 [0xff, 0xaa, 0x55],
14 [0x00, 0x01, 0x02, 0x03],
15 new Iterable.generate(13).toList(),
16 new Iterable.generate(254).toList(),
17 new Iterable.generate(255).toList(),
18 new Iterable.generate(256).toList()]) {
19 testRoundtrip(list, "List#${list.length}");
20 testRoundtrip(new Uint8List.fromList(list), "Uint8List#${list.length}");
21 }
22 testErrors();
23 }
24
25 void testRoundtrip(list, name) {
26 // Direct.
27 String encoded = BASE64.encode(list);
28 List result = BASE64.decode(encoded);
29 Expect.listEquals(list, result, name);
30
31 int increment = list.length ~/ 7 + 1;
32 // Chunked.
33 for (int i = 0; i < list.length; i += increment) {
34 for (int j = i; j < list.length; j += increment) {
35 {
36 // Using add/close
37 var results;
38 var sink = new ChunkedConversionSink.withCallback((v) { results = v; });
39 var encoder = BASE64.encoder.startChunkedConversion(sink);
40 encoder.add(list.sublist(0, i));
41 encoder.add(list.sublist(i, j));
42 encoder.add(list.sublist(j, list.length));
43 encoder.close();
44 var name = "0-$i-$j-${list.length}: list";
45 Expect.equals(encoded, results.join(""), name);
46 }
47 {
48 // Using addSlice
49 var results;
50 var sink = new ChunkedConversionSink.withCallback((v) { results = v; });
51 var encoder = BASE64.encoder.startChunkedConversion(sink);
52 encoder.addSlice(list, 0, i, false);
53 encoder.addSlice(list, i, j, false);
54 encoder.addSlice(list, j, list.length, true);
55 var name = "0-$i-$j-${list.length}: $list";
56 Expect.equals(encoded, results.join(""), name);
57 }
58 }
59 }
60
61 increment = encoded.length ~/ 7 + 1;
62 for (int i = 0; i < encoded.length; i += increment) {
63 for (int j = i; j < encoded.length; j += increment) {
64 {
65 // Using add/close
66 var results;
67 var sink = new ChunkedConversionSink.withCallback((v) { results = v; });
68 var decoder = BASE64.decoder.startChunkedConversion(sink);
69 decoder.add(encoded.substring(0, i));
70 decoder.add(encoded.substring(i, j));
71 decoder.add(encoded.substring(j, encoded.length));
72 decoder.close();
73 var name = "0-$i-$j-${encoded.length}: $encoded";
74 Expect.listEquals(list, results.expand((x)=>x).toList(), name);
75 }
76 {
77 // Using addSlice
78 var results;
79 var sink = new ChunkedConversionSink.withCallback((v) { results = v; });
80 var decoder = BASE64.decoder.startChunkedConversion(sink);
81 decoder.addSlice(encoded, 0, i, false);
82 decoder.addSlice(encoded, i, j, false);
83 decoder.addSlice(encoded, j, encoded.length, true);
84 var name = "0-$i-$j-${encoded.length}: $encoded";
85 Expect.listEquals(list, results.expand((x)=>x).toList(), name);
86 }
87 }
88 }
89 }
90
91 bool isFormatException(e) => e is FormatException;
92 bool isArgumentError(e) => e is ArgumentError;
93
94 void testErrors() {
95 void badChunkDecode(List<String> list) {
96 Expect.throws(() {
97 var sink = new ChunkedConversionSink.withCallback((v) {
98 Expect.fail("Should have thrown: chunk $list");
99 });
100 var c = BASE64.decoder.startChunkedConversion(sink);
101 for (String string in list) {
102 c.add(string);
103 }
104 c.close();
105 }, isFormatException, "chunk $list");
106 }
107 void badDecode(String string) {
108 Expect.throws(() => BASE64.decode(string), isFormatException, string);
109 badChunkDecode([string]);
110 badChunkDecode(["", string]);
111 badChunkDecode([string, ""]);
112 badChunkDecode([string, "", ""]);
113 badChunkDecode(["", string, ""]);
114 }
115
116 badDecode("A");
117 badDecode("AA");
118 badDecode("AAA");
119 badDecode("AAAAA");
120 badDecode("AAAAAA");
121 badDecode("AAAAAAA");
122 badDecode("AAAA=");
123 badDecode("AAAA==");
124 badDecode("AAAA===");
125 badDecode("AAAA====");
126 badDecode("A=");
127 badDecode("A=A");
128 badDecode("A==");
129 badDecode("A==A");
130 badDecode("A===");
131 badDecode("====");
132 badDecode("AA=");
133 badDecode("AA===");
134 badDecode("AAA==");
135 badDecode("AAA=AAAA");
136 badDecode("AAA-");
137 badDecode("AAA_");
138 badDecode("AAA\x00");
139 badDecode("AAA=\x00");
140 badDecode("AAA\x80");
141 badDecode("AAA\xFF");
142 badDecode("AAA\u{141}");
143 badDecode("AAA\u{1041}");
144 badDecode("AAA\u{10041}");
145
146 var alphabet =
147 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/";
148 var units = alphabet.codeUnits;
149 for (int i = 0; i < 128; i++) {
150 if (!units.contains(i)) {
151 badDecode(new String.fromCharCode(i) * 4);
152 }
153 }
154
155 badChunkDecode(["A", "A"]);
156 badChunkDecode(["A", "A", "A"]);
157 badChunkDecode(["A", "A", "="]);
158 badChunkDecode(["A", "A", "=", ""]);
159 badChunkDecode(["A", "A", "=", "=", "="]);
160 badChunkDecode(["AAA", "=="]);
161 badChunkDecode(["A", "A", "A"]);
162 badChunkDecode(["AAA", ""]);
163 badChunkDecode(["AA=", ""]);
164 badChunkDecode(["AB==", ""]);
165
166
167 badChunkEncode(list) {
168 for (int i = 0; i < list.length; i++) {
169 for (int j = 0; j < list.length; j++) {
170 Expect.throws(() {
171 var sink = new ChunkedConversionSink.withCallback((v) {
172 Expect.fail("Should have thrown: chunked $list");
173 });
174 var c = BASE64.encoder.startChunkedConversion(sink);
175 c.add(list.sublist(0, i));
176 c.add(list.sublist(i, j));
177 c.add(list.sublist(j, list.length));
178 c.close();
179 }, isArgumentError, "chunk $list");
180 }
181 }
182 for (int i = 0; i < list.length; i++) {
183 for (int j = 0; j < list.length; j++) {
184 Expect.throws(() {
185 var sink = new ChunkedConversionSink.withCallback((v) {
186 Expect.fail("Should have thrown: chunked $list");
187 });
188 var c = BASE64.encoder.startChunkedConversion(sink);
189 c.addSlice(list, 0, i, false);
190 c.addSlice(list, i, j, false);
191 c.addSlice(list, j, list.length, true);
192 }, isArgumentError, "chunk $list");
193 }
194 }
195 }
196
197 void badEncode(int invalid) {
198 Expect.throws(() {
199 BASE64.encode([invalid]);
200 }, isArgumentError, "$invalid");
201 Expect.throws(() {
202 BASE64.encode([0, invalid, 0]);
203 }, isArgumentError, "$invalid");
204 badChunkEncode([invalid]);
205 badChunkEncode([0, invalid]);
206 badChunkEncode([0, 0, invalid]);
207 badChunkEncode([0, invalid, 0]);
208 badChunkEncode([invalid, 0, 0]);
209 }
210
211 badEncode(-1);
212 badEncode(0x100);
213 badEncode(0x1000);
214 badEncode(0x10000);
215 badEncode(0x100000000); /// 01: ok
216 badEncode(0x10000000000000000); /// 01: continued
217 }
OLDNEW
« no previous file with comments | « sdk/lib/convert/convert_sources.gypi ('k') | tests/lib/lib.status » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698