auth_test.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. package handler
  2. import (
  3. "github.com/google/uuid"
  4. "github.com/mkaminski/goaim/oscar"
  5. "github.com/mkaminski/goaim/server"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/mock"
  8. "testing"
  9. )
  10. func TestReceiveAndSendBUCPLoginRequest(t *testing.T) {
  11. userGoodPwd := server.User{
  12. ScreenName: "sn_user_a",
  13. AuthKey: "auth_key_user",
  14. }
  15. assert.NoError(t, userGoodPwd.HashPassword("good_pwd"))
  16. userBadPwd := userGoodPwd
  17. assert.NoError(t, userBadPwd.HashPassword("bad_pwd"))
  18. cases := []struct {
  19. name string
  20. cfg server.Config
  21. userInDB server.User
  22. sessionUUID uuid.UUID
  23. inputSNAC oscar.SNAC_0x17_0x02_BUCPLoginRequest
  24. // expectOutput is the SNAC payload sent from the server to the
  25. // recipient client
  26. expectOutput oscar.XMessage
  27. }{
  28. {
  29. name: "login with valid password, expect OK login response",
  30. cfg: server.Config{
  31. OSCARHost: "127.0.0.1",
  32. BOSPort: 1234,
  33. },
  34. userInDB: userGoodPwd,
  35. sessionUUID: uuid.UUID{1, 2, 3},
  36. inputSNAC: oscar.SNAC_0x17_0x02_BUCPLoginRequest{
  37. TLVRestBlock: oscar.TLVRestBlock{
  38. TLVList: oscar.TLVList{
  39. oscar.NewTLV(oscar.TLVPasswordHash, userGoodPwd.PassHash),
  40. oscar.NewTLV(oscar.TLVScreenName, userGoodPwd.ScreenName),
  41. },
  42. },
  43. },
  44. expectOutput: oscar.XMessage{
  45. SnacFrame: oscar.SnacFrame{
  46. FoodGroup: oscar.BUCP,
  47. SubGroup: oscar.BUCPLoginResponse,
  48. },
  49. SnacOut: oscar.SNAC_0x17_0x03_BUCPLoginResponse{
  50. TLVRestBlock: oscar.TLVRestBlock{
  51. TLVList: oscar.TLVList{
  52. oscar.NewTLV(oscar.TLVScreenName, userGoodPwd.ScreenName),
  53. oscar.NewTLV(oscar.TLVReconnectHere, "127.0.0.1:1234"),
  54. oscar.NewTLV(oscar.TLVAuthorizationCookie, uuid.UUID{1, 2, 3}.String()),
  55. },
  56. },
  57. },
  58. },
  59. },
  60. {
  61. name: "login with bad password, expect OK login response (Cfg.DisableAuth=true)",
  62. cfg: server.Config{
  63. OSCARHost: "127.0.0.1",
  64. BOSPort: 1234,
  65. DisableAuth: true,
  66. },
  67. userInDB: userGoodPwd,
  68. sessionUUID: uuid.UUID{1, 2, 3},
  69. inputSNAC: oscar.SNAC_0x17_0x02_BUCPLoginRequest{
  70. TLVRestBlock: oscar.TLVRestBlock{
  71. TLVList: oscar.TLVList{
  72. oscar.NewTLV(oscar.TLVPasswordHash, userBadPwd.PassHash),
  73. oscar.NewTLV(oscar.TLVScreenName, userBadPwd.ScreenName),
  74. },
  75. },
  76. },
  77. expectOutput: oscar.XMessage{
  78. SnacFrame: oscar.SnacFrame{
  79. FoodGroup: oscar.BUCP,
  80. SubGroup: oscar.BUCPLoginResponse,
  81. },
  82. SnacOut: oscar.SNAC_0x17_0x03_BUCPLoginResponse{
  83. TLVRestBlock: oscar.TLVRestBlock{
  84. TLVList: oscar.TLVList{
  85. oscar.NewTLV(oscar.TLVScreenName, userBadPwd.ScreenName),
  86. oscar.NewTLV(oscar.TLVReconnectHere, "127.0.0.1:1234"),
  87. oscar.NewTLV(oscar.TLVAuthorizationCookie, uuid.UUID{1, 2, 3}.String()),
  88. },
  89. },
  90. },
  91. },
  92. },
  93. {
  94. name: "login with bad password, expect failed login response (Cfg.DisableAuth=false)",
  95. cfg: server.Config{
  96. OSCARHost: "127.0.0.1",
  97. BOSPort: 1234,
  98. },
  99. userInDB: userGoodPwd,
  100. sessionUUID: uuid.UUID{1, 2, 3},
  101. inputSNAC: oscar.SNAC_0x17_0x02_BUCPLoginRequest{
  102. TLVRestBlock: oscar.TLVRestBlock{
  103. TLVList: oscar.TLVList{
  104. oscar.NewTLV(oscar.TLVPasswordHash, userBadPwd.PassHash),
  105. oscar.NewTLV(oscar.TLVScreenName, userBadPwd.ScreenName),
  106. },
  107. },
  108. },
  109. expectOutput: oscar.XMessage{
  110. SnacFrame: oscar.SnacFrame{
  111. FoodGroup: oscar.BUCP,
  112. SubGroup: oscar.BUCPLoginResponse,
  113. },
  114. SnacOut: oscar.SNAC_0x17_0x03_BUCPLoginResponse{
  115. TLVRestBlock: oscar.TLVRestBlock{
  116. TLVList: oscar.TLVList{
  117. oscar.NewTLV(oscar.TLVScreenName, userBadPwd.ScreenName),
  118. oscar.NewTLV(oscar.TLVErrorSubcode, uint16(0x01)),
  119. },
  120. },
  121. },
  122. },
  123. },
  124. }
  125. for _, tc := range cases {
  126. t.Run(tc.name, func(t *testing.T) {
  127. sess := newTestSession(tc.userInDB.ScreenName, sessOptID(tc.sessionUUID.String()))
  128. um := server.NewMockUserManager(t)
  129. um.EXPECT().
  130. GetUser(tc.userInDB.ScreenName).
  131. Return(&userGoodPwd, nil).
  132. Maybe()
  133. um.EXPECT().
  134. UpsertUser(mock.Anything).
  135. Return(nil).
  136. Maybe()
  137. sm := server.NewMockSessionManager(t)
  138. sm.EXPECT().
  139. NewSessionWithSN(tc.sessionUUID.String(), tc.userInDB.ScreenName).
  140. Return(sess).
  141. Maybe()
  142. svc := AuthService{
  143. cfg: tc.cfg,
  144. sm: sm,
  145. um: um,
  146. }
  147. fnNewUUID := func() uuid.UUID {
  148. return tc.sessionUUID
  149. }
  150. outputSNAC, err := svc.ReceiveAndSendBUCPLoginRequest(tc.inputSNAC, fnNewUUID)
  151. assert.NoError(t, err)
  152. assert.Equal(t, tc.expectOutput, outputSNAC)
  153. })
  154. }
  155. }
  156. func TestReceiveAndSendAuthChallenge(t *testing.T) {
  157. cases := []struct {
  158. name string
  159. cfg server.Config
  160. userInDB *server.User
  161. fnNewUUID uuid.UUID
  162. inputSNAC oscar.SNAC_0x17_0x06_BUCPChallengeRequest
  163. expectOutput oscar.XMessage
  164. }{
  165. {
  166. name: "login with valid username, expect OK login response",
  167. cfg: server.Config{
  168. OSCARHost: "127.0.0.1",
  169. BOSPort: 1234,
  170. },
  171. userInDB: &server.User{
  172. ScreenName: "sn_user_a",
  173. AuthKey: "auth_key_user_a",
  174. },
  175. fnNewUUID: uuid.UUID{1, 2, 3},
  176. inputSNAC: oscar.SNAC_0x17_0x06_BUCPChallengeRequest{
  177. TLVRestBlock: oscar.TLVRestBlock{
  178. TLVList: oscar.TLVList{
  179. oscar.NewTLV(oscar.TLVScreenName, "sn_user_a"),
  180. },
  181. },
  182. },
  183. expectOutput: oscar.XMessage{
  184. SnacFrame: oscar.SnacFrame{
  185. FoodGroup: oscar.BUCP,
  186. SubGroup: oscar.BUCPChallengeResponse,
  187. },
  188. SnacOut: oscar.SNAC_0x17_0x07_BUCPChallengeResponse{
  189. AuthKey: "auth_key_user_a",
  190. },
  191. },
  192. },
  193. {
  194. name: "login with invalid username, expect OK login response (Cfg.DisableAuth=true)",
  195. cfg: server.Config{
  196. OSCARHost: "127.0.0.1",
  197. BOSPort: 1234,
  198. DisableAuth: true,
  199. },
  200. userInDB: nil,
  201. fnNewUUID: uuid.UUID{1, 2, 3},
  202. inputSNAC: oscar.SNAC_0x17_0x06_BUCPChallengeRequest{
  203. TLVRestBlock: oscar.TLVRestBlock{
  204. TLVList: oscar.TLVList{
  205. oscar.NewTLV(oscar.TLVScreenName, "sn_user_b"),
  206. },
  207. },
  208. },
  209. expectOutput: oscar.XMessage{
  210. SnacFrame: oscar.SnacFrame{
  211. FoodGroup: oscar.BUCP,
  212. SubGroup: oscar.BUCPChallengeResponse,
  213. },
  214. SnacOut: oscar.SNAC_0x17_0x07_BUCPChallengeResponse{
  215. AuthKey: uuid.UUID{1, 2, 3}.String(),
  216. },
  217. },
  218. },
  219. {
  220. name: "login with invalid username, expect failed login response (Cfg.DisableAuth=false)",
  221. cfg: server.Config{
  222. OSCARHost: "127.0.0.1",
  223. BOSPort: 1234,
  224. },
  225. userInDB: nil,
  226. fnNewUUID: uuid.UUID{1, 2, 3},
  227. inputSNAC: oscar.SNAC_0x17_0x06_BUCPChallengeRequest{
  228. TLVRestBlock: oscar.TLVRestBlock{
  229. TLVList: oscar.TLVList{
  230. oscar.NewTLV(oscar.TLVScreenName, "sn_user_b"),
  231. },
  232. },
  233. },
  234. expectOutput: oscar.XMessage{
  235. SnacFrame: oscar.SnacFrame{
  236. FoodGroup: oscar.BUCP,
  237. SubGroup: oscar.BUCPLoginResponse,
  238. },
  239. SnacOut: oscar.SNAC_0x17_0x03_BUCPLoginResponse{
  240. TLVRestBlock: oscar.TLVRestBlock{
  241. TLVList: oscar.TLVList{
  242. oscar.NewTLV(oscar.TLVErrorSubcode, uint16(0x01)),
  243. },
  244. },
  245. },
  246. },
  247. },
  248. }
  249. for _, tc := range cases {
  250. t.Run(tc.name, func(t *testing.T) {
  251. um := server.NewMockUserManager(t)
  252. um.EXPECT().
  253. GetUser(string(tc.inputSNAC.TLVList[0].Val)).
  254. Return(tc.userInDB, nil).
  255. Maybe()
  256. svc := AuthService{
  257. cfg: tc.cfg,
  258. um: um,
  259. }
  260. fnNewUUID := func() uuid.UUID {
  261. return tc.fnNewUUID
  262. }
  263. outputSNAC, err := svc.ReceiveAndSendAuthChallenge(tc.inputSNAC, fnNewUUID)
  264. assert.NoError(t, err)
  265. assert.Equal(t, tc.expectOutput, outputSNAC)
  266. })
  267. }
  268. }