buddy_prefs_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package wire
  2. import (
  3. "bytes"
  4. "testing"
  5. )
  6. func TestBuddyPref(t *testing.T) {
  7. tests := []struct {
  8. name string
  9. prefNum uint16
  10. list TLVList
  11. want bool
  12. }{
  13. {
  14. // PlayIMSound (0x15) defaults to true when the bitmask is absent.
  15. name: "absent bitmask => default true",
  16. prefNum: FeedbagBuddyPrefsPlayIMSound,
  17. list: TLVList{},
  18. want: true,
  19. },
  20. {
  21. // AcceptCustomBart (0x0B) defaults to false when the bitmask is absent.
  22. name: "absent bitmask => default false",
  23. prefNum: FeedbagBuddyPrefsAcceptCustomBart,
  24. list: TLVList{},
  25. want: false,
  26. },
  27. {
  28. // pref 0x34 (=52) lives in BuddyPrefs2 at offset 19 (index 2, mask 0x10).
  29. // Explicitly set to false, overriding the true default.
  30. name: "offline messages disabled",
  31. prefNum: FeedbagBuddyPrefsAcceptOfflineIM,
  32. list: TLVList{
  33. {Tag: FeedbagAttributesBuddyPrefsValid, Value: []byte{0, 0, 24, 64}},
  34. {Tag: FeedbagAttributesBuddyPrefs, Value: []byte{0, 0, 24, 64}},
  35. {Tag: FeedbagAttributesBuddyPrefs2Valid, Value: []byte{0, 0, 17}},
  36. {Tag: FeedbagAttributesBuddyPrefs2, Value: []byte{0, 0, 1}},
  37. },
  38. want: false,
  39. },
  40. {
  41. // pref 22 lives in BuddyPrefs at index 1, mask 0x40.
  42. name: "typing events enabled (pref 22 in BuddyPrefs)",
  43. prefNum: 22,
  44. list: TLVList{
  45. {Tag: FeedbagAttributesBuddyPrefsValid, Value: []byte{0, 0x40, 0, 0}},
  46. {Tag: FeedbagAttributesBuddyPrefs, Value: []byte{0, 0x40, 0, 0}},
  47. },
  48. want: true,
  49. },
  50. {
  51. // prefs 32 and 33 both fall at offset 0 in BuddyPrefs2.
  52. name: "pref 33 at shared offset 0",
  53. prefNum: 33,
  54. list: TLVList{
  55. {Tag: FeedbagAttributesBuddyPrefs2Valid, Value: []byte{0x80, 0, 0, 0}},
  56. {Tag: FeedbagAttributesBuddyPrefs2, Value: []byte{0x80, 0, 0, 0}},
  57. },
  58. want: true,
  59. },
  60. }
  61. for _, tt := range tests {
  62. t.Run(tt.name, func(t *testing.T) {
  63. if got := BuddyPref(tt.list, tt.prefNum); got != tt.want {
  64. t.Fatalf("BuddyPref(%d) = %v, want %v", tt.prefNum, got, tt.want)
  65. }
  66. })
  67. }
  68. }
  69. func TestSetBuddyPref_RoundTrip(t *testing.T) {
  70. // Cover both bitmasks (0-31 and 32+), the 32==33 edge, and true/false.
  71. prefs := []uint16{0x00, 0x16, 0x20, 0x34, 0x49}
  72. for _, want := range []bool{true, false} {
  73. for _, prefNum := range prefs {
  74. list := SetBuddyPref(TLVList{}, prefNum, want)
  75. if value := BuddyPref(list, prefNum); value != want {
  76. t.Errorf("pref 0x%02x round-trip: got %v, want %v", prefNum, value, want)
  77. }
  78. }
  79. }
  80. }
  81. func TestSetBuddyPref_PreservesOtherBits(t *testing.T) {
  82. // Two prefs in the same BuddyPrefs byte, plus one in BuddyPrefs2.
  83. list := TLVList{}
  84. list = SetBuddyPref(list, 0x15, false) // playIMSound off
  85. list = SetBuddyPref(list, 0x16, true) // discloseTyping on
  86. list = SetBuddyPref(list, 0x34, true) // acceptOffLineIM on
  87. for _, tc := range []struct {
  88. prefNum uint16
  89. wantValue bool
  90. }{
  91. {0x15, false},
  92. {0x16, true},
  93. {0x34, true},
  94. } {
  95. if value := BuddyPref(list, tc.prefNum); value != tc.wantValue {
  96. t.Errorf("pref 0x%02x = %v, want %v", tc.prefNum, value, tc.wantValue)
  97. }
  98. }
  99. }
  100. func TestSetBuddyPref_Encoding(t *testing.T) {
  101. // pref 0x34 (=52) enabled from empty: BuddyPrefs2 index 2, mask 0x10.
  102. list := SetBuddyPref(TLVList{}, 0x34, true)
  103. valid, _ := list.Bytes(FeedbagAttributesBuddyPrefs2Valid)
  104. value, _ := list.Bytes(FeedbagAttributesBuddyPrefs2)
  105. if !bytes.Equal(valid, []byte{0, 0, 0x10}) {
  106. t.Errorf("BuddyPrefs2Valid = %v, want [0 0 16]", valid)
  107. }
  108. if !bytes.Equal(value, []byte{0, 0, 0x10}) {
  109. t.Errorf("BuddyPrefs2 = %v, want [0 0 16]", value)
  110. }
  111. // A pref in the fixed bitmask allocates the full 4 bytes.
  112. list = SetBuddyPref(TLVList{}, 0x15, true) // index 1, mask 0x20
  113. valid, _ = list.Bytes(FeedbagAttributesBuddyPrefsValid)
  114. value, _ = list.Bytes(FeedbagAttributesBuddyPrefs)
  115. if !bytes.Equal(valid, []byte{0, 0x20, 0, 0}) {
  116. t.Errorf("BuddyPrefsValid = %v, want [0 32 0 0]", valid)
  117. }
  118. if !bytes.Equal(value, []byte{0, 0x20, 0, 0}) {
  119. t.Errorf("BuddyPrefs = %v, want [0 32 0 0]", value)
  120. }
  121. }