Files
device-code-app/test/device_code_test.dart
T
2026-10-20 12:45:50 +07:00

73 lines
2.5 KiB
Dart

import 'package:device_code_app/device_code.dart';
import 'package:device_code_app/secret.dart';
import 'package:flutter_test/flutter_test.dart';
/// Reference vectors produced by running the real backend
/// `DeviceController.GetDeviceCode` (under faketime for each date).
/// The uppercase-MAC column intentionally differs: the MAC is hashed as typed.
const _macs = [
'74:25:84:c0:02:4a',
'00:00:00:00:00:00',
'ff:ff:ff:ff:ff:ff',
'a1:b2:c3:d4:e5:f6',
'74:25:84:C0:02:4A',
];
const _vectors = <String, List<String>>{
'20261020': ['24720116', '05200455', '56623865', '18749676', '63294203'],
'20261231': ['78353344', '41529234', '71527760', '38956422', '45456286'],
'20270101': ['55726780', '77367158', '08109311', '93484586', '89704380'],
'20280229': ['43234334', '83967713', '74278897', '83465745', '89773533'],
};
void main() {
group('generateDeviceCode matches backend vectors', () {
for (final entry in _vectors.entries) {
for (var i = 0; i < _macs.length; i++) {
test('${entry.key} ${_macs[i]} -> ${entry.value[i]}', () {
expect(
generateDeviceCode(
mac: _macs[i],
date: entry.key,
secret: deviceCodeSecret,
),
entry.value[i],
);
});
}
}
});
test('a different secret gives a different code (secret is really used)', () {
expect(
generateDeviceCode(mac: _macs[0], date: '20261020', secret: 'wrong'),
isNot('24720116'),
);
});
test('formatDateForCode is yyyyMMdd with zero padding', () {
expect(formatDateForCode(DateTime(2027, 1, 1)), '20270101');
expect(formatDateForCode(DateTime(2028, 2, 29)), '20280229');
});
group('formatMac mirrors the web page', () {
test('strips, lowercases, caps at 12 and joins pairs', () {
expect(formatMac('742584C0024A'), '74:25:84:c0:02:4a');
expect(formatMac('74-25-84-C0-02-4A'), '74:25:84:c0:02:4a');
expect(formatMac('74:25:84:c0:02:4a:ff:ee'), '74:25:84:c0:02:4a');
expect(formatMac(' zz 74 25 '), '74:25');
});
test('formats partial input progressively', () {
expect(formatMac(''), '');
expect(formatMac('7'), '7');
expect(formatMac('742'), '74:2');
expect(formatMac('7425'), '74:25');
});
test('isCompleteMac only accepts 12 formatted hex digits', () {
expect(isCompleteMac('74:25:84:c0:02:4a'), isTrue);
expect(isCompleteMac('74:25:84:c0:02:4'), isFalse);
expect(isCompleteMac('74:25:84:C0:02:4A'), isFalse);
});
});
}