testbasenenc.lpr 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2021 by Michael Van Canneyt,
  4. member of the Free Pascal development team
  5. Test for Base 16,32,32hex,32-crockford, 64,64url encoding/decoding, with or without padding
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. program testbasenenc;
  13. {$mode objfpc}
  14. {$h+}
  15. uses sysutils, basenenc;
  16. Procedure AssertEquals(Const aActual,aExpected : TBytes; aMsg : String);
  17. function ToStr(aBytes : TBytes) : String;
  18. Var
  19. I : Integer;
  20. begin
  21. Result:='';
  22. For I:=0 to Length(aBytes) do
  23. begin
  24. if I>0 then
  25. Result:=Result+',';
  26. Result:=Result+IntToStr(aBytes[i]);
  27. end;
  28. Result:='['+Result+']';
  29. end;
  30. begin
  31. if (Length(aActual)<>Length(aExpected))
  32. or Not CompareMem(PByte(aActual),PByte(aExpected),Length(aActual)) then
  33. begin
  34. Writeln(aMsg,': results differ, actual: "',ToStr(aActual),'" <> "',ToStr(aExpected),'" (expected)');
  35. Halt(1);
  36. end;
  37. end;
  38. Procedure AssertEquals(Const aActual,aExpected,aMsg : String);
  39. begin
  40. if aActual<>aExpected then
  41. begin
  42. Writeln(aMsg,': results differ, actual: "',aActual,'" <> "',aExpected,'" (expected)');
  43. Halt(1);
  44. end;
  45. end;
  46. Procedure DoTest(B : Tbytes; aExpected : String; aURL : Boolean = False);
  47. Var
  48. B2 : TBytes;
  49. S : Ansistring;
  50. begin
  51. if aURL then
  52. S:=Base64URL.Encode(B)
  53. else
  54. S:=Base64.Encode(B);
  55. AssertEquals(S,aExpected,'DoTest Wrong encode');
  56. if aURL then
  57. B2:=Base64URL.Decode(S)
  58. else
  59. B2:=Base64.Decode(S);
  60. AssertEquals(B2,B,'DoTest Wrong decode');
  61. end;
  62. Procedure DoTest64(aValue, aExpected : String);
  63. begin
  64. DoTest(TEncoding.UTF8.GetAnsiBytes(aValue),aExpected);
  65. end;
  66. Procedure DoTest32(aValue, aExpected : String);
  67. Var
  68. B2 : TBytes;
  69. S : Ansistring;
  70. begin
  71. S:=Base32.Encode(aValue);
  72. AssertEquals(S,aExpected,'base32 encode');
  73. B2:=Base32.Decode(S);
  74. AssertEquals(b2,TEncoding.UTF8.GetAnsiBytes(aValue),'Base32 Wrong encode for '+aValue);
  75. end;
  76. Procedure DoTest32Hex(aValue, aExpected : String);
  77. Var
  78. B2 : TBytes;
  79. S : Ansistring;
  80. begin
  81. S:=Base32Hex.Encode(aValue);
  82. AssertEquals(S,aExpected,'Base32-hex Wrong encode for '+aValue);
  83. B2:=Base32Hex.Decode(S);
  84. AssertEquals(B2,TEncoding.UTF8.GetAnsiBytes(aValue),'Base32Hex Wrong encode for '+aValue);
  85. end;
  86. Procedure DoTest16(aValue, aExpected : String);
  87. Var
  88. B2 : TBytes;
  89. S : Ansistring;
  90. begin
  91. S:=Base16.Encode(aValue);
  92. AssertEquals(S,aExpected,'Base16 Wrong encode for '+aValue);
  93. B2:=Base16.Decode(S);
  94. AssertEquals(B2,TEncoding.UTF8.GetAnsiBytes(aValue),'Base16 Wrong decode for '+aValue);
  95. end;
  96. begin
  97. // From RFC 3548
  98. DoTest([$14,$fb,$9c,$03,$d9,$7e],'FPucA9l+');
  99. DoTest([$14,$fb,$9c,$03,$d9],'FPucA9k=');
  100. DoTest([$14,$fb,$9c,$03],'FPucAw==');
  101. DoTest([$14,$fb,$9c,$03,$d9,$7e],'FPucA9l-',True);
  102. // From RFC 4648
  103. DoTest64('','');
  104. DoTest64('f','Zg==');
  105. DoTest64('fo','Zm8=');
  106. DoTest64('foo','Zm9v');
  107. DoTest64('foob','Zm9vYg==');
  108. DoTest64('fooba','Zm9vYmE=');
  109. DoTest64('foobar','Zm9vYmFy');
  110. DoTest32('','');
  111. DoTest32('f','MY======');
  112. DoTest32('fo','MZXQ====');
  113. DoTest32('foo','MZXW6===');
  114. DoTest32('foob','MZXW6YQ=');
  115. DoTest32('fooba','MZXW6YTB');
  116. DoTest32('foobar','MZXW6YTBOI======');
  117. DoTest32HEX('','');
  118. DoTest32HEX('f','CO======');
  119. DoTest32HEX('fo','CPNG====');
  120. DoTest32HEX('foo','CPNMU===');
  121. DoTest32HEX('foob','CPNMUOG=');
  122. DoTest32HEX('fooba','CPNMUOJ1');
  123. DoTest32HEX('foobar','CPNMUOJ1E8======');
  124. DoTest16('','');
  125. DoTest16('f','66');
  126. DoTest16('fo','666F');
  127. DoTest16('foo','666F6F');
  128. DoTest16('foob','666F6F62');
  129. DoTest16('fooba','666F6F6261');
  130. DoTest16('foobar','666F6F626172');
  131. Writeln('All OK');
  132. end.