alert_test.go 2.4 KB

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