@@ -131,3 +131,58 @@ func TestCreateAdvertisementPayload(t *testing.T) {
131
131
}
132
132
}
133
133
}
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\n expected: %#v\n actual: %#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\n has: %#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\n expected: %#v\n actual: %#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\n has: %#v" , uuid , fields .ServiceUUIDs ())
185
+ }
186
+ }
187
+ }
188
+ }
0 commit comments