Test_Utility.dpr 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. (* _ _
  2. * | |__ _ __ ___ ___ | | __
  3. * | '_ \| '__/ _ \ / _ \| |/ /
  4. * | |_) | | | (_) | (_) | <
  5. * |_.__/|_| \___/ \___/|_|\_\
  6. *
  7. * Microframework which helps to develop web Pascal applications.
  8. *
  9. * Copyright (c) 2012-2020 Silvio Clecio <[email protected]>
  10. *
  11. * Brook framework is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * Brook framework is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with Brook framework; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *)
  25. program Test_Utility;
  26. {$I Tests.inc}
  27. uses
  28. RTLConsts,
  29. SysUtils,
  30. DateUtils,
  31. {$IFNDEF FPC}
  32. IOUtils,
  33. Hash,
  34. {$ENDIF}
  35. Platform,
  36. libsagui,
  37. BrookLibraryLoader,
  38. BrookUtility,
  39. Test;
  40. procedure Test_SaguiVersion;
  41. begin
  42. Assert(Sagui.Version = (SG_VERSION_MAJOR shl 16) or
  43. (SG_VERSION_MINOR shl 8) or SG_VERSION_PATCH);
  44. Assert(Sagui.VersionStr = Format('%d.%d.%d', [SG_VERSION_MAJOR,
  45. SG_VERSION_MINOR, SG_VERSION_PATCH]));
  46. end;
  47. procedure Test_SaguiMalloc;
  48. const
  49. TEST_MEM_BUF_LEN = 10;
  50. var
  51. B: Pcchar;
  52. begin
  53. B := Sagui.Malloc(TEST_MEM_BUF_LEN);
  54. Assert(Assigned(B));
  55. FillChar(B[0], Pred(TEST_MEM_BUF_LEN), 'a');
  56. B[TEST_MEM_BUF_LEN - 1] := #0;
  57. Assert(Length(B) = Pred(TEST_MEM_BUF_LEN));
  58. Sagui.Free(B);
  59. end;
  60. procedure Test_SaguiAlloc;
  61. const
  62. TEST_MEM_BUF_LEN = 10;
  63. var
  64. B: Pcchar;
  65. I: Integer;
  66. begin
  67. B := Sagui.Alloc(TEST_MEM_BUF_LEN);
  68. Assert(Assigned(B));
  69. for I := 0 to Pred(TEST_MEM_BUF_LEN) do
  70. Assert(B[I] = #0);
  71. FillChar(B[0], Pred(TEST_MEM_BUF_LEN), 'a');
  72. B[TEST_MEM_BUF_LEN - 1] := #0;
  73. Assert(Length(B) = Pred(TEST_MEM_BUF_LEN));
  74. Sagui.Free(B);
  75. end;
  76. procedure Test_SaguiRealloc;
  77. const
  78. TEST_MEM_BUF_LEN = 10;
  79. var
  80. B: Pcchar;
  81. begin
  82. B := Sagui.Alloc(TEST_MEM_BUF_LEN div 2);
  83. Assert(Assigned(B));
  84. B := Sagui.Realloc(B, TEST_MEM_BUF_LEN);
  85. Assert(Assigned(B));
  86. FillChar(B[0], Pred(TEST_MEM_BUF_LEN), 'a');
  87. B[TEST_MEM_BUF_LEN - 1] := #0;
  88. Assert(Length(B) = Pred(TEST_MEM_BUF_LEN));
  89. Sagui.Free(B);
  90. end;
  91. procedure Test_SaguiFree;
  92. begin
  93. Sagui.Free(nil);
  94. end;
  95. procedure Test_SaguiStrError;
  96. var
  97. E: string;
  98. begin
  99. Sagui.StrError(0, E, 0);
  100. Assert(E = '');
  101. Sagui.StrError(EINVAL, E, SG_ERR_SIZE);
  102. Assert(E = 'Invalid argument');
  103. end;
  104. procedure Test_SaguiIsPost;
  105. begin
  106. Assert(not Sagui.IsPost(''));
  107. Assert(not Sagui.IsPost('abc'));
  108. Assert(not Sagui.IsPost('GET'));
  109. Assert(not Sagui.IsPost('HEAD'));
  110. Assert(Sagui.IsPost('POST'));
  111. Assert(Sagui.IsPost('PUT'));
  112. Assert(Sagui.IsPost('DELETE'));
  113. Assert(Sagui.IsPost('OPTIONS'));
  114. end;
  115. procedure Test_SaguiExtractEntryPoint;
  116. begin
  117. Assert(Sagui.ExtractEntryPoint('') = '/');
  118. Assert(Sagui.ExtractEntryPoint('/') = '/');
  119. Assert(Sagui.ExtractEntryPoint('//') = '/');
  120. Assert(Sagui.ExtractEntryPoint('///////') = '/');
  121. Assert(Sagui.ExtractEntryPoint('foo') = '/foo');
  122. Assert(Sagui.ExtractEntryPoint('/foo') = '/foo');
  123. Assert(Sagui.ExtractEntryPoint('//foo') = '/foo');
  124. Assert(Sagui.ExtractEntryPoint('///////foo') = '/foo');
  125. Assert(Sagui.ExtractEntryPoint('foo/') = '/foo');
  126. Assert(Sagui.ExtractEntryPoint('foo//') = '/foo');
  127. Assert(Sagui.ExtractEntryPoint('/foo/') = '/foo');
  128. Assert(Sagui.ExtractEntryPoint('///foo///') = '/foo');
  129. Assert(Sagui.ExtractEntryPoint('/foo/bar') = '/foo');
  130. Assert(Sagui.ExtractEntryPoint('///foo/bar') = '/foo');
  131. Assert(Sagui.ExtractEntryPoint('/foo///bar') = '/foo');
  132. Assert(Sagui.ExtractEntryPoint('///foo///bar') = '/foo');
  133. Assert(Sagui.ExtractEntryPoint('/a') = '/a');
  134. Assert(Sagui.ExtractEntryPoint('/a/b') = '/a');
  135. Assert(Sagui.ExtractEntryPoint('//a/b') = '/a');
  136. Assert(Sagui.ExtractEntryPoint('//a//b') = '/a');
  137. end;
  138. procedure Test_SaguiTmpDir;
  139. begin
  140. {$IFDEF ANDROID}
  141. Assert(Sagui.TmpDir = '/data/local/tmp');
  142. {$ELSE}
  143. Assert(IncludeTrailingPathDelimiter(Sagui.TmpDir) =
  144. {$IFDEF FPC}GetTempDir{$ELSE}TPath.GetTempPath{$ENDIF});
  145. {$ENDIF}
  146. end;
  147. procedure Test_SaguiEOR;
  148. begin
  149. Assert(Sagui.EOR(False) = -1);
  150. Assert(Sagui.EOR(True) = -2);
  151. end;
  152. procedure DoSaguiIPParamIsNil;
  153. begin
  154. Sagui.IP(nil);
  155. end;
  156. procedure Test_SaguiIP;
  157. begin
  158. AssertExcept(DoSaguiIPParamIsNil, EArgumentNilException,
  159. Format(SParamIsNil, ['ASocket']));
  160. end;
  161. procedure Test_BrookDAYS;
  162. begin
  163. Assert(Brook.DAYS[1] = 'Sun');
  164. Assert(Brook.DAYS[2] = 'Mon');
  165. Assert(Brook.DAYS[3] = 'Tue');
  166. Assert(Brook.DAYS[4] = 'Wed');
  167. Assert(Brook.DAYS[5] = 'Thu');
  168. Assert(Brook.DAYS[6] = 'Fri');
  169. Assert(Brook.DAYS[7] = 'Sat');
  170. end;
  171. procedure Test_BrookMONTHS;
  172. begin
  173. Assert(Brook.MONTHS[1] = 'Jan');
  174. Assert(Brook.MONTHS[2] = 'Feb');
  175. Assert(Brook.MONTHS[3] = 'Mar');
  176. Assert(Brook.MONTHS[4] = 'Apr');
  177. Assert(Brook.MONTHS[5] = 'May');
  178. Assert(Brook.MONTHS[6] = 'Jun');
  179. Assert(Brook.MONTHS[7] = 'Jul');
  180. Assert(Brook.MONTHS[8] = 'Aug');
  181. Assert(Brook.MONTHS[9] = 'Sep');
  182. Assert(Brook.MONTHS[10] = 'Oct');
  183. Assert(Brook.MONTHS[11] = 'Nov');
  184. Assert(Brook.MONTHS[12] = 'Dec');
  185. end;
  186. procedure Test_BrookFixPath;
  187. begin
  188. Assert(Brook.FixPath('') = '/');
  189. Assert(Brook.FixPath('/') = '/');
  190. Assert(Brook.FixPath('abc') = '/abc');
  191. Assert(Brook.FixPath('/abc') = '/abc');
  192. Assert(Brook.FixPath('abc/') = '/abc');
  193. Assert(Brook.FixPath('/abc/') = '/abc');
  194. Assert(Brook.FixPath('abc/def') = '/abc/def');
  195. end;
  196. procedure Test_BrookFixEntryPoint;
  197. begin
  198. Assert(Brook.FixEntryPoint('') = '/');
  199. Assert(Brook.FixEntryPoint('/') = '/');
  200. Assert(Brook.FixEntryPoint('abc') = '/abc');
  201. Assert(Brook.FixEntryPoint('/abc') = '/abc');
  202. Assert(Brook.FixEntryPoint('abc/') = '/abc');
  203. Assert(Brook.FixEntryPoint('/abc/') = '/abc');
  204. Assert(Brook.FixEntryPoint('abc/def') = '/abc');
  205. Assert(Brook.FixEntryPoint('/abc/def') = '/abc');
  206. Assert(Brook.FixEntryPoint('abc/def/') = '/abc');
  207. Assert(Brook.FixEntryPoint('/abc/def/') = '/abc');
  208. end;
  209. procedure Test_BrookDateTimeToUTC;
  210. begin
  211. Assert(Brook.DateTimeToUTC(Now) > 0);
  212. end;
  213. procedure Test_BrookDateTimeToGMT;
  214. begin
  215. Assert(Brook.DateTimeToGMT(EncodeDateTime(2019, 11, 29, 14, 27, 52, 0)) =
  216. Concat(Brook.DAYS[6], ', 29 Nov 2019 14:27:52 GMT'));
  217. end;
  218. procedure Test_BrookSHA1;
  219. begin
  220. Assert(Brook.SHA1('abc123') = '6367c48dd193d56ea7b0baad25b19455e529f5ee');
  221. end;
  222. procedure Test_HTTPRequestMethodHelperToString;
  223. var
  224. M: TBrookHTTPRequestMethod;
  225. begin
  226. M := rmUnknown;
  227. Assert(M.ToString = 'Unknown');
  228. M := rmGET;
  229. Assert(M.ToString = 'GET');
  230. M := rmPOST;
  231. Assert(M.ToString = 'POST');
  232. M := rmPUT;
  233. Assert(M.ToString = 'PUT');
  234. M := rmDELETE;
  235. Assert(M.ToString = 'DELETE');
  236. M := rmPATCH;
  237. Assert(M.ToString = 'PATCH');
  238. M := rmOPTIONS;
  239. Assert(M.ToString = 'OPTIONS');
  240. M := rmHEAD;
  241. Assert(M.ToString = 'HEAD');
  242. end;
  243. procedure Test_HTTPRequestMethodHelperFromString;
  244. var
  245. M: TBrookHTTPRequestMethod;
  246. begin
  247. M := Default(TBrookHTTPRequestMethod);
  248. Assert(M.FromString('Unknown') = rmUnknown);
  249. Assert(M.FromString('GET') = rmGET);
  250. Assert(M.FromString('POST') = rmPOST);
  251. Assert(M.FromString('PUT') = rmPUT);
  252. Assert(M.FromString('DELETE') = rmDELETE);
  253. Assert(M.FromString('PATCH') = rmPATCH);
  254. Assert(M.FromString('OPTIONS') = rmOPTIONS);
  255. Assert(M.FromString('HEAD') = rmHEAD);
  256. end;
  257. begin
  258. {$IF (NOT DEFINED(FPC)) AND DEFINED(DEBUG)}
  259. ReportMemoryLeaksOnShutdown := True;
  260. {$ENDIF}
  261. TBrookLibraryLoader.Load;
  262. Test_SaguiVersion;
  263. Test_SaguiMalloc;
  264. Test_SaguiAlloc;
  265. Test_SaguiRealloc;
  266. Test_SaguiFree;
  267. Test_SaguiStrError;
  268. Test_SaguiIsPost;
  269. Test_SaguiExtractEntryPoint;
  270. Test_SaguiTmpDir;
  271. Test_SaguiEOR;
  272. Test_SaguiIP;
  273. Test_BrookDAYS;
  274. Test_BrookMONTHS;
  275. Test_BrookFixPath;
  276. Test_BrookFixEntryPoint;
  277. Test_BrookDateTimeToUTC;
  278. Test_BrookDateTimeToGMT;
  279. Test_BrookSHA1;
  280. Test_HTTPRequestMethodHelperToString;
  281. Test_HTTPRequestMethodHelperFromString;
  282. end.