bucp_test.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. package server
  2. import (
  3. "bytes"
  4. "os"
  5. "testing"
  6. "github.com/google/uuid"
  7. "github.com/mkaminski/goaim/oscar"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestReceiveAndSendBUCPLoginRequest(t *testing.T) {
  11. userGoodPwd := 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 Config
  21. userInDB User
  22. sessionUUID uuid.UUID
  23. inputSNAC oscar.SNAC_0x17_0x02_BUCPLoginRequest
  24. expectSnacFrame oscar.SnacFrame
  25. expectSNACBody oscar.SNAC_0x17_0x03_BUCPLoginResponse
  26. }{
  27. {
  28. name: "login with valid password, expect OK login response",
  29. cfg: Config{
  30. OSCARHost: "127.0.0.1",
  31. BOSPort: 1234,
  32. },
  33. userInDB: userGoodPwd,
  34. sessionUUID: uuid.UUID{1, 2, 3},
  35. inputSNAC: oscar.SNAC_0x17_0x02_BUCPLoginRequest{
  36. TLVRestBlock: oscar.TLVRestBlock{
  37. TLVList: oscar.TLVList{
  38. oscar.TLV{
  39. TType: oscar.TLVPasswordHash, Val: userGoodPwd.PassHash,
  40. },
  41. oscar.TLV{
  42. TType: oscar.TLVScreenName, Val: userGoodPwd.ScreenName,
  43. },
  44. },
  45. },
  46. },
  47. expectSnacFrame: oscar.SnacFrame{
  48. FoodGroup: oscar.BUCP,
  49. SubGroup: BUCPLoginResponse,
  50. },
  51. expectSNACBody: oscar.SNAC_0x17_0x03_BUCPLoginResponse{
  52. TLVRestBlock: oscar.TLVRestBlock{
  53. TLVList: oscar.TLVList{
  54. {
  55. TType: oscar.TLVScreenName,
  56. Val: userGoodPwd.ScreenName,
  57. },
  58. {
  59. TType: oscar.TLVReconnectHere,
  60. Val: "127.0.0.1:1234",
  61. },
  62. {
  63. TType: oscar.TLVAuthorizationCookie,
  64. Val: uuid.UUID{1, 2, 3}.String(),
  65. },
  66. },
  67. },
  68. },
  69. },
  70. {
  71. name: "login with bad password, expect OK login response (cfg.DisableAuth=true)",
  72. cfg: Config{
  73. OSCARHost: "127.0.0.1",
  74. BOSPort: 1234,
  75. DisableAuth: true,
  76. },
  77. userInDB: userGoodPwd,
  78. sessionUUID: uuid.UUID{1, 2, 3},
  79. inputSNAC: oscar.SNAC_0x17_0x02_BUCPLoginRequest{
  80. TLVRestBlock: oscar.TLVRestBlock{
  81. TLVList: oscar.TLVList{
  82. oscar.TLV{
  83. TType: oscar.TLVPasswordHash, Val: userBadPwd.PassHash,
  84. },
  85. oscar.TLV{
  86. TType: oscar.TLVScreenName, Val: userBadPwd.ScreenName,
  87. },
  88. },
  89. },
  90. },
  91. expectSnacFrame: oscar.SnacFrame{
  92. FoodGroup: oscar.BUCP,
  93. SubGroup: BUCPLoginResponse,
  94. },
  95. expectSNACBody: oscar.SNAC_0x17_0x03_BUCPLoginResponse{
  96. TLVRestBlock: oscar.TLVRestBlock{
  97. TLVList: oscar.TLVList{
  98. {
  99. TType: oscar.TLVScreenName,
  100. Val: userBadPwd.ScreenName,
  101. },
  102. {
  103. TType: oscar.TLVReconnectHere,
  104. Val: "127.0.0.1:1234",
  105. },
  106. {
  107. TType: oscar.TLVAuthorizationCookie,
  108. Val: uuid.UUID{1, 2, 3}.String(),
  109. },
  110. },
  111. },
  112. },
  113. },
  114. {
  115. name: "login with bad password, expect failed login response (cfg.DisableAuth=false)",
  116. cfg: Config{
  117. OSCARHost: "127.0.0.1",
  118. BOSPort: 1234,
  119. },
  120. userInDB: userGoodPwd,
  121. sessionUUID: uuid.UUID{1, 2, 3},
  122. inputSNAC: oscar.SNAC_0x17_0x02_BUCPLoginRequest{
  123. TLVRestBlock: oscar.TLVRestBlock{
  124. TLVList: oscar.TLVList{
  125. oscar.TLV{
  126. TType: oscar.TLVPasswordHash, Val: userBadPwd.PassHash,
  127. },
  128. oscar.TLV{
  129. TType: oscar.TLVScreenName, Val: userBadPwd.ScreenName,
  130. },
  131. },
  132. },
  133. },
  134. expectSnacFrame: oscar.SnacFrame{
  135. FoodGroup: oscar.BUCP,
  136. SubGroup: BUCPLoginResponse,
  137. },
  138. expectSNACBody: oscar.SNAC_0x17_0x03_BUCPLoginResponse{
  139. TLVRestBlock: oscar.TLVRestBlock{
  140. TLVList: oscar.TLVList{
  141. {
  142. TType: oscar.TLVScreenName,
  143. Val: userBadPwd.ScreenName,
  144. },
  145. {
  146. TType: oscar.TLVErrorSubcode,
  147. Val: uint16(0x01),
  148. },
  149. },
  150. },
  151. },
  152. },
  153. }
  154. for _, tc := range cases {
  155. t.Run(tc.name, func(t *testing.T) {
  156. //
  157. // initialize dependencies
  158. //
  159. const testFile string = "aim_test.db"
  160. defer func() {
  161. assert.NoError(t, os.Remove(testFile))
  162. }()
  163. fs, err := NewFeedbagStore(testFile)
  164. if err != nil {
  165. assert.NoError(t, err)
  166. }
  167. assert.NoError(t, fs.InsertUser(tc.userInDB))
  168. sm := NewSessionManager()
  169. //
  170. // send input SNAC
  171. //
  172. input := &bytes.Buffer{}
  173. var seq uint32
  174. assert.NoError(t, writeOutSNAC(oscar.SnacFrame{}, oscar.SnacFrame{}, tc.inputSNAC, &seq, input))
  175. //
  176. // receive response
  177. //
  178. output := &bytes.Buffer{}
  179. fnNewUUID := func() uuid.UUID {
  180. return tc.sessionUUID
  181. }
  182. assert.NoError(t, ReceiveAndSendBUCPLoginRequest(tc.cfg, sm, fs, input, output, &seq, fnNewUUID))
  183. flap := oscar.FlapFrame{}
  184. assert.NoError(t, oscar.Unmarshal(&flap, output))
  185. //
  186. // verify output SNAC frame
  187. //
  188. SnacFrame := oscar.SnacFrame{}
  189. assert.NoError(t, oscar.Unmarshal(&SnacFrame, output))
  190. assert.Equal(t, tc.expectSnacFrame, SnacFrame)
  191. //
  192. // verify output SNAC body
  193. //
  194. assert.NoError(t, tc.expectSNACBody.SerializeInPlace())
  195. actual := oscar.SNAC_0x17_0x03_BUCPLoginResponse{}
  196. assert.NoError(t, oscar.Unmarshal(&actual, output))
  197. assert.Equal(t, tc.expectSNACBody, actual)
  198. assert.Equalf(t, 0, output.Len(), "the rest of the buffer is unread")
  199. })
  200. }
  201. }
  202. func TestReceiveAndSendAuthChallenge(t *testing.T) {
  203. cases := []struct {
  204. name string
  205. cfg Config
  206. userInDB User
  207. fnNewUUID uuid.UUID
  208. inputSNAC oscar.SNAC_0x17_0x06_BUCPChallengeRequest
  209. expectSnacFrame oscar.SnacFrame
  210. expectSNACBody any
  211. }{
  212. {
  213. name: "login with valid username, expect OK login response",
  214. cfg: Config{
  215. OSCARHost: "127.0.0.1",
  216. BOSPort: 1234,
  217. },
  218. userInDB: User{
  219. ScreenName: "sn_user_a",
  220. AuthKey: "auth_key_user_a",
  221. },
  222. fnNewUUID: uuid.UUID{1, 2, 3},
  223. inputSNAC: oscar.SNAC_0x17_0x06_BUCPChallengeRequest{
  224. TLVRestBlock: oscar.TLVRestBlock{
  225. TLVList: oscar.TLVList{
  226. oscar.TLV{
  227. TType: oscar.TLVScreenName, Val: "sn_user_a",
  228. },
  229. },
  230. },
  231. },
  232. expectSnacFrame: oscar.SnacFrame{
  233. FoodGroup: oscar.BUCP,
  234. SubGroup: BUCPChallengeResponse,
  235. },
  236. expectSNACBody: oscar.SNAC_0x17_0x07_BUCPChallengeResponse{
  237. AuthKey: "auth_key_user_a",
  238. },
  239. },
  240. {
  241. name: "login with invalid username, expect OK login response (cfg.DisableAuth=true)",
  242. cfg: Config{
  243. OSCARHost: "127.0.0.1",
  244. BOSPort: 1234,
  245. DisableAuth: true,
  246. },
  247. userInDB: User{
  248. ScreenName: "sn_user_a",
  249. AuthKey: "auth_key_user_a",
  250. },
  251. fnNewUUID: uuid.UUID{1, 2, 3},
  252. inputSNAC: oscar.SNAC_0x17_0x06_BUCPChallengeRequest{
  253. TLVRestBlock: oscar.TLVRestBlock{
  254. TLVList: oscar.TLVList{
  255. oscar.TLV{
  256. TType: oscar.TLVScreenName, Val: "sn_user_b",
  257. },
  258. },
  259. },
  260. },
  261. expectSnacFrame: oscar.SnacFrame{
  262. FoodGroup: oscar.BUCP,
  263. SubGroup: BUCPChallengeResponse,
  264. },
  265. expectSNACBody: oscar.SNAC_0x17_0x07_BUCPChallengeResponse{
  266. AuthKey: uuid.UUID{1, 2, 3}.String(),
  267. },
  268. },
  269. {
  270. name: "login with invalid username, expect failed login response (cfg.DisableAuth=false)",
  271. cfg: Config{
  272. OSCARHost: "127.0.0.1",
  273. BOSPort: 1234,
  274. },
  275. userInDB: User{
  276. ScreenName: "sn_user_a",
  277. AuthKey: "auth_key_user_a",
  278. },
  279. fnNewUUID: uuid.UUID{1, 2, 3},
  280. inputSNAC: oscar.SNAC_0x17_0x06_BUCPChallengeRequest{
  281. TLVRestBlock: oscar.TLVRestBlock{
  282. TLVList: oscar.TLVList{
  283. oscar.TLV{
  284. TType: oscar.TLVScreenName, Val: "sn_user_b",
  285. },
  286. },
  287. },
  288. },
  289. expectSnacFrame: oscar.SnacFrame{
  290. FoodGroup: oscar.BUCP,
  291. SubGroup: BUCPLoginResponse,
  292. },
  293. expectSNACBody: oscar.SNAC_0x17_0x03_BUCPLoginResponse{
  294. TLVRestBlock: oscar.TLVRestBlock{
  295. TLVList: oscar.TLVList{
  296. {
  297. TType: oscar.TLVErrorSubcode,
  298. Val: uint16(0x01),
  299. },
  300. },
  301. },
  302. },
  303. },
  304. }
  305. for _, tc := range cases {
  306. t.Run(tc.name, func(t *testing.T) {
  307. //
  308. // initialize dependencies
  309. //
  310. const testFile string = "aim_test.db"
  311. defer func() {
  312. assert.NoError(t, os.Remove(testFile))
  313. }()
  314. fs, err := NewFeedbagStore(testFile)
  315. if err != nil {
  316. assert.NoError(t, err)
  317. }
  318. assert.NoError(t, fs.InsertUser(tc.userInDB))
  319. //
  320. // send input SNAC
  321. //
  322. input := &bytes.Buffer{}
  323. var seq uint32
  324. assert.NoError(t, writeOutSNAC(oscar.SnacFrame{}, oscar.SnacFrame{}, tc.inputSNAC, &seq, input))
  325. //
  326. // receive response
  327. //
  328. output := &bytes.Buffer{}
  329. fnNewUUID := func() uuid.UUID {
  330. return tc.fnNewUUID
  331. }
  332. assert.NoError(t, ReceiveAndSendAuthChallenge(tc.cfg, fs, input, output, &seq, fnNewUUID))
  333. flap := oscar.FlapFrame{}
  334. assert.NoError(t, oscar.Unmarshal(&flap, output))
  335. //
  336. // verify output SNAC frame
  337. //
  338. SnacFrame := oscar.SnacFrame{}
  339. assert.NoError(t, oscar.Unmarshal(&SnacFrame, output))
  340. assert.Equal(t, tc.expectSnacFrame, SnacFrame)
  341. //
  342. // verify output SNAC body
  343. //
  344. switch v := tc.expectSNACBody.(type) {
  345. case oscar.SNAC_0x17_0x07_BUCPChallengeResponse:
  346. actual := oscar.SNAC_0x17_0x07_BUCPChallengeResponse{}
  347. assert.NoError(t, oscar.Unmarshal(&actual, output))
  348. assert.Equal(t, v, actual)
  349. case oscar.SNAC_0x17_0x03_BUCPLoginResponse:
  350. assert.NoError(t, v.SerializeInPlace())
  351. actual := oscar.SNAC_0x17_0x03_BUCPLoginResponse{}
  352. assert.NoError(t, oscar.Unmarshal(&actual, output))
  353. assert.Equal(t, v, actual)
  354. default:
  355. t.Fatalf("unexpected output SNAC type")
  356. }
  357. assert.Equalf(t, 0, output.Len(), "the rest of the buffer is unread")
  358. })
  359. }
  360. }