Files
swiftcontrol/test/bluetooth/ble_string_test.dart
skyiea314 fa56e76a9b fix(ble): match Onelap Key faces and discover it on Android (#440)
* fix(ble): match Onelap Key faces and strip firmware padding

The settings page drew the firmware revision's trailing padding as tofu after 0.105. Device Information strings now stop at the first NUL and drop 0xFF or non-UTF-8 tails. The controller diagram uses the molded cross: circle, triangle, square, dot pad, and ONELAP.

* fix(ble): show legacy Onelap Key advertisements on Android

universal_ble's default Android scan asks only for BLE 5 extended advertisements, so a BLE 4.x Onelap Key never appears. Request legacy advertisements at low latency, and accept a later packet that is the first one to carry the controller name.

* style: keep the Onelap scan change to the discovery lines

The previous commit reformatted connection.dart. Restore the surrounding file and leave the legacy-scan and name-arrival checks.

* fix(ble): preserve valid UTF-8 prefixes

Keep the valid device-information prefix before malformed UTF-8.
Discard printable bytes after the error and cover truncated sequences
and valid replacement characters with regression tests.

* fix(ble): retain extended advertisements

Allow both legacy and extended Android advertisements while keeping
low-latency scanning. Verify the configuration at the BLE platform
boundary and remove the incorrect extended-only explanation.

* style: use English comments and test descriptions

---------

Co-authored-by: haoxiang.yang <haoxiang.yang@megatronix.co>
2026-11-06 08:38:36 +01:00

77 lines
2.5 KiB
Dart

import 'dart:convert';
import 'package:bike_control/bluetooth/ble_string.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
test('keeps a plain firmware version', () {
expect(decodeBleDeviceInfoString('1.2.0'.codeUnits), '1.2.0');
expect(decodeBleDeviceInfoString('0.105'.codeUnits), '0.105');
});
test('drops NUL padding that would render as tofu', () {
expect(
decodeBleDeviceInfoString([...'0.105'.codeUnits, 0, 0, 0, 0, 0xFF, 0xFF]),
'0.105',
);
});
test('drops 0xFF fill and non-UTF-8 tails after the version', () {
expect(decodeBleDeviceInfoString([...'0.105'.codeUnits, 0xFF, 0xFF, 0xFF]), '0.105');
expect(
decodeBleDeviceInfoString([...'0.105'.codeUnits, 0x80, 0x81, 0xFE, 0xFD]),
'0.105',
);
});
test('keeps a valid UTF-8 manufacturer name', () {
final name = utf8.encode('迈金');
expect(decodeBleDeviceInfoString(name), '迈金');
expect(decodeBleDeviceInfoString([...name, 0, 0xFF]), '迈金');
});
test('malformed UTF-8 tails preserve the valid Chinese prefix', () {
expect(decodeBleDeviceInfoString([...utf8.encode('迈金'), 0x80, 0xFF]), '迈金');
});
test('ASCII after the first malformed sequence is not appended to the firmware version', () {
for (final tail in [
[0x80, 0x41],
[0xE2, 0x41],
[0xE2, 0x82, 0x41],
[0xC0, 0xAF, 0x41],
[0xED, 0xA0, 0x80, 0x41],
[0xF4, 0x90, 0x80, 0x80, 0x41],
[0xFF, 0x41],
]) {
expect(decodeBleDeviceInfoString([...'0.105'.codeUnits, ...tail]), '0.105', reason: '$tail');
}
});
test('incomplete multibyte tails preserve the valid Unicode prefix', () {
for (final tail in [
[0xC2],
[0xE2, 0x82],
[0xF0, 0x9F, 0x92],
]) {
expect(decodeBleDeviceInfoString([...utf8.encode('迈金🚴'), ...tail]), '迈金🚴', reason: '$tail');
}
});
test('valid replacement characters do not truncate subsequent device information', () {
final name = utf8.encode('迈\uFFFD金');
expect(decodeBleDeviceInfoString(name), '迈金');
expect(decodeBleDeviceInfoString([...name, 0x80]), '迈金');
});
test('malformed leading bytes do not expose subsequent ASCII as device information', () {
expect(decodeBleDeviceInfoString([0x80, ...'0.105'.codeUnits]), isNull);
});
test('returns null for an empty or padding-only buffer', () {
expect(decodeBleDeviceInfoString(const []), isNull);
expect(decodeBleDeviceInfoString([0, 0, 0xFF]), isNull);
expect(decodeBleDeviceInfoString([0xFF, 0xFF]), isNull);
});
}