chat_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package oscar
  2. import (
  3. "bytes"
  4. "context"
  5. "io"
  6. "log/slog"
  7. "testing"
  8. "github.com/mk6i/retro-aim-server/state"
  9. "github.com/mk6i/retro-aim-server/wire"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/stretchr/testify/mock"
  12. )
  13. func TestChatService_handleNewConnection(t *testing.T) {
  14. sess := state.NewSession()
  15. clientReader, serverWriter := io.Pipe()
  16. serverReader, clientWriter := io.Pipe()
  17. go func() {
  18. // < receive FLAPSignonFrame
  19. flap := wire.FLAPFrame{}
  20. assert.NoError(t, wire.Unmarshal(&flap, serverReader))
  21. buf, err := flap.ReadBody(serverReader)
  22. assert.NoError(t, err)
  23. flapSignonFrame := wire.FLAPSignonFrame{}
  24. assert.NoError(t, wire.Unmarshal(&flapSignonFrame, buf))
  25. // > send FLAPSignonFrame
  26. flapSignonFrame = wire.FLAPSignonFrame{
  27. FLAPVersion: 1,
  28. }
  29. flapSignonFrame.Append(wire.NewTLV(wire.OServiceTLVTagsLoginCookie, []byte(`the-chat-login-cookie`)))
  30. buf = &bytes.Buffer{}
  31. assert.NoError(t, wire.Marshal(flapSignonFrame, buf))
  32. flap = wire.FLAPFrame{
  33. StartMarker: 42,
  34. FrameType: wire.FLAPFrameSignon,
  35. PayloadLength: uint16(buf.Len()),
  36. }
  37. assert.NoError(t, wire.Marshal(flap, serverWriter))
  38. _, err = serverWriter.Write(buf.Bytes())
  39. assert.NoError(t, err)
  40. // < receive SNAC_0x01_0x03_OServiceHostOnline
  41. flap = wire.FLAPFrame{}
  42. assert.NoError(t, wire.Unmarshal(&flap, serverReader))
  43. buf, err = flap.ReadBody(serverReader)
  44. assert.NoError(t, err)
  45. frame := wire.SNACFrame{}
  46. assert.NoError(t, wire.Unmarshal(&frame, buf))
  47. body := wire.SNAC_0x01_0x03_OServiceHostOnline{}
  48. assert.NoError(t, wire.Unmarshal(&body, buf))
  49. // send the first request that should get relayed to BOSRouter.Handle
  50. flapc := wire.NewFlapClient(0, nil, serverWriter)
  51. frame = wire.SNACFrame{
  52. FoodGroup: wire.Chat,
  53. SubGroup: wire.ChatNavNavInfo,
  54. }
  55. assert.NoError(t, flapc.SendSNAC(frame, struct{}{}))
  56. assert.NoError(t, serverWriter.Close())
  57. }()
  58. authService := newMockAuthService(t)
  59. authService.EXPECT().
  60. RegisterChatSession([]byte(`the-chat-login-cookie`)).
  61. Return(sess, nil)
  62. authService.EXPECT().
  63. SignoutChat(mock.Anything, sess)
  64. onlineNotifier := newMockOnlineNotifier(t)
  65. onlineNotifier.EXPECT().
  66. HostOnline().
  67. Return(wire.SNACMessage{
  68. Frame: wire.SNACFrame{
  69. FoodGroup: wire.OService,
  70. SubGroup: wire.OServiceHostOnline,
  71. },
  72. Body: wire.SNAC_0x01_0x03_OServiceHostOnline{},
  73. })
  74. bosRouter := newMockHandler(t)
  75. bosRouter.EXPECT().
  76. Handle(mock.Anything, sess, mock.Anything, mock.Anything, mock.Anything).
  77. Return(nil)
  78. rt := ChatServer{
  79. AuthService: authService,
  80. Handler: bosRouter,
  81. Logger: slog.Default(),
  82. OnlineNotifier: onlineNotifier,
  83. }
  84. rwc := pipeRWC{
  85. PipeReader: clientReader,
  86. PipeWriter: clientWriter,
  87. }
  88. assert.NoError(t, rt.handleNewConnection(context.Background(), rwc))
  89. }