Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 

111 rindas
3.2 KiB

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