Repository navigation
Expand file tree
/
Copy pathmain_test.go
More file actions
140 lines (119 loc) · 3.48 KB
/
Copy pathmain_test.go
File metadata and controls
140 lines (119 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// SPDX-License-Identifier: BSD-2-Clause
// Copyright (c) 2018- Joonas Kuorilehto
package main
import (
"encoding/hex"
"flag"
"io"
"log"
"os"
"testing"
"github.com/joneskoo/ruuvi-prometheus/internal/hci"
"github.com/joneskoo/ruuvi-prometheus/internal/host"
)
func TestDeviceFlag(t *testing.T) {
var dev string
f := &deviceFlag{&dev}
if err := f.Set(""); err == nil {
t.Error("Set(\"\") = nil, want error for empty device name")
}
if err := f.Set("hci5"); err != nil {
t.Fatalf("Set(\"hci5\") = %v, want nil", err)
}
if got := f.Get(); got != "hci5" {
t.Errorf("Get() = %q, want hci5", got)
}
if got := f.String(); got != "hci5" {
t.Errorf("String() = %q, want hci5", got)
}
// A deviceFlag with a nil value must not panic and reports empty.
if got := (deviceFlag{}).Get(); got != "" {
t.Errorf("Get() on nil value = %q, want empty", got)
}
}
func TestParseSettings(t *testing.T) {
oldArgs, oldCL := os.Args, flag.CommandLine
defer func() { os.Args, flag.CommandLine = oldArgs, oldCL }()
flag.CommandLine = flag.NewFlagSet("test", flag.ContinueOnError)
os.Args = []string{"ruuvi-prometheus", "-listen", ":1234", "-device", "hci9", "-debug"}
s := parseSettings()
if s.listen != ":1234" {
t.Errorf("listen = %q, want :1234", s.listen)
}
if s.device != "hci9" {
t.Errorf("device = %q, want hci9", s.device)
}
if !s.debug {
t.Error("debug = false, want true")
}
}
func TestParseSettingsDefaults(t *testing.T) {
oldArgs, oldCL := os.Args, flag.CommandLine
defer func() { os.Args, flag.CommandLine = oldArgs, oldCL }()
flag.CommandLine = flag.NewFlagSet("test", flag.ContinueOnError)
os.Args = []string{"ruuvi-prometheus"}
s := parseSettings()
if s.listen != defaultListen {
t.Errorf("listen = %q, want default %q", s.listen, defaultListen)
}
if s.device != "hci0" {
t.Errorf("device = %q, want default hci0", s.device)
}
if s.debug {
t.Error("debug = true, want default false")
}
}
func TestGetDebugLogger(t *testing.T) {
if getDebugLogger(true) == nil {
t.Error("getDebugLogger(true) = nil")
}
if getDebugLogger(false) == nil {
t.Error("getDebugLogger(false) = nil")
}
}
func TestHandleRuuviAdvertisement(t *testing.T) {
log.SetOutput(io.Discard)
defer log.SetOutput(os.Stderr)
addr, err := hci.BtAddressFromString("ee:36:80:be:ec:fe")
if err != nil {
t.Fatalf("invalid address: %v", err)
}
frame, err := hex.DecodeString("99040501c4271ac87dfc18fc18fc18af166403e8010203040506")
if err != nil {
t.Fatalf("invalid frame hex: %v", err)
}
// Valid Ruuvi manufacturer data plus a non-Ruuvi AD structure that fails to
// decode: the handler must process the good one and skip the bad one without
// panicking.
sr := &host.ScanReport{
Address: addr,
Data: []*hci.AdStructure{
{Data: frame},
{Data: []byte{0x00, 0x01}},
},
}
handleRuuviAdvertisement(sr)
}
func TestRuuviAdFilter(t *testing.T) {
filt := ruuviAdFilter()
// Manufacturer specific data prefixed with the Ruuvi company id (0x0499,
// little-endian 0x99 0x04) must match; a different vendor must not.
ruuvi := &hci.AdvertisingReport{
Data: []*hci.AdStructure{{
Typ: hci.AdManufacturerSpecific,
Data: []byte{0x99, 0x04, 0x05, 0x00},
}},
}
if !filt.Filter(ruuvi) {
t.Error("ruuviAdFilter did not match Ruuvi manufacturer data")
}
other := &hci.AdvertisingReport{
Data: []*hci.AdStructure{{
Typ: hci.AdManufacturerSpecific,
Data: []byte{0x4c, 0x00, 0x05, 0x00},
}},
}
if filt.Filter(other) {
t.Error("ruuviAdFilter matched non-Ruuvi manufacturer data")
}
}