2018-03-10 21:26:19 +00:00
|
|
|
package send
|
2018-03-09 20:44:13 +00:00
|
|
|
|
|
|
|
import (
|
2018-05-28 18:05:06 +00:00
|
|
|
"cwtch.im/cwtch/protocol"
|
|
|
|
"cwtch.im/cwtch/protocol/spam"
|
2018-06-23 16:15:36 +00:00
|
|
|
"git.openprivacy.ca/openprivacy/libricochet-go/channels"
|
|
|
|
"git.openprivacy.ca/openprivacy/libricochet-go/wire/control"
|
2018-06-29 19:20:07 +00:00
|
|
|
"github.com/golang/protobuf/proto"
|
2018-03-09 20:44:13 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPeerSendChannelAttributes(t *testing.T) {
|
|
|
|
cssc := new(CwtchPeerSendChannel)
|
|
|
|
if cssc.Type() != "im.cwtch.server.send" {
|
|
|
|
t.Errorf("cwtch channel type is incorrect %v", cssc.Type())
|
|
|
|
}
|
|
|
|
|
|
|
|
if !cssc.OnlyClientCanOpen() {
|
|
|
|
t.Errorf("only clients should be able to open im.cwtch.server.send channel")
|
|
|
|
}
|
|
|
|
|
|
|
|
if cssc.Bidirectional() {
|
|
|
|
t.Errorf("im.cwtch.server.listen should not be bidirectional")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !cssc.Singleton() {
|
|
|
|
t.Errorf("im.cwtch.server.listen should be a Singleton")
|
|
|
|
}
|
|
|
|
|
|
|
|
if cssc.RequiresAuthentication() != "none" {
|
|
|
|
t.Errorf("cwtch channel required auth is incorrect %v", cssc.RequiresAuthentication())
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPeerSendChannelOpenInbound(t *testing.T) {
|
|
|
|
cssc := new(CwtchPeerSendChannel)
|
|
|
|
channel := new(channels.Channel)
|
|
|
|
_, err := cssc.OpenInbound(channel, nil)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("client implementation of im.cwtch.server.Listen should never open an inbound channel")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPeerSendChannelClosesOnPacket(t *testing.T) {
|
|
|
|
pfc := new(CwtchPeerSendChannel)
|
|
|
|
channel := new(channels.Channel)
|
|
|
|
closed := false
|
2018-03-09 21:02:42 +00:00
|
|
|
channel.CloseChannel = func() {
|
|
|
|
closed = true
|
2018-03-09 20:44:13 +00:00
|
|
|
}
|
|
|
|
|
2018-03-09 21:02:42 +00:00
|
|
|
pfc.OpenOutbound(channel)
|
|
|
|
pfc.Packet([]byte{})
|
|
|
|
if !closed {
|
|
|
|
t.Errorf("send channel should close if server attempts to send packets")
|
|
|
|
}
|
2018-03-09 20:44:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPeerSendChannel(t *testing.T) {
|
|
|
|
pfc := new(CwtchPeerSendChannel)
|
|
|
|
|
|
|
|
channel := new(channels.Channel)
|
|
|
|
channel.ID = 3
|
|
|
|
success := false
|
2018-03-09 21:02:42 +00:00
|
|
|
|
2018-03-14 22:23:35 +00:00
|
|
|
var sg spam.Guard
|
2018-03-09 20:44:13 +00:00
|
|
|
sg.Difficulty = 2
|
2018-03-09 21:02:42 +00:00
|
|
|
|
2018-03-31 19:33:32 +00:00
|
|
|
closed := false
|
|
|
|
channel.CloseChannel = func() {
|
|
|
|
closed = true
|
|
|
|
}
|
|
|
|
|
2018-03-09 20:44:13 +00:00
|
|
|
channel.SendMessage = func(message []byte) {
|
2018-03-09 21:02:42 +00:00
|
|
|
packet := new(protocol.CwtchServerPacket)
|
|
|
|
proto.Unmarshal(message[:], packet)
|
|
|
|
if packet.GetGroupMessage() != nil {
|
|
|
|
success = sg.ValidateChallenge(packet.GetGroupMessage().GetCiphertext(), packet.GetGroupMessage().GetSpamguard())
|
|
|
|
}
|
2018-03-09 20:44:13 +00:00
|
|
|
}
|
2018-03-09 21:02:42 +00:00
|
|
|
result, err := pfc.OpenOutbound(channel)
|
2018-03-09 20:44:13 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("expected result but also got non-nil error: result:%v, err: %v", result, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
challenge := sg.GenerateChallenge(3)
|
2018-03-09 21:02:42 +00:00
|
|
|
control := new(Protocol_Data_Control.Packet)
|
2018-03-09 20:44:13 +00:00
|
|
|
proto.Unmarshal(challenge[:], control)
|
|
|
|
|
|
|
|
pfc.OpenOutboundResult(nil, control.GetChannelResult())
|
|
|
|
if channel.Pending {
|
|
|
|
t.Errorf("once opened channel should no longer be pending")
|
|
|
|
}
|
|
|
|
|
2018-03-09 21:02:42 +00:00
|
|
|
gm := &protocol.GroupMessage{Ciphertext: []byte("hello")}
|
|
|
|
pfc.SendGroupMessage(gm)
|
|
|
|
if !success {
|
|
|
|
t.Errorf("send channel should have successfully sent a valid group message")
|
|
|
|
}
|
2018-03-09 20:44:13 +00:00
|
|
|
|
2018-03-31 19:33:32 +00:00
|
|
|
if !closed {
|
|
|
|
t.Errorf("send channel should have successfully closed after a valid group message")
|
|
|
|
}
|
|
|
|
|
2018-03-09 20:44:13 +00:00
|
|
|
pfc.Closed(nil)
|
|
|
|
|
|
|
|
}
|