mockIPv6 function
Generate random IPv6 address.
format
argument accepts integers in range from 0 to 65536,
separated by colons(:
), which represent a group or hextet in IPv6.
Group can also be represented with *
, which generates any
hexadecimal number of 16 bits for a particular group.
Returns IP as String.
Example usage:
mockIPv6('*:e331:93bf:*:a7c9:a63:*:*')
mockIPv6('e1b3:7bae:*:3474:*:c0cc:462:c4b9')
mockIPv6() == mockIPv6('*:*:*:*:*:*:*:*')
Implementation
String mockIPv6([String format = '*:*:*:*:*:*:*:*']) {
var ip = format.split(':');
if (ip.length != 8) {
throw ArgumentError('Invalid IPv6 format - Must contain 8 groups');
}
var _ip = ip
.map((s) {
if (s == '*') {
return '${random.nextInt(65535 + 1).toRadixString(16).padLeft(4, '0')}:';
}
var parsedGroup = int.tryParse(s, radix: 16);
if (parsedGroup != null && parsedGroup >= 0 && parsedGroup <= 65536) {
return '${s.padLeft(4, '0')}:';
} else {
throw ArgumentError('Integers must be in range of 0 and 65536');
}
})
.toList()
.join();
return _ip.substring(0, _ip.length - 1);
}