client_test.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package oscarclient
  2. import (
  3. "context"
  4. "errors"
  5. "log/slog"
  6. "net"
  7. "os"
  8. "testing"
  9. "time"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/stretchr/testify/require"
  12. "github.com/mk6i/open-oscar-server/wire"
  13. )
  14. // stubServer serves each connection with serve on a goroutine of its own, or
  15. // refuses it with refuse if set.
  16. type stubServer struct {
  17. serve func(conn net.Conn)
  18. refuse error
  19. }
  20. func (s stubServer) ServeConn(conn net.Conn) error {
  21. if s.refuse != nil {
  22. _ = conn.Close()
  23. return s.refuse
  24. }
  25. go func() {
  26. defer func() { _ = conn.Close() }()
  27. s.serve(conn)
  28. }()
  29. return nil
  30. }
  31. func TestClient_serverRefusesConnection(t *testing.T) {
  32. refused := errors.New("shutting down")
  33. c := Client{Server: stubServer{refuse: refused}, Logger: slog.Default()}
  34. clientAddr := &net.TCPAddr{IP: net.IPv4(10, 0, 0, 5), Port: 4321}
  35. _, err := c.Login(clientAddr, nil)
  36. assert.ErrorIs(t, err, refused)
  37. _, err = c.ConnectService(context.Background(), clientAddr, []byte("cookie"))
  38. assert.ErrorIs(t, err, refused)
  39. }
  40. func TestClient_ConnectService(t *testing.T) {
  41. later := make(chan struct{})
  42. server := stubServer{
  43. serve: func(conn net.Conn) {
  44. flapc := wire.NewFlapClient(100, conn, conn)
  45. assert.NoError(t, flapc.SendSignonFrame(nil))
  46. _, err := flapc.ReceiveSignonFrame()
  47. assert.NoError(t, err)
  48. assert.NoError(t, flapc.SendSNAC(
  49. wire.SNACFrame{FoodGroup: wire.OService, SubGroup: wire.OServiceHostOnline},
  50. wire.SNAC_0x01_0x03_OServiceHostOnline{}))
  51. <-later
  52. assert.NoError(t, flapc.SendSNAC(
  53. wire.SNACFrame{FoodGroup: wire.OService, SubGroup: wire.OServiceEvilNotification},
  54. wire.SNAC_0x01_0x10_OServiceEvilNotification{NewEvil: 1}))
  55. },
  56. }
  57. c := Client{Server: server, Logger: slog.Default()}
  58. ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
  59. defer cancel()
  60. conn, err := c.ConnectService(ctx, &net.TCPAddr{IP: net.IPv4(10, 0, 0, 5), Port: 4321}, []byte("cookie"))
  61. require.NoError(t, err)
  62. defer conn.Close()
  63. go conn.Run(context.Background())
  64. // The handshake's deadline doesn't outlive it.
  65. <-ctx.Done()
  66. time.Sleep(20 * time.Millisecond)
  67. close(later)
  68. select {
  69. case msg := <-conn.Messages():
  70. assert.Equal(t, wire.SNAC_0x01_0x10_OServiceEvilNotification{NewEvil: 1}, msg.Body)
  71. case <-conn.Closed():
  72. t.Fatal("the connection closed after the handshake's deadline")
  73. case <-time.After(time.Second):
  74. t.Fatal("no SNAC arrived")
  75. }
  76. }
  77. func TestClient_ConnectService_handshakeDeadline(t *testing.T) {
  78. server := stubServer{
  79. serve: func(conn net.Conn) {
  80. // never starts the handshake; returns once the client hangs up
  81. _, _ = conn.Read(make([]byte, 1))
  82. },
  83. }
  84. c := Client{Server: server, Logger: slog.Default()}
  85. ctx, cancel := context.WithTimeout(context.Background(), 20*time.Millisecond)
  86. defer cancel()
  87. errCh := make(chan error, 1)
  88. go func() {
  89. _, err := c.ConnectService(ctx, &net.TCPAddr{IP: net.IPv4(10, 0, 0, 5), Port: 4321}, []byte("cookie"))
  90. errCh <- err
  91. }()
  92. select {
  93. case err := <-errCh:
  94. assert.ErrorIs(t, err, os.ErrDeadlineExceeded)
  95. case <-time.After(time.Second):
  96. t.Fatal("ConnectService outlived its deadline")
  97. }
  98. }
  99. func TestClient_Login(t *testing.T) {
  100. clientAddr := &net.TCPAddr{IP: net.IPv4(10, 0, 0, 5), Port: 4321}
  101. signonTLVs := []wire.TLV{
  102. wire.NewTLVBE(wire.LoginTLVTagsScreenName, "me"),
  103. wire.NewTLVBE(wire.LoginTLVTagsRoastedTOCPassword, []byte("roasted")),
  104. }
  105. tests := []struct {
  106. name string
  107. // reply answers the signon; nil hangs up without answering
  108. reply func(t *testing.T, flapc *wire.FlapClient)
  109. want wire.TLVRestBlock
  110. wantErr string
  111. }{
  112. {
  113. name: "signoff with a login cookie",
  114. reply: func(t *testing.T, flapc *wire.FlapClient) {
  115. assert.NoError(t, flapc.NewSignoff(wire.TLVRestBlock{TLVList: wire.TLVList{
  116. wire.NewTLVBE(wire.LoginTLVTagsAuthorizationCookie, []byte("cookie")),
  117. }}))
  118. },
  119. want: wire.TLVRestBlock{TLVList: wire.TLVList{
  120. wire.NewTLVBE(wire.LoginTLVTagsAuthorizationCookie, []byte("cookie")),
  121. }},
  122. },
  123. {
  124. name: "signoff with an error code",
  125. reply: func(t *testing.T, flapc *wire.FlapClient) {
  126. assert.NoError(t, flapc.NewSignoff(wire.TLVRestBlock{TLVList: wire.TLVList{
  127. wire.NewTLVBE(wire.LoginTLVTagsErrorSubcode, wire.LoginErrInvalidPassword),
  128. }}))
  129. },
  130. want: wire.TLVRestBlock{TLVList: wire.TLVList{
  131. wire.NewTLVBE(wire.LoginTLVTagsErrorSubcode, wire.LoginErrInvalidPassword),
  132. }},
  133. },
  134. {
  135. name: "BUCP login response",
  136. reply: func(t *testing.T, flapc *wire.FlapClient) {
  137. assert.NoError(t, flapc.SendSNAC(
  138. wire.SNACFrame{FoodGroup: wire.BUCP, SubGroup: wire.BUCPLoginResponse},
  139. wire.SNAC_0x17_0x03_BUCPLoginResponse{TLVRestBlock: wire.TLVRestBlock{TLVList: wire.TLVList{
  140. wire.NewTLVBE(wire.LoginTLVTagsErrorSubcode, wire.LoginErrRateLimitExceeded),
  141. }}}))
  142. },
  143. want: wire.TLVRestBlock{TLVList: wire.TLVList{
  144. wire.NewTLVBE(wire.LoginTLVTagsErrorSubcode, wire.LoginErrRateLimitExceeded),
  145. }},
  146. },
  147. {
  148. name: "SNAC other than a BUCP login response",
  149. reply: func(t *testing.T, flapc *wire.FlapClient) {
  150. assert.NoError(t, flapc.SendSNAC(
  151. wire.SNACFrame{FoodGroup: wire.OService, SubGroup: wire.OServiceHostOnline},
  152. wire.SNAC_0x01_0x03_OServiceHostOnline{}))
  153. },
  154. wantErr: "unexpected login reply",
  155. },
  156. {
  157. name: "frame other than signoff or data",
  158. reply: func(t *testing.T, flapc *wire.FlapClient) {
  159. assert.NoError(t, flapc.SendKeepAliveFrame())
  160. },
  161. wantErr: "unexpected login reply frame type",
  162. },
  163. {
  164. name: "server hangs up without replying",
  165. wantErr: "ReceiveFLAP",
  166. },
  167. }
  168. for _, tt := range tests {
  169. t.Run(tt.name, func(t *testing.T) {
  170. server := stubServer{
  171. serve: func(conn net.Conn) {
  172. assert.Equal(t, clientAddr, conn.RemoteAddr())
  173. flapc := wire.NewFlapClient(100, conn, conn)
  174. assert.NoError(t, flapc.SendSignonFrame(nil))
  175. signon, err := flapc.ReceiveSignonFrame()
  176. assert.NoError(t, err)
  177. assert.Equal(t, wire.TLVList(signonTLVs), signon.TLVList)
  178. if tt.reply != nil {
  179. tt.reply(t, flapc)
  180. }
  181. },
  182. }
  183. c := Client{Server: server, Logger: slog.Default()}
  184. got, err := c.Login(clientAddr, signonTLVs)
  185. if tt.wantErr != "" {
  186. assert.ErrorContains(t, err, tt.wantErr)
  187. return
  188. }
  189. assert.NoError(t, err)
  190. assert.Equal(t, tt.want, got)
  191. })
  192. }
  193. }