2016-06-27 01:56:23 +00:00
package goricochet
import "testing"
import "time"
import "log"
type TestBadUsageService struct {
StandardRicochetService
BadUsageErrorCount int
UnknownTypeErrorCount int
ChannelClosed int
}
2016-10-09 00:51:13 +00:00
type TestBadUsageConnection struct {
StandardRicochetConnection
Service * TestBadUsageService
}
func ( ts * TestBadUsageService ) OnNewConnection ( oc * OpenConnection ) {
ts . StandardRicochetService . OnNewConnection ( oc )
go oc . Process ( & TestBadUsageConnection { Service : ts } )
}
func ( tc * TestBadUsageConnection ) OnReady ( oc * OpenConnection ) {
2016-06-27 01:56:23 +00:00
if oc . Client {
oc . OpenChannel ( 17 , "im.ricochet.auth.hidden-service" ) // Fail because no Extension
}
2016-10-09 00:51:13 +00:00
tc . StandardRicochetConnection . OnReady ( oc )
2016-06-27 01:56:23 +00:00
if oc . Client {
oc . Authenticate ( 103 ) // Should Fail because cannot open more than one auth-hidden-service channel at once
}
}
2016-10-09 00:51:13 +00:00
func ( tc * TestBadUsageConnection ) OnAuthenticationProof ( channelID int32 , publicKey [ ] byte , signature [ ] byte ) {
tc . Conn . Authenticate ( 2 ) // Try to authenticate again...will fail servers don't auth
tc . Conn . SendContactRequest ( 4 , "test" , "test" ) // Only clients can send contact requests
tc . StandardRicochetConnection . OnAuthenticationProof ( channelID , publicKey , signature )
tc . Conn . OpenChatChannel ( 5 ) // Fail because server can only open even numbered channels
tc . Conn . OpenChatChannel ( 3 ) // Fail because already in use...
2016-06-27 01:56:23 +00:00
}
// OnContactRequest is called when a client sends a new contact request
2016-10-09 00:51:13 +00:00
func ( tc * TestBadUsageConnection ) OnContactRequest ( channelID int32 , nick string , message string ) {
tc . Conn . AckContactRequestOnResponse ( channelID , "Pending" ) // Done to keep the contact request channel open
2016-06-27 01:56:23 +00:00
}
2016-10-09 00:51:13 +00:00
func ( tc * TestBadUsageConnection ) OnAuthenticationResult ( channelID int32 , result bool , isKnownContact bool ) {
tc . StandardRicochetConnection . OnAuthenticationResult ( channelID , result , isKnownContact )
2016-06-27 01:56:23 +00:00
2016-10-09 00:51:13 +00:00
tc . Conn . OpenChatChannel ( 3 ) // Succeed
tc . Conn . OpenChatChannel ( 3 ) // Should fail as duplicate (channel already in use)
2016-06-27 01:56:23 +00:00
2016-10-09 00:51:13 +00:00
tc . Conn . OpenChatChannel ( 6 ) // Should fail because clients are not allowed to open even numbered channels
2016-06-27 01:56:23 +00:00
2016-10-09 00:51:13 +00:00
tc . Conn . SendMessage ( 101 , "test" ) // Should fail as 101 doesn't exist
2016-06-27 01:56:23 +00:00
2016-10-09 00:51:13 +00:00
tc . Conn . Authenticate ( 1 ) // Try to authenticate again...will fail because we have already authenticated
2016-06-27 01:56:23 +00:00
2016-10-09 00:51:13 +00:00
tc . Conn . OpenChannel ( 19 , "im.ricochet.contact.request" ) // Will Fail
tc . Conn . SendContactRequest ( 11 , "test" , "test" ) // Succeed
tc . Conn . SendContactRequest ( 13 , "test" , "test" ) // Trigger singleton contact request check
2016-06-27 01:56:23 +00:00
2016-10-09 00:51:13 +00:00
tc . Conn . OpenChannel ( 15 , "im.ricochet.not-a-real-type" ) // Fail UnknownType
2016-06-27 01:56:23 +00:00
}
// OnChannelClose is called when a client or server closes an existing channel
2016-10-09 00:51:13 +00:00
func ( tc * TestBadUsageConnection ) OnChannelClosed ( channelID int32 ) {
2016-06-27 01:56:23 +00:00
if channelID == 101 {
log . Printf ( "Received Channel Closed: %v" , channelID )
2016-10-09 00:51:13 +00:00
tc . Service . ChannelClosed ++
2016-06-27 01:56:23 +00:00
}
}
2016-10-09 00:51:13 +00:00
func ( tc * TestBadUsageConnection ) OnFailedChannelOpen ( channelID int32 , errorType string ) {
2016-06-27 01:56:23 +00:00
log . Printf ( "Failed Channel Open %v %v" , channelID , errorType )
2016-10-09 00:51:13 +00:00
tc . StandardRicochetConnection . OnFailedChannelOpen ( channelID , errorType )
2016-06-27 01:56:23 +00:00
if errorType == "BadUsageError" {
2016-10-09 00:51:13 +00:00
tc . Service . BadUsageErrorCount ++
2016-06-27 01:56:23 +00:00
} else if errorType == "UnknownTypeError" {
2016-10-09 00:51:13 +00:00
tc . Service . UnknownTypeErrorCount ++
2016-06-27 01:56:23 +00:00
}
}
func TestBadUsageServer ( t * testing . T ) {
ricochetService := new ( TestBadUsageService )
err := ricochetService . Init ( "./private_key" )
if err != nil {
t . Errorf ( "Could not initate ricochet service: %v" , err )
}
go ricochetService . Listen ( ricochetService , 9884 )
time . Sleep ( time . Second * 2 )
ricochetService2 := new ( TestBadUsageService )
err = ricochetService2 . Init ( "./private_key" )
if err != nil {
t . Errorf ( "Could not initate ricochet service: %v" , err )
}
go ricochetService2 . Listen ( ricochetService2 , 9885 )
2016-10-09 00:51:13 +00:00
oc , err := ricochetService2 . Connect ( "127.0.0.1:9884|kwke2hntvyfqm7dr" )
2016-06-27 01:56:23 +00:00
if err != nil {
t . Errorf ( "Could not connect to ricochet service: %v" , err )
}
2016-10-09 00:51:13 +00:00
go oc . Process ( & TestBadUsageConnection {
Service : ricochetService2 ,
StandardRicochetConnection : StandardRicochetConnection {
PrivateKey : ricochetService2 . PrivateKey ,
} ,
} )
2016-06-27 01:56:23 +00:00
time . Sleep ( time . Second * 3 )
if ricochetService2 . ChannelClosed != 1 || ricochetService2 . BadUsageErrorCount != 7 || ricochetService . BadUsageErrorCount != 4 || ricochetService2 . UnknownTypeErrorCount != 1 {
t . Errorf ( "Invalid number of errors seen Closed:%v, Client Bad Usage:%v UnknownTypeErrorCount: %v, Server Bad Usage: %v " , ricochetService2 . ChannelClosed , ricochetService2 . BadUsageErrorCount , ricochetService2 . UnknownTypeErrorCount , ricochetService . BadUsageErrorCount )
}
}