@@ -23,6 +23,7 @@ public void BasicDecoding()
23
23
24
24
Span < byte > source = new byte [ numBytes ] ;
25
25
Base64TestHelper . InitializeUrlDecodableBytes ( source , numBytes ) ;
26
+ source [ numBytes - 1 ] = 65 ; // make sure unused bits set 0
26
27
27
28
Span < byte > decodedBytes = new byte [ Base64Url . GetMaxDecodedLength ( source . Length ) ] ;
28
29
Assert . Equal ( OperationStatus . Done , Base64Url . DecodeFromUtf8 ( source , decodedBytes , out int consumed , out int decodedByteCount ) ) ;
@@ -46,6 +47,7 @@ public void BasicDecodingByteArrayReturnOverload()
46
47
47
48
Span < byte > source = new byte [ numBytes ] ;
48
49
Base64TestHelper . InitializeUrlDecodableBytes ( source , numBytes ) ;
50
+ source [ numBytes - 1 ] = 65 ; // make sure unused bits set 0
49
51
50
52
Span < byte > decodedBytes = Base64Url . DecodeFromUtf8 ( source ) ;
51
53
Assert . Equal ( decodedBytes . Length , Base64Url . GetMaxDecodedLength ( source . Length ) ) ;
@@ -197,6 +199,7 @@ public void DecodingOutputTooSmall()
197
199
{
198
200
Span < byte > source = new byte [ 12 ] ;
199
201
Base64TestHelper . InitializeUrlDecodableBytes ( source ) ;
202
+ source [ 9 ] = 65 ; // make sure unused bits set 0
200
203
source [ 10 ] = Base64TestHelper . EncodingPad ;
201
204
source [ 11 ] = Base64TestHelper . EncodingPad ;
202
205
@@ -211,6 +214,7 @@ public void DecodingOutputTooSmall()
211
214
{
212
215
Span < byte > source = new byte [ 12 ] ;
213
216
Base64TestHelper . InitializeUrlDecodableBytes ( source ) ;
217
+ source [ 10 ] = 77 ; // make sure unused bits set 0
214
218
source [ 11 ] = Base64TestHelper . EncodingPad ;
215
219
216
220
Span < byte > decodedBytes = new byte [ 7 ] ;
@@ -287,6 +291,23 @@ public void BasicDecodingWithFinalBlockTrueKnownInputDone(string inputString, in
287
291
Assert . True ( Base64TestHelper . VerifyUrlDecodingCorrectness ( inputString . Length , expectedWritten , source , decodedBytes ) ) ;
288
292
}
289
293
294
+ [ Theory ]
295
+ [ InlineData ( "AR" ) ]
296
+ [ InlineData ( "AQJ" ) ]
297
+ [ InlineData ( "AQIDBB%%" ) ]
298
+ [ InlineData ( "AQIDBAV%" ) ]
299
+ [ InlineData ( "AQIDBAUHCAkKCwwNDz" ) ]
300
+ [ InlineData ( "AQIDBAUHCAkKCwwNDxD" ) ]
301
+ public void BasicDecodingWithNonZeroUnusedBits ( string inputString )
302
+ {
303
+ byte [ ] source = Encoding . ASCII . GetBytes ( inputString ) ;
304
+ Span < byte > decodedBytes = new byte [ Base64Url . GetMaxDecodedLength ( source . Length ) ] ;
305
+
306
+ Assert . False ( Base64Url . IsValid ( inputString . AsSpan ( ) ) ) ;
307
+ Assert . Equal ( OperationStatus . InvalidData , Base64Url . DecodeFromUtf8 ( source , decodedBytes , out int _ , out int _ ) ) ;
308
+ Assert . Throws < FormatException > ( ( ) => Base64Url . DecodeFromUtf8InPlace ( source ) ) ;
309
+ }
310
+
290
311
[ Theory ]
291
312
[ InlineData ( "A" , 0 , 0 , OperationStatus . InvalidData ) ]
292
313
[ InlineData ( "A===" , 0 , 0 , OperationStatus . InvalidData ) ]
@@ -460,7 +481,7 @@ public void DecodingInvalidBytes(bool isFinalBlock)
460
481
// When isFinalBlock = true input that is not a multiple of 4 is invalid for Base64, but valid for Base64Url
461
482
if ( isFinalBlock )
462
483
{
463
- Span < byte > source = "2222PPP "u8 . ToArray ( ) ; // incomplete input
484
+ Span < byte > source = "2222PPM "u8 . ToArray ( ) ; // incomplete input
464
485
Span < byte > decodedBytes = new byte [ Base64Url . GetMaxDecodedLength ( source . Length ) ] ;
465
486
Assert . Equal ( 5 , Base64Url . DecodeFromUtf8 ( source , decodedBytes ) ) ;
466
487
Assert . True ( Base64TestHelper . VerifyUrlDecodingCorrectness ( 7 , 5 , source , decodedBytes ) ) ;
@@ -517,10 +538,9 @@ public void DecodingInvalidBytesPadding(bool isFinalBlock)
517
538
518
539
// The last byte or the last 2 bytes being the padding character is valid, if isFinalBlock = true
519
540
{
520
- Span < byte > source = new byte [ ] { 50 , 50 , 50 , 50 , 80 , 80 , 80 , 80 } ;
541
+ Span < byte > source = new byte [ ] { 50 , 50 , 50 , 50 , 80 , 65 ,
542
+ Base64TestHelper . EncodingPad , Base64TestHelper . EncodingPad } ; // valid input - "2222PA=="
521
543
Span < byte > decodedBytes = new byte [ Base64Url . GetMaxDecodedLength ( source . Length ) ] ;
522
- source [ 6 ] = Base64TestHelper . EncodingPad ;
523
- source [ 7 ] = Base64TestHelper . EncodingPad ; // valid input - "2222PP=="
524
544
525
545
OperationStatus expectedStatus = isFinalBlock ? OperationStatus . Done : OperationStatus . InvalidData ;
526
546
int expectedConsumed = isFinalBlock ? source . Length : 4 ;
@@ -531,9 +551,9 @@ public void DecodingInvalidBytesPadding(bool isFinalBlock)
531
551
Assert . Equal ( expectedWritten , decodedByteCount ) ;
532
552
Assert . True ( Base64TestHelper . VerifyUrlDecodingCorrectness ( expectedConsumed , expectedWritten , source , decodedBytes ) ) ;
533
553
534
- source = new byte [ ] { 50 , 50 , 50 , 50 , 80 , 80 , 80 , 80 } ;
554
+ source = new byte [ ] { 50 , 50 , 50 , 50 , 80 , 80 , 77 , 80 } ;
535
555
decodedBytes = new byte [ Base64Url . GetMaxDecodedLength ( source . Length ) ] ;
536
- source [ 7 ] = Base64TestHelper . UrlEncodingPad ; // valid input - "2222PPP ="
556
+ source [ 7 ] = Base64TestHelper . UrlEncodingPad ; // valid input - "2222PPM ="
537
557
538
558
expectedConsumed = isFinalBlock ? source . Length : 4 ;
539
559
expectedWritten = isFinalBlock ? 5 : 3 ;
@@ -685,9 +705,8 @@ public void DecodeInPlaceInvalidBytesPaddingThrowsFormatException()
685
705
686
706
// The last byte or the last 2 bytes being the padding character is valid
687
707
{
688
- Span < byte > buffer = new byte [ ] { 50 , 50 , 50 , 50 , 80 , 80 , 80 , 80 } ;
689
- buffer [ 6 ] = Base64TestHelper . UrlEncodingPad ;
690
- buffer [ 7 ] = Base64TestHelper . EncodingPad ; // valid input - "2222PP=="
708
+ Span < byte > buffer = new byte [ ] { 50 , 50 , 50 , 50 , 80 , 65 ,
709
+ Base64TestHelper . UrlEncodingPad , Base64TestHelper . EncodingPad } ; // valid input - "2222PA=="
691
710
string sourceString = Encoding . ASCII . GetString ( buffer . ToArray ( ) ) ;
692
711
int bytesWritten = Base64Url . DecodeFromUtf8InPlace ( buffer ) ;
693
712
@@ -696,8 +715,7 @@ public void DecodeInPlaceInvalidBytesPaddingThrowsFormatException()
696
715
}
697
716
698
717
{
699
- Span < byte > buffer = new byte [ ] { 50 , 50 , 50 , 50 , 80 , 80 , 80 , 80 } ;
700
- buffer [ 7 ] = Base64TestHelper . EncodingPad ; // valid input - "2222PPP="
718
+ Span < byte > buffer = new byte [ ] { 50 , 50 , 50 , 50 , 80 , 80 , 77 , Base64TestHelper . EncodingPad } ; // valid input - "2222PPM="
701
719
string sourceString = Encoding . ASCII . GetString ( buffer . ToArray ( ) ) ;
702
720
int bytesWritten = Base64Url . DecodeFromUtf8InPlace ( buffer ) ;
703
721
@@ -707,7 +725,7 @@ public void DecodeInPlaceInvalidBytesPaddingThrowsFormatException()
707
725
708
726
// The last byte or the last 2 bytes being the padding character is valid
709
727
{
710
- Span < byte > buffer = new byte [ ] { 50 , 50 , 50 , 50 , 80 , 80 } ; // valid input without padding "2222PP "
728
+ Span < byte > buffer = new byte [ ] { 50 , 50 , 50 , 50 , 80 , 65 } ; // valid input without padding "2222PA "
711
729
712
730
string sourceString = Encoding . ASCII . GetString ( buffer . ToArray ( ) ) ;
713
731
int bytesWritten = Base64Url . DecodeFromUtf8InPlace ( buffer ) ;
@@ -775,12 +793,12 @@ public void DecodingInPlaceWithOnlyCharsToBeIgnored(string utf8WithCharsToBeIgno
775
793
}
776
794
777
795
[ Theory ]
778
- [ InlineData ( new byte [ ] { 0xa , 0xa , 0x2d , 0x2d } , 251 ) ]
779
- [ InlineData ( new byte [ ] { 0xa , 0x5f , 0xa , 0x2d } , 255 ) ]
780
- [ InlineData ( new byte [ ] { 0x5f , 0x5f , 0xa , 0xa } , 255 ) ]
781
- [ InlineData ( new byte [ ] { 0x70 , 0xa , 0x61 , 0xa } , 165 ) ]
782
- [ InlineData ( new byte [ ] { 0xa , 0x70 , 0xa , 0x61 , 0xa } , 165 ) ]
783
- [ InlineData ( new byte [ ] { 0x70 , 0xa , 0x61 , 0xa , 0x3d , 0x3d } , 165 ) ]
796
+ [ InlineData ( new byte [ ] { 0xa , 0xa , 0x2d , 0x77 } , 251 ) ]
797
+ [ InlineData ( new byte [ ] { 0xa , 0x5f , 0xa , 0x77 } , 255 ) ]
798
+ [ InlineData ( new byte [ ] { 0x5f , 0x77 , 0xa , 0xa } , 255 ) ]
799
+ [ InlineData ( new byte [ ] { 0x70 , 0xa , 0x51 , 0xa } , 165 ) ]
800
+ [ InlineData ( new byte [ ] { 0xa , 0x70 , 0xa , 0x51 , 0xa } , 165 ) ]
801
+ [ InlineData ( new byte [ ] { 0x70 , 0xa , 0x51 , 0xa , 0x3d , 0x3d } , 165 ) ]
784
802
public void DecodingLessThan4BytesWithWhiteSpaces ( byte [ ] utf8Bytes , byte decoded )
785
803
{
786
804
Assert . True ( Base64Url . IsValid ( utf8Bytes , out int decodedLength ) ) ;
@@ -802,12 +820,12 @@ public void DecodingLessThan4BytesWithWhiteSpaces(byte[] utf8Bytes, byte decoded
802
820
}
803
821
804
822
[ Theory ]
805
- [ InlineData ( new char [ ] { '\r ' , '\r ' , '-' , '- ' } , 251 ) ]
806
- [ InlineData ( new char [ ] { '\r ' , '_' , '\r ' , '- ' } , 255 ) ]
807
- [ InlineData ( new char [ ] { '_' , '_ ' , '\r ' , '\r ' } , 255 ) ]
808
- [ InlineData ( new char [ ] { 'p' , '\r ' , 'a ' , '\r ' } , 165 ) ]
809
- [ InlineData ( new char [ ] { '\r ' , 'p' , '\r ' , 'a ' , '\r ' } , 165 ) ]
810
- [ InlineData ( new char [ ] { 'p' , '\r ' , 'a ' , '\r ' , '=' , '=' } , 165 ) ]
823
+ [ InlineData ( new char [ ] { '\r ' , '\r ' , '-' , 'w ' } , 251 ) ]
824
+ [ InlineData ( new char [ ] { '\r ' , '_' , '\r ' , 'w ' } , 255 ) ]
825
+ [ InlineData ( new char [ ] { '_' , 'w ' , '\r ' , '\r ' } , 255 ) ]
826
+ [ InlineData ( new char [ ] { 'p' , '\r ' , 'Q ' , '\r ' } , 165 ) ]
827
+ [ InlineData ( new char [ ] { '\r ' , 'p' , '\r ' , 'Q ' , '\r ' } , 165 ) ]
828
+ [ InlineData ( new char [ ] { 'p' , '\r ' , 'Q ' , '\r ' , '=' , '=' } , 165 ) ]
811
829
public void DecodingLessThan4CharsWithWhiteSpaces ( char [ ] utf8Bytes , byte decoded )
812
830
{
813
831
Assert . True ( Base64Url . IsValid ( utf8Bytes , out int decodedLength ) ) ;
@@ -825,8 +843,8 @@ public void DecodingLessThan4CharsWithWhiteSpaces(char[] utf8Bytes, byte decoded
825
843
}
826
844
827
845
[ Theory ]
828
- [ InlineData ( new byte [ ] { 0x4a , 0x74 , 0xa , 0x4a , 0x4a , 0x74 , 0xa , 0x4a } , new byte [ ] { 38 , 210 , 73 , 180 } ) ]
829
- [ InlineData ( new byte [ ] { 0xa , 0x2d , 0x56 , 0xa , 0xa , 0xa , 0x2d , 0x4a , 0x4a , 0x4a , } , new byte [ ] { 249 , 95 , 137 , 36 } ) ]
846
+ [ InlineData ( new byte [ ] { 0x4a , 0x74 , 0xa , 0x4a , 0x4a , 0x74 , 0xa , 0x41 } , new byte [ ] { 38 , 210 , 73 , 180 } ) ]
847
+ [ InlineData ( new byte [ ] { 0xa , 0x2d , 0x56 , 0xa , 0xa , 0xa , 0x2d , 0x4a , 0x4a , 0x41 , } , new byte [ ] { 249 , 95 , 137 , 36 } ) ]
830
848
public void DecodingNotMultipleOf4WithWhiteSpace ( byte [ ] utf8Bytes , byte [ ] decoded )
831
849
{
832
850
Assert . True ( Base64Url . IsValid ( utf8Bytes , out int decodedLength ) ) ;
@@ -847,8 +865,8 @@ public void DecodingNotMultipleOf4WithWhiteSpace(byte[] utf8Bytes, byte[] decode
847
865
}
848
866
849
867
[ Theory ]
850
- [ InlineData ( new char [ ] { 'J' , 't' , '\r ' , 'J' , 'J' , 't' , '\r ' , 'J ' } , new byte [ ] { 38 , 210 , 73 , 180 } ) ]
851
- [ InlineData ( new char [ ] { '\r ' , '-' , 'V' , '\r ' , '\r ' , '\r ' , '-' , 'J' , 'J' , 'J ' , } , new byte [ ] { 249 , 95 , 137 , 36 } ) ]
868
+ [ InlineData ( new char [ ] { 'J' , 't' , '\r ' , 'J' , 'J' , 't' , '\r ' , 'A ' } , new byte [ ] { 38 , 210 , 73 , 180 } ) ]
869
+ [ InlineData ( new char [ ] { '\r ' , '-' , 'V' , '\r ' , '\r ' , '\r ' , '-' , 'J' , 'J' , 'A ' , } , new byte [ ] { 249 , 95 , 137 , 36 } ) ]
852
870
public void DecodingNotMultipleOf4CharsWithWhiteSpace ( char [ ] utf8Bytes , byte [ ] decoded )
853
871
{
854
872
Assert . True ( Base64Url . IsValid ( utf8Bytes , out int decodedLength ) ) ;
0 commit comments