Test_Utility.dpr 11 KB

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