connection_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package oscar
  2. import (
  3. "context"
  4. "io"
  5. "log/slog"
  6. "sync"
  7. "testing"
  8. "github.com/mk6i/retro-aim-server/config"
  9. "github.com/mk6i/retro-aim-server/state"
  10. "github.com/mk6i/retro-aim-server/wire"
  11. "github.com/stretchr/testify/assert"
  12. "github.com/stretchr/testify/mock"
  13. )
  14. func TestHandleChatConnection_MessageRelay(t *testing.T) {
  15. sessionManager := state.NewInMemorySessionManager(slog.Default())
  16. // add a user to session that will receive relayed messages
  17. sess := sessionManager.AddSession("bob-sess-id", "bob")
  18. // start the server connection handler in the background
  19. serverReader, _ := io.Pipe()
  20. clientReader, serverWriter := io.Pipe()
  21. go func() {
  22. flapc := &flapClient{
  23. w: serverWriter,
  24. }
  25. err := dispatchIncomingMessages(context.Background(), sess, flapc, serverReader, slog.Default(), nil, config.Config{})
  26. assert.NoError(t, err)
  27. }()
  28. inboundMsgs := []wire.SNACMessage{
  29. {
  30. Frame: wire.SNACFrame{
  31. FoodGroup: wire.Chat,
  32. SubGroup: wire.ChatUsersJoined,
  33. },
  34. Body: wire.SNAC_0x0E_0x03_ChatUsersJoined{
  35. Users: []wire.TLVUserInfo{
  36. {
  37. ScreenName: "screenname1",
  38. },
  39. },
  40. },
  41. },
  42. {
  43. Frame: wire.SNACFrame{
  44. FoodGroup: wire.Chat,
  45. SubGroup: wire.ChatUsersLeft,
  46. },
  47. Body: wire.SNAC_0x0E_0x03_ChatUsersJoined{
  48. Users: []wire.TLVUserInfo{
  49. {
  50. ScreenName: "screenname2",
  51. },
  52. },
  53. },
  54. },
  55. }
  56. // relay messages to user session
  57. for _, msg := range inboundMsgs {
  58. sessionManager.RelayToScreenName(context.Background(), "bob", msg)
  59. }
  60. // consume and verify the relayed messages
  61. for i := 0; i < len(inboundMsgs); i++ {
  62. flap := wire.FLAPFrame{}
  63. assert.NoError(t, wire.Unmarshal(&flap, clientReader))
  64. snac, err := flap.ReadBody(clientReader)
  65. assert.NoError(t, err)
  66. frame := wire.SNACFrame{}
  67. assert.NoError(t, wire.Unmarshal(&frame, snac))
  68. assert.Equal(t, inboundMsgs[i].Frame, frame)
  69. body := wire.SNAC_0x0E_0x03_ChatUsersJoined{}
  70. assert.NoError(t, wire.Unmarshal(&body, snac))
  71. assert.Equal(t, inboundMsgs[i].Body, body)
  72. }
  73. // stop the session, which terminates the connection handler goroutine
  74. sess.Close()
  75. <-sess.Closed()
  76. // verify the connection handler sends client disconnection message before
  77. // terminating
  78. flap := wire.FLAPFrame{}
  79. assert.NoError(t, wire.Unmarshal(&flap, clientReader))
  80. assert.Equal(t, wire.FLAPFrameSignoff, flap.FrameType)
  81. }
  82. func TestHandleChatConnection_ClientRequest(t *testing.T) {
  83. sessionManager := state.NewInMemorySessionManager(slog.Default())
  84. // add session so that the function can terminate upon closure
  85. sess := sessionManager.AddSession("bob-sess-id", "bob")
  86. inboundMsgs := []wire.SNACMessage{
  87. {
  88. Frame: wire.SNACFrame{
  89. FoodGroup: wire.Chat,
  90. SubGroup: wire.ChatUsersJoined,
  91. },
  92. Body: wire.SNAC_0x0E_0x03_ChatUsersJoined{
  93. Users: []wire.TLVUserInfo{
  94. {
  95. ScreenName: "screenname1",
  96. },
  97. },
  98. },
  99. },
  100. {
  101. Frame: wire.SNACFrame{
  102. FoodGroup: wire.Chat,
  103. SubGroup: wire.ChatUsersLeft,
  104. },
  105. Body: wire.SNAC_0x0E_0x03_ChatUsersJoined{
  106. Users: []wire.TLVUserInfo{
  107. {
  108. ScreenName: "screenname2",
  109. },
  110. },
  111. },
  112. },
  113. }
  114. wg := &sync.WaitGroup{}
  115. wg.Add(len(inboundMsgs))
  116. // set up mock handlers to receive messages and verify their contents
  117. router := newMockHandler(t)
  118. for _, msg := range inboundMsgs {
  119. msg := msg
  120. router.EXPECT().
  121. Handle(mock.Anything, sess, msg.Frame, mock.Anything, mock.Anything).
  122. Run(func(ctx context.Context, sess *state.Session, inFrame wire.SNACFrame, r io.Reader, rw ResponseWriter) {
  123. defer wg.Done()
  124. body := wire.SNAC_0x0E_0x03_ChatUsersJoined{}
  125. assert.NoError(t, wire.Unmarshal(&body, r))
  126. assert.Equal(t, msg.Body, body)
  127. }).
  128. Return(nil)
  129. }
  130. // start the server connection handler in the background
  131. serverReader, clientWriter := io.Pipe()
  132. clientReader, serverWriter := io.Pipe()
  133. go func() {
  134. flapc := &flapClient{
  135. w: serverWriter,
  136. }
  137. assert.NoError(t, dispatchIncomingMessages(context.Background(), sess, flapc, serverReader, slog.Default(), router, config.Config{}))
  138. }()
  139. // send client messages
  140. flapc := flapClient{
  141. w: clientWriter,
  142. }
  143. for _, msg := range inboundMsgs {
  144. err := flapc.SendSNAC(msg.Frame, msg.Body)
  145. assert.NoError(t, err)
  146. }
  147. wg.Wait()
  148. // stop the session, which terminates the connection handler goroutine
  149. sess.Close()
  150. <-sess.Closed()
  151. // verify the connection handler sends client disconnection message before
  152. // terminating
  153. flap := wire.FLAPFrame{}
  154. assert.NoError(t, wire.Unmarshal(&flap, clientReader))
  155. assert.Equal(t, wire.FLAPFrameSignoff, flap.FrameType)
  156. }