|
| 1 | +import 'dart:typed_data'; |
| 2 | + |
| 3 | +import 'package:files_tech_core/files_tech_core.dart'; |
| 4 | +import 'package:flutter_test/flutter_test.dart'; |
| 5 | + |
| 6 | +void main() { |
| 7 | + group('SecretBytes.fromHex — rejet des entrées non hexadécimales', () { |
| 8 | + // Régression : `int.parse(..., radix: 16)` accepte un signe de tête et des |
| 9 | + // espaces. `fromHex('-a')` rendait donc l'octet 246 (troncature deux |
| 10 | + // compléments par Uint8List) au lieu de lever, alors que la doc promet une |
| 11 | + // FormatException. Ces cas verrouillent le contrat. |
| 12 | + for (final bad in const ['+a', '-a', ' a', 'a ', '+0', '-0', 'g0', '0g']) { |
| 13 | + test('rejette "$bad"', () { |
| 14 | + expect(() => SecretBytes.fromHex(bad), throwsFormatException); |
| 15 | + }); |
| 16 | + } |
| 17 | + |
| 18 | + test('rejette une longueur impaire', () { |
| 19 | + expect(() => SecretBytes.fromHex('abc'), throwsFormatException); |
| 20 | + }); |
| 21 | + |
| 22 | + test('accepte le hex valide, casses mélangées', () { |
| 23 | + expect(SecretBytes.fromHex('00ffAb'), [0x00, 0xff, 0xab]); |
| 24 | + }); |
| 25 | + |
| 26 | + test('chaîne vide → buffer vide', () { |
| 27 | + expect(SecretBytes.fromHex(''), isEmpty); |
| 28 | + }); |
| 29 | + |
| 30 | + test('round-trip toHex ∘ fromHex est l\'identité', () { |
| 31 | + final src = Uint8List.fromList([0, 1, 15, 16, 127, 128, 254, 255]); |
| 32 | + expect(SecretBytes.fromHex(SecretBytes.toHex(src)), src); |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + group('SecretBytes.wipe — signale l\'échec au lieu de l\'avaler', () { |
| 37 | + test('buffer modifiable : zéroïse et retourne true', () { |
| 38 | + final b = Uint8List.fromList([1, 2, 3, 4]); |
| 39 | + expect(SecretBytes.wipe(b), isTrue); |
| 40 | + expect(b, everyElement(0)); |
| 41 | + }); |
| 42 | + |
| 43 | + test('vue non modifiable : retourne false sans lever', () { |
| 44 | + // C'est le cas que certaines impls FFI de `cryptography_flutter` |
| 45 | + // produisent. Avant, l'échec était silencieux : l'appelant croyait le |
| 46 | + // matériel de clé effacé alors qu'il restait intact en RAM. |
| 47 | + final ro = Uint8List.fromList([1, 2, 3, 4]).asUnmodifiableView(); |
| 48 | + expect(SecretBytes.wipe(ro), isFalse); |
| 49 | + expect(ro, [1, 2, 3, 4]); |
| 50 | + }); |
| 51 | + |
| 52 | + test('buffer vide : succès trivial', () { |
| 53 | + expect(SecretBytes.wipe(Uint8List(0)), isTrue); |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + group('SecretBytes.constantTimeEq', () { |
| 58 | + test('longueurs différentes → false', () { |
| 59 | + expect(SecretBytes.constantTimeEq([1, 2], [1, 2, 3]), isFalse); |
| 60 | + }); |
| 61 | + |
| 62 | + test('contenu identique → true', () { |
| 63 | + expect(SecretBytes.constantTimeEq([1, 2, 3], [1, 2, 3]), isTrue); |
| 64 | + }); |
| 65 | + |
| 66 | + test('un seul bit différent → false', () { |
| 67 | + expect(SecretBytes.constantTimeEq([1, 2, 3], [1, 2, 2]), isFalse); |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + group('SecretBytes.randomBytes', () { |
| 72 | + test('longueur négative → ArgumentError', () { |
| 73 | + expect(() => SecretBytes.randomBytes(-1), throwsArgumentError); |
| 74 | + }); |
| 75 | + |
| 76 | + test('n == 0 → buffer vide', () { |
| 77 | + expect(SecretBytes.randomBytes(0), isEmpty); |
| 78 | + }); |
| 79 | + |
| 80 | + test('deux tirages de 32 octets diffèrent', () { |
| 81 | + expect(SecretBytes.randomBytes(32), isNot(SecretBytes.randomBytes(32))); |
| 82 | + }); |
| 83 | + }); |
| 84 | +} |
0 commit comments