Test_HTTPServer.dpr 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. (* _ _
  2. * | |__ _ __ ___ ___ | | __
  3. * | '_ \| '__/ _ \ / _ \| |/ /
  4. * | |_) | | | (_) | (_) | <
  5. * |_.__/|_| \___/ \___/|_|\_\
  6. *
  7. * Microframework which helps to develop web Pascal applications.
  8. *
  9. * Copyright (c) 2012-2019 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_HTTPServer;
  26. {$I Tests.inc}
  27. uses
  28. SysUtils,
  29. BrookLibraryLoader,
  30. BrookHTTPServer,
  31. Test;
  32. procedure Test_HTTPServerSecurityAssign;
  33. var
  34. S, D: TBrookHTTPServerSecurity;
  35. begin
  36. S := TBrookHTTPServerSecurity.Create;
  37. D := TBrookHTTPServerSecurity.Create;
  38. try
  39. Assert(D.PrivateKey.IsEmpty);
  40. Assert(D.PrivatePassword.IsEmpty);
  41. Assert(D.Certificate.IsEmpty);
  42. Assert(D.Trust.IsEmpty);
  43. Assert(D.DHParams.IsEmpty);
  44. S.PrivateKey := 'fake_pkey';
  45. S.PrivatePassword := 'fake_ppass';
  46. S.Certificate := 'fake_cert';
  47. S.Trust := 'fake_trust';
  48. S.DHParams := 'fake_dhparams';
  49. D.Assign(S);
  50. Assert(D.PrivateKey = 'fake_pkey');
  51. Assert(D.PrivatePassword = 'fake_ppass');
  52. Assert(D.Certificate = 'fake_cert');
  53. Assert(D.Trust = 'fake_trust');
  54. Assert(D.DHParams = 'fake_dhparams');
  55. finally
  56. S.Free;
  57. D.Free;
  58. end;
  59. end;
  60. procedure Test_HTTPServerSecurityClear;
  61. var
  62. S: TBrookHTTPServerSecurity;
  63. begin
  64. S := TBrookHTTPServerSecurity.Create;
  65. try
  66. S.Active := True;
  67. S.PrivateKey := 'fake_pkey';
  68. S.PrivatePassword := 'fake_ppass';
  69. S.Certificate := 'fake_cert';
  70. S.Trust := 'fake_trust';
  71. S.DHParams := 'fake_dhparams';
  72. Assert(S.Active);
  73. Assert(not S.PrivateKey.IsEmpty);
  74. Assert(not S.PrivatePassword.IsEmpty);
  75. Assert(not S.Certificate.IsEmpty);
  76. Assert(not S.Trust.IsEmpty);
  77. Assert(not S.DHParams.IsEmpty);
  78. S.Clear;
  79. Assert(not S.Active);
  80. Assert(S.PrivateKey.IsEmpty);
  81. Assert(S.PrivatePassword.IsEmpty);
  82. Assert(S.Certificate.IsEmpty);
  83. Assert(S.Trust.IsEmpty);
  84. Assert(S.DHParams.IsEmpty);
  85. finally
  86. S.Free;
  87. end;
  88. end;
  89. procedure DoHTTPServerSecurityValidateError(const AArgs: array of const);
  90. begin
  91. TBrookHTTPServerSecurity(AArgs[0].VObject).Validate;
  92. end;
  93. procedure Test_HTTPServerSecurityValidate;
  94. var
  95. S: TBrookHTTPServerSecurity;
  96. begin
  97. S := TBrookHTTPServerSecurity.Create;
  98. try
  99. S.PrivateKey := 'fake_pkey';
  100. S.Certificate := 'fake_cert';
  101. Assert(not S.PrivateKey.IsEmpty);
  102. Assert(not S.Certificate.IsEmpty);
  103. S.PrivateKey := '';
  104. AssertExcept(DoHTTPServerSecurityValidateError, EBrookHTTPServerSecurity,
  105. SBrookEmptyPrivateKey, [S]);
  106. S.PrivateKey := 'fake_pkey';
  107. S.Certificate := '';
  108. AssertExcept(DoHTTPServerSecurityValidateError, EBrookHTTPServerSecurity,
  109. SBrookEmptyCertificate, [S]);
  110. finally
  111. S.Free;
  112. end;
  113. end;
  114. procedure Test_HTTPServerSecurityActive;
  115. var
  116. S: TBrookHTTPServerSecurity;
  117. begin
  118. S := TBrookHTTPServerSecurity.Create;
  119. try
  120. Assert(not S.Active);
  121. S.Active := not S.Active;
  122. Assert(S.Active);
  123. finally
  124. S.Free;
  125. end;
  126. end;
  127. procedure Test_HTTPServerSecurityPrivateKey;
  128. var
  129. S: TBrookHTTPServerSecurity;
  130. begin
  131. S := TBrookHTTPServerSecurity.Create;
  132. try
  133. Assert(S.PrivateKey.IsEmpty);
  134. S.PrivateKey := 'fake_pkey';
  135. Assert(S.PrivateKey = 'fake_pkey');
  136. finally
  137. S.Free;
  138. end;
  139. end;
  140. procedure Test_HTTPServerSecurityPrivatePassword;
  141. var
  142. S: TBrookHTTPServerSecurity;
  143. begin
  144. S := TBrookHTTPServerSecurity.Create;
  145. try
  146. Assert(S.PrivatePassword.IsEmpty);
  147. S.PrivatePassword := 'fake_ppass';
  148. Assert(S.PrivatePassword = 'fake_ppass');
  149. finally
  150. S.Free;
  151. end;
  152. end;
  153. procedure Test_HTTPServerSecurityCertificate;
  154. var
  155. S: TBrookHTTPServerSecurity;
  156. begin
  157. S := TBrookHTTPServerSecurity.Create;
  158. try
  159. Assert(S.Certificate.IsEmpty);
  160. S.Certificate := 'fake_cert';
  161. Assert(S.Certificate = 'fake_cert');
  162. finally
  163. S.Free;
  164. end;
  165. end;
  166. procedure Test_HTTPServerSecurityTrust;
  167. var
  168. S: TBrookHTTPServerSecurity;
  169. begin
  170. S := TBrookHTTPServerSecurity.Create;
  171. try
  172. Assert(S.Trust.IsEmpty);
  173. S.Trust := 'fake_trust';
  174. Assert(S.Trust = 'fake_trust');
  175. finally
  176. S.Free;
  177. end;
  178. end;
  179. procedure Test_HTTPServerSecurityDHParams;
  180. var
  181. S: TBrookHTTPServerSecurity;
  182. begin
  183. S := TBrookHTTPServerSecurity.Create;
  184. try
  185. Assert(S.DHParams.IsEmpty);
  186. S.DHParams := 'fake_dhparams';
  187. Assert(S.DHParams = 'fake_dhparams');
  188. finally
  189. S.Free;
  190. end;
  191. end;
  192. begin
  193. {$IF (NOT DEFINED(FPC)) AND DEFINED(DEBUG)}
  194. ReportMemoryLeaksOnShutdown := True;
  195. {$ENDIF}
  196. TBrookLibraryLoader.Load;
  197. try
  198. Test_HTTPServerSecurityAssign;
  199. Test_HTTPServerSecurityClear;
  200. Test_HTTPServerSecurityValidate;
  201. Test_HTTPServerSecurityActive;
  202. Test_HTTPServerSecurityPrivateKey;
  203. Test_HTTPServerSecurityPrivatePassword;
  204. Test_HTTPServerSecurityCertificate;
  205. Test_HTTPServerSecurityTrust;
  206. Test_HTTPServerSecurityDHParams;
  207. finally
  208. TBrookLibraryLoader.Unload;
  209. end;
  210. end.