@@ -26,8 +26,11 @@ const tests = [
26
26
27
27
// Testing non-ascii characters (1-10 in chinese)
28
28
[
29
- '\xe4\xb8\x80\xe4\xba\x8c\xe4\xb8\x89\xe5\x9b\x9b\xe4\xba\x94\xe5' +
30
- '\x85\xad\xe4\xb8\x83\xe5\x85\xab\xe4\xb9\x9d\xe5\x8d\x81' ,
29
+ {
30
+ binary : '\xe4\xb8\x80\xe4\xba\x8c\xe4\xb8\x89\xe5\x9b\x9b\xe4\xba\x94' +
31
+ '\xe5\x85\xad\xe4\xb8\x83\xe5\x85\xab\xe4\xb9\x9d\xe5\x8d\x81' ,
32
+ text : '一二三四五六七八九十' ,
33
+ } ,
31
34
[
32
35
'5LiA5LqM5LiJ5Zub5LqU5YWt5LiD5YWr5Lmd5Y2B' ,
33
36
'5LiA5LqM5LiJ5Zub5LqU5YWt5LiD5YWr5Lmd5Y2B' ,
@@ -40,73 +43,100 @@ const tests = [
40
43
// Testing for web-safe alphabets
41
44
[
42
45
'>>>???>>>???=/' ,
43
- [ 'Pj4+Pz8/Pj4+Pz8/PS8=' , 'Pj4+Pz8/Pj4+Pz8/PS8' , 'Pj4-Pz8_Pj4-Pz8_PS8=' , 'Pj4-Pz8_Pj4-Pz8_PS8.' , 'Pj4-Pz8_Pj4-Pz8_PS8' ] ,
46
+ [
47
+ 'Pj4+Pz8/Pj4+Pz8/PS8=' ,
48
+ 'Pj4+Pz8/Pj4+Pz8/PS8' ,
49
+ 'Pj4-Pz8_Pj4-Pz8_PS8=' ,
50
+ 'Pj4-Pz8_Pj4-Pz8_PS8.' ,
51
+ 'Pj4-Pz8_Pj4-Pz8_PS8' ,
52
+ ] ,
44
53
] ,
45
54
] ;
46
55
// clang-format on
47
56
48
57
/**
49
58
* Asserts encodings
50
- * @param {string } input an input string.
59
+ * @param {string|{binary: string, text: string} } input an input string.
51
60
* @param {!Array<string> } expectedOutputs expected outputs in the order of
52
61
* base64.Alphabet enum.
53
62
*/
54
63
function assertEncodings ( input , expectedOutputs ) {
55
- const arr = crypt . stringToByteArray ( input ) ;
64
+ const { text, binary} =
65
+ typeof input === 'string' ? { text : input , binary : input } : input ;
66
+ const arr = crypt . stringToByteArray ( binary ) ;
67
+
68
+ // quick validity test
69
+ assertArrayEquals ( arr , crypt . stringToUtf8ByteArray ( text ) ) ;
56
70
57
71
// encodeString
58
72
for ( const name in base64 . Alphabet ) {
59
73
const alphabet = base64 . Alphabet [ name ] ;
60
74
assertEquals (
61
- base64 . encodeString ( input , alphabet ) , expectedOutputs [ alphabet ] ) ;
75
+ expectedOutputs [ alphabet ] , base64 . encodeStringUtf8 ( text , alphabet ) ) ;
76
+ assertEquals (
77
+ expectedOutputs [ alphabet ] , base64 . encodeString ( binary , alphabet ) ) ;
62
78
}
79
+ // default case
80
+ assertEquals (
81
+ expectedOutputs [ base64 . Alphabet . DEFAULT ] , base64 . encodeStringUtf8 ( text ) ) ;
63
82
assertEquals (
64
- base64 . encodeString ( input ) , // default case
65
- expectedOutputs [ base64 . Alphabet . DEFAULT ] ) ;
83
+ expectedOutputs [ base64 . Alphabet . DEFAULT ] , base64 . encodeString ( binary ) ) ;
66
84
67
85
// encodeByteArray with Array<number>
68
86
for ( const name in base64 . Alphabet ) {
69
87
const alphabet = base64 . Alphabet [ name ] ;
70
88
assertEquals (
71
- base64 . encodeByteArray ( arr , alphabet ) , expectedOutputs [ alphabet ] ) ;
89
+ expectedOutputs [ alphabet ] , base64 . encodeByteArray ( arr , alphabet ) ) ;
72
90
}
91
+ // default case
73
92
assertEquals (
74
- base64 . encodeByteArray ( arr ) , // default case
75
- expectedOutputs [ base64 . Alphabet . DEFAULT ] ) ;
93
+ expectedOutputs [ base64 . Alphabet . DEFAULT ] , base64 . encodeByteArray ( arr ) ) ;
76
94
77
95
// encodeByteArray with Uint8Array
78
96
if ( SUPPORT_TYPED_ARRAY ) {
79
97
const uint8Arr = new Uint8Array ( arr ) ;
80
98
for ( const name in base64 . Alphabet ) {
81
99
const alphabet = base64 . Alphabet [ name ] ;
82
100
assertEquals (
83
- base64 . encodeByteArray ( uint8Arr , alphabet ) ,
84
- expectedOutputs [ alphabet ] ) ;
101
+ expectedOutputs [ alphabet ] ,
102
+ base64 . encodeByteArray ( uint8Arr , alphabet ) ) ;
85
103
}
104
+ // default case
86
105
assertEquals (
87
- base64 . encodeByteArray ( uint8Arr ) , // default case
88
- expectedOutputs [ base64 . Alphabet . DEFAULT ] ) ;
106
+ expectedOutputs [ base64 . Alphabet . DEFAULT ] ,
107
+ base64 . encodeByteArray ( uint8Arr ) ) ;
89
108
}
90
109
}
91
110
92
111
/**
93
112
* Assert decodings
94
113
* @param {!Array<string> } inputs input strings in various encodings.
95
- * @param {string } stringOutput expected output in string.
114
+ * @param {string|{text: string, binary: string} } expectedOutput expected output
115
+ * in string (optionally split out for text/binary).
96
116
*/
97
- function assertDecodings ( inputs , stringOutput ) {
98
- const arrOutput = crypt . stringToByteArray ( stringOutput ) ;
117
+ function assertDecodings ( inputs , expectedOutput ) {
118
+ const textOutput =
119
+ typeof expectedOutput === 'string' ? expectedOutput : expectedOutput . text ;
120
+ const binaryOutput = typeof expectedOutput === 'string' ?
121
+ expectedOutput :
122
+ expectedOutput . binary ;
123
+ const arrOutput = crypt . stringToByteArray ( binaryOutput ) ;
99
124
const uint8ArrOutput = SUPPORT_TYPED_ARRAY ? new Uint8Array ( arrOutput ) : null ;
100
125
126
+ // Quick validity check that decoding the text version is equivalent.
127
+ assertArrayEquals ( arrOutput , crypt . stringToUtf8ByteArray ( textOutput ) ) ;
128
+
101
129
for ( let i = 0 ; i < inputs . length ; i ++ ) {
102
130
const input = inputs [ i ] ;
103
131
104
132
// decodeString
105
- assertEquals ( base64 . decodeString ( input , true ) , stringOutput ) ;
133
+ assertEquals ( textOutput , base64 . decodeStringUtf8 ( input , true ) ) ;
134
+ assertEquals ( binaryOutput , base64 . decodeString ( input , true ) ) ;
106
135
107
136
if ( i === 0 ) {
108
137
// For Alphabet.DEFAULT, test with native decoder too
109
- assertEquals ( base64 . decodeString ( input ) , stringOutput ) ;
138
+ assertEquals ( textOutput , base64 . decodeStringUtf8 ( input ) ) ;
139
+ assertEquals ( binaryOutput , base64 . decodeString ( input ) ) ;
110
140
}
111
141
112
142
// decodeStringToByteArray
@@ -163,7 +193,9 @@ testSuite({
163
193
const decodedArr = crypt . stringToByteArray ( decoded ) ;
164
194
165
195
assertEquals ( base64 . decodeString ( encoded ) , decoded ) ; // native
196
+ assertEquals ( base64 . decodeStringUtf8 ( encoded ) , decoded ) ;
166
197
assertEquals ( base64 . decodeString ( encoded , true ) , decoded ) ; // custom
198
+ assertEquals ( base64 . decodeStringUtf8 ( encoded , true ) , decoded ) ;
167
199
assertArrayEquals ( base64 . decodeStringToByteArray ( encoded ) , decodedArr ) ;
168
200
169
201
if ( SUPPORT_TYPED_ARRAY ) {
0 commit comments