helpers_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package icq_legacy
  2. import (
  3. "context"
  4. "net"
  5. "strconv"
  6. "github.com/mk6i/open-oscar-server/state"
  7. "github.com/mk6i/open-oscar-server/wire"
  8. "github.com/stretchr/testify/mock"
  9. )
  10. // mockParams is a helper struct that centralizes mock function call parameters
  11. // in one place for a table test
  12. type mockParams struct {
  13. userManagerParams
  14. sessionRetrieverParams
  15. offlineMessageManagerParams
  16. icqUserFinderParams
  17. icbmFoodgroupParams
  18. }
  19. // icbmFoodgroupParams is a helper struct that contains mock parameters for
  20. // ICBMService methods
  21. type icbmFoodgroupParams struct {
  22. channelMsgToHostParams
  23. }
  24. // channelMsgToHostParams is the list of parameters passed at the mock
  25. // ICBMService.ChannelMsgToHost call site
  26. type channelMsgToHostParams []struct {
  27. screenName state.IdentScreenName
  28. inFrame wire.SNACFrame
  29. inBody wire.SNAC_0x04_0x06_ICBMChannelMsgToHost
  30. result *wire.SNACMessage
  31. err error
  32. }
  33. // userManagerParams is a helper struct that contains mock parameters for
  34. // UserManager methods
  35. type userManagerParams struct {
  36. userParams
  37. deleteUserParams
  38. }
  39. // userParams is the list of parameters passed at the mock
  40. // UserManager.User call site
  41. type userParams []struct {
  42. screenName state.IdentScreenName
  43. result *state.User
  44. err error
  45. }
  46. // deleteUserParams is the list of parameters passed at the mock
  47. // UserManager.DeleteUser call site
  48. type deleteUserParams []struct {
  49. screenName state.IdentScreenName
  50. err error
  51. }
  52. // sessionRetrieverParams is a helper struct that contains mock parameters for
  53. // SessionRetriever methods
  54. type sessionRetrieverParams struct {
  55. retrieveSessionParams
  56. }
  57. // retrieveSessionParams is the list of parameters passed at the mock
  58. // SessionRetriever.RetrieveSession call site
  59. type retrieveSessionParams []struct {
  60. screenName state.IdentScreenName
  61. result *state.Session
  62. }
  63. // offlineMessageManagerParams is a helper struct that contains mock parameters for
  64. // OfflineMessageManager methods
  65. type offlineMessageManagerParams struct {
  66. retrieveMessagesParams
  67. }
  68. // retrieveMessagesParams is the list of parameters passed at the mock
  69. // OfflineMessageManager.RetrieveMessages call site
  70. type retrieveMessagesParams []struct {
  71. recip state.IdentScreenName
  72. messages []state.OfflineMessage
  73. err error
  74. }
  75. // icqUserFinderParams is a helper struct that contains mock parameters for
  76. // ICQUserFinder methods
  77. type icqUserFinderParams struct {
  78. findByUINParams
  79. findByICQEmailParams
  80. findByICQNameParams
  81. }
  82. // findByUINParams is the list of parameters passed at the mock
  83. // ICQUserFinder.FindByUIN call site
  84. type findByUINParams []struct {
  85. UIN uint32
  86. result state.User
  87. err error
  88. }
  89. // findByICQEmailParams is the list of parameters passed at the mock
  90. // ICQUserFinder.FindByICQEmail call site
  91. type findByICQEmailParams []struct {
  92. email string
  93. result state.User
  94. err error
  95. }
  96. // findByICQNameParams is the list of parameters passed at the mock
  97. // ICQUserFinder.FindByICQName call site
  98. type findByICQNameParams []struct {
  99. firstName string
  100. lastName string
  101. nickName string
  102. result []state.User
  103. err error
  104. }
  105. // newTestLegacySession creates a *LegacySession with configurable fields for
  106. // use in table-driven tests. Fields not set remain at their zero values.
  107. func newTestLegacySession(uin uint32, opts ...func(*LegacySession)) *LegacySession {
  108. legacySess := &LegacySession{
  109. UIN: uin,
  110. Addr: &net.UDPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 4000},
  111. }
  112. for _, opt := range opts {
  113. opt(legacySess)
  114. }
  115. return legacySess
  116. }
  117. // legacySessionOptVersion sets the protocol version on the legacy session.
  118. func legacySessionOptVersion(version uint16) func(*LegacySession) {
  119. return func(s *LegacySession) {
  120. s.Version = version
  121. }
  122. }
  123. // legacySessionOptOSCARSess attaches an OSCAR session instance for tests that need Instance set.
  124. func legacySessionOptOSCARSess(s *LegacySession) {
  125. oscarSess := state.NewSession()
  126. oscarSess.SetUIN(s.UIN)
  127. oscarSess.SetDisplayScreenName(state.DisplayScreenName(strconv.FormatUint(uint64(s.UIN), 10)))
  128. oscarSess.SetIdentScreenName(oscarSess.DisplayScreenName().IdentScreenName())
  129. s.Instance = oscarSess.AddInstance()
  130. }
  131. func newTestOSCARInstance(screenName state.DisplayScreenName) *state.SessionInstance {
  132. oscarSess := state.NewSession()
  133. oscarSess.SetDisplayScreenName(screenName)
  134. oscarSess.SetIdentScreenName(screenName.IdentScreenName())
  135. return oscarSess.AddInstance()
  136. }
  137. // legacySessionOptContactList sets the contact list on the legacy session.
  138. func legacySessionOptContactList(contacts []uint32) func(*LegacySession) {
  139. return func(s *LegacySession) {
  140. s.ContactList = make([]uint32, len(contacts))
  141. copy(s.ContactList, contacts)
  142. }
  143. }
  144. // matchContext matches any instance of context.Context interface.
  145. func matchContext() interface{} {
  146. return mock.MatchedBy(func(ctx any) bool {
  147. _, ok := ctx.(context.Context)
  148. return ok
  149. })
  150. }
  151. // matchSession matches a mock call based session ident screen name.
  152. func matchSession(mustMatch state.IdentScreenName) interface{} {
  153. return mock.MatchedBy(func(s *state.SessionInstance) bool {
  154. return mustMatch == s.IdentScreenName()
  155. })
  156. }