snacs_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package wire
  2. import (
  3. "bytes"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestBARTInfo_HasClearIconHash(t *testing.T) {
  8. tests := []struct {
  9. name string
  10. bartInfo BARTInfo
  11. want bool
  12. }{
  13. {
  14. bartInfo: BARTInfo{
  15. Hash: GetClearIconHash(),
  16. },
  17. want: true,
  18. },
  19. {
  20. bartInfo: BARTInfo{
  21. Hash: []byte{'s', 'o', 'm', 'e', 'd', 'a', 't', 'a'},
  22. },
  23. want: false,
  24. },
  25. }
  26. for _, tt := range tests {
  27. t.Run(tt.name, func(t *testing.T) {
  28. assert.Equal(t, tt.want, tt.bartInfo.HasClearIconHash())
  29. })
  30. }
  31. }
  32. func TestSNAC_0x01_0x14_OServiceSetPrivacyFlags_IdleFlag(t *testing.T) {
  33. type fields struct {
  34. PrivacyFlags uint32
  35. }
  36. tests := []struct {
  37. name string
  38. fields fields
  39. want bool
  40. }{
  41. {
  42. name: "flag is set",
  43. fields: fields{
  44. PrivacyFlags: OServicePrivacyFlagIdle | OServicePrivacyFlagMember,
  45. },
  46. want: true,
  47. },
  48. {
  49. name: "flag is not set",
  50. fields: fields{
  51. PrivacyFlags: OServicePrivacyFlagMember,
  52. },
  53. want: false,
  54. },
  55. }
  56. for _, tt := range tests {
  57. t.Run(tt.name, func(t *testing.T) {
  58. s := SNAC_0x01_0x14_OServiceSetPrivacyFlags{
  59. PrivacyFlags: tt.fields.PrivacyFlags,
  60. }
  61. assert.Equal(t, tt.want, s.IdleFlag())
  62. })
  63. }
  64. }
  65. func TestSNAC_0x01_0x14_OServiceSetPrivacyFlags_MemberFlag(t *testing.T) {
  66. type fields struct {
  67. PrivacyFlags uint32
  68. }
  69. tests := []struct {
  70. name string
  71. fields fields
  72. want bool
  73. }{
  74. {
  75. name: "flag is set",
  76. fields: fields{
  77. PrivacyFlags: OServicePrivacyFlagIdle | OServicePrivacyFlagMember,
  78. },
  79. want: true,
  80. },
  81. {
  82. name: "flag is not set",
  83. fields: fields{
  84. PrivacyFlags: OServicePrivacyFlagIdle,
  85. },
  86. want: false,
  87. },
  88. }
  89. for _, tt := range tests {
  90. t.Run(tt.name, func(t *testing.T) {
  91. s := SNAC_0x01_0x14_OServiceSetPrivacyFlags{
  92. PrivacyFlags: tt.fields.PrivacyFlags,
  93. }
  94. assert.Equal(t, tt.want, s.MemberFlag())
  95. })
  96. }
  97. }
  98. func TestUnmarshalChatMessageText(t *testing.T) {
  99. tests := []struct {
  100. name string
  101. b []byte
  102. want string
  103. wantErr string
  104. }{
  105. {
  106. name: "happy path",
  107. b: func() []byte {
  108. tlv := TLVRestBlock{
  109. TLVList: TLVList{
  110. NewTLVBE(ChatTLVMessageInfoText, "<p>hello world!</p>"),
  111. },
  112. }
  113. b := &bytes.Buffer{}
  114. err := MarshalBE(tlv, b)
  115. assert.NoError(t, err)
  116. return b.Bytes()
  117. }(),
  118. want: "<p>hello world!</p>",
  119. },
  120. {
  121. name: "missing ChatTLVMessageInfoText",
  122. b: func() []byte {
  123. tlv := TLVRestBlock{TLVList: TLVList{}}
  124. b := &bytes.Buffer{}
  125. err := MarshalBE(tlv, b)
  126. assert.NoError(t, err)
  127. return b.Bytes()
  128. }(),
  129. wantErr: "has no chat msg text TLV",
  130. },
  131. }
  132. for _, tt := range tests {
  133. t.Run(tt.name, func(t *testing.T) {
  134. got, err := UnmarshalChatMessageText(tt.b)
  135. if tt.wantErr != "" {
  136. assert.ErrorContains(t, err, tt.wantErr)
  137. } else {
  138. assert.NoError(t, err)
  139. assert.Equal(t, tt.want, got)
  140. }
  141. })
  142. }
  143. }