@@ -26,8 +26,11 @@ const tests = [
2626
2727 // Testing non-ascii characters (1-10 in chinese)
2828 [
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+ } ,
3134 [
3235 '5LiA5LqM5LiJ5Zub5LqU5YWt5LiD5YWr5Lmd5Y2B' ,
3336 '5LiA5LqM5LiJ5Zub5LqU5YWt5LiD5YWr5Lmd5Y2B' ,
@@ -40,73 +43,100 @@ const tests = [
4043 // Testing for web-safe alphabets
4144 [
4245 '>>>???>>>???=/' ,
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+ ] ,
4453 ] ,
4554] ;
4655// clang-format on
4756
4857/**
4958 * Asserts encodings
50- * @param {string } input an input string.
59+ * @param {string|{binary: string, text: string} } input an input string.
5160 * @param {!Array<string> } expectedOutputs expected outputs in the order of
5261 * base64.Alphabet enum.
5362 */
5463function 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 ) ) ;
5670
5771 // encodeString
5872 for ( const name in base64 . Alphabet ) {
5973 const alphabet = base64 . Alphabet [ name ] ;
6074 assertEquals (
61- base64 . encodeString ( input , alphabet ) , expectedOutputs [ alphabet ] ) ;
75+ expectedOutputs [ alphabet ] , base64 . encodeStringUtf8 ( text , alphabet ) ) ;
76+ assertEquals (
77+ expectedOutputs [ alphabet ] , base64 . encodeString ( binary , alphabet ) ) ;
6278 }
79+ // default case
80+ assertEquals (
81+ expectedOutputs [ base64 . Alphabet . DEFAULT ] , base64 . encodeStringUtf8 ( text ) ) ;
6382 assertEquals (
64- base64 . encodeString ( input ) , // default case
65- expectedOutputs [ base64 . Alphabet . DEFAULT ] ) ;
83+ expectedOutputs [ base64 . Alphabet . DEFAULT ] , base64 . encodeString ( binary ) ) ;
6684
6785 // encodeByteArray with Array<number>
6886 for ( const name in base64 . Alphabet ) {
6987 const alphabet = base64 . Alphabet [ name ] ;
7088 assertEquals (
71- base64 . encodeByteArray ( arr , alphabet ) , expectedOutputs [ alphabet ] ) ;
89+ expectedOutputs [ alphabet ] , base64 . encodeByteArray ( arr , alphabet ) ) ;
7290 }
91+ // default case
7392 assertEquals (
74- base64 . encodeByteArray ( arr ) , // default case
75- expectedOutputs [ base64 . Alphabet . DEFAULT ] ) ;
93+ expectedOutputs [ base64 . Alphabet . DEFAULT ] , base64 . encodeByteArray ( arr ) ) ;
7694
7795 // encodeByteArray with Uint8Array
7896 if ( SUPPORT_TYPED_ARRAY ) {
7997 const uint8Arr = new Uint8Array ( arr ) ;
8098 for ( const name in base64 . Alphabet ) {
8199 const alphabet = base64 . Alphabet [ name ] ;
82100 assertEquals (
83- base64 . encodeByteArray ( uint8Arr , alphabet ) ,
84- expectedOutputs [ alphabet ] ) ;
101+ expectedOutputs [ alphabet ] ,
102+ base64 . encodeByteArray ( uint8Arr , alphabet ) ) ;
85103 }
104+ // default case
86105 assertEquals (
87- base64 . encodeByteArray ( uint8Arr ) , // default case
88- expectedOutputs [ base64 . Alphabet . DEFAULT ] ) ;
106+ expectedOutputs [ base64 . Alphabet . DEFAULT ] ,
107+ base64 . encodeByteArray ( uint8Arr ) ) ;
89108 }
90109}
91110
92111/**
93112 * Assert decodings
94113 * @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).
96116 */
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 ) ;
99124 const uint8ArrOutput = SUPPORT_TYPED_ARRAY ? new Uint8Array ( arrOutput ) : null ;
100125
126+ // Quick validity check that decoding the text version is equivalent.
127+ assertArrayEquals ( arrOutput , crypt . stringToUtf8ByteArray ( textOutput ) ) ;
128+
101129 for ( let i = 0 ; i < inputs . length ; i ++ ) {
102130 const input = inputs [ i ] ;
103131
104132 // decodeString
105- assertEquals ( base64 . decodeString ( input , true ) , stringOutput ) ;
133+ assertEquals ( textOutput , base64 . decodeStringUtf8 ( input , true ) ) ;
134+ assertEquals ( binaryOutput , base64 . decodeString ( input , true ) ) ;
106135
107136 if ( i === 0 ) {
108137 // 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 ) ) ;
110140 }
111141
112142 // decodeStringToByteArray
@@ -163,7 +193,9 @@ testSuite({
163193 const decodedArr = crypt . stringToByteArray ( decoded ) ;
164194
165195 assertEquals ( base64 . decodeString ( encoded ) , decoded ) ; // native
196+ assertEquals ( base64 . decodeStringUtf8 ( encoded ) , decoded ) ;
166197 assertEquals ( base64 . decodeString ( encoded , true ) , decoded ) ; // custom
198+ assertEquals ( base64 . decodeStringUtf8 ( encoded , true ) , decoded ) ;
167199 assertArrayEquals ( base64 . decodeStringToByteArray ( encoded ) , decodedArr ) ;
168200
169201 if ( SUPPORT_TYPED_ARRAY ) {
0 commit comments