2021-10-09 07:12:12 -05:00
|
|
|
import 'dart:typed_data';
|
|
|
|
import 'package:simplexmq/src/buffer.dart';
|
|
|
|
import 'package:test/test.dart';
|
2021-10-03 13:10:11 -05:00
|
|
|
|
|
|
|
final hello123 = Uint8List.fromList([104, 101, 108, 108, 111, 49, 50, 51]);
|
|
|
|
|
|
|
|
class Base64Test {
|
|
|
|
final String description;
|
|
|
|
final String binary;
|
|
|
|
final String base64;
|
|
|
|
|
|
|
|
Base64Test(this.binary, this.base64) : description = binary;
|
|
|
|
Base64Test.withDescription(this.description, this.binary, this.base64);
|
|
|
|
}
|
|
|
|
|
|
|
|
void main() {
|
2021-10-09 07:12:12 -05:00
|
|
|
group('ascii encoding/decoding', () {
|
|
|
|
test('encodeAscii', () {
|
|
|
|
expect(encodeAscii('hello123'), hello123);
|
2021-10-03 13:10:11 -05:00
|
|
|
});
|
|
|
|
|
2021-10-09 07:12:12 -05:00
|
|
|
test('decodeAscii', () {
|
|
|
|
expect(decodeAscii(hello123), 'hello123');
|
2021-10-03 13:10:11 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-10-09 07:12:12 -05:00
|
|
|
group('base-64 encoding/decoding', () {
|
2021-10-03 13:10:11 -05:00
|
|
|
String allBinaryChars() {
|
|
|
|
final a = Uint8List(256);
|
|
|
|
for (var i = 0; i < 256; i++) {
|
|
|
|
a[i] = i;
|
|
|
|
}
|
|
|
|
return decodeAscii(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
final base64tests = [
|
2021-10-09 07:12:12 -05:00
|
|
|
Base64Test('\x12\x34\x56\x78', 'EjRWeA=='),
|
|
|
|
Base64Test('hello123', 'aGVsbG8xMjM='),
|
|
|
|
Base64Test('Hello world', 'SGVsbG8gd29ybGQ='),
|
|
|
|
Base64Test('Hello worlds!', 'SGVsbG8gd29ybGRzIQ=='),
|
|
|
|
Base64Test('May', 'TWF5'),
|
|
|
|
Base64Test('Ma', 'TWE='),
|
|
|
|
Base64Test('M', 'TQ=='),
|
2021-10-03 13:10:11 -05:00
|
|
|
Base64Test.withDescription(
|
2021-10-09 07:12:12 -05:00
|
|
|
'all binary chars',
|
2021-10-03 13:10:11 -05:00
|
|
|
allBinaryChars(),
|
2021-10-09 07:12:12 -05:00
|
|
|
'AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w==',
|
2021-10-03 13:10:11 -05:00
|
|
|
),
|
|
|
|
];
|
|
|
|
|
2021-10-09 07:12:12 -05:00
|
|
|
test('encode64', () {
|
2021-10-03 13:10:11 -05:00
|
|
|
for (final t in base64tests) {
|
|
|
|
expect(encode64(encodeAscii(t.binary)), encodeAscii(t.base64));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-10-09 07:12:12 -05:00
|
|
|
test('decode64', () {
|
2021-10-03 13:10:11 -05:00
|
|
|
for (final t in base64tests) {
|
|
|
|
expect(decode64(encodeAscii(t.base64)), encodeAscii(t.binary));
|
|
|
|
}
|
2021-10-09 07:12:12 -05:00
|
|
|
expect(decode64(encodeAscii('TWE')), null);
|
|
|
|
expect(decode64(encodeAscii('TWE==')), null);
|
|
|
|
expect(decode64(encodeAscii('TW!=')), null);
|
2021-10-03 13:10:11 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|