Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

112 wiersze
3.2 KiB

  1. package utils
  2. import (
  3. "testing"
  4. "github.com/AFASystems/presence/internal/pkg/common/appcontext"
  5. "github.com/AFASystems/presence/internal/pkg/common/utils"
  6. "github.com/AFASystems/presence/internal/pkg/model"
  7. )
  8. func TestParseADFast_Empty(t *testing.T) {
  9. result := utils.ParseADFast([]byte{})
  10. if len(result) != 0 {
  11. t.Errorf("Expected empty result, got %d structures", len(result))
  12. }
  13. }
  14. func TestParseADFast_SingleStructure(t *testing.T) {
  15. // Length 2, type 0x01, data 0x06 -> [0,3)
  16. data := []byte{0x02, 0x01, 0x06}
  17. result := utils.ParseADFast(data)
  18. if len(result) != 1 {
  19. t.Fatalf("Expected 1 structure, got %d", len(result))
  20. }
  21. if result[0][0] != 0 || result[0][1] != 3 {
  22. t.Errorf("Expected [0,3), got [%d,%d)", result[0][0], result[0][1])
  23. }
  24. }
  25. func TestParseADFast_MultipleStructures(t *testing.T) {
  26. // First: len=2 (bytes 0-2), Second: len=3 (bytes 3-6)
  27. data := []byte{0x02, 0x01, 0x06, 0x03, 0xFF, 0x4C, 0x00}
  28. result := utils.ParseADFast(data)
  29. if len(result) != 2 {
  30. t.Fatalf("Expected 2 structures, got %d", len(result))
  31. }
  32. if result[0][0] != 0 || result[0][1] != 3 {
  33. t.Errorf("First structure: expected [0,3), got [%d,%d)", result[0][0], result[0][1])
  34. }
  35. if result[1][0] != 3 || result[1][1] != 7 {
  36. t.Errorf("Second structure: expected [3,7), got [%d,%d)", result[1][0], result[1][1])
  37. }
  38. }
  39. func TestParseADFast_ZeroLengthBreaks(t *testing.T) {
  40. data := []byte{0x00, 0x01, 0x02}
  41. result := utils.ParseADFast(data)
  42. if len(result) != 0 {
  43. t.Errorf("Expected 0 structures (zero length breaks), got %d", len(result))
  44. }
  45. }
  46. func TestRemoveFlagBytes_WithFlags(t *testing.T) {
  47. // AD structure: len=2, type=0x01 (flags), data=0x06
  48. // Remaining: 0x03, 0xFF, 0x4C
  49. data := []byte{0x02, 0x01, 0x06, 0x03, 0xFF, 0x4C, 0x00}
  50. result := utils.RemoveFlagBytes(data)
  51. if len(result) != 4 {
  52. t.Errorf("Expected 4 bytes after flag removal, got %d", len(result))
  53. }
  54. if result[0] != 0x03 && result[1] != 0xFF {
  55. t.Errorf("Expected flag bytes removed, got %v", result)
  56. }
  57. }
  58. func TestRemoveFlagBytes_WithoutFlags(t *testing.T) {
  59. data := []byte{0x02, 0xFF, 0x4C, 0x00} // type 0xFF, not 0x01
  60. result := utils.RemoveFlagBytes(data)
  61. if len(result) != 4 {
  62. t.Errorf("Expected unchanged 4 bytes, got %d", len(result))
  63. }
  64. }
  65. func TestRemoveFlagBytes_TooShort(t *testing.T) {
  66. data := []byte{0x01}
  67. result := utils.RemoveFlagBytes(data)
  68. if len(result) != 1 {
  69. t.Errorf("Expected 1 byte, got %d", len(result))
  70. }
  71. }
  72. func TestCalculateDistance(t *testing.T) {
  73. tests := []struct {
  74. name string
  75. rssi int64
  76. txPower string
  77. }{
  78. {"typical beacon", -65, "C5"}, // -59 in two's complement
  79. {"weak signal", -90, "C5"},
  80. {"strong signal", -40, "C5"},
  81. }
  82. for _, tt := range tests {
  83. t.Run(tt.name, func(t *testing.T) {
  84. adv := appcontext.BeaconAdvertisement{RSSI: tt.rssi, TXPower: tt.txPower}
  85. d := utils.CalculateDistance(adv)
  86. if d < 0 {
  87. t.Errorf("Distance should be non-negative, got %f", d)
  88. }
  89. })
  90. }
  91. }
  92. func TestLoopADStructures_NoParsers(t *testing.T) {
  93. registry := &model.ParserRegistry{ParserList: make(map[string]model.BeaconParser)}
  94. data := []byte{0x02, 0x01, 0x06}
  95. indices := utils.ParseADFast(data)
  96. event := utils.LoopADStructures(data, indices, "beacon-1", registry, "")
  97. if event.ID != "" {
  98. t.Errorf("Expected empty event with no parsers, got ID %s", event.ID)
  99. }
  100. }