Test_HTTPAuthentication.dpr 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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_HTTPAuthentication;
  26. {$I Tests.inc}
  27. uses
  28. SysUtils,
  29. StrUtils,
  30. Platform,
  31. Marshalling,
  32. libsagui,
  33. BrookLibraryLoader,
  34. BrookHTTPAuthentication,
  35. Test;
  36. type
  37. TFakeHTTPAuth = class
  38. public
  39. ErrorCode: Word;
  40. Canceled: Boolean;
  41. end;
  42. var
  43. FakeHTTPAuth: TFakeHTTPAuth;
  44. FakeHTTPAuthHandle: Pointer;
  45. FakeRealm: string;
  46. function fake_httpauth_usr(auth: Psg_httpauth): Pcchar; cdecl;
  47. begin
  48. Assert(auth = FakeHTTPAuthHandle);
  49. Result := 'foo';
  50. end;
  51. function fake_httpauth_pwd(auth: Psg_httpauth): Pcchar; cdecl;
  52. begin
  53. Assert(auth = FakeHTTPAuthHandle);
  54. Result := 'bar';
  55. end;
  56. function fake_httpauth_realm(auth: Psg_httpauth): pcchar; cdecl;
  57. var
  58. M: TMarshaller;
  59. begin
  60. Assert(auth = FakeHTTPAuthHandle);
  61. Result := M.ToCString(FakeRealm);
  62. end;
  63. function fake_httpauth_set_realm(auth: Psg_httpauth;
  64. const realm: Pcchar): cint; cdecl;
  65. begin
  66. Assert(auth = FakeHTTPAuthHandle);
  67. FakeRealm := TMarshal.ToString(realm);
  68. Result := 0;
  69. end;
  70. function fake_httpauth_deny2(auth: Psg_httpauth; const reason: Pcchar;
  71. const content_type: Pcchar; status: cuint): cint; cdecl;
  72. begin
  73. Assert(auth = FakeHTTPAuthHandle);
  74. Assert(reason = 'fake_reason');
  75. Assert(content_type = 'fake_content_type');
  76. Assert(status = 200);
  77. Result := TFakeHTTPAuth(auth).ErrorCode;
  78. end;
  79. function fake_httpauth_deny(auth: Psg_httpauth; const reason: Pcchar;
  80. const content_type: Pcchar): cint; cdecl;
  81. begin
  82. Assert(auth = FakeHTTPAuthHandle);
  83. Assert(reason = 'fake_reason');
  84. Assert(content_type = 'fake_content_type');
  85. Result := TFakeHTTPAuth(auth).ErrorCode;
  86. end;
  87. function fake_httpauth_cancel(auth: Psg_httpauth): cint; cdecl;
  88. var
  89. A: TFakeHTTPAuth;
  90. begin
  91. Assert(auth = FakeHTTPAuthHandle);
  92. A := TFakeHTTPAuth(auth);
  93. A.Canceled := True;
  94. Result := A.ErrorCode;
  95. end;
  96. procedure AssignFakeAPI; {$IFNDEF DEBUG}inline;{$ENDIF}
  97. begin
  98. sg_httpauth_usr := fake_httpauth_usr;
  99. sg_httpauth_pwd := fake_httpauth_pwd;
  100. sg_httpauth_realm := fake_httpauth_realm;
  101. sg_httpauth_set_realm := fake_httpauth_set_realm;
  102. sg_httpauth_deny2 := fake_httpauth_deny2;
  103. sg_httpauth_deny := fake_httpauth_deny;
  104. sg_httpauth_cancel := fake_httpauth_cancel;
  105. end;
  106. procedure Test_HTTPCredentialsCreate;
  107. var
  108. C: TBrookHTTPCredentials;
  109. begin
  110. C := TBrookHTTPCredentials.Create(FakeHTTPAuthHandle);
  111. try
  112. Assert(C.Handle = FakeHTTPAuthHandle);
  113. Assert(C.UserName = 'foo');
  114. Assert(C.Password = 'bar');
  115. finally
  116. C.Free;
  117. end;
  118. end;
  119. procedure Test_HTTPCredentialsRealm;
  120. var
  121. C: TBrookHTTPCredentials;
  122. begin
  123. C := TBrookHTTPCredentials.Create(FakeHTTPAuthHandle);
  124. try
  125. C.Realm := 'foo';
  126. Assert(C.Realm = 'foo');
  127. C.Realm := 'bar';
  128. Assert(C.Realm = 'bar');
  129. finally
  130. C.Free;
  131. end;
  132. end;
  133. procedure Test_HTTPCredentialsUserName;
  134. var
  135. C: TBrookHTTPCredentials;
  136. begin
  137. C := TBrookHTTPCredentials.Create(FakeHTTPAuthHandle);
  138. try
  139. Assert(C.UserName = 'foo');
  140. finally
  141. C.Free;
  142. end;
  143. end;
  144. procedure Test_HTTPCredentialsPassword;
  145. var
  146. C: TBrookHTTPCredentials;
  147. begin
  148. C := TBrookHTTPCredentials.Create(FakeHTTPAuthHandle);
  149. try
  150. Assert(C.Password = 'bar');
  151. finally
  152. C.Free;
  153. end;
  154. end;
  155. procedure Test_HTTPAuthenticationCreate;
  156. var
  157. A: TBrookHTTPAuthentication;
  158. begin
  159. A := TBrookHTTPAuthentication.Create(FakeHTTPAuthHandle);
  160. try
  161. Assert(A.Handle = FakeHTTPAuthHandle);
  162. Assert(Assigned(A.Credentials));
  163. finally
  164. A.Free;
  165. end;
  166. end;
  167. procedure DoHTTPAuthenticationDenyLibraryNotLoaded1(const AArgs: array of const);
  168. begin
  169. TBrookHTTPAuthentication(AArgs[0].VObject).Deny('', '', 200);
  170. end;
  171. procedure DoHTTPAuthenticationDenyInvalidArgument1(const AArgs: array of const);
  172. begin
  173. TBrookHTTPAuthentication(AArgs[0].VObject).Deny('fake_reason',
  174. 'fake_content_type', 200);
  175. end;
  176. procedure DoHTTPAuthenticationDenyLibraryNotLoaded2(const AArgs: array of const);
  177. begin
  178. TBrookHTTPAuthentication(AArgs[0].VObject).Deny('', '');
  179. end;
  180. procedure DoHTTPAuthenticationDenyInvalidArgument2(const AArgs: array of const);
  181. begin
  182. TBrookHTTPAuthentication(AArgs[0].VObject).Deny('fake_reason',
  183. 'fake_content_type');
  184. end;
  185. procedure Test_HTTPAuthenticationDeny;
  186. var
  187. A: TBrookHTTPAuthentication;
  188. begin
  189. A := TBrookHTTPAuthentication.Create(FakeHTTPAuthHandle);
  190. try
  191. TBrookLibraryLoader.Unload;
  192. try
  193. AssertExcept(DoHTTPAuthenticationDenyLibraryNotLoaded1, ESgLibNotLoaded,
  194. Format(SSgLibNotLoaded, [IfThen(SgLib.GetLastName = '', SG_LIB_NAME,
  195. SgLib.GetLastName)]), [A]);
  196. finally
  197. TBrookLibraryLoader.Load;
  198. end;
  199. AssignFakeAPI;
  200. FakeHTTPAuth.ErrorCode := EINVAL;
  201. AssertOSExcept(DoHTTPAuthenticationDenyInvalidArgument1, EINVAL, [A]);
  202. FakeHTTPAuth.ErrorCode := 0;
  203. A.Deny('fake_reason', 'fake_content_type', 200);
  204. A.Deny('%s_%s', ['fake', 'reason'], 'fake_content_type', 200);
  205. TBrookLibraryLoader.Unload;
  206. try
  207. AssertExcept(DoHTTPAuthenticationDenyLibraryNotLoaded2, ESgLibNotLoaded,
  208. Format(SSgLibNotLoaded, [IfThen(SgLib.GetLastName = '', SG_LIB_NAME,
  209. SgLib.GetLastName)]), [A]);
  210. finally
  211. TBrookLibraryLoader.Load;
  212. end;
  213. AssignFakeAPI;
  214. FakeHTTPAuth.ErrorCode := EINVAL;
  215. AssertOSExcept(DoHTTPAuthenticationDenyInvalidArgument2, EINVAL, [A]);
  216. FakeHTTPAuth.ErrorCode := 0;
  217. A.Deny('fake_reason', 'fake_content_type');
  218. A.Deny('%s_%s', ['fake', 'reason'], 'fake_content_type');
  219. finally
  220. A.Free;
  221. end;
  222. end;
  223. procedure DoHTTPAuthenticationCancelLibraryNotLoaded(const AArgs: array of const);
  224. begin
  225. TBrookHTTPAuthentication(AArgs[0].VObject).Cancel;
  226. end;
  227. procedure DoHTTPAuthenticationCancelInvalidArgument(const AArgs: array of const);
  228. begin
  229. TBrookHTTPAuthentication(AArgs[0].VObject).Cancel;
  230. end;
  231. procedure Test_HTTPAuthenticationCancel;
  232. var
  233. A: TBrookHTTPAuthentication;
  234. begin
  235. A := TBrookHTTPAuthentication.Create(FakeHTTPAuthHandle);
  236. try
  237. TBrookLibraryLoader.Unload;
  238. try
  239. AssertExcept(DoHTTPAuthenticationCancelLibraryNotLoaded, ESgLibNotLoaded,
  240. Format(SSgLibNotLoaded, [IfThen(SgLib.GetLastName = '', SG_LIB_NAME,
  241. SgLib.GetLastName)]), [A]);
  242. finally
  243. TBrookLibraryLoader.Load;
  244. end;
  245. AssignFakeAPI;
  246. FakeHTTPAuth.ErrorCode := EINVAL;
  247. AssertOSExcept(DoHTTPAuthenticationCancelInvalidArgument, EINVAL, [A]);
  248. FakeHTTPAuth.ErrorCode := 0;
  249. FakeHTTPAuth.Canceled := False;
  250. A.Cancel;
  251. Assert(FakeHTTPAuth.Canceled);
  252. finally
  253. A.Free;
  254. end;
  255. end;
  256. procedure Test_HTTPAuthenticationCredentials;
  257. var
  258. A: TBrookHTTPAuthentication;
  259. begin
  260. A := TBrookHTTPAuthentication.Create(FakeHTTPAuthHandle);
  261. try
  262. Assert(Assigned(A.Credentials));
  263. Assert(A.Credentials.UserName = 'foo');
  264. Assert(A.Credentials.Password = 'bar');
  265. finally
  266. A.Free;
  267. end;
  268. end;
  269. begin
  270. {$IF (NOT DEFINED(FPC)) AND DEFINED(DEBUG)}
  271. ReportMemoryLeaksOnShutdown := True;
  272. {$ENDIF}
  273. TBrookLibraryLoader.Load;
  274. FakeHTTPAuth := TFakeHTTPAuth.Create;
  275. FakeHTTPAuthHandle := FakeHTTPAuth;
  276. try
  277. AssignFakeAPI;
  278. Test_HTTPCredentialsCreate;
  279. Test_HTTPCredentialsRealm;
  280. Test_HTTPCredentialsUserName;
  281. Test_HTTPCredentialsPassword;
  282. Test_HTTPAuthenticationCreate;
  283. Test_HTTPAuthenticationCreate;
  284. // Test_HTTPAuthenticationDestroy - not required
  285. Test_HTTPAuthenticationDeny;
  286. Test_HTTPAuthenticationCancel;
  287. Test_HTTPAuthenticationCredentials;
  288. finally
  289. FakeHTTPAuth.Free;
  290. TBrookLibraryLoader.Unload;
  291. end;
  292. end.