chat_nav_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package server
  2. import (
  3. "bytes"
  4. "testing"
  5. "github.com/mkaminski/goaim/oscar"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/mock"
  8. )
  9. func TestChatNavRouter_RouteChatNavRouter(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 ChatNavRequestChatRights, return ChatNavNavInfo",
  24. input: oscar.SNACMessage{
  25. Frame: oscar.SNACFrame{
  26. FoodGroup: oscar.ChatNav,
  27. SubGroup: oscar.ChatNavRequestChatRights,
  28. },
  29. Body: struct{}{},
  30. },
  31. output: oscar.SNACMessage{
  32. Frame: oscar.SNACFrame{
  33. FoodGroup: oscar.ChatNav,
  34. SubGroup: oscar.ChatNavNavInfo,
  35. },
  36. Body: oscar.SNAC_0x0D_0x09_ChatNavNavInfo{
  37. TLVRestBlock: oscar.TLVRestBlock{
  38. TLVList: oscar.TLVList{
  39. oscar.NewTLV(0x02, uint8(10)),
  40. },
  41. },
  42. },
  43. },
  44. },
  45. {
  46. name: "receive ChatNavRequestRoomInfo, return ChatNavNavInfo",
  47. input: oscar.SNACMessage{
  48. Frame: oscar.SNACFrame{
  49. FoodGroup: oscar.ChatNav,
  50. SubGroup: oscar.ChatNavRequestRoomInfo,
  51. },
  52. Body: oscar.SNAC_0x0D_0x04_ChatNavRequestRoomInfo{
  53. Exchange: 1,
  54. },
  55. },
  56. output: oscar.SNACMessage{
  57. Frame: oscar.SNACFrame{
  58. FoodGroup: oscar.ChatNav,
  59. SubGroup: oscar.ChatNavNavInfo,
  60. },
  61. Body: oscar.SNAC_0x0D_0x09_ChatNavNavInfo{
  62. TLVRestBlock: oscar.TLVRestBlock{
  63. TLVList: oscar.TLVList{
  64. oscar.NewTLV(0x02, uint8(10)),
  65. },
  66. },
  67. },
  68. },
  69. },
  70. {
  71. name: "receive ChatNavCreateRoom, return ChatNavNavInfo",
  72. input: oscar.SNACMessage{
  73. Frame: oscar.SNACFrame{
  74. FoodGroup: oscar.ChatNav,
  75. SubGroup: oscar.ChatNavCreateRoom,
  76. },
  77. Body: oscar.SNAC_0x0E_0x02_ChatRoomInfoUpdate{
  78. Exchange: 1,
  79. },
  80. },
  81. output: oscar.SNACMessage{
  82. Frame: oscar.SNACFrame{
  83. FoodGroup: oscar.ChatNav,
  84. SubGroup: oscar.ChatNavNavInfo,
  85. },
  86. Body: oscar.SNAC_0x0D_0x09_ChatNavNavInfo{
  87. TLVRestBlock: oscar.TLVRestBlock{
  88. TLVList: oscar.TLVList{
  89. oscar.NewTLV(0x02, uint8(10)),
  90. },
  91. },
  92. },
  93. },
  94. },
  95. {
  96. name: "receive ChatNavRequestOccupantList, return ErrUnsupportedSubGroup",
  97. input: oscar.SNACMessage{
  98. Frame: oscar.SNACFrame{
  99. FoodGroup: oscar.ChatNav,
  100. SubGroup: oscar.ChatNavRequestOccupantList,
  101. },
  102. Body: struct{}{},
  103. },
  104. output: oscar.SNACMessage{},
  105. expectErr: ErrUnsupportedSubGroup,
  106. },
  107. }
  108. for _, tc := range cases {
  109. t.Run(tc.name, func(t *testing.T) {
  110. svc := newMockChatNavHandler(t)
  111. svc.EXPECT().
  112. RequestChatRightsHandler(mock.Anything, tc.input.Frame).
  113. Return(tc.output).
  114. Maybe()
  115. svc.EXPECT().
  116. RequestRoomInfoHandler(mock.Anything, tc.input.Frame, tc.input.Body).
  117. Return(tc.output, tc.handlerErr).
  118. Maybe()
  119. svc.EXPECT().
  120. CreateRoomHandler(mock.Anything, mock.Anything, tc.input.Frame, tc.input.Body).
  121. Return(tc.output, tc.handlerErr).
  122. Maybe()
  123. router := ChatNavRouter{
  124. ChatNavHandler: svc,
  125. RouteLogger: RouteLogger{
  126. Logger: NewLogger(Config{}),
  127. },
  128. }
  129. bufIn := &bytes.Buffer{}
  130. assert.NoError(t, oscar.Marshal(tc.input.Body, bufIn))
  131. bufOut := &bytes.Buffer{}
  132. seq := uint32(0)
  133. err := router.RouteChatNav(nil, nil, tc.input.Frame, bufIn, bufOut, &seq)
  134. assert.ErrorIs(t, err, tc.expectErr)
  135. if tc.expectErr != nil {
  136. return
  137. }
  138. if tc.output.Frame == (oscar.SNACFrame{}) {
  139. return
  140. }
  141. // verify the FLAP frame
  142. flap := oscar.FLAPFrame{}
  143. assert.NoError(t, oscar.Unmarshal(&flap, bufOut))
  144. // make sure the sequence increments
  145. assert.Equal(t, seq, uint32(1))
  146. assert.Equal(t, flap.Sequence, uint16(0))
  147. flapBuf, err := flap.SNACBuffer(bufOut)
  148. assert.NoError(t, err)
  149. // verify the SNAC frame
  150. snacFrame := oscar.SNACFrame{}
  151. assert.NoError(t, oscar.Unmarshal(&snacFrame, flapBuf))
  152. assert.Equal(t, tc.output.Frame, snacFrame)
  153. // verify the SNAC message
  154. snacBuf := &bytes.Buffer{}
  155. assert.NoError(t, oscar.Marshal(tc.output.Body, snacBuf))
  156. assert.Equal(t, snacBuf.Bytes(), flapBuf.Bytes())
  157. })
  158. }
  159. }