totp_test.dart 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import 'package:test/test.dart';
  2. import 'package:pb_authenticator_totp/totp.dart';
  3. import 'package:pb_authenticator_totp/totp_algorithm.dart';
  4. import 'package:pb_authenticator_totp/otp_uri.dart';
  5. void main() {
  6. // https://datatracker.ietf.org/doc/html/rfc4648#section-10
  7. // https://stackoverflow.com/a/54263961
  8. test('Base32', () {
  9. expect(Base32.decode("MY======"), "f".codeUnits);
  10. expect(Base32.decode("MZXQ===="), "fo".codeUnits);
  11. expect(Base32.decode("MZXW6==="), "foo".codeUnits);
  12. expect(Base32.decode("MZXW6YQ="), "foob".codeUnits);
  13. expect(Base32.decode("MZXW6YTB"), "fooba".codeUnits);
  14. expect(Base32.decode("MZXW6YTBOI======"), "foobar".codeUnits);
  15. expect(Base32.isBase32("MZXW6YTBOI======"), true);
  16. expect(Base32.decode("AE======"), "\x01".codeUnits);
  17. expect(Base32.decode("AA======"), "\x00".codeUnits);
  18. });
  19. test('TOTP - Key URI 1', () {
  20. var parsed = OtpUri.fromUri(
  21. "otpauth://totp/Example:alice@example.com?secret=JBSWY3DPEHPK3PXP&issuer=Example");
  22. expect(parsed.accountName, "alice@example.com");
  23. expect(parsed.algorithm, OtpHashAlgorithm.sha1);
  24. expect(parsed.digits, 6);
  25. expect(parsed.issuer, "Example");
  26. expect(parsed.period, 30);
  27. expect(parsed.secret, "JBSWY3DPEHPK3PXP");
  28. });
  29. test('TOTP - Key URI Complete', () {
  30. var parsed = OtpUri.fromUri(
  31. "otpauth://totp/ACME%20Co:john.doe@email.com?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=ACME%20Co&algorithm=SHA1&digits=6&period=30");
  32. expect(parsed.accountName, "john.doe@email.com");
  33. expect(parsed.algorithm, OtpHashAlgorithm.sha1);
  34. expect(parsed.digits, 6);
  35. expect(parsed.issuer, "ACME Co");
  36. expect(parsed.period, 30);
  37. expect(parsed.secret, "HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ");
  38. });
  39. test('TOTP - Bad Key URI', () {
  40. expect(
  41. () => OtpUri.fromUri(
  42. "otpauth://totp/Example:alice@example.com?secret=JBSWY3DPEHPK3PXP&issuer=Example&digits=12"),
  43. throwsA(allOf(
  44. isFormatException,
  45. predicate((e) =>
  46. e is FormatException && e.message == "Incorrect parameters"))));
  47. });
  48. test('TOTP - Bad Algorithm', () {
  49. expect(
  50. () => OtpUri.fromUri(
  51. "otpauth://totp/Example:alice@example.com?secret=JBSWY3DPEHPK3PXP&issuer=Example&digits=12&algorithm=sha??"),
  52. throwsA(allOf(
  53. isFormatException,
  54. predicate((e) =>
  55. e is FormatException &&
  56. e.message == "Unrecognised algorithm"))));
  57. });
  58. test('TOTP - 1542791843', () {
  59. expect(Totp.generateCode(1542791843, "JBSWY3DPEHPK3PXP"), "092264");
  60. });
  61. test('TOTP - 59', () {
  62. expect(Totp.generateCode(59, "GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ", 8),
  63. "94287082");
  64. });
  65. test('TOTP - 20000000000', () {
  66. expect(
  67. Totp.generateCode(20000000000, "GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ", 8),
  68. "65353130");
  69. });
  70. test('TOTP - Multiple', () {
  71. expect(
  72. Totp.generateCodes(
  73. [59, 20000000000], "GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ", 8),
  74. ["94287082", "65353130"]);
  75. });
  76. test('TOTP Item attributes', () {
  77. var secret = "A";
  78. var digits = 8;
  79. var period = 60;
  80. var algorithm = OtpHashAlgorithm.sha256;
  81. var issuer = "bar";
  82. var accountName = "foo@bar";
  83. var item = TotpItem(secret, digits, period, algorithm, issuer, accountName);
  84. expect(item.secret, secret);
  85. expect(item.digits, digits);
  86. expect(item.period, period);
  87. expect(item.algorithm, algorithm);
  88. expect(item.issuer, issuer);
  89. expect(item.accountName, accountName);
  90. });
  91. test('TOTP Item equality', () {
  92. var item1 = TotpItem("A");
  93. var item2 = TotpItem("A");
  94. var item3 = TotpItem("B");
  95. // Same object
  96. expect(item1 == item1, true);
  97. // Same secret (and other fields apart from id), so still equal
  98. expect(item1 == item2, true);
  99. // Secret changed
  100. expect(item1 == item3, false);
  101. });
  102. }