OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 // Library tag to allow the test to run on Dartium. | 5 // Library tag to allow the test to run on Dartium. |
6 library base64_test; | 6 library base64_test; |
7 | 7 |
8 import 'dart:math'; | 8 import 'dart:math'; |
9 import 'dart:async'; | 9 import 'dart:async'; |
10 | 10 |
11 import "package:crypto/crypto.dart"; | 11 import "package:crypto/crypto.dart"; |
12 import "package:test/test.dart"; | 12 import "package:test/test.dart"; |
13 | 13 |
14 void main() { | 14 void main() { |
15 test('encoder', _testEncoder); | 15 test('encoder', _testEncoder); |
16 test('decoder', _testDecoder); | 16 test('decoder', _testDecoder); |
17 test('decoder for malformed input', _testDecoderForMalformedInput); | 17 test('decoder for malformed input', _testDecoderForMalformedInput); |
18 test('encode decode lists', _testEncodeDecodeLists); | 18 test('encode decode lists', _testEncodeDecodeLists); |
19 test('url safe encode-decode', _testUrlSafeEncodeDecode); | 19 test('url safe encode-decode', _testUrlSafeEncodeDecode); |
20 test('percent-encoded padding character encode-decode', | 20 test( |
21 _testPaddingCharacter); | 21 'percent-encoded padding character encode-decode', _testPaddingCharacter); |
22 test('streaming encoder', _testStreamingEncoder); | 22 test('streaming encoder', _testStreamingEncoder); |
23 test('streaming decoder', _testStreamingDecoder); | 23 test('streaming decoder', _testStreamingDecoder); |
24 test('streaming decoder for malformed input', | 24 test('streaming decoder for malformed input', |
25 _testStreamingDecoderForMalformedInput); | 25 _testStreamingDecoderForMalformedInput); |
26 test('streaming encoder for different decompositions of a list of bytes', | 26 test('streaming encoder for different decompositions of a list of bytes', |
27 _testStreamingEncoderForDecompositions); | 27 _testStreamingEncoderForDecompositions); |
28 test('streaming decoder for different decompositions of a string', | 28 test('streaming decoder for different decompositions of a string', |
29 _testStreamingDecoderForDecompositions); | 29 _testStreamingDecoderForDecompositions); |
30 test('streaming for encoded padding character', | 30 test('streaming for encoded padding character', |
31 _testStreamingForEncodedPadding); | 31 _testStreamingForEncodedPadding); |
32 test('old api', _testOldApi); | 32 test('old api', _testOldApi); |
33 test('url safe streaming encoder/decoder', _testUrlSafeStreaming); | 33 test('url safe streaming encoder/decoder', _testUrlSafeStreaming); |
34 test('performance', _testPerformance); | 34 test('performance', _testPerformance); |
35 | |
36 | |
37 } | 35 } |
38 | 36 |
39 // Data from http://tools.ietf.org/html/rfc4648. | 37 // Data from http://tools.ietf.org/html/rfc4648. |
40 const _INPUTS = | 38 const _INPUTS = const ['', 'f', 'fo', 'foo', 'foob', 'fooba', 'foobar']; |
41 const [ '', 'f', 'fo', 'foo', 'foob', 'fooba', 'foobar']; | 39 const _RESULTS = const [ |
42 const _RESULTS = | 40 '', |
43 const [ '', 'Zg==', 'Zm8=', 'Zm9v', 'Zm9vYg==', 'Zm9vYmE=', 'Zm9vYmFy']; | 41 'Zg==', |
| 42 'Zm8=', |
| 43 'Zm9v', |
| 44 'Zm9vYg==', |
| 45 'Zm9vYmE=', |
| 46 'Zm9vYmFy' |
| 47 ]; |
44 | 48 |
45 const _PADDING_INPUT = const [2, 8]; | 49 const _PADDING_INPUT = const [2, 8]; |
46 | 50 |
47 var _STREAMING_ENCODER_INPUT = | 51 var _STREAMING_ENCODER_INPUT = [ |
48 [[102, 102], [111, 102], | 52 [102, 102], |
49 [111, 111, 102, 111, 111, 98, 102, 111, | 53 [111, 102], |
50 111, 98, 97, 102, 111, 111, 98, 97, 114]]; | 54 [ |
| 55 111, 111, 102, 111, 111, 98, 102, 111, 111, 98, 97, 102, 111, 111, 98, 97, |
| 56 114 |
| 57 ] |
| 58 ]; |
51 | 59 |
52 const _STREAMING_ENCODED = 'ZmZvZm9vZm9vYmZvb2JhZm9vYmFy'; | 60 const _STREAMING_ENCODED = 'ZmZvZm9vZm9vYmZvb2JhZm9vYmFy'; |
53 const _STREAMING_DECODER_INPUT = | 61 const _STREAMING_DECODER_INPUT = const ['YmFz', 'ZTY', '0I', 'GRlY29kZXI=']; |
54 const ['YmFz', 'ZTY', '0I', 'GRlY29kZXI=']; | 62 const _STREAMING_DECODED = const [ |
55 const _STREAMING_DECODED = | 63 98, 97, 115, 101, 54, 52, 32, 100, 101, 99, 111, 100, 101, 114 |
56 const [98, 97, 115, 101, 54, 52, 32, 100, 101, 99, 111, 100, 101, 114]; | 64 ]; |
57 const _STREAMING_DECODER_INPUT_FOR_ZEROES = | 65 const _STREAMING_DECODER_INPUT_FOR_ZEROES = const ['AAAA', 'AAA=', 'AA==', '']; |
58 const ['AAAA', 'AAA=', 'AA==', '']; | |
59 var _STREAMING_DECODED_ZEROES = [0, 0, 0, 0, 0, 0]; | 66 var _STREAMING_DECODED_ZEROES = [0, 0, 0, 0, 0, 0]; |
60 | 67 |
61 var _DECOMPOSITIONS_FOR_DECODING = [ | 68 var _DECOMPOSITIONS_FOR_DECODING = [ |
62 ["A", "", "BCD"], ["A", "BCD", "", ""], ["A", "B", "", "", "CD", ""], | 69 ["A", "", "BCD"], |
63 ["", "A", "BC", "", "D"], ["", "AB", "C", "", "", "D"], ["AB", "CD", ""], | 70 ["A", "BCD", "", ""], |
64 ["", "ABC", "", "D"], ["", "ABC", "D", ""], ["", "", "ABCD", ""], | 71 ["A", "B", "", "", "CD", ""], |
65 ["A", "B", "C", "D"], ["", "A", "B", "C", "D", ""], | 72 ["", "A", "BC", "", "D"], |
66 ["", "A", "B", "", "", "C", "", "D", ""]]; | 73 ["", "AB", "C", "", "", "D"], |
| 74 ["AB", "CD", ""], |
| 75 ["", "ABC", "", "D"], |
| 76 ["", "ABC", "D", ""], |
| 77 ["", "", "ABCD", ""], |
| 78 ["A", "B", "C", "D"], |
| 79 ["", "A", "B", "C", "D", ""], |
| 80 ["", "A", "B", "", "", "C", "", "D", ""] |
| 81 ]; |
67 | 82 |
68 const _DECOMPOSITION_DECODED = const [0, 16, 131]; | 83 const _DECOMPOSITION_DECODED = const [0, 16, 131]; |
69 | 84 |
70 var _DECOMPOSITIONS_FOR_ENCODING = [ | 85 var _DECOMPOSITIONS_FOR_ENCODING = [ |
71 [[196, 16], [], [158], [196]], | 86 [[196, 16], [], [158], [196]], |
72 [[196, 16], [158, 196], [], []], | 87 [[196, 16], [158, 196], [], []], |
73 [[196], [], [16], [], [], [158], [], [196]], | 88 [[196], [], [16], [], [], [158], [], [196]], |
74 [[196], [], [16], [158, 196], [], []], | 89 [[196], [], [16], [158, 196], [], []], |
75 [[], [196], [], [], [16, 158], [], [196]], | 90 [[], [196], [], [], [16, 158], [], [196]], |
76 [[], [196], [16, 158, 196], []], | 91 [[], [196], [16, 158, 196], []], |
77 [[196, 16, 158], [], [], [196]], | 92 [[196, 16, 158], [], [], [196]], |
78 [[196, 16, 158], [], [196], []], | 93 [[196, 16, 158], [], [196], []], |
79 [[196, 16, 158, 196], [], [], []]]; | 94 [[196, 16, 158, 196], [], [], []] |
| 95 ]; |
80 | 96 |
81 const _DECOMPOSITION_ENCODED = 'xBCexA=='; | 97 const _DECOMPOSITION_ENCODED = 'xBCexA=='; |
82 | 98 |
83 // Test data with only zeroes. | 99 // Test data with only zeroes. |
84 var inputsWithZeroes = [[0, 0, 0], [0, 0], [0], []]; | 100 var inputsWithZeroes = [[0, 0, 0], [0, 0], [0], []]; |
85 const _RESULTS_WITH_ZEROS = const ['AAAA', 'AAA=', 'AA==', '']; | 101 const _RESULTS_WITH_ZEROS = const ['AAAA', 'AAA=', 'AA==', '']; |
86 | 102 |
87 const _LONG_LINE = | 103 const _LONG_LINE = |
88 "Man is distinguished, not only by his reason, but by this singular " | 104 "Man is distinguished, not only by his reason, but by this singular " |
89 "passion from other animals, which is a lust of the mind, that by a " | 105 "passion from other animals, which is a lust of the mind, that by a " |
90 "perseverance of delight in the continued and indefatigable generation " | 106 "perseverance of delight in the continued and indefatigable generation " |
91 "of knowledge, exceeds the short vehemence of any carnal pleasure."; | 107 "of knowledge, exceeds the short vehemence of any carnal pleasure."; |
92 | 108 |
93 const _LONG_LINE_RESULT = | 109 const _LONG_LINE_RESULT = "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbm" |
94 "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbm" | |
95 "x5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz\r\n" | 110 "x5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz\r\n" |
96 "IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlci" | 111 "IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlci" |
97 "BhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg\r\n" | 112 "BhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg\r\n" |
98 "dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcm" | 113 "dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcm" |
99 "FuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu\r\n" | 114 "FuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu\r\n" |
100 "dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYX" | 115 "dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYX" |
101 "Rpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo\r\n" | 116 "Rpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo\r\n" |
102 "ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm" | 117 "ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm" |
103 "5hbCBwbGVhc3VyZS4="; | 118 "5hbCBwbGVhc3VyZS4="; |
104 | 119 |
105 const _LONG_LINE_RESULT_NO_BREAK = | 120 const _LONG_LINE_RESULT_NO_BREAK = "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbm" |
106 "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbm" | |
107 "x5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz" | 121 "x5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz" |
108 "IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlci" | 122 "IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlci" |
109 "BhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg" | 123 "BhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg" |
110 "dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcm" | 124 "dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcm" |
111 "FuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu" | 125 "FuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu" |
112 "dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYX" | 126 "dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYX" |
113 "Rpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo" | 127 "Rpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo" |
114 "ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm" | 128 "ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm" |
115 "5hbCBwbGVhc3VyZS4="; | 129 "5hbCBwbGVhc3VyZS4="; |
116 | 130 |
117 void _testEncoder() { | 131 void _testEncoder() { |
118 for (var i = 0; i < _INPUTS.length; i++) { | 132 for (var i = 0; i < _INPUTS.length; i++) { |
119 expect(BASE64.encode(_INPUTS[i].codeUnits), _RESULTS[i]); | 133 expect(BASE64.encode(_INPUTS[i].codeUnits), _RESULTS[i]); |
120 } | 134 } |
121 for (var i = 0; i < inputsWithZeroes.length; i++) { | 135 for (var i = 0; i < inputsWithZeroes.length; i++) { |
122 expect(BASE64.encode(inputsWithZeroes[i]), | 136 expect(BASE64.encode(inputsWithZeroes[i]), _RESULTS_WITH_ZEROS[i]); |
123 _RESULTS_WITH_ZEROS[i]); | |
124 } | 137 } |
125 expect(BASE64.encode(_LONG_LINE.codeUnits, addLineSeparator : true), | 138 expect(BASE64.encode(_LONG_LINE.codeUnits, addLineSeparator: true), |
126 _LONG_LINE_RESULT); | 139 _LONG_LINE_RESULT); |
127 expect(BASE64.encode(_LONG_LINE.codeUnits), | 140 expect(BASE64.encode(_LONG_LINE.codeUnits), _LONG_LINE_RESULT_NO_BREAK); |
128 _LONG_LINE_RESULT_NO_BREAK); | |
129 } | 141 } |
130 | 142 |
131 void _testDecoder() { | 143 void _testDecoder() { |
132 for (var i = 0; i < _RESULTS.length; i++) { | 144 for (var i = 0; i < _RESULTS.length; i++) { |
133 expect( | 145 expect(new String.fromCharCodes(BASE64.decode(_RESULTS[i])), _INPUTS[i]); |
134 new String.fromCharCodes(BASE64.decode(_RESULTS[i])), | |
135 _INPUTS[i]); | |
136 } | 146 } |
137 | 147 |
138 for (var i = 0; i < _RESULTS_WITH_ZEROS.length; i++) { | 148 for (var i = 0; i < _RESULTS_WITH_ZEROS.length; i++) { |
139 expect(BASE64.decode(_RESULTS_WITH_ZEROS[i]), | 149 expect(BASE64.decode(_RESULTS_WITH_ZEROS[i]), inputsWithZeroes[i]); |
140 inputsWithZeroes[i]); | |
141 } | 150 } |
142 | 151 |
143 var longLineDecoded = BASE64.decode(_LONG_LINE_RESULT); | 152 var longLineDecoded = BASE64.decode(_LONG_LINE_RESULT); |
144 expect(new String.fromCharCodes(longLineDecoded), _LONG_LINE); | 153 expect(new String.fromCharCodes(longLineDecoded), _LONG_LINE); |
145 | 154 |
146 var longLineResultNoBreak = BASE64.decode(_LONG_LINE_RESULT); | 155 var longLineResultNoBreak = BASE64.decode(_LONG_LINE_RESULT); |
147 expect(new String.fromCharCodes(longLineResultNoBreak), _LONG_LINE); | 156 expect(new String.fromCharCodes(longLineResultNoBreak), _LONG_LINE); |
148 } | 157 } |
149 | 158 |
150 void _testPaddingCharacter() { | 159 void _testPaddingCharacter() { |
151 var encoded = BASE64.encode(_PADDING_INPUT, encodePaddingCharacter: true); | 160 var encoded = BASE64.encode(_PADDING_INPUT, encodePaddingCharacter: true); |
152 expect(encoded, 'Agg%3D'); | 161 expect(encoded, 'Agg%3D'); |
153 expect(BASE64.decode(encoded), _PADDING_INPUT); | 162 expect(BASE64.decode(encoded), _PADDING_INPUT); |
154 } | 163 } |
155 | 164 |
156 Future _testStreamingEncoder() async { | 165 Future _testStreamingEncoder() async { |
157 expect( | 166 expect( |
158 await new Stream.fromIterable(_STREAMING_ENCODER_INPUT) | 167 await new Stream.fromIterable(_STREAMING_ENCODER_INPUT) |
159 .transform(BASE64.encoder) | 168 .transform(BASE64.encoder) |
160 .join(), | 169 .join(), |
161 _STREAMING_ENCODED); | 170 _STREAMING_ENCODED); |
162 } | 171 } |
163 | 172 |
164 Future _testStreamingDecoder() async { | 173 Future _testStreamingDecoder() async { |
165 expect( | 174 expect( |
166 await new Stream.fromIterable(_STREAMING_DECODER_INPUT) | 175 await new Stream.fromIterable(_STREAMING_DECODER_INPUT) |
167 .transform(BASE64.decoder) | 176 .transform(BASE64.decoder) |
168 .expand((l) => l) | 177 .expand((l) => l) |
169 .toList(), | 178 .toList(), |
170 _STREAMING_DECODED); | 179 _STREAMING_DECODED); |
171 | 180 |
172 expect( | 181 expect( |
173 await new Stream.fromIterable(_STREAMING_DECODER_INPUT_FOR_ZEROES) | 182 await new Stream.fromIterable(_STREAMING_DECODER_INPUT_FOR_ZEROES) |
174 .transform(BASE64.decoder) | 183 .transform(BASE64.decoder) |
175 .expand((l) => l) | 184 .expand((l) => l) |
176 .toList(), | 185 .toList(), |
177 _STREAMING_DECODED_ZEROES); | 186 _STREAMING_DECODED_ZEROES); |
178 } | 187 } |
179 | 188 |
180 Future _testStreamingDecoderForMalformedInput() async { | 189 Future _testStreamingDecoderForMalformedInput() async { |
181 expect(new Stream.fromIterable(['ABz']) | 190 expect(new Stream.fromIterable(['ABz']).transform(BASE64.decoder).toList(), |
182 .transform(BASE64.decoder) | 191 throwsFormatException); |
183 .toList(), | |
184 throwsFormatException); | |
185 | 192 |
186 expect(new Stream.fromIterable(['AB', 'Lx', 'z', 'xx']) | 193 expect( |
187 .transform(BASE64.decoder) | 194 new Stream.fromIterable(['AB', 'Lx', 'z', 'xx']) |
188 .toList(), | 195 .transform(BASE64.decoder) |
189 throwsFormatException); | 196 .toList(), |
| 197 throwsFormatException); |
190 } | 198 } |
191 | 199 |
192 Future _testStreamingEncoderForDecompositions() async { | 200 Future _testStreamingEncoderForDecompositions() async { |
193 for(var decomposition in _DECOMPOSITIONS_FOR_ENCODING) { | 201 for (var decomposition in _DECOMPOSITIONS_FOR_ENCODING) { |
194 expect( | 202 expect( |
195 await new Stream.fromIterable(decomposition) | 203 await new Stream.fromIterable(decomposition) |
196 .transform(BASE64.encoder) | 204 .transform(BASE64.encoder) |
197 .join(), | 205 .join(), |
198 _DECOMPOSITION_ENCODED); | 206 _DECOMPOSITION_ENCODED); |
199 } | 207 } |
200 } | 208 } |
201 | 209 |
202 Future _testStreamingDecoderForDecompositions() async { | 210 Future _testStreamingDecoderForDecompositions() async { |
203 for(var decomposition in _DECOMPOSITIONS_FOR_DECODING) { | 211 for (var decomposition in _DECOMPOSITIONS_FOR_DECODING) { |
204 expect( | 212 expect( |
205 await new Stream.fromIterable(decomposition) | 213 await new Stream.fromIterable(decomposition) |
206 .transform(BASE64.decoder) | 214 .transform(BASE64.decoder) |
207 .expand((x) => x) | 215 .expand((x) => x) |
208 .toList(), | 216 .toList(), |
209 _DECOMPOSITION_DECODED); | 217 _DECOMPOSITION_DECODED); |
210 } | 218 } |
211 } | 219 } |
212 | 220 |
213 void _testDecoderForMalformedInput() { | 221 void _testDecoderForMalformedInput() { |
214 expect(() { | 222 expect(() { |
215 BASE64.decode('AB~'); | 223 BASE64.decode('AB~'); |
216 }, throwsFormatException); | 224 }, throwsFormatException); |
217 | 225 |
218 expect(() { | 226 expect(() { |
219 BASE64.decode('A'); | 227 BASE64.decode('A'); |
220 }, throwsFormatException); | 228 }, throwsFormatException); |
221 } | 229 } |
222 | 230 |
223 Future _testUrlSafeStreaming() async { | 231 Future _testUrlSafeStreaming() async { |
224 String encUrlSafe = '-_A='; | 232 String encUrlSafe = '-_A='; |
225 List<List<int>> dec = [BASE64.decode('+/A=')]; | 233 List<List<int>> dec = [BASE64.decode('+/A=')]; |
226 var streamedResult = await new Stream.fromIterable(dec) | 234 var streamedResult = await new Stream.fromIterable(dec) |
227 .transform(new Base64Encoder(urlSafe: true)).join(); | 235 .transform(new Base64Encoder(urlSafe: true)) |
| 236 .join(); |
228 | 237 |
229 expect(streamedResult, encUrlSafe); | 238 expect(streamedResult, encUrlSafe); |
230 } | 239 } |
231 | 240 |
232 Future _testStreamingForEncodedPadding() async { | 241 Future _testStreamingForEncodedPadding() async { |
233 List<String> withEncodedPadding = ['AA%', '3D', '%', '3', 'DEFGZ']; | 242 List<String> withEncodedPadding = ['AA%', '3D', '%', '3', 'DEFGZ']; |
234 List<int> decoded = BASE64.decode('AA==EFGZ'); | 243 List<int> decoded = BASE64.decode('AA==EFGZ'); |
235 var streamedResult = await new Stream.fromIterable(withEncodedPadding) | 244 var streamedResult = await new Stream.fromIterable(withEncodedPadding) |
236 .transform(BASE64.decoder).expand((x) => x).toList(); | 245 .transform(BASE64.decoder) |
| 246 .expand((x) => x) |
| 247 .toList(); |
237 | 248 |
238 expect(streamedResult, decoded); | 249 expect(streamedResult, decoded); |
239 } | 250 } |
240 | 251 |
241 void _testUrlSafeEncodeDecode() { | 252 void _testUrlSafeEncodeDecode() { |
242 List<int> decUrlSafe = BASE64.decode('-_A='); | 253 List<int> decUrlSafe = BASE64.decode('-_A='); |
243 List<int> dec = BASE64.decode('+/A='); | 254 List<int> dec = BASE64.decode('+/A='); |
244 expect(decUrlSafe, orderedEquals(dec)); | 255 expect(decUrlSafe, orderedEquals(dec)); |
245 expect(BASE64.encode(dec, urlSafe: true), '-_A='); | 256 expect(BASE64.encode(dec, urlSafe: true), '-_A='); |
246 expect(BASE64.encode(dec), '+/A='); | 257 expect(BASE64.encode(dec), '+/A='); |
(...skipping 21 matching lines...) Expand all Loading... |
268 } | 279 } |
269 | 280 |
270 void _testOldApi() { | 281 void _testOldApi() { |
271 for (int i = 0; i < _INPUTS.length; i++) { | 282 for (int i = 0; i < _INPUTS.length; i++) { |
272 expect(CryptoUtils.bytesToBase64(_INPUTS[i].codeUnits), _RESULTS[i]); | 283 expect(CryptoUtils.bytesToBase64(_INPUTS[i].codeUnits), _RESULTS[i]); |
273 expect(CryptoUtils.base64StringToBytes(_RESULTS[i]), _INPUTS[i].codeUnits); | 284 expect(CryptoUtils.base64StringToBytes(_RESULTS[i]), _INPUTS[i].codeUnits); |
274 } | 285 } |
275 } | 286 } |
276 | 287 |
277 void _testPerformance() { | 288 void _testPerformance() { |
278 var l = new List<int>(1024); | 289 var l = new List<int>(1024); |
279 var iters = 5000; | 290 var iters = 5000; |
280 _fillRandom(l); | 291 _fillRandom(l); |
281 String enc; | 292 String enc; |
282 var w = new Stopwatch()..start(); | 293 var w = new Stopwatch()..start(); |
283 for( int i = 0; i < iters; ++i ) { | 294 for (int i = 0; i < iters; ++i) { |
284 enc = BASE64.encode(l); | 295 enc = BASE64.encode(l); |
285 } | 296 } |
286 int ms = w.elapsedMilliseconds; | 297 int ms = w.elapsedMilliseconds; |
287 int perSec = (iters * l.length) * 1000 ~/ ms; | 298 int perSec = (iters * l.length) * 1000 ~/ ms; |
288 // print("Encode 1024 bytes for $iters times: $ms msec. $perSec b/s"); | 299 // print("Encode 1024 bytes for $iters times: $ms msec. $perSec b/s"); |
289 w..reset(); | 300 w..reset(); |
290 for( int i = 0; i < iters; ++i ) { | 301 for (int i = 0; i < iters; ++i) { |
291 BASE64.decode(enc); | 302 BASE64.decode(enc); |
292 } | 303 } |
293 ms = w.elapsedMilliseconds; | 304 ms = w.elapsedMilliseconds; |
294 perSec = (iters * l.length) * 1000 ~/ ms; | 305 perSec = (iters * l.length) * 1000 ~/ ms; |
295 // ('''Decode into ${l.length} bytes for $iters | 306 // ('''Decode into ${l.length} bytes for $iters |
296 // times: $ms msec. $perSec b/s'''); | 307 // times: $ms msec. $perSec b/s'''); |
297 } | 308 } |
OLD | NEW |