alert_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package server
  2. import (
  3. "bytes"
  4. "context"
  5. "testing"
  6. "github.com/mkaminski/goaim/oscar"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestAlertRouter_RouteAlert(t *testing.T) {
  10. cases := []struct {
  11. // name is the unit test name
  12. name string
  13. // input is the request payload
  14. input oscar.SNACMessage
  15. // output is the response payload
  16. output oscar.SNACMessage
  17. // handlerErr is the mocked handler error response
  18. handlerErr error
  19. // expectErr is the expected error returned by the router
  20. expectErr error
  21. }{
  22. {
  23. name: "receive AlertNotifyCapabilities, return no response",
  24. input: oscar.SNACMessage{
  25. Frame: oscar.SNACFrame{
  26. FoodGroup: oscar.Alert,
  27. SubGroup: oscar.AlertNotifyCapabilities,
  28. },
  29. Body: oscar.SNACFrame{},
  30. },
  31. output: oscar.SNACMessage{},
  32. },
  33. {
  34. name: "receive AlertNotifyDisplayCapabilities, return no response",
  35. input: oscar.SNACMessage{
  36. Frame: oscar.SNACFrame{
  37. FoodGroup: oscar.Alert,
  38. SubGroup: oscar.AlertNotifyDisplayCapabilities,
  39. },
  40. Body: oscar.SNACFrame{},
  41. },
  42. output: oscar.SNACMessage{},
  43. },
  44. {
  45. name: "receive AlertGetSubsRequest, expect ErrUnsupportedSubGroup",
  46. input: oscar.SNACMessage{
  47. Frame: oscar.SNACFrame{
  48. FoodGroup: oscar.Alert,
  49. SubGroup: oscar.AlertGetSubsRequest,
  50. },
  51. Body: struct{}{}, // empty SNAC
  52. },
  53. output: oscar.SNACMessage{}, // empty SNAC
  54. expectErr: ErrUnsupportedSubGroup,
  55. },
  56. }
  57. for _, tc := range cases {
  58. t.Run(tc.name, func(t *testing.T) {
  59. router := AlertRouter{
  60. RouteLogger: RouteLogger{
  61. Logger: NewLogger(Config{}),
  62. },
  63. }
  64. bufIn := &bytes.Buffer{}
  65. assert.NoError(t, oscar.Marshal(tc.input.Body, bufIn))
  66. bufOut := &bytes.Buffer{}
  67. seq := uint32(1)
  68. err := router.RouteAlert(context.Background(), tc.input.Frame)
  69. assert.ErrorIs(t, err, tc.expectErr)
  70. if tc.expectErr != nil {
  71. return
  72. }
  73. if tc.output == (oscar.SNACMessage{}) {
  74. // make sure no response was sent
  75. assert.Empty(t, bufOut.Bytes())
  76. return
  77. }
  78. // verify the FLAP frame
  79. flap := oscar.FLAPFrame{}
  80. assert.NoError(t, oscar.Unmarshal(&flap, bufOut))
  81. // make sure the sequence number was incremented
  82. assert.Equal(t, uint32(2), seq)
  83. flapBuf, err := flap.SNACBuffer(bufOut)
  84. assert.NoError(t, err)
  85. // verify the SNAC frame
  86. snacFrame := oscar.SNACFrame{}
  87. assert.NoError(t, oscar.Unmarshal(&snacFrame, flapBuf))
  88. assert.Equal(t, tc.output.Frame, snacFrame)
  89. // verify the SNAC message
  90. snacBuf := &bytes.Buffer{}
  91. assert.NoError(t, oscar.Marshal(tc.output.Body, snacBuf))
  92. assert.Equal(t, snacBuf.Bytes(), flapBuf.Bytes())
  93. })
  94. }
  95. }