helpers_test.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. package toc
  2. import (
  3. "context"
  4. "time"
  5. "github.com/stretchr/testify/mock"
  6. "github.com/mk6i/open-oscar-server/state"
  7. "github.com/mk6i/open-oscar-server/wire"
  8. )
  9. type adminParams struct {
  10. infoChangeRequestParams
  11. }
  12. type infoChangeRequestParams []struct {
  13. me state.IdentScreenName
  14. inBody wire.SNAC_0x07_0x04_AdminInfoChangeRequest
  15. msg wire.SNACMessage
  16. err error
  17. }
  18. type addBuddiesParams []struct {
  19. me state.IdentScreenName
  20. inBody wire.SNAC_0x03_0x04_BuddyAddBuddies
  21. err error
  22. }
  23. type broadcastBuddyDepartedParams []struct {
  24. me state.IdentScreenName
  25. err error
  26. }
  27. type delBuddiesParams []struct {
  28. me state.IdentScreenName
  29. inBody wire.SNAC_0x03_0x05_BuddyDelBuddies
  30. err error
  31. }
  32. type buddyParams struct {
  33. addBuddiesParams
  34. broadcastBuddyDepartedParams
  35. delBuddiesParams
  36. }
  37. type chatParams struct {
  38. channelMsgToHostParamsChat
  39. }
  40. type createRoomParams []struct {
  41. me state.IdentScreenName
  42. inBody wire.SNAC_0x0E_0x02_ChatRoomInfoUpdate
  43. msg wire.SNACMessage
  44. err error
  45. }
  46. type requestRoomInfoParams []struct {
  47. inBody wire.SNAC_0x0D_0x04_ChatNavRequestRoomInfo
  48. msg wire.SNACMessage
  49. err error
  50. }
  51. type chatNavParams struct {
  52. createRoomParams
  53. requestRoomInfoParams
  54. }
  55. type channelMsgToHostParamsChat []struct {
  56. sender state.IdentScreenName
  57. inBody wire.SNAC_0x0E_0x05_ChatChannelMsgToHost
  58. result *wire.SNACMessage
  59. err error
  60. }
  61. type channelMsgToHostParamsICBM []struct {
  62. sender state.IdentScreenName
  63. inFrame wire.SNACFrame
  64. inBody wire.SNAC_0x04_0x06_ICBMChannelMsgToHost
  65. result *wire.SNACMessage
  66. err error
  67. }
  68. type evilRequestParams []struct {
  69. me state.IdentScreenName
  70. inBody wire.SNAC_0x04_0x08_ICBMEvilRequest
  71. msg wire.SNACMessage
  72. err error
  73. }
  74. type icbmParams struct {
  75. channelMsgToHostParamsICBM
  76. evilRequestParams
  77. }
  78. type clientOnlineParams []struct {
  79. body wire.SNAC_0x01_0x02_OServiceClientOnline
  80. me state.IdentScreenName
  81. err error
  82. }
  83. type idleNotificationParams []struct {
  84. me state.IdentScreenName
  85. bodyIn wire.SNAC_0x01_0x11_OServiceIdleNotification
  86. err error
  87. }
  88. type serviceRequestParams []struct {
  89. me state.IdentScreenName
  90. bodyIn wire.SNAC_0x01_0x04_OServiceServiceRequest
  91. msg wire.SNACMessage
  92. err error
  93. }
  94. type oServiceParams struct {
  95. clientOnlineParams
  96. idleNotificationParams
  97. serviceRequestParams
  98. }
  99. type flapLoginParams []struct {
  100. frame wire.FLAPSignonFrame
  101. newUserFn func(screenName state.DisplayScreenName) (state.User, error)
  102. tlv wire.TLVRestBlock
  103. err error
  104. }
  105. type registerBOSSessionParams []struct {
  106. authCookie state.ServerCookie
  107. instance *state.SessionInstance
  108. err error
  109. }
  110. type registerChatSessionParams []struct {
  111. authCookie state.ServerCookie
  112. instance *state.SessionInstance
  113. err error
  114. }
  115. type signoutParams []struct {
  116. me state.IdentScreenName
  117. }
  118. type signoutChatParams []struct {
  119. me state.IdentScreenName
  120. }
  121. type authParams struct {
  122. crackCookieParams
  123. flapLoginParams
  124. registerBOSSessionParams
  125. registerChatSessionParams
  126. signoutParams
  127. signoutChatParams
  128. }
  129. type crackCookieParams []struct {
  130. cookieIn []byte
  131. cookieOut state.ServerCookie
  132. err error
  133. }
  134. type setDirInfoParams []struct {
  135. me state.IdentScreenName
  136. inBody wire.SNAC_0x02_0x09_LocateSetDirInfo
  137. msg wire.SNACMessage
  138. err error
  139. }
  140. type setInfoParams []struct {
  141. me state.IdentScreenName
  142. inBody wire.SNAC_0x02_0x04_LocateSetInfo
  143. err error
  144. }
  145. type userInfoQueryParams []struct {
  146. me state.IdentScreenName
  147. inBody wire.SNAC_0x02_0x05_LocateUserInfoQuery
  148. msg wire.SNACMessage
  149. err error
  150. }
  151. type dirInfoParams []struct {
  152. body wire.SNAC_0x02_0x0B_LocateGetDirInfo
  153. msg wire.SNACMessage
  154. err error
  155. }
  156. type locateParams struct {
  157. setDirInfoParams
  158. setInfoParams
  159. userInfoQueryParams
  160. dirInfoParams
  161. }
  162. type infoQueryParams []struct {
  163. inBody wire.SNAC_0x0F_0x02_InfoQuery
  164. msg wire.SNACMessage
  165. err error
  166. }
  167. type dirSearchParams struct {
  168. infoQueryParams
  169. }
  170. type addDenyListEntriesParams []struct {
  171. me state.IdentScreenName
  172. body wire.SNAC_0x09_0x07_PermitDenyAddDenyListEntries
  173. err error
  174. }
  175. type addPermListEntriesParams []struct {
  176. me state.IdentScreenName
  177. body wire.SNAC_0x09_0x05_PermitDenyAddPermListEntries
  178. err error
  179. }
  180. type permitDenyParams struct {
  181. addDenyListEntriesParams
  182. addPermListEntriesParams
  183. }
  184. type registerBuddyListParams []struct {
  185. user state.IdentScreenName
  186. err error
  187. }
  188. type unregisterBuddyListParams []struct {
  189. user state.IdentScreenName
  190. err error
  191. }
  192. type buddyListRegistryParams struct {
  193. registerBuddyListParams
  194. unregisterBuddyListParams
  195. }
  196. type setTOCConfigParams []struct {
  197. user state.IdentScreenName
  198. config string
  199. err error
  200. }
  201. type userParams []struct {
  202. screenName state.IdentScreenName
  203. returnedUser *state.User
  204. err error
  205. }
  206. type retrieveSessionParams []struct {
  207. screenName state.IdentScreenName
  208. returnedSession *state.Session
  209. }
  210. type sessionRetrieverParams struct {
  211. retrieveSessionParams
  212. }
  213. type tocConfigParams struct {
  214. setTOCConfigParams
  215. userParams
  216. }
  217. type mockParams struct {
  218. adminParams
  219. authParams
  220. buddyListRegistryParams
  221. buddyParams
  222. chatNavParams
  223. chatParams
  224. cookieBakerParams
  225. dirSearchParams
  226. icbmParams
  227. locateParams
  228. oServiceParams
  229. permitDenyParams
  230. sessionRetrieverParams
  231. tocConfigParams
  232. }
  233. // issueParams holds multiple scenarios for the Issue method.
  234. type issueParams []struct {
  235. data []byte
  236. returnData []byte
  237. returnErr error
  238. }
  239. // cookieBakerParams groups the method scenarios for a CookieBaker.
  240. type cookieBakerParams struct {
  241. issueParams issueParams
  242. }
  243. // matchSession matches a mock call based session ident screen name.
  244. func matchSession(mustMatch state.IdentScreenName) interface{} {
  245. return mock.MatchedBy(func(s *state.SessionInstance) bool {
  246. return mustMatch == s.IdentScreenName()
  247. })
  248. }
  249. // newTestSession creates a session object with 0 or more functional options
  250. // applied
  251. func newTestSession(screenName state.DisplayScreenName, options ...func(instance *state.SessionInstance)) *state.SessionInstance {
  252. s := state.NewSession().AddInstance()
  253. s.Session().SetIdentScreenName(screenName.IdentScreenName())
  254. s.Session().SetDisplayScreenName(screenName)
  255. s.SetSignonComplete()
  256. s.Session().SetRateClasses(time.Now(), wire.NewRateLimitClasses([5]wire.RateClass{
  257. {
  258. ID: 1,
  259. WindowSize: 80,
  260. ClearLevel: 2500,
  261. AlertLevel: 2000,
  262. LimitLevel: 1500,
  263. DisconnectLevel: 800,
  264. MaxLevel: 6000,
  265. },
  266. {
  267. ID: 2,
  268. WindowSize: 80,
  269. ClearLevel: 3000,
  270. AlertLevel: 2000,
  271. LimitLevel: 1500,
  272. DisconnectLevel: 1000,
  273. MaxLevel: 6000,
  274. },
  275. {
  276. ID: 3,
  277. WindowSize: 20,
  278. ClearLevel: 5100,
  279. AlertLevel: 5000,
  280. LimitLevel: 4000,
  281. DisconnectLevel: 3000,
  282. MaxLevel: 6000,
  283. },
  284. {
  285. ID: 4,
  286. WindowSize: 20,
  287. ClearLevel: 5500,
  288. AlertLevel: 5300,
  289. LimitLevel: 4200,
  290. DisconnectLevel: 3000,
  291. MaxLevel: 8000,
  292. },
  293. {
  294. ID: 5,
  295. WindowSize: 10,
  296. ClearLevel: 5500,
  297. AlertLevel: 5300,
  298. LimitLevel: 4200,
  299. DisconnectLevel: 3000,
  300. MaxLevel: 8000,
  301. },
  302. }))
  303. for _, op := range options {
  304. op(s)
  305. }
  306. return s
  307. }
  308. // matchContext matches any instance of Context interface.
  309. func matchContext() interface{} {
  310. return mock.MatchedBy(func(ctx any) bool {
  311. _, ok := ctx.(context.Context)
  312. return ok
  313. })
  314. }