Test_Utility.dpr 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. (* _ _
  2. * | |__ _ __ ___ ___ | | __
  3. * | '_ \| '__/ _ \ / _ \| |/ /
  4. * | |_) | | | (_) | (_) | <
  5. * |_.__/|_| \___/ \___/|_|\_\
  6. *
  7. * Microframework which helps to develop web Pascal applications.
  8. *
  9. * Copyright (c) 2012-2021 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. Classes,
  32. SyncObjs,
  33. {$IFNDEF FPC}
  34. IOUtils,
  35. Hash,
  36. {$ENDIF}
  37. Platform,
  38. libsagui,
  39. BrookLibraryLoader,
  40. BrookUtility,
  41. Test;
  42. type
  43. TFakeMutex = class(TCriticalSection)
  44. Muted: Boolean;
  45. procedure Acquire; override;
  46. procedure Release; override;
  47. end;
  48. TFakeLocker = class(TBrookLocker)
  49. protected
  50. function CreateMutex: TCriticalSection; override;
  51. public
  52. property Mutex;
  53. end;
  54. { TFakeMutex }
  55. procedure TFakeMutex.Acquire;
  56. begin
  57. Muted := True;
  58. end;
  59. procedure TFakeMutex.Release;
  60. begin
  61. Muted := False;
  62. end;
  63. { TFakeLocker }
  64. function TFakeLocker.CreateMutex: TCriticalSection;
  65. begin
  66. Result := TFakeMutex.Create;
  67. end;
  68. procedure Test_LockerCreate;
  69. var
  70. L: TFakeLocker;
  71. begin
  72. L := TFakeLocker.Create;
  73. try
  74. Assert(Assigned(L.Mutex));
  75. Assert(L.Active);
  76. finally
  77. L.Free;
  78. end;
  79. end;
  80. procedure Test_LockerLock;
  81. var
  82. L: TFakeLocker;
  83. M: TFakeMutex;
  84. begin
  85. L := TFakeLocker.Create;
  86. try
  87. M := TFakeMutex(L.Mutex);
  88. Assert(not M.Muted);
  89. L.Lock;
  90. Assert(M.Muted);
  91. L.Active := False;
  92. M.Muted := False;
  93. L.Lock;
  94. Assert(not M.Muted);
  95. finally
  96. L.Free;
  97. end;
  98. end;
  99. procedure Test_LockerUnlock;
  100. var
  101. L: TFakeLocker;
  102. M: TFakeMutex;
  103. begin
  104. L := TFakeLocker.Create;
  105. try
  106. M := TFakeMutex(L.Mutex);
  107. M.Muted := True;
  108. L.Unlock;
  109. Assert(not M.Muted);
  110. L.Active := False;
  111. M.Muted := True;
  112. L.Unlock;
  113. Assert(M.Muted);
  114. finally
  115. L.Free;
  116. end;
  117. end;
  118. procedure Test_LockerTryLock;
  119. var
  120. L: TBrookLocker;
  121. begin
  122. L := TBrookLocker.Create;
  123. try
  124. L.Active := False;
  125. Assert(not L.TryLock);
  126. L.Active := True;
  127. Assert(L.TryLock);
  128. finally
  129. L.Free;
  130. end;
  131. end;
  132. procedure Test_LockerActive;
  133. var
  134. L: TFakeLocker;
  135. begin
  136. L := TFakeLocker.Create;
  137. try
  138. Assert(L.Active);
  139. L.Active := False;
  140. Assert(not L.Active);
  141. L.Active := True;
  142. Assert(L.Active);
  143. finally
  144. L.Free;
  145. end;
  146. end;
  147. procedure Test_SaguiVersion;
  148. begin
  149. Assert(Sagui.Version = (SG_VERSION_MAJOR shl 16) or
  150. (SG_VERSION_MINOR shl 8) or SG_VERSION_PATCH);
  151. Assert(Sagui.VersionStr = Format('%d.%d.%d', [SG_VERSION_MAJOR,
  152. SG_VERSION_MINOR, SG_VERSION_PATCH]));
  153. end;
  154. procedure Test_SaguiMalloc;
  155. const
  156. TEST_MEM_BUF_LEN = 10;
  157. var
  158. B: Pcchar;
  159. begin
  160. B := Sagui.Malloc(TEST_MEM_BUF_LEN);
  161. Assert(Assigned(B));
  162. FillChar(B[0], Pred(TEST_MEM_BUF_LEN), 'a');
  163. B[TEST_MEM_BUF_LEN - 1] := #0;
  164. Assert(Length(B) = Pred(TEST_MEM_BUF_LEN));
  165. Sagui.Free(B);
  166. end;
  167. procedure Test_SaguiAlloc;
  168. const
  169. TEST_MEM_BUF_LEN = 10;
  170. var
  171. B: Pcchar;
  172. I: Integer;
  173. begin
  174. B := Sagui.Alloc(TEST_MEM_BUF_LEN);
  175. Assert(Assigned(B));
  176. for I := 0 to Pred(TEST_MEM_BUF_LEN) do
  177. Assert(B[I] = #0);
  178. FillChar(B[0], Pred(TEST_MEM_BUF_LEN), 'a');
  179. B[TEST_MEM_BUF_LEN - 1] := #0;
  180. Assert(Length(B) = Pred(TEST_MEM_BUF_LEN));
  181. Sagui.Free(B);
  182. end;
  183. procedure Test_SaguiRealloc;
  184. const
  185. TEST_MEM_BUF_LEN = 10;
  186. var
  187. B: Pcchar;
  188. begin
  189. B := Sagui.Alloc(TEST_MEM_BUF_LEN div 2);
  190. Assert(Assigned(B));
  191. B := Sagui.Realloc(B, TEST_MEM_BUF_LEN);
  192. Assert(Assigned(B));
  193. FillChar(B[0], Pred(TEST_MEM_BUF_LEN), 'a');
  194. B[TEST_MEM_BUF_LEN - 1] := #0;
  195. Assert(Length(B) = Pred(TEST_MEM_BUF_LEN));
  196. Sagui.Free(B);
  197. end;
  198. procedure Test_SaguiFree;
  199. begin
  200. Sagui.Free(nil);
  201. end;
  202. procedure Test_SaguiStrError;
  203. var
  204. E: string;
  205. begin
  206. Sagui.StrError(0, E, 0);
  207. Assert(E = '');
  208. Sagui.StrError(EINVAL, E, SG_ERR_SIZE);
  209. Assert(E = 'Invalid argument');
  210. end;
  211. procedure Test_SaguiIsPost;
  212. begin
  213. Assert(not Sagui.IsPost(''));
  214. Assert(not Sagui.IsPost('abc'));
  215. Assert(not Sagui.IsPost('GET'));
  216. Assert(not Sagui.IsPost('HEAD'));
  217. Assert(Sagui.IsPost('POST'));
  218. Assert(Sagui.IsPost('PUT'));
  219. Assert(Sagui.IsPost('DELETE'));
  220. Assert(Sagui.IsPost('OPTIONS'));
  221. end;
  222. procedure Test_SaguiExtractEntryPoint;
  223. begin
  224. Assert(Sagui.ExtractEntryPoint('') = '/');
  225. Assert(Sagui.ExtractEntryPoint('/') = '/');
  226. Assert(Sagui.ExtractEntryPoint('//') = '/');
  227. Assert(Sagui.ExtractEntryPoint('///////') = '/');
  228. Assert(Sagui.ExtractEntryPoint('foo') = '/foo');
  229. Assert(Sagui.ExtractEntryPoint('/foo') = '/foo');
  230. Assert(Sagui.ExtractEntryPoint('//foo') = '/foo');
  231. Assert(Sagui.ExtractEntryPoint('///////foo') = '/foo');
  232. Assert(Sagui.ExtractEntryPoint('foo/') = '/foo');
  233. Assert(Sagui.ExtractEntryPoint('foo//') = '/foo');
  234. Assert(Sagui.ExtractEntryPoint('/foo/') = '/foo');
  235. Assert(Sagui.ExtractEntryPoint('///foo///') = '/foo');
  236. Assert(Sagui.ExtractEntryPoint('/foo/bar') = '/foo');
  237. Assert(Sagui.ExtractEntryPoint('///foo/bar') = '/foo');
  238. Assert(Sagui.ExtractEntryPoint('/foo///bar') = '/foo');
  239. Assert(Sagui.ExtractEntryPoint('///foo///bar') = '/foo');
  240. Assert(Sagui.ExtractEntryPoint('/a') = '/a');
  241. Assert(Sagui.ExtractEntryPoint('/a/b') = '/a');
  242. Assert(Sagui.ExtractEntryPoint('//a/b') = '/a');
  243. Assert(Sagui.ExtractEntryPoint('//a//b') = '/a');
  244. end;
  245. procedure Test_SaguiTmpDir;
  246. begin
  247. {$IFDEF ANDROID}
  248. Assert(Sagui.TmpDir = '/data/local/tmp');
  249. {$ELSE}
  250. Assert(IncludeTrailingPathDelimiter(Sagui.TmpDir) =
  251. {$IFDEF FPC}GetTempDir{$ELSE}TPath.GetTempPath{$ENDIF});
  252. {$ENDIF}
  253. end;
  254. procedure DoSaguiIPParamIsNil;
  255. begin
  256. Sagui.IP(nil);
  257. end;
  258. procedure Test_SaguiIP;
  259. begin
  260. AssertExcept(DoSaguiIPParamIsNil, EArgumentNilException,
  261. Format(SParamIsNil, ['ASocket']));
  262. end;
  263. procedure Test_BrookDAYS;
  264. begin
  265. Assert(Brook.DAYS[1] = 'Sun');
  266. Assert(Brook.DAYS[2] = 'Mon');
  267. Assert(Brook.DAYS[3] = 'Tue');
  268. Assert(Brook.DAYS[4] = 'Wed');
  269. Assert(Brook.DAYS[5] = 'Thu');
  270. Assert(Brook.DAYS[6] = 'Fri');
  271. Assert(Brook.DAYS[7] = 'Sat');
  272. end;
  273. procedure Test_BrookMONTHS;
  274. begin
  275. Assert(Brook.MONTHS[1] = 'Jan');
  276. Assert(Brook.MONTHS[2] = 'Feb');
  277. Assert(Brook.MONTHS[3] = 'Mar');
  278. Assert(Brook.MONTHS[4] = 'Apr');
  279. Assert(Brook.MONTHS[5] = 'May');
  280. Assert(Brook.MONTHS[6] = 'Jun');
  281. Assert(Brook.MONTHS[7] = 'Jul');
  282. Assert(Brook.MONTHS[8] = 'Aug');
  283. Assert(Brook.MONTHS[9] = 'Sep');
  284. Assert(Brook.MONTHS[10] = 'Oct');
  285. Assert(Brook.MONTHS[11] = 'Nov');
  286. Assert(Brook.MONTHS[12] = 'Dec');
  287. end;
  288. procedure Test_BrookFixPath;
  289. begin
  290. Assert(Brook.FixPath('') = '/');
  291. Assert(Brook.FixPath('/') = '/');
  292. Assert(Brook.FixPath('abc') = '/abc');
  293. Assert(Brook.FixPath('/abc') = '/abc');
  294. Assert(Brook.FixPath('abc/') = '/abc');
  295. Assert(Brook.FixPath('/abc/') = '/abc');
  296. Assert(Brook.FixPath('abc/def') = '/abc/def');
  297. end;
  298. procedure Test_BrookFixEntryPoint;
  299. begin
  300. Assert(Brook.FixEntryPoint('') = '/');
  301. Assert(Brook.FixEntryPoint('/') = '/');
  302. Assert(Brook.FixEntryPoint('abc') = '/abc');
  303. Assert(Brook.FixEntryPoint('/abc') = '/abc');
  304. Assert(Brook.FixEntryPoint('abc/') = '/abc');
  305. Assert(Brook.FixEntryPoint('/abc/') = '/abc');
  306. Assert(Brook.FixEntryPoint('abc/def') = '/abc');
  307. Assert(Brook.FixEntryPoint('/abc/def') = '/abc');
  308. Assert(Brook.FixEntryPoint('abc/def/') = '/abc');
  309. Assert(Brook.FixEntryPoint('/abc/def/') = '/abc');
  310. end;
  311. procedure Test_BrookDateTimeToUTC;
  312. begin
  313. Assert(Brook.DateTimeToUTC(Now) > 0);
  314. end;
  315. procedure Test_BrookDateTimeToGMT;
  316. begin
  317. Assert(Brook.DateTimeToGMT(EncodeDateTime(2019, 11, 29, 14, 27, 52, 0)) =
  318. Concat(Brook.DAYS[6], ', 29 Nov 2019 14:27:52 GMT'));
  319. end;
  320. procedure Test_BrookSHA1;
  321. begin
  322. Assert(Brook.SHA1('abc123') = '6367c48dd193d56ea7b0baad25b19455e529f5ee');
  323. end;
  324. procedure Test_HTTPRequestMethodHelperToString;
  325. var
  326. M: TBrookHTTPRequestMethod;
  327. begin
  328. M := rmUnknown;
  329. Assert(M.ToString = 'Unknown');
  330. M := rmGET;
  331. Assert(M.ToString = 'GET');
  332. M := rmPOST;
  333. Assert(M.ToString = 'POST');
  334. M := rmPUT;
  335. Assert(M.ToString = 'PUT');
  336. M := rmDELETE;
  337. Assert(M.ToString = 'DELETE');
  338. M := rmPATCH;
  339. Assert(M.ToString = 'PATCH');
  340. M := rmOPTIONS;
  341. Assert(M.ToString = 'OPTIONS');
  342. M := rmHEAD;
  343. Assert(M.ToString = 'HEAD');
  344. end;
  345. procedure Test_HTTPRequestMethodHelperFromString;
  346. var
  347. M: TBrookHTTPRequestMethod;
  348. begin
  349. M := Default(TBrookHTTPRequestMethod);
  350. Assert(M.FromString('Unknown') = rmUnknown);
  351. Assert(M.FromString('GET') = rmGET);
  352. Assert(M.FromString('POST') = rmPOST);
  353. Assert(M.FromString('PUT') = rmPUT);
  354. Assert(M.FromString('DELETE') = rmDELETE);
  355. Assert(M.FromString('PATCH') = rmPATCH);
  356. Assert(M.FromString('OPTIONS') = rmOPTIONS);
  357. Assert(M.FromString('HEAD') = rmHEAD);
  358. end;
  359. begin
  360. {$IF (NOT DEFINED(FPC)) AND DEFINED(DEBUG)}
  361. ReportMemoryLeaksOnShutdown := True;
  362. {$ENDIF}
  363. TBrookLibraryLoader.Load;
  364. Test_LockerCreate;
  365. // Test_LockerDestroy - not required
  366. Test_LockerLock;
  367. Test_LockerUnlock;
  368. Test_LockerTryLock;
  369. Test_LockerActive;
  370. Test_SaguiVersion;
  371. Test_SaguiMalloc;
  372. Test_SaguiAlloc;
  373. Test_SaguiRealloc;
  374. Test_SaguiFree;
  375. Test_SaguiStrError;
  376. Test_SaguiIsPost;
  377. Test_SaguiExtractEntryPoint;
  378. Test_SaguiTmpDir;
  379. // Test_SaguiEOR; - not easy to test
  380. Test_SaguiIP;
  381. Test_BrookDAYS;
  382. Test_BrookMONTHS;
  383. Test_BrookFixPath;
  384. Test_BrookFixEntryPoint;
  385. Test_BrookDateTimeToUTC;
  386. Test_BrookDateTimeToGMT;
  387. Test_BrookSHA1;
  388. Test_HTTPRequestMethodHelperToString;
  389. Test_HTTPRequestMethodHelperFromString;
  390. end.