@@ -24,13 +24,14 @@ public class TorrentParserTests
2424 public TorrentParserTests ( )
2525 {
2626 BencodeParser = Substitute . For < IBencodeParser > ( ) ;
27- BencodeParser . Parse < BDictionary > ( ( BencodeReader ) null ) . ReturnsForAnyArgs ( x => ParsedData ) ;
27+ BencodeParser . Parse < BDictionary > ( null ) . ReturnsForAnyArgs ( x => ParsedData ) ;
2828
2929 ValidSingleFileTorrentData = new BDictionary
3030 {
3131 [ TorrentFields . Info ] = new BDictionary
3232 {
3333 [ TorrentInfoFields . Name ] = ( BString ) "" ,
34+ [ TorrentInfoFields . NameUtf8 ] = ( BString ) "" ,
3435 [ TorrentInfoFields . Pieces ] = ( BString ) "" ,
3536 [ TorrentInfoFields . PieceLength ] = ( BNumber ) 0 ,
3637 [ TorrentInfoFields . Length ] = ( BNumber ) 0
@@ -42,6 +43,7 @@ public TorrentParserTests()
4243 [ TorrentFields . Info ] = new BDictionary
4344 {
4445 [ TorrentInfoFields . Name ] = ( BString ) "" ,
46+ [ TorrentInfoFields . NameUtf8 ] = ( BString ) "" ,
4547 [ TorrentInfoFields . Pieces ] = ( BString ) "" ,
4648 [ TorrentInfoFields . PieceLength ] = ( BNumber ) 0 ,
4749 [ TorrentInfoFields . Files ] = new BList ( )
@@ -394,13 +396,14 @@ public void AnnounceAndAnnounceList_DoesNotContainDuplicatesInPrimaryList(string
394396
395397 [ Theory ]
396398 [ AutoMockedData ]
397- public void SingleFileInfo_IsParsed ( long length , string fileName , string md5Sum )
399+ public void SingleFileInfo_IsParsed ( long length , string fileName , string fileNameUtf8 , string md5Sum )
398400 {
399401 // Arrange
400402 ParsedData = ValidSingleFileTorrentData ;
401403 var info = ParsedData . Get < BDictionary > ( TorrentFields . Info ) ;
402404 info [ TorrentInfoFields . Length ] = ( BNumber ) length ;
403405 info [ TorrentInfoFields . Name ] = ( BString ) fileName ;
406+ info [ TorrentInfoFields . NameUtf8 ] = ( BString ) fileNameUtf8 ;
404407 info [ TorrentInfoFields . Md5Sum ] = ( BString ) md5Sum ;
405408
406409 // Act
@@ -414,12 +417,34 @@ public void SingleFileInfo_IsParsed(long length, string fileName, string md5Sum)
414417 torrent . File . Should ( ) . NotBeNull ( ) ;
415418 torrent . File . FileSize . Should ( ) . Be ( length ) ;
416419 torrent . File . FileName . Should ( ) . Be ( fileName ) ;
420+ torrent . File . FileNameUtf8 . Should ( ) . Be ( fileNameUtf8 ) ;
417421 torrent . File . Md5Sum . Should ( ) . Be ( md5Sum ) ;
418422 }
419423
424+ [ Fact ]
425+ public void SingleFileInfo_NameUtf8_IsParsedAsUtf8EncodingIndependentlyOfActualEncoding ( )
426+ {
427+ var encoding = "ISO-8859-1" ;
428+ var fileName = "øæå" ; // Use characters with different byte values for UTF8 and ISO-8859-1
429+
430+ ParsedData = ValidSingleFileTorrentData ;
431+ ParsedData [ TorrentFields . Encoding ] = ( BString ) encoding ;
432+ var info = ParsedData . Get < BDictionary > ( TorrentFields . Info ) ;
433+ info [ TorrentInfoFields . Name ] = new BString ( fileName , Encoding . GetEncoding ( encoding ) ) ;
434+ info [ TorrentInfoFields . NameUtf8 ] = new BString ( fileName , Encoding . UTF8 ) ;
435+
436+ // Act
437+ var parser = new TorrentParser ( BencodeParser ) ;
438+ var torrent = parser . Parse ( ( BencodeReader ) null ) ;
439+
440+ // Assert
441+ torrent . File . FileName . Should ( ) . Be ( fileName ) ;
442+ torrent . File . FileNameUtf8 . Should ( ) . Be ( fileName ) ;
443+ }
444+
420445 [ Theory ]
421446 [ AutoMockedData ]
422- public void MultiFileInfo_IsParsed ( string directoryName , long length1 , IList < string > paths1 , string md5Sum1 , long length2 , IList < string > paths2 , string md5Sum2 )
447+ public void MultiFileInfo_IsParsed ( string directoryName , long length1 , IList < string > paths1 , IList < string > paths1Utf8 , string md5Sum1 , long length2 , IList < string > paths2 , IList < string > paths2Utf8 , string md5Sum2 )
423448 {
424449 // Arrange
425450 ParsedData = ValidMultiFileTorrentData ;
@@ -431,12 +456,14 @@ public void MultiFileInfo_IsParsed(string directoryName, long length1, IList<str
431456 {
432457 [ TorrentFilesFields . Length ] = ( BNumber ) length1 ,
433458 [ TorrentFilesFields . Path ] = new BList ( paths1 ) ,
459+ [ TorrentFilesFields . PathUtf8 ] = new BList ( paths1Utf8 ) ,
434460 [ TorrentFilesFields . Md5Sum ] = ( BString ) md5Sum1
435461 } ,
436462 new BDictionary
437463 {
438464 [ TorrentFilesFields . Length ] = ( BNumber ) length2 ,
439465 [ TorrentFilesFields . Path ] = new BList ( paths2 ) ,
466+ [ TorrentFilesFields . PathUtf8 ] = new BList ( paths2Utf8 ) ,
440467 [ TorrentFilesFields . Md5Sum ] = ( BString ) md5Sum2
441468 }
442469 } ;
@@ -453,9 +480,11 @@ public void MultiFileInfo_IsParsed(string directoryName, long length1, IList<str
453480 torrent . Files . Should ( ) . HaveCount ( 2 ) ;
454481 torrent . Files [ 0 ] . FileSize . Should ( ) . Be ( length1 ) ;
455482 torrent . Files [ 0 ] . Path . Should ( ) . BeEquivalentTo ( paths1 ) ;
483+ torrent . Files [ 0 ] . PathUtf8 . Should ( ) . BeEquivalentTo ( paths1Utf8 ) ;
456484 torrent . Files [ 0 ] . Md5Sum . Should ( ) . Be ( md5Sum1 ) ;
457485 torrent . Files [ 1 ] . FileSize . Should ( ) . Be ( length2 ) ;
458486 torrent . Files [ 1 ] . Path . Should ( ) . BeEquivalentTo ( paths2 ) ;
487+ torrent . Files [ 1 ] . PathUtf8 . Should ( ) . BeEquivalentTo ( paths2Utf8 ) ;
459488 torrent . Files [ 1 ] . Md5Sum . Should ( ) . Be ( md5Sum2 ) ;
460489 }
461490
0 commit comments