You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
2.1 KiB
60 lines
2.1 KiB
// Helpers of der_test.dart and der_vectors_test.dart: the walk of
|
|
// FuzzDERCheck of datekeys-go, and the mutations of a fuzzer.
|
|
|
|
import 'dart:math';
|
|
import 'dart:typed_data';
|
|
|
|
import 'package:datekeys/datekeys.dart' show concatBytes, toHex;
|
|
import 'package:datekeys/src/der.dart' as der;
|
|
import 'package:test/test.dart';
|
|
|
|
/// Checks that split, content and parseTime read the element [b], which
|
|
/// check accepted, element by element: the children of a constructed element
|
|
/// are its content exactly, a primitive one is not split, and a time of the
|
|
/// universal class is read by parseTime.
|
|
void walkAccepted(Uint8List b) {
|
|
final c = der.content(b);
|
|
if (b[0] & 0x20 == 0) {
|
|
expect(() => der.split(b), throwsA(isA<der.DerException>()));
|
|
if (b[0] == 0x17 || b[0] == 0x18) der.parseTime(b);
|
|
return;
|
|
}
|
|
final s = der.split(b);
|
|
expect(s.id, b[0]);
|
|
expect(toHex(concatBytes(s.children)), toHex(c), reason: toHex(b));
|
|
for (final child in s.children) {
|
|
walkAccepted(child);
|
|
}
|
|
}
|
|
|
|
// Bytes that matter to DER: identifiers, lengths, and the characters of a
|
|
// time.
|
|
const _boundaries = [
|
|
0x00, 0x01, 0x02, 0x04, 0x05, 0x06, 0x17, 0x18, 0x1f, 0x30, 0x31, 0x2e, //
|
|
0x5a, 0x7f, 0x80, 0x81, 0x82, 0x83, 0x84, 0xa0, 0xff,
|
|
];
|
|
|
|
/// [b] with one to three random edits, as the mutations of a fuzzer.
|
|
Uint8List mutateDer(Random r, List<int> b) {
|
|
final out = List<int>.of(b);
|
|
for (var n = 1 + r.nextInt(3); n > 0; n--) {
|
|
final k = r.nextInt(7);
|
|
if (k == 0 && out.isNotEmpty) {
|
|
out[r.nextInt(out.length)] ^= 1 << r.nextInt(8);
|
|
} else if (k == 1 && out.isNotEmpty) {
|
|
out[r.nextInt(out.length)] = _boundaries[r.nextInt(_boundaries.length)];
|
|
} else if (k == 2 && out.isNotEmpty) {
|
|
out.length = r.nextInt(out.length);
|
|
} else if (k == 3) {
|
|
out.insert(r.nextInt(out.length + 1), r.nextInt(256));
|
|
} else if (k == 4 && out.length > 1) {
|
|
out.removeAt(r.nextInt(out.length));
|
|
} else if (k == 5 && out.isNotEmpty) {
|
|
out[r.nextInt(out.length)] = 0x30 + r.nextInt(10);
|
|
} else {
|
|
out.add(r.nextInt(256));
|
|
}
|
|
}
|
|
return Uint8List.fromList(out);
|
|
}
|