chat_test.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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.UnmarshalBE(&flap, serverReader))
  21. flapSignonFrame := wire.FLAPSignonFrame{}
  22. assert.NoError(t, wire.UnmarshalBE(&flapSignonFrame, bytes.NewBuffer(flap.Payload)))
  23. // > send FLAPSignonFrame
  24. flapSignonFrame = wire.FLAPSignonFrame{
  25. FLAPVersion: 1,
  26. }
  27. flapSignonFrame.Append(wire.NewTLVBE(wire.OServiceTLVTagsLoginCookie, []byte(`the-chat-login-cookie`)))
  28. buf := &bytes.Buffer{}
  29. assert.NoError(t, wire.MarshalBE(flapSignonFrame, buf))
  30. flap = wire.FLAPFrame{
  31. StartMarker: 42,
  32. FrameType: wire.FLAPFrameSignon,
  33. Payload: buf.Bytes(),
  34. }
  35. assert.NoError(t, wire.MarshalBE(flap, serverWriter))
  36. flapc := wire.NewFlapClient(0, serverReader, serverWriter)
  37. // < receive SNAC_0x01_0x03_OServiceHostOnline
  38. frame := wire.SNACFrame{}
  39. body := wire.SNAC_0x01_0x03_OServiceHostOnline{}
  40. assert.NoError(t, flapc.ReceiveSNAC(&frame, &body))
  41. // send the first request that should get relayed to BOSRouter.Handle
  42. frame = wire.SNACFrame{
  43. FoodGroup: wire.Chat,
  44. SubGroup: wire.ChatNavNavInfo,
  45. }
  46. assert.NoError(t, flapc.SendSNAC(frame, struct{}{}))
  47. assert.NoError(t, serverWriter.Close())
  48. }()
  49. authService := newMockAuthService(t)
  50. authService.EXPECT().
  51. RegisterChatSession(mock.Anything, []byte(`the-chat-login-cookie`)).
  52. Return(sess, nil)
  53. authService.EXPECT().
  54. SignoutChat(mock.Anything, sess)
  55. onlineNotifier := newMockOnlineNotifier(t)
  56. onlineNotifier.EXPECT().
  57. HostOnline().
  58. Return(wire.SNACMessage{
  59. Frame: wire.SNACFrame{
  60. FoodGroup: wire.OService,
  61. SubGroup: wire.OServiceHostOnline,
  62. },
  63. Body: wire.SNAC_0x01_0x03_OServiceHostOnline{},
  64. })
  65. bosRouter := newMockHandler(t)
  66. bosRouter.EXPECT().
  67. Handle(mock.Anything, sess, mock.Anything, mock.Anything, mock.Anything).
  68. Return(nil)
  69. rt := ChatServer{
  70. AuthService: authService,
  71. Handler: bosRouter,
  72. Logger: slog.Default(),
  73. OnlineNotifier: onlineNotifier,
  74. }
  75. rwc := pipeRWC{
  76. PipeReader: clientReader,
  77. PipeWriter: clientWriter,
  78. }
  79. assert.NoError(t, rt.handleNewConnection(context.Background(), rwc))
  80. }