Skip to content

Commit 0613dfc

Browse files
mookdeadprogram
authored andcommitted
gap: Add tests for AdvertisementPayload.ServiceUUIDs()
This catches an issue in rawAdvertisementPayload where NewUUID creates a UUID with the reversed byte order. Fixes: 873d49b
1 parent a6c741b commit 0613dfc

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

gap.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,9 @@ func (buf *rawAdvertisementPayload) ServiceUUIDs() []UUID {
350350
b = buf.findField(0x06) // Incomplete List of 128-bit Service Class UUIDs
351351
}
352352
for i := 0; i < len(b)/16; i++ {
353-
uuids = append(uuids, NewUUID([16]byte(b[i*16:i*16+16])))
353+
var uuid UUID
354+
_ = uuid.UnmarshalBinary(b[i*16 : i*16+16])
355+
uuids = append(uuids, uuid)
354356
}
355357
return uuids
356358
}

gap_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,58 @@ func TestCreateAdvertisementPayload(t *testing.T) {
131131
}
132132
}
133133
}
134+
135+
func TestServiceUUIDs(t *testing.T) {
136+
type testCase struct {
137+
raw string
138+
expected []UUID
139+
}
140+
uuidBytes := ServiceUUIDAdafruitSound.Bytes()
141+
tests := []testCase{
142+
{},
143+
{
144+
raw: "\x03\x03\x0d\x18", // service UUID
145+
expected: []UUID{ServiceUUIDHeartRate},
146+
},
147+
{
148+
raw: "\x03\x02\x0f\x18", // Service UUID
149+
expected: []UUID{ServiceUUIDBattery},
150+
},
151+
{
152+
raw: "\x11\x07" + string(uuidBytes[:]),
153+
expected: []UUID{ServiceUUIDAdafruitSound},
154+
},
155+
{
156+
raw: "\x11\x06" + string(uuidBytes[:]),
157+
expected: []UUID{ServiceUUIDAdafruitSound},
158+
},
159+
{
160+
raw: "\x11\x06" + string(uuidBytes[:15]), // data was cut off
161+
},
162+
}
163+
for _, tc := range tests {
164+
raw := rawAdvertisementPayload{len: uint8(len(tc.raw))}
165+
copy(raw.data[:], []byte(tc.raw))
166+
actual := raw.ServiceUUIDs()
167+
if !reflect.DeepEqual(actual, tc.expected) {
168+
t.Errorf("unexpected raw service UUIDs: %#v\nexpected: %#v\nactual: %#v\n",
169+
tc.raw, tc.expected, actual)
170+
}
171+
for _, uuid := range actual {
172+
if !raw.HasServiceUUID(uuid) {
173+
t.Errorf("raw payload does not have UUID %#v\nhas: %#v", uuid, raw.ServiceUUIDs())
174+
}
175+
}
176+
fields := advertisementFields{AdvertisementFields: AdvertisementFields{ServiceUUIDs: tc.expected}}
177+
actual = fields.ServiceUUIDs()
178+
if !reflect.DeepEqual(actual, tc.expected) {
179+
t.Errorf("unexpected structured service UUIDs: %#v\nexpected: %#v\nactual: %#v\n",
180+
tc.raw, tc.expected, actual)
181+
}
182+
for _, uuid := range actual {
183+
if !fields.HasServiceUUID(uuid) {
184+
t.Errorf("structured payload does not have UUID %#v\nhas: %#v", uuid, fields.ServiceUUIDs())
185+
}
186+
}
187+
}
188+
}

0 commit comments

Comments
 (0)