Official cwtch.im peer implementation. https://cwtch.im
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
4.1 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package send
  2. import (
  3. "cwtch.im/cwtch/protocol"
  4. "cwtch.im/cwtch/protocol/spam"
  5. "git.openprivacy.ca/openprivacy/libricochet-go/channels"
  6. "git.openprivacy.ca/openprivacy/libricochet-go/wire/control"
  7. "github.com/golang/protobuf/proto"
  8. "testing"
  9. )
  10. type TestHandler struct {
  11. Received bool
  12. }
  13. func (th *TestHandler) HandleGroupMessage(m *protocol.GroupMessage) {
  14. th.Received = true
  15. }
  16. func TestServerSendChannelAttributes(t *testing.T) {
  17. cssc := new(CwtchServerSendChannel)
  18. if cssc.Type() != "im.cwtch.server.send" {
  19. t.Errorf("cwtch channel type is incorrect %v", cssc.Type())
  20. }
  21. if !cssc.OnlyClientCanOpen() {
  22. t.Errorf("only clients should be able to open im.cwtch.server.send channel")
  23. }
  24. if cssc.Bidirectional() {
  25. t.Errorf("im.cwtch.server.send should not be bidirectional")
  26. }
  27. if !cssc.Singleton() {
  28. t.Errorf("im.cwtch.server.send should be a Singleton")
  29. }
  30. if cssc.RequiresAuthentication() != "none" {
  31. t.Errorf("cwtch channel required auth is incorrect %v", cssc.RequiresAuthentication())
  32. }
  33. }
  34. func TestServerSendChannelOpenOutbound(t *testing.T) {
  35. cssc := new(CwtchServerSendChannel)
  36. channel := new(channels.Channel)
  37. _, err := cssc.OpenOutbound(channel)
  38. if err == nil {
  39. t.Errorf("server implementation of im.cwtch.server.send should never open an outbound channel")
  40. }
  41. }
  42. func TestServerSendChannel(t *testing.T) {
  43. cssc := new(CwtchServerSendChannel)
  44. th := new(TestHandler)
  45. cssc.Handler = th
  46. channel := new(channels.Channel)
  47. channel.ID = 1
  48. closed := false
  49. channel.CloseChannel = func() {
  50. closed = true
  51. }
  52. oc := &Protocol_Data_Control.OpenChannel{
  53. ChannelIdentifier: proto.Int32(1),
  54. ChannelType: proto.String(cssc.Type()),
  55. }
  56. resp, err := cssc.OpenInbound(channel, oc)
  57. if err != nil {
  58. t.Errorf("OpenInbound for im.cwtch.server.send should have succeeded, instead: %v", err)
  59. }
  60. control := new(Protocol_Data_Control.Packet)
  61. proto.Unmarshal(resp[:], control)
  62. if control.GetChannelResult() != nil {
  63. var spamguard spam.Guard
  64. spamguard.Difficulty = 2
  65. ce, _ := proto.GetExtension(control.GetChannelResult(), protocol.E_ServerNonce)
  66. challenge := ce.([]byte)[:]
  67. sgsolve := spamguard.SolveChallenge(challenge, []byte("Hello"))
  68. //t.Logf("Solved: %x", sgsolve)
  69. gm := &protocol.GroupMessage{
  70. Ciphertext: []byte("Hello"),
  71. Signature: []byte{},
  72. Spamguard: sgsolve,
  73. }
  74. csp := &protocol.CwtchServerPacket{
  75. GroupMessage: gm,
  76. }
  77. packet, _ := proto.Marshal(csp)
  78. cssc.Packet(packet)
  79. if !th.Received {
  80. t.Errorf("group message should have been received")
  81. }
  82. if !closed {
  83. t.Errorf("im.cwtch.server.send should have been closed after use")
  84. }
  85. } else {
  86. t.Errorf("Expected ChannelResult from im.cwtch.server.send, instead: %v", control)
  87. }
  88. }
  89. func TestServerSendChannelNoSpamGuard(t *testing.T) {
  90. cssc := new(CwtchServerSendChannel)
  91. th := new(TestHandler)
  92. th.Received = false
  93. cssc.Handler = th
  94. channel := new(channels.Channel)
  95. channel.ID = 1
  96. closed := false
  97. channel.CloseChannel = func() {
  98. closed = true
  99. }
  100. oc := &Protocol_Data_Control.OpenChannel{
  101. ChannelIdentifier: proto.Int32(1),
  102. ChannelType: proto.String(cssc.Type()),
  103. }
  104. resp, err := cssc.OpenInbound(channel, oc)
  105. if err != nil {
  106. t.Errorf("OpenInbound for im.cwtch.server.send should have succeeded, instead: %v", err)
  107. }
  108. control := new(Protocol_Data_Control.Packet)
  109. proto.Unmarshal(resp[:], control)
  110. if control.GetChannelResult() != nil {
  111. var spamguard spam.Guard
  112. spamguard.Difficulty = 2
  113. ce, _ := proto.GetExtension(control.GetChannelResult(), protocol.E_ServerNonce)
  114. challenge := ce.([]byte)[:]
  115. sgsolve := spamguard.SolveChallenge(challenge, []byte("4234"))
  116. //t.Logf("Solved: %x", sgsolve)
  117. gm := &protocol.GroupMessage{
  118. Ciphertext: []byte("hello"),
  119. Signature: []byte{},
  120. Spamguard: sgsolve,
  121. }
  122. csp := &protocol.CwtchServerPacket{
  123. GroupMessage: gm,
  124. }
  125. packet, _ := proto.Marshal(csp)
  126. cssc.Packet(packet)
  127. if th.Received == true {
  128. t.Errorf("group message should not have been received")
  129. }
  130. if !closed {
  131. t.Errorf("im.cwtch.server.send should have been closed after use")
  132. }
  133. } else {
  134. t.Errorf("Expected ChannelResult from im.cwtch.server.send, instead: %v", control)
  135. }
  136. }