auth_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package oscar
  2. import (
  3. "bytes"
  4. "io"
  5. "log/slog"
  6. "testing"
  7. "github.com/mk6i/retro-aim-server/wire"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/mock"
  10. )
  11. func TestBUCPAuthService_handleNewConnection(t *testing.T) {
  12. clientReader, serverWriter := io.Pipe()
  13. serverReader, clientWriter := io.Pipe()
  14. go func() {
  15. // < receive FLAPSignonFrame
  16. flap := wire.FLAPFrame{}
  17. assert.NoError(t, wire.Unmarshal(&flap, serverReader))
  18. buf, err := flap.ReadBody(serverReader)
  19. assert.NoError(t, err)
  20. flapSignonFrame := wire.FLAPSignonFrame{}
  21. assert.NoError(t, wire.Unmarshal(&flapSignonFrame, buf))
  22. // > send FLAPSignonFrame
  23. flapSignonFrame = wire.FLAPSignonFrame{
  24. FLAPVersion: 1,
  25. }
  26. buf = &bytes.Buffer{}
  27. assert.NoError(t, wire.Marshal(flapSignonFrame, buf))
  28. flap = wire.FLAPFrame{
  29. StartMarker: 42,
  30. FrameType: wire.FLAPFrameSignon,
  31. PayloadLength: uint16(buf.Len()),
  32. }
  33. assert.NoError(t, wire.Marshal(flap, serverWriter))
  34. _, err = serverWriter.Write(buf.Bytes())
  35. assert.NoError(t, err)
  36. // > send SNAC_0x17_0x06_BUCPChallengeRequest
  37. flapc := flapClient{
  38. r: serverReader,
  39. w: serverWriter,
  40. }
  41. frame := wire.SNACFrame{
  42. FoodGroup: wire.BUCP,
  43. SubGroup: wire.BUCPChallengeRequest,
  44. }
  45. bodyIn := wire.SNAC_0x17_0x06_BUCPChallengeRequest{}
  46. assert.NoError(t, flapc.SendSNAC(frame, bodyIn))
  47. // < receive SNAC_0x17_0x07_BUCPChallengeResponse
  48. frame = wire.SNACFrame{}
  49. assert.NoError(t, flapc.ReceiveSNAC(&frame, &wire.SNAC_0x17_0x07_BUCPChallengeResponse{}))
  50. assert.Equal(t, wire.SNACFrame{FoodGroup: wire.BUCP, SubGroup: wire.BUCPChallengeResponse}, frame)
  51. // > send SNAC_0x17_0x02_BUCPLoginRequest
  52. frame = wire.SNACFrame{
  53. FoodGroup: wire.BUCP,
  54. SubGroup: wire.BUCPLoginRequest,
  55. }
  56. assert.NoError(t, flapc.SendSNAC(frame, wire.SNAC_0x17_0x02_BUCPLoginRequest{}))
  57. // < receive SNAC_0x17_0x03_BUCPLoginResponse
  58. frame = wire.SNACFrame{}
  59. assert.NoError(t, flapc.ReceiveSNAC(&frame, &wire.SNAC_0x17_0x03_BUCPLoginResponse{}))
  60. assert.Equal(t, wire.SNACFrame{FoodGroup: wire.BUCP, SubGroup: wire.BUCPLoginResponse}, frame)
  61. assert.NoError(t, serverWriter.Close())
  62. }()
  63. authService := newMockAuthService(t)
  64. authService.EXPECT().
  65. BUCPChallenge(mock.Anything, mock.Anything).
  66. Return(wire.SNACMessage{
  67. Frame: wire.SNACFrame{
  68. FoodGroup: wire.BUCP,
  69. SubGroup: wire.BUCPChallengeResponse,
  70. },
  71. Body: wire.SNAC_0x17_0x07_BUCPChallengeResponse{},
  72. }, nil)
  73. authService.EXPECT().
  74. BUCPLogin(mock.Anything, mock.Anything, mock.Anything).
  75. Return(wire.SNACMessage{
  76. Frame: wire.SNACFrame{
  77. FoodGroup: wire.BUCP,
  78. SubGroup: wire.BUCPLoginResponse,
  79. },
  80. Body: wire.SNAC_0x17_0x03_BUCPLoginResponse{},
  81. }, nil)
  82. rt := AuthServer{
  83. AuthService: authService,
  84. Logger: slog.Default(),
  85. }
  86. rwc := pipeRWC{
  87. PipeReader: clientReader,
  88. PipeWriter: clientWriter,
  89. }
  90. assert.NoError(t, rt.handleNewConnection(rwc))
  91. }