testbasenenc.lpr 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. uses sysutils, basenenc;
  14. Procedure AssertEquals(Const aActual,aExpected : TBytes; aMsg : String);
  15. function ToStr(aBytes : TBytes) : string;
  16. Var
  17. I : Integer;
  18. begin
  19. Result:='';
  20. For I:=0 to Length(aBytes) do
  21. begin
  22. if I>0 then
  23. Result:=Result+',';
  24. Result:=Result+IntToStr(aBytes[i]);
  25. end;
  26. Result:='['+Result+']';
  27. end;
  28. begin
  29. if (Length(aActual)<>Length(aExpected))
  30. or Not CompareMem(PByte(aActual),PByte(aExpected),Length(aActual)) then
  31. begin
  32. Writeln(aMsg,': results differ, actual: "',ToStr(aActual),'" <> "',ToStr(aExpected),'" (expected)');
  33. Halt(1);
  34. end;
  35. end;
  36. Procedure AssertEquals(Const aActual,aExpected,aMsg : String);
  37. begin
  38. if aActual<>aExpected then
  39. begin
  40. Writeln(aMsg,': results differ, actual: "',aActual,'" <> "',aExpected,'" (expected)');
  41. Halt(1);
  42. end;
  43. end;
  44. Procedure DoTest(B : Tbytes; aExpected : String; aURL : Boolean = False);
  45. Var
  46. B2 : TBytes;
  47. S : Ansistring;
  48. begin
  49. if aURL then
  50. S:=Base64URL.Encode(B)
  51. else
  52. S:=Base64.Encode(B);
  53. AssertEquals(S,aExpected,'DoTest Wrong encode');
  54. if aURL then
  55. B2:=Base64URL.Decode(S)
  56. else
  57. B2:=Base64.Decode(S);
  58. AssertEquals(B2,B,'DoTest Wrong decode');
  59. end;
  60. Procedure DoTest64(aValue, aExpected : String);
  61. begin
  62. DoTest(TEncoding.UTF8.GetAnsiBytes(aValue),aExpected);
  63. end;
  64. Procedure DoTest32(aValue, aExpected : String);
  65. Var
  66. B2 : TBytes;
  67. S : Ansistring;
  68. begin
  69. S:=Base32.Encode(aValue);
  70. AssertEquals(S,aExpected,'base32 encode');
  71. B2:=Base32.Decode(S);
  72. AssertEquals(b2,TEncoding.UTF8.GetAnsiBytes(aValue),'Base32 Wrong encode for '+aValue);
  73. end;
  74. Procedure DoTest32Hex(aValue, aExpected : String);
  75. Var
  76. B2 : TBytes;
  77. S : Ansistring;
  78. begin
  79. S:=Base32Hex.Encode(aValue);
  80. AssertEquals(S,aExpected,'Base32-hex Wrong encode for '+aValue);
  81. B2:=Base32Hex.Decode(S);
  82. AssertEquals(B2,TEncoding.UTF8.GetAnsiBytes(aValue),'Base32Hex Wrong encode for '+aValue);
  83. end;
  84. Procedure DoTest16(aValue, aExpected : String);
  85. Var
  86. B2 : TBytes;
  87. S : Ansistring;
  88. begin
  89. S:=Base16.Encode(aValue);
  90. AssertEquals(S,aExpected,'Base16 Wrong encode for '+aValue);
  91. B2:=Base16.Decode(S);
  92. AssertEquals(B2,TEncoding.UTF8.GetAnsiBytes(aValue),'Base16 Wrong decode for '+aValue);
  93. end;
  94. begin
  95. // From RFC 3548
  96. DoTest([$14,$fb,$9c,$03,$d9,$7e],'FPucA9l+');
  97. DoTest([$14,$fb,$9c,$03,$d9],'FPucA9k=');
  98. DoTest([$14,$fb,$9c,$03],'FPucAw==');
  99. DoTest([$14,$fb,$9c,$03,$d9,$7e],'FPucA9l-',True);
  100. // From RFC 4648
  101. DoTest64('','');
  102. DoTest64('f','Zg==');
  103. DoTest64('fo','Zm8=');
  104. DoTest64('foo','Zm9v');
  105. DoTest64('foob','Zm9vYg==');
  106. DoTest64('fooba','Zm9vYmE=');
  107. DoTest64('foobar','Zm9vYmFy');
  108. DoTest32('','');
  109. DoTest32('f','MY======');
  110. DoTest32('fo','MZXQ====');
  111. DoTest32('foo','MZXW6===');
  112. DoTest32('foob','MZXW6YQ=');
  113. DoTest32('fooba','MZXW6YTB');
  114. DoTest32('foobar','MZXW6YTBOI======');
  115. DoTest32HEX('','');
  116. DoTest32HEX('f','CO======');
  117. DoTest32HEX('fo','CPNG====');
  118. DoTest32HEX('foo','CPNMU===');
  119. DoTest32HEX('foob','CPNMUOG=');
  120. DoTest32HEX('fooba','CPNMUOJ1');
  121. DoTest32HEX('foobar','CPNMUOJ1E8======');
  122. DoTest16('','');
  123. DoTest16('f','66');
  124. DoTest16('fo','666F');
  125. DoTest16('foo','666F6F');
  126. DoTest16('foob','666F6F62');
  127. DoTest16('fooba','666F6F6261');
  128. DoTest16('foobar','666F6F626172');
  129. end.