locate_test.go 6.9 KB

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