chat_nav_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package server
  2. import (
  3. "bytes"
  4. "testing"
  5. "time"
  6. "github.com/mkaminski/goaim/oscar"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestSendAndReceiveCreateRoom(t *testing.T) {
  10. //
  11. // build dependencies
  12. //
  13. userSess := newTestSession(Session{
  14. ID: "sess-id",
  15. ScreenName: "user-screen-name",
  16. })
  17. cr := NewChatRegistry()
  18. sm := NewMockSessionManager(t)
  19. sm.EXPECT().NewSessionWithSN(userSess.ID, userSess.ScreenName).
  20. Return(&Session{})
  21. crf := func() ChatRoom {
  22. return ChatRoom{
  23. Cookie: "dummy-cookie",
  24. CreateTime: time.UnixMilli(0),
  25. SessionManager: sm,
  26. }
  27. }
  28. //
  29. // send input SNAC
  30. //
  31. inputSNACFrame := oscar.SnacFrame{
  32. FoodGroup: CHAT_NAV,
  33. SubGroup: ChatNavCreateRoom,
  34. }
  35. inputSNAC := oscar.SNAC_0x0E_0x02_ChatRoomInfoUpdate{
  36. Exchange: 1,
  37. Cookie: "create", // actual canned value sent by AIM client
  38. InstanceNumber: 2,
  39. DetailLevel: 3,
  40. TLVBlock: oscar.TLVBlock{
  41. TLVList: oscar.TLVList{
  42. {
  43. TType: oscar.ChatTLVRoomName,
  44. Val: "the-chat-room-name",
  45. },
  46. },
  47. },
  48. }
  49. input := &bytes.Buffer{}
  50. assert.NoError(t, oscar.Marshal(inputSNAC, input))
  51. var seq uint32
  52. output := &bytes.Buffer{}
  53. assert.NoError(t, SendAndReceiveCreateRoom(userSess, cr, crf, inputSNACFrame, input, output, &seq))
  54. //
  55. // verify chat room created by handler
  56. //
  57. expectChatRoom := ChatRoom{
  58. SessionManager: sm,
  59. Cookie: "dummy-cookie",
  60. CreateTime: time.UnixMilli(0),
  61. DetailLevel: 3,
  62. Exchange: 1,
  63. InstanceNumber: 2,
  64. Name: "the-chat-room-name",
  65. }
  66. chatRoom, err := cr.Retrieve("dummy-cookie")
  67. assert.NoError(t, err)
  68. assert.Equal(t, expectChatRoom, chatRoom)
  69. //
  70. // verify SNAC frame
  71. //
  72. expectSNACFrame := oscar.SnacFrame{
  73. FoodGroup: CHAT_NAV,
  74. SubGroup: ChatNavNavInfo,
  75. }
  76. flap := oscar.FlapFrame{}
  77. assert.NoError(t, oscar.Unmarshal(&flap, output))
  78. snacFrame := oscar.SnacFrame{}
  79. assert.NoError(t, oscar.Unmarshal(&snacFrame, output))
  80. assert.Equal(t, expectSNACFrame, snacFrame)
  81. //
  82. // verify SNAC body
  83. //
  84. expectSNAC := oscar.SNAC_0x0D_0x09_ChatNavNavInfo{
  85. TLVRestBlock: oscar.TLVRestBlock{
  86. TLVList: oscar.TLVList{
  87. {
  88. TType: oscar.ChatNavTLVRoomInfo,
  89. Val: oscar.SNAC_0x0E_0x02_ChatRoomInfoUpdate{
  90. Exchange: chatRoom.Exchange,
  91. Cookie: chatRoom.Cookie,
  92. InstanceNumber: chatRoom.InstanceNumber,
  93. DetailLevel: chatRoom.DetailLevel,
  94. TLVBlock: oscar.TLVBlock{
  95. TLVList: chatRoom.TLVList(),
  96. },
  97. },
  98. },
  99. },
  100. },
  101. }
  102. assert.NoError(t, expectSNAC.SerializeInPlace())
  103. outputSNAC := oscar.SNAC_0x0D_0x09_ChatNavNavInfo{}
  104. assert.NoError(t, oscar.Unmarshal(&outputSNAC, output))
  105. assert.Equal(t, expectSNAC, outputSNAC)
  106. assert.Equalf(t, 0, output.Len(), "the rest of the buffer is unread")
  107. }