From c3f9d593de34ee286ec556bd631d1ab88208281b Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Mon, 31 Jul 2023 15:29:45 +0200 Subject: [PATCH] sd: test creation of raw BLE advertisement packets I realized we didn't have any tests for this yet, which we really should have. So here they are. --- gap_test.go | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 gap_test.go diff --git a/gap_test.go b/gap_test.go new file mode 100644 index 0000000..c504325 --- /dev/null +++ b/gap_test.go @@ -0,0 +1,70 @@ +package bluetooth + +import ( + "testing" + "time" +) + +func TestCreateAdvertisementPayload(t *testing.T) { + type testCase struct { + raw string + parsed AdvertisementOptions + } + tests := []testCase{ + { + raw: "\x02\x01\x06", // flags + parsed: AdvertisementOptions{}, + }, + { + raw: "\x02\x01\x06", // flags + parsed: AdvertisementOptions{ + // Interval doesn't affect the advertisement payload. + Interval: NewDuration(100 * time.Millisecond), + }, + }, + { + raw: "\x02\x01\x06" + // flags + "\x07\x09foobar", // local name + parsed: AdvertisementOptions{ + LocalName: "foobar", + }, + }, + { + raw: "\x02\x01\x06" + // flags + "\x0b\x09Heart rate" + // local name + "\x03\x03\x0d\x18", // service UUID + parsed: AdvertisementOptions{ + LocalName: "Heart rate", + ServiceUUIDs: []UUID{ + ServiceUUIDHeartRate, + }, + }, + }, + { + // Note: the two service UUIDs should really be merged into one to + // save space. + raw: "\x02\x01\x06" + // flags + "\x0b\x09Heart rate" + // local name + "\x03\x03\x0d\x18" + // heart rate service UUID + "\x03\x03\x0f\x18", // battery service UUID + parsed: AdvertisementOptions{ + LocalName: "Heart rate", + ServiceUUIDs: []UUID{ + ServiceUUIDHeartRate, + ServiceUUIDBattery, + }, + }, + }, + } + for _, tc := range tests { + var expectedRaw rawAdvertisementPayload + expectedRaw.len = uint8(len(tc.raw)) + copy(expectedRaw.data[:], tc.raw) + + var raw rawAdvertisementPayload + raw.addFromOptions(tc.parsed) + if raw != expectedRaw { + t.Errorf("error when serializing options: %#v\nexpected: %#v\nactual: %#v\n", tc.parsed, tc.raw, string(raw.data[:raw.len])) + } + } +}