locate_test.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. package handler
  2. import (
  3. "bytes"
  4. "log/slog"
  5. "testing"
  6. "github.com/mk6i/retro-aim-server/wire"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/mock"
  9. )
  10. func TestLocateHandler_GetDirInfo(t *testing.T) {
  11. input := wire.SNACMessage{
  12. Frame: wire.SNACFrame{
  13. FoodGroup: wire.Locate,
  14. SubGroup: wire.LocateGetDirInfo,
  15. },
  16. Body: wire.SNAC_0x02_0x0B_LocateGetDirInfo{
  17. WatcherScreenNames: "screen-name",
  18. },
  19. }
  20. svc := newMockLocateService(t)
  21. h := NewLocateHandler(svc, slog.Default())
  22. responseWriter := newMockResponseWriter(t)
  23. buf := &bytes.Buffer{}
  24. assert.NoError(t, wire.Marshal(input.Body, buf))
  25. assert.NoError(t, h.GetDirInfo(nil, nil, input.Frame, buf, responseWriter))
  26. }
  27. func TestLocateHandler_RightsQuery(t *testing.T) {
  28. input := wire.SNACMessage{
  29. Frame: wire.SNACFrame{
  30. FoodGroup: wire.Locate,
  31. SubGroup: wire.LocateRightsQuery,
  32. },
  33. Body: struct{}{},
  34. }
  35. output := wire.SNACMessage{
  36. Frame: wire.SNACFrame{
  37. FoodGroup: wire.Locate,
  38. SubGroup: wire.LocateRightsReply,
  39. },
  40. Body: wire.SNAC_0x02_0x03_LocateRightsReply{
  41. TLVRestBlock: wire.TLVRestBlock{
  42. TLVList: wire.TLVList{
  43. wire.NewTLV(0x01, uint16(1000)),
  44. },
  45. },
  46. },
  47. }
  48. svc := newMockLocateService(t)
  49. svc.EXPECT().
  50. RightsQuery(mock.Anything, input.Frame).
  51. Return(output)
  52. h := NewLocateHandler(svc, slog.Default())
  53. responseWriter := newMockResponseWriter(t)
  54. responseWriter.EXPECT().
  55. SendSNAC(output.Frame, output.Body).
  56. Return(nil)
  57. buf := &bytes.Buffer{}
  58. assert.NoError(t, wire.Marshal(input.Body, buf))
  59. assert.NoError(t, h.RightsQuery(nil, nil, input.Frame, buf, responseWriter))
  60. }
  61. func TestLocateHandler_SetDirInfo(t *testing.T) {
  62. input := wire.SNACMessage{
  63. Frame: wire.SNACFrame{
  64. FoodGroup: wire.Locate,
  65. SubGroup: wire.LocateSetDirInfo,
  66. },
  67. Body: wire.SNAC_0x02_0x09_LocateSetDirInfo{
  68. TLVRestBlock: wire.TLVRestBlock{
  69. TLVList: wire.TLVList{
  70. {
  71. Tag: 0x01,
  72. Value: []byte{1, 2, 3, 4},
  73. },
  74. },
  75. },
  76. },
  77. }
  78. output := wire.SNACMessage{
  79. Frame: wire.SNACFrame{
  80. FoodGroup: wire.Locate,
  81. SubGroup: wire.LocateSetDirReply,
  82. },
  83. Body: wire.SNAC_0x02_0x0A_LocateSetDirReply{
  84. Result: 1,
  85. },
  86. }
  87. svc := newMockLocateService(t)
  88. svc.EXPECT().
  89. SetDirInfo(mock.Anything, input.Frame).
  90. Return(output)
  91. h := NewLocateHandler(svc, slog.Default())
  92. responseWriter := newMockResponseWriter(t)
  93. responseWriter.EXPECT().
  94. SendSNAC(output.Frame, output.Body).
  95. Return(nil)
  96. buf := &bytes.Buffer{}
  97. assert.NoError(t, wire.Marshal(input.Body, buf))
  98. assert.NoError(t, h.SetDirInfo(nil, nil, input.Frame, buf, responseWriter))
  99. }
  100. func TestLocateHandler_SetInfo(t *testing.T) {
  101. input := wire.SNACMessage{
  102. Frame: wire.SNACFrame{
  103. FoodGroup: wire.Locate,
  104. SubGroup: wire.LocateSetInfo,
  105. },
  106. Body: wire.SNAC_0x02_0x04_LocateSetInfo{
  107. TLVRestBlock: wire.TLVRestBlock{
  108. TLVList: wire.TLVList{
  109. {
  110. Tag: 0x01,
  111. Value: []byte{1, 2, 3, 4},
  112. },
  113. },
  114. },
  115. },
  116. }
  117. svc := newMockLocateService(t)
  118. svc.EXPECT().
  119. SetInfo(mock.Anything, mock.Anything, input.Body).
  120. Return(nil)
  121. h := NewLocateHandler(svc, slog.Default())
  122. responseWriter := newMockResponseWriter(t)
  123. buf := &bytes.Buffer{}
  124. assert.NoError(t, wire.Marshal(input.Body, buf))
  125. assert.NoError(t, h.SetInfo(nil, nil, input.Frame, buf, responseWriter))
  126. }
  127. func TestLocateHandler_SetKeywordInfo(t *testing.T) {
  128. input := wire.SNACMessage{
  129. Frame: wire.SNACFrame{
  130. FoodGroup: wire.Locate,
  131. SubGroup: wire.LocateSetKeywordInfo,
  132. },
  133. Body: wire.SNAC_0x02_0x0F_LocateSetKeywordInfo{
  134. TLVRestBlock: wire.TLVRestBlock{
  135. TLVList: wire.TLVList{
  136. {
  137. Tag: 0x01,
  138. Value: []byte{1, 2, 3, 4},
  139. },
  140. },
  141. },
  142. },
  143. }
  144. output := wire.SNACMessage{
  145. Frame: wire.SNACFrame{
  146. FoodGroup: wire.Locate,
  147. SubGroup: wire.LocateSetKeywordReply,
  148. },
  149. Body: wire.SNAC_0x02_0x10_LocateSetKeywordReply{
  150. Unknown: 1,
  151. },
  152. }
  153. svc := newMockLocateService(t)
  154. svc.EXPECT().
  155. SetKeywordInfo(mock.Anything, input.Frame).
  156. Return(output)
  157. h := NewLocateHandler(svc, slog.Default())
  158. responseWriter := newMockResponseWriter(t)
  159. responseWriter.EXPECT().
  160. SendSNAC(output.Frame, output.Body).
  161. Return(nil)
  162. buf := &bytes.Buffer{}
  163. assert.NoError(t, wire.Marshal(input.Body, buf))
  164. assert.NoError(t, h.SetKeywordInfo(nil, nil, input.Frame, buf, responseWriter))
  165. }
  166. func TestLocateHandler_UserInfoQuery(t *testing.T) {
  167. input := wire.SNACMessage{
  168. Frame: wire.SNACFrame{
  169. FoodGroup: wire.Locate,
  170. SubGroup: wire.LocateUserInfoQuery,
  171. },
  172. Body: wire.SNAC_0x02_0x05_LocateUserInfoQuery{
  173. Type: 1,
  174. },
  175. }
  176. output := wire.SNACMessage{
  177. Frame: wire.SNACFrame{
  178. FoodGroup: wire.Locate,
  179. SubGroup: wire.LocateUserInfoReply,
  180. },
  181. Body: wire.SNAC_0x02_0x06_LocateUserInfoReply{
  182. TLVUserInfo: wire.TLVUserInfo{
  183. ScreenName: "screen-name",
  184. },
  185. LocateInfo: wire.TLVRestBlock{
  186. TLVList: wire.TLVList{
  187. {
  188. Tag: 0x01,
  189. Value: []byte{1, 2, 3, 4},
  190. },
  191. },
  192. },
  193. },
  194. }
  195. svc := newMockLocateService(t)
  196. svc.EXPECT().
  197. UserInfoQuery(mock.Anything, mock.Anything, input.Frame, input.Body).
  198. Return(output, nil)
  199. h := NewLocateHandler(svc, slog.Default())
  200. responseWriter := newMockResponseWriter(t)
  201. responseWriter.EXPECT().
  202. SendSNAC(output.Frame, output.Body).
  203. Return(nil)
  204. buf := &bytes.Buffer{}
  205. assert.NoError(t, wire.Marshal(input.Body, buf))
  206. assert.NoError(t, h.UserInfoQuery(nil, nil, input.Frame, buf, responseWriter))
  207. }
  208. func TestLocateHandler_UserInfoQuery2(t *testing.T) {
  209. input := wire.SNACMessage{
  210. Frame: wire.SNACFrame{
  211. FoodGroup: wire.Locate,
  212. SubGroup: wire.LocateUserInfoQuery2,
  213. },
  214. Body: wire.SNAC_0x02_0x15_LocateUserInfoQuery2{
  215. Type2: 1,
  216. },
  217. }
  218. output := wire.SNACMessage{
  219. Frame: wire.SNACFrame{
  220. FoodGroup: wire.Locate,
  221. SubGroup: wire.LocateUserInfoReply,
  222. },
  223. Body: wire.SNAC_0x02_0x06_LocateUserInfoReply{
  224. TLVUserInfo: wire.TLVUserInfo{
  225. ScreenName: "screen-name",
  226. },
  227. LocateInfo: wire.TLVRestBlock{
  228. TLVList: wire.TLVList{
  229. {
  230. Tag: 0x01,
  231. Value: []byte{1, 2, 3, 4},
  232. },
  233. },
  234. },
  235. },
  236. }
  237. svc := newMockLocateService(t)
  238. svc.EXPECT().
  239. UserInfoQuery(mock.Anything, mock.Anything, input.Frame, wire.SNAC_0x02_0x05_LocateUserInfoQuery{Type: 1}).
  240. Return(output, nil)
  241. h := NewLocateHandler(svc, slog.Default())
  242. responseWriter := newMockResponseWriter(t)
  243. responseWriter.EXPECT().
  244. SendSNAC(output.Frame, output.Body).
  245. Return(nil)
  246. buf := &bytes.Buffer{}
  247. assert.NoError(t, wire.Marshal(input.Body, buf))
  248. assert.NoError(t, h.UserInfoQuery2(nil, nil, input.Frame, buf, responseWriter))
  249. }