-
-
Notifications
You must be signed in to change notification settings - Fork 64
Match device ids case-insensitively across event streams #269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import 'dart:typed_data'; | ||
|
|
||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:universal_ble/src/utils/cache_handler.dart'; | ||
| import 'package:universal_ble/universal_ble.dart'; | ||
|
|
||
| import 'universal_ble_test_mock.dart'; | ||
|
|
||
| /// A BLE device id is a case-insensitive identifier, but platforms report it in different cases (Android | ||
| /// upper-cases MACs, Windows/WinRT lower-cases them). These tests guard that universal_ble treats the two | ||
| /// cases as ONE device — both the event-stream matching AND the per-device state keyed internally (pairing | ||
| /// dedup, connection-parameters dedup, the service cache) — so a caller holding the id in a different case | ||
| /// than the platform reports doesn't miss events, doesn't hang connect(), and doesn't split across entries. | ||
| class _MockPlatform extends UniversalBlePlatformMock { | ||
| @override | ||
| Future<int> readRssi(String deviceId) => throw UnimplementedError(); | ||
|
|
||
| @override | ||
| Future<void> connect( | ||
| String deviceId, { | ||
| bool autoConnect = false, | ||
| Duration? connectionTimeout, | ||
| ConnectionPlatformConfig? platformConfig, | ||
| }) async { | ||
| // Report the connection back with a DIFFERENT case than the caller passed — the original hang scenario. | ||
| updateConnection(deviceId.toLowerCase(), true); | ||
| } | ||
| } | ||
|
|
||
| void main() { | ||
| const upper = 'AA:BB:CC:DD:EE:FF'; | ||
| const lower = 'aa:bb:cc:dd:ee:ff'; | ||
| const charId = '0000fff1-0000-1000-8000-00805f9b34fb'; | ||
|
|
||
| test('connectionStream matches a device id reported in a different case', () async { | ||
| final platform = _MockPlatform(); | ||
| final event = platform.connectionStream(upper).first; | ||
| platform.updateConnection(lower, true); | ||
| expect(await event, isTrue); | ||
| }); | ||
|
|
||
| test('characteristicValueStream matches a device id reported in a different case', () async { | ||
| final platform = _MockPlatform(); | ||
| final event = platform.characteristicValueStream(upper, charId).first; | ||
| platform.updateCharacteristicValue( | ||
| lower, charId, Uint8List.fromList([1, 2, 3]), null); | ||
| expect(await event, Uint8List.fromList([1, 2, 3])); | ||
| }); | ||
|
|
||
| test('pairingStateStream matches a device id reported in a different case', () async { | ||
| final platform = _MockPlatform(); | ||
| final event = platform.pairingStateStream(upper).first; | ||
| platform.updatePairingState(lower, true); | ||
| expect(await event, isTrue); | ||
| }); | ||
|
|
||
| test('connect() completes when the platform reports the id in a different case', () async { | ||
| UniversalBle.setInstance(_MockPlatform()); | ||
| // Must not throw / time out: connect(upper) awaits a lower-case connection update (the original hang). | ||
| await UniversalBle.connect(upper, timeout: const Duration(seconds: 2)); | ||
| }); | ||
|
|
||
| test('pairing-state dedup treats the two cases as one device', () async { | ||
| final platform = _MockPlatform(); | ||
| final events = <bool>[]; | ||
| final sub = platform.pairingStateStream(upper).listen(events.add); | ||
| platform.updatePairingState(lower, true); // first -> emits | ||
| platform.updatePairingState(upper, true); // same device+value, other case -> deduped, no second emit | ||
| await Future<void>.delayed(const Duration(milliseconds: 20)); | ||
| await sub.cancel(); | ||
| expect(events, [true]); | ||
| }); | ||
|
|
||
| test('connection-parameters dedup treats the two cases as one device', () async { | ||
| final platform = _MockPlatform(); | ||
| final events = <String>[]; | ||
| platform.onConnectionParametersChange = (u) => events.add(u.deviceId); | ||
| BleConnectionParametersUpdated params(String id) => | ||
| BleConnectionParametersUpdated( | ||
| deviceId: id, interval: 12, latency: 0, supervisionTimeout: 500, status: 0); | ||
| platform.updateConnectionParameters(params(lower)); // first -> fires | ||
| platform.updateConnectionParameters(params(upper)); // identical params, other case -> deduped | ||
| await Future<void>.delayed(const Duration(milliseconds: 20)); | ||
| expect(events, [lower]); | ||
| }); | ||
|
|
||
| test('service cache is keyed case-insensitively (save one case, get/clear another)', () { | ||
| final cache = CacheHandler.instance; | ||
| cache.resetDeviceCache(upper); // clean slate | ||
| cache.saveServices(upper, const []); // non-null -> cached | ||
| expect(cache.getServices(lower), isNotNull); // found via the other case | ||
| cache.resetDeviceCache(lower); // cleared via the other case | ||
| expect(cache.getServices(upper), isNull); | ||
| }); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stream filtering is now case-insensitive, but _pairStateMap and _lastConnectionParametersMap use case-sensitive device ID keys. Since platforms report device IDs with different cases (Android upper-cases MACs, Windows lower-cases them), the same device will create multiple map entries with different cases. This causes .remove() calls to miss entries, deduplication checks to fail, and state tracking to become inconsistent.
Please add tests when fixing it.