// C o p y r i g h t © 2 0 2 3 R a n g e p r o o f P t y L t d . A l l r i g h t s r e s e r v e d .
import Foundation
import SessionUtilitiesKit
import Quick
import Nimble
@ testable import SessionSnodeKit
class BencodeResponseSpec : QuickSpec {
override class func spec ( ) {
// MARK: C o n f i g u r a t i o n
@ TestState var dependencies : TestDependencies ! = TestDependencies ( )
// MARK: - B e n c o d e R e s p o n s e
describe ( " BencodeResponse " ) {
// MARK: - - w h e n d e c o d i n g
context ( " when decoding " ) {
// MARK: - - - - w i t h a d e c o d a b l e t y p e
context ( " with a decodable type " ) {
// MARK: - - - - - - d e c o d e s s u c c e s s f u l l y
it ( " decodes successfully " ) {
let data : Data = " ld8:intValuei100e11:stringValue4:Teste5: \ u{01} \ u{02} \ u{03} \ u{04} \ u{05}e "
. data ( using : . utf8 ) !
let result : BencodeResponse < TestType > ? = try ? BencodeDecoder ( using : dependencies )
. decode ( BencodeResponse < TestType > . self , from : data )
expect ( result )
. to ( equal (
BencodeResponse (
info : TestType (
intValue : 100 ,
stringValue : " Test "
) ,
data : Data ( [ 1 , 2 , 3 , 4 , 5 ] )
)
) )
}
// MARK: - - - - - - d e c o d e s s u c c e s s f u l l y w i t h n o b o d y
it ( " decodes successfully with no body " ) {
let data : Data = " ld8:intValuei100e11:stringValue4:Teste "
. data ( using : . utf8 ) !
let result : BencodeResponse < TestType > ? = try ? BencodeDecoder ( using : dependencies )
. decode ( BencodeResponse < TestType > . self , from : data )
expect ( result )
. to ( equal (
BencodeResponse (
info : TestType (
intValue : 100 ,
stringValue : " Test "
) ,
data : nil
)
) )
}
}
// MARK: - - - - w i t h s t r i n g i f i e d j s o n i n f o
context ( " with stringified json info " ) {
// MARK: - - - - - - d e c o d e s s u c c e s s f u l l y
it ( " decodes successfully " ) {
let data : Data = " l37:{ \" intValue \" :100, \" stringValue \" : \" Test \" }5: \ u{01} \ u{02} \ u{03} \ u{04} \ u{05}e "
. data ( using : . utf8 ) !
let result : BencodeResponse < TestType > ? = try ? BencodeDecoder ( using : dependencies )
. decode ( BencodeResponse < TestType > . self , from : data )
expect ( result )
. to ( equal (
BencodeResponse (
info : TestType (
intValue : 100 ,
stringValue : " Test "
) ,
data : Data ( [ 1 , 2 , 3 , 4 , 5 ] )
)
) )
}
// MARK: - - - - - - d e c o d e s s u c c e s s f u l l y w i t h n o b o d y
it ( " decodes successfully with no body " ) {
let data : Data = " l37:{ \" intValue \" :100, \" stringValue \" : \" Test \" }e "
. data ( using : . utf8 ) !
let result : BencodeResponse < TestType > ? = try ? BencodeDecoder ( using : dependencies )
. decode ( BencodeResponse < TestType > . self , from : data )
expect ( result )
. to ( equal (
BencodeResponse (
info : TestType (
intValue : 100 ,
stringValue : " Test "
) ,
data : nil
)
) )
}
// MARK: - - - - - - t h r o w s a p a r s i n g e r r o r w h e n i n v a l i d
it ( " throws a parsing error when invalid " ) {
let data : Data = " l36:{ \" INVALID \" :100, \" stringValue \" : \" Test \" }5: \ u{01} \ u{02} \ u{03} \ u{04} \ u{05}e "
. data ( using : . utf8 ) !
expect {
try BencodeDecoder ( using : dependencies ) . decode ( BencodeResponse < TestType > . self , from : data )
} . to ( throwError ( DecodingError . keyNotFound ( TestType . CodingKeys . intValue , DecodingError . Context ( codingPath : [ ] , debugDescription : " No value associated with key \( TestType . CodingKeys . intValue ) " ) ) ) )
}
}
// MARK: - - - - w i t h a s t r i n g v a l u e
context ( " with a string value " ) {
// MARK: - - - - - - d e c o d e s s u c c e s s f u l l y
it ( " decodes successfully " ) {
let data : Data = " l4:Test5: \ u{01} \ u{02} \ u{03} \ u{04} \ u{05}e " . data ( using : . utf8 ) !
let result : BencodeResponse < String > ? = try ? BencodeDecoder ( using : dependencies )
. decode ( BencodeResponse < String > . self , from : data )
expect ( result )
. to ( equal (
BencodeResponse (
info : " Test " ,
data : Data ( [ 1 , 2 , 3 , 4 , 5 ] )
)
) )
}
// MARK: - - - - - - d e c o d e s s u c c e s s f u l l y w i t h n o b o d y
it ( " decodes successfully with no body " ) {
let data : Data = " l4:Teste " . data ( using : . utf8 ) !
let result : BencodeResponse < String > ? = try ? BencodeDecoder ( using : dependencies )
. decode ( BencodeResponse < String > . self , from : data )
expect ( result )
. to ( equal (
BencodeResponse (
info : " Test " ,
data : nil
)
) )
}
// MARK: - - - - - - t h r o w s a p a r s i n g e r r o r w h e n i n v a l i d
it ( " throws a parsing error when invalid " ) {
let data : Data = " l10:Teste " . data ( using : . utf8 ) !
expect {
try BencodeDecoder ( using : dependencies ) . decode ( BencodeResponse < String > . self , from : data )
} . to ( throwError ( DecodingError . dataCorrupted ( DecodingError . Context ( codingPath : [ ] , debugDescription : " failed to decode String " ) ) ) )
}
}
// MARK: - - - - w i t h a n i n t v a l u e
context ( " with an int value " ) {
// MARK: - - - - - - d e c o d e s s u c c e s s f u l l y
it ( " decodes successfully " ) {
let data : Data = " li100e5: \ u{01} \ u{02} \ u{03} \ u{04} \ u{05}e " . data ( using : . utf8 ) !
let result : BencodeResponse < Int > ? = try ? BencodeDecoder ( using : dependencies )
. decode ( BencodeResponse < Int > . self , from : data )
expect ( result )
. to ( equal (
BencodeResponse (
info : 100 ,
data : Data ( [ 1 , 2 , 3 , 4 , 5 ] )
)
) )
}
// MARK: - - - - - - d e c o d e s s u c c e s s f u l l y w i t h n o b o d y
it ( " decodes successfully with no body " ) {
let data : Data = " li100ee " . data ( using : . utf8 ) !
let result : BencodeResponse < Int > ? = try ? BencodeDecoder ( using : dependencies )
. decode ( BencodeResponse < Int > . self , from : data )
expect ( result )
. to ( equal (
BencodeResponse (
info : 100 ,
data : nil
)
) )
}
// MARK: - - - - - - t h r o w s a p a r s i n g e r r o r w h e n i n v a l i d
it ( " throws a parsing error when invalid " ) {
let data : Data = " l4:Teste " . data ( using : . utf8 ) !
expect {
try BencodeDecoder ( using : dependencies ) . decode ( BencodeResponse < Int > . self , from : data )
} . to ( throwError ( DecodingError . dataCorrupted ( DecodingError . Context (
codingPath : [ ] ,
debugDescription : " The given data was not valid JSON " ,
underlyingError : NSError (
domain : " NSCocoaErrorDomain " ,
code : 3840 ,
userInfo : [
" NSJSONSerializationErrorIndex " : 0 ,
" NSDebugDescription " : " Unexpected character 'T' around line 1, column 1. "
]
)
) ) ) )
}
}
}
}
}
}
// MARK: - T e s t T y p e s
fileprivate struct TestType : Codable , Equatable {
public enum CodingKeys : String , CodingKey {
case intValue
case stringValue
}
let intValue : Int
let stringValue : String
}