2016-06-27 01:56:23 +00:00
|
|
|
package goricochet
|
|
|
|
|
|
|
|
import "testing"
|
|
|
|
import "time"
|
|
|
|
import "log"
|
|
|
|
|
|
|
|
type TestUnknownContactService struct {
|
|
|
|
StandardRicochetService
|
2016-10-09 00:51:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ts *TestUnknownContactService) OnNewConnection(oc *OpenConnection) {
|
|
|
|
go oc.Process(&TestUnknownContactConnection{})
|
|
|
|
}
|
|
|
|
|
|
|
|
type TestUnknownContactConnection struct {
|
|
|
|
StandardRicochetConnection
|
2016-06-27 01:56:23 +00:00
|
|
|
FailedToOpen bool
|
|
|
|
}
|
|
|
|
|
2016-10-09 00:51:13 +00:00
|
|
|
func (tc *TestUnknownContactConnection) IsKnownContact(hostname string) bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tc *TestUnknownContactConnection) OnAuthenticationProof(channelID int32, publicKey, signature []byte) {
|
|
|
|
result := tc.Conn.ValidateProof(channelID, publicKey, signature)
|
|
|
|
tc.Conn.SendAuthenticationResult(channelID, result, false)
|
|
|
|
tc.Conn.IsAuthed = result
|
|
|
|
tc.Conn.CloseChannel(channelID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tc *TestUnknownContactConnection) OnAuthenticationResult(channelID int32, result bool, isKnownContact bool) {
|
2016-06-27 01:56:23 +00:00
|
|
|
log.Printf("Authentication Result")
|
2016-10-09 00:51:13 +00:00
|
|
|
tc.StandardRicochetConnection.OnAuthenticationResult(channelID, result, isKnownContact)
|
|
|
|
tc.Conn.OpenChatChannel(5)
|
2016-06-27 01:56:23 +00:00
|
|
|
}
|
|
|
|
|
2016-10-09 00:51:13 +00:00
|
|
|
func (tc *TestUnknownContactConnection) OnFailedChannelOpen(channelID int32, errorType string) {
|
2016-06-27 01:56:23 +00:00
|
|
|
log.Printf("Failed Channel Open %v", errorType)
|
2016-10-09 00:51:13 +00:00
|
|
|
tc.Conn.UnsetChannel(channelID)
|
2016-06-27 01:56:23 +00:00
|
|
|
if errorType == "UnauthorizedError" {
|
2016-10-09 00:51:13 +00:00
|
|
|
tc.FailedToOpen = true
|
2016-06-27 01:56:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnknownContactServer(t *testing.T) {
|
2016-10-09 00:51:13 +00:00
|
|
|
ricochetService := new(TestUnknownContactService)
|
2016-06-27 01:56:23 +00:00
|
|
|
err := ricochetService.Init("./private_key")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Could not initate ricochet service: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
go ricochetService.Listen(ricochetService, 9882)
|
|
|
|
|
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
|
2016-10-09 00:51:13 +00:00
|
|
|
oc, err := ricochetService.Connect("127.0.0.1:9882|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
|
|
|
connectionHandler := &TestUnknownContactConnection{
|
|
|
|
StandardRicochetConnection: StandardRicochetConnection{
|
|
|
|
PrivateKey: ricochetService.PrivateKey,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
go oc.Process(connectionHandler)
|
2016-06-27 01:56:23 +00:00
|
|
|
|
|
|
|
time.Sleep(time.Second * 2)
|
2016-10-09 00:51:13 +00:00
|
|
|
if !connectionHandler.FailedToOpen {
|
2016-06-27 01:56:23 +00:00
|
|
|
t.Errorf("Test server did receive message should have failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|