1 |
(*
|
2 |
* Firebird Interface (fbintf) Test suite. This program is used to
|
3 |
* test the Firebird Pascal Interface and provide a semi-automated
|
4 |
* pass/fail check for each test.
|
5 |
*
|
6 |
* The contents of this file are subject to the Initial Developer's
|
7 |
* Public License Version 1.0 (the "License"); you may not use this
|
8 |
* file except in compliance with the License. You may obtain a copy
|
9 |
* of the License here:
|
10 |
*
|
11 |
* http://www.firebirdsql.org/index.php?op=doc&id=idpl
|
12 |
*
|
13 |
* Software distributed under the License is distributed on an "AS
|
14 |
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
15 |
* implied. See the License for the specific language governing rights
|
16 |
* and limitations under the License.
|
17 |
*
|
18 |
* The Initial Developer of the Original Code is Tony Whyman.
|
19 |
*
|
20 |
* The Original Code is (C) 2016 Tony Whyman, MWA Software
|
21 |
* (http://www.mwasoftware.co.uk).
|
22 |
*
|
23 |
* All Rights Reserved.
|
24 |
*
|
25 |
* Contributor(s): ______________________________________.
|
26 |
*
|
27 |
*)
|
28 |
|
29 |
unit Test11;
|
30 |
{$IFDEF MSWINDOWS}
|
31 |
{$DEFINE WINDOWS}
|
32 |
{$ENDIF}
|
33 |
|
34 |
{$IFDEF FPC}
|
35 |
{$mode delphi}
|
36 |
{$codepage utf8}
|
37 |
{$ENDIF}
|
38 |
|
39 |
{Test 11: Services API}
|
40 |
|
41 |
{
|
42 |
This test attaches to the Server Manager associated with the Employee Database
|
43 |
and then accesses and prints out:
|
44 |
|
45 |
Version Info
|
46 |
Config Params
|
47 |
Database Information
|
48 |
User Information
|
49 |
Statistics
|
50 |
Licence Info
|
51 |
Licence Mask Info
|
52 |
Capabilities
|
53 |
limbo transactions
|
54 |
|
55 |
Note that two exceptions are normal as Firebird does not provide licence info.
|
56 |
}
|
57 |
|
58 |
interface
|
59 |
|
60 |
uses
|
61 |
Classes, SysUtils, TestApplication, FBTestApp, IB;
|
62 |
|
63 |
type
|
64 |
|
65 |
{ TTest11 }
|
66 |
|
67 |
TTest11 = class(TFBTestBase)
|
68 |
private
|
69 |
procedure GetStatistics(Service: IServiceManager; DBName: AnsiString);
|
70 |
procedure BackupRestore(Service: IServiceManager; DBName: AnsiString);
|
71 |
public
|
72 |
function TestTitle: AnsiString; override;
|
73 |
procedure RunTest(CharSet: AnsiString; SQLDialect: integer); override;
|
74 |
end;
|
75 |
|
76 |
implementation
|
77 |
|
78 |
{ TTest11 }
|
79 |
|
80 |
procedure TTest11.GetStatistics(Service: IServiceManager; DBName: AnsiString);
|
81 |
var Req: ISRB;
|
82 |
Results: IServiceQueryResults;
|
83 |
begin
|
84 |
{Version Info}
|
85 |
Req := Service.AllocateSRB;
|
86 |
Req.Add(isc_info_svc_version);
|
87 |
Req.Add(isc_info_svc_server_version);
|
88 |
Req.Add(isc_info_svc_implementation);
|
89 |
Results := Service.Query(nil,Req);
|
90 |
WriteServiceQueryResult(Results);
|
91 |
|
92 |
{Config Params}
|
93 |
Req := Service.AllocateSRB;
|
94 |
Req.Add(isc_info_svc_get_env_lock);
|
95 |
Req.Add(isc_info_svc_get_config);
|
96 |
Req.Add(isc_info_svc_get_env_msg);
|
97 |
Req.Add(isc_info_svc_user_dbpath);
|
98 |
Results := Service.Query(nil,Req);
|
99 |
WriteServiceQueryResult(Results);
|
100 |
|
101 |
{Database Information}
|
102 |
Req := Service.AllocateSRB;
|
103 |
Req.Add(isc_info_svc_svr_db_info);
|
104 |
Results := Service.Query(nil,Req);
|
105 |
WriteServiceQueryResult(Results);
|
106 |
|
107 |
{User Information}
|
108 |
Req := Service.AllocateSRB;
|
109 |
Req.Add(isc_action_svc_display_user);
|
110 |
Service.Start(Req);
|
111 |
Req := Service.AllocateSRB;
|
112 |
Req.Add(isc_info_svc_get_users);
|
113 |
Results := Service.Query(nil,Req);
|
114 |
WriteServiceQueryResult(Results);
|
115 |
|
116 |
Service.Detach;
|
117 |
Service.Attach;
|
118 |
|
119 |
{Statistics}
|
120 |
|
121 |
if Owner.ShowStatistics then
|
122 |
begin
|
123 |
writeln(OutFile,'Get Statistics');
|
124 |
writeln(OutFile);
|
125 |
Req := Service.AllocateSRB;
|
126 |
Req.Add(isc_action_svc_db_stats);
|
127 |
Req.Add(isc_spb_dbname).SetAsString(DBName);
|
128 |
Req.Add(isc_spb_options).SetAsInteger(isc_spb_sts_data_pages or {
|
129 |
isc_spb_sts_hdr_pages or} isc_spb_sts_idx_pages or
|
130 |
isc_spb_sts_sys_relations);
|
131 |
|
132 |
try
|
133 |
Service.Start(Req);
|
134 |
Req := Service.AllocateSRB;
|
135 |
Req.Add(isc_info_svc_line);
|
136 |
repeat
|
137 |
Results := Service.Query(nil,Req);
|
138 |
until not WriteServiceQueryResult(Results);
|
139 |
except on E: Exception do
|
140 |
writeln(OutFile,'Statistics Service Start: ',E.Message);
|
141 |
end;
|
142 |
writeln(OutFile);
|
143 |
end;
|
144 |
|
145 |
{Licence Info}
|
146 |
Req := Service.AllocateSRB;
|
147 |
Req.Add(isc_info_svc_get_license);
|
148 |
Req.Add(isc_info_svc_get_licensed_users);
|
149 |
try
|
150 |
Results := Service.Query(nil,Req);
|
151 |
WriteServiceQueryResult(Results);
|
152 |
except on E: Exception do
|
153 |
writeln(OutFile,'Licence Info: ',E.Message);
|
154 |
end;
|
155 |
writeln(OutFile);
|
156 |
|
157 |
{Licence Mask Info}
|
158 |
Req := Service.AllocateSRB;
|
159 |
Req.Add(isc_info_svc_get_license_mask);
|
160 |
try
|
161 |
Results := Service.Query(nil,Req);
|
162 |
WriteServiceQueryResult(Results);
|
163 |
except on E: Exception do
|
164 |
writeln(OutFile,'Licence Mask Info: ',E.Message);
|
165 |
end;
|
166 |
writeln(OutFile);
|
167 |
|
168 |
{Capabilities}
|
169 |
Req := Service.AllocateSRB;
|
170 |
Req.Add(isc_info_svc_capabilities);
|
171 |
try
|
172 |
Results := Service.Query(nil,Req);
|
173 |
WriteServiceQueryResult(Results);
|
174 |
except on E: Exception do
|
175 |
writeln(OutFile,'Capabilities: ',E.Message);
|
176 |
end;
|
177 |
writeln(OutFile);
|
178 |
|
179 |
{limbo transactions}
|
180 |
|
181 |
writeln(OutFile,'Get Limbo transactions');
|
182 |
writeln(OutFile);
|
183 |
Req := Service.AllocateSRB;
|
184 |
Req.Add(isc_info_svc_limbo_trans);
|
185 |
try
|
186 |
Results := Service.Query(nil,Req);
|
187 |
WriteServiceQueryResult(Results);
|
188 |
except on E: Exception do
|
189 |
writeln(OutFile,'limbo transactions: ',E.Message);
|
190 |
end;
|
191 |
writeln(OutFile);
|
192 |
end;
|
193 |
|
194 |
procedure TTest11.BackupRestore(Service: IServiceManager; DBName: AnsiString);
|
195 |
var Req: ISRB;
|
196 |
Results: IServiceQueryResults;
|
197 |
BakFile: TFileStream;
|
198 |
QueryResultsItem: IServiceQueryResultItem;
|
199 |
ReqLength: integer;
|
200 |
SQPB: ISQPB;
|
201 |
bytesWritten: integer;
|
202 |
bytesAvailable: integer;
|
203 |
RestoreDBName: AnsiString;
|
204 |
Attachment: IAttachment;
|
205 |
DPB: IDPB;
|
206 |
begin
|
207 |
{Local Backup}
|
208 |
|
209 |
writeln(OutFile,'Local Backup');
|
210 |
Req := Service.AllocateSRB;
|
211 |
Req.Add(isc_action_svc_backup);
|
212 |
Req.Add(isc_spb_dbname).AsString := DBName;
|
213 |
Req.Add(isc_spb_bkp_file).AsString := 'stdout';
|
214 |
try
|
215 |
BakFile := TFileStream.Create(Owner.GetBackupFileName,fmCreate);
|
216 |
try
|
217 |
Service.Start(Req);
|
218 |
Req := Service.AllocateSRB;
|
219 |
Req.Add(isc_info_svc_to_eof);
|
220 |
repeat
|
221 |
bytesWritten := 0;
|
222 |
Results := Service.Query(Req);
|
223 |
QueryResultsItem := Results.Find(isc_info_svc_to_eof);
|
224 |
if QueryResultsItem <> nil then
|
225 |
bytesWritten := QueryResultsItem.CopyTo(BakFile,0);
|
226 |
until ((bytesWritten = 0) and (Results.Find(isc_info_svc_timeout) = nil))
|
227 |
or not WriteServiceQueryResult(Results);
|
228 |
writeln(OutFile,'Local Backup Complete');
|
229 |
finally
|
230 |
BakFile.Free;
|
231 |
end;
|
232 |
except on E: Exception do
|
233 |
writeln(OutFile,'Local Backup Service: ',E.Message);
|
234 |
end;
|
235 |
writeln(OutFile);
|
236 |
|
237 |
{Local Restore}
|
238 |
writeln(OutFile,'Local Restore');
|
239 |
RestoreDBName := ExtractDBName(Owner.GetNewDatabaseName);
|
240 |
Req := Service.AllocateSRB;
|
241 |
Req.Add(isc_action_svc_restore);
|
242 |
Req.Add(isc_spb_dbname).AsString := RestoreDBName;
|
243 |
Req.Add(isc_spb_verbose);
|
244 |
Req.Add(isc_spb_bkp_file).AsString := 'stdin';
|
245 |
Req.Add(isc_spb_res_access_mode).AsByte := isc_spb_prp_am_readwrite;
|
246 |
Req.Add(isc_spb_options).SetAsInteger(isc_spb_res_create);
|
247 |
BakFile := TFileStream.Create(Owner.GetBackupFileName,fmOpenRead);
|
248 |
try
|
249 |
bytesAvailable := BakFile.Size;
|
250 |
try
|
251 |
Service.Start(Req);
|
252 |
ReqLength := 0;
|
253 |
repeat
|
254 |
SQPB := Service.AllocateSQPB;
|
255 |
if ReqLength > 0 then
|
256 |
bytesWritten := SQPB.Add(isc_info_svc_line).CopyFrom(BakFile,ReqLength);
|
257 |
bytesAvailable := bytesAvailable - bytesWritten;
|
258 |
Req := Service.AllocateSRB;
|
259 |
Req.Add(isc_info_svc_stdin);
|
260 |
Req.Add(isc_info_svc_line);
|
261 |
Results := Service.Query(SQPB,Req);
|
262 |
QueryResultsItem := Results.Find(isc_info_svc_stdin);
|
263 |
if QueryResultsItem <> nil then
|
264 |
ReqLength := QueryResultsItem.AsInteger;
|
265 |
WriteServiceQueryResult(Results);
|
266 |
until (ReqLength = 0) ;
|
267 |
writeln(OutFile,'Local Restore Complete');
|
268 |
except on E: Exception do
|
269 |
writeln(OutFile,'Local Restore Service: ',E.Message);
|
270 |
end;
|
271 |
finally
|
272 |
BakFile.free;
|
273 |
end;
|
274 |
writeln(OutFile);
|
275 |
writeln(OutFile,'Open Database Check');
|
276 |
DPB := FirebirdAPI.AllocateDPB;
|
277 |
DPB.Add(isc_dpb_user_name).AsString := Owner.GetUserName;
|
278 |
DPB.Add(isc_dpb_password).AsString := Owner.GetPassword;
|
279 |
Attachment := FirebirdAPI.OpenDatabase(Owner.GetNewDatabaseName,DPB);
|
280 |
if Attachment <> nil then
|
281 |
writeln(OutFile,'Database OK');
|
282 |
Attachment.DropDatabase;
|
283 |
writeln(OutFile,'Database Dropped');
|
284 |
end;
|
285 |
|
286 |
function TTest11.TestTitle: AnsiString;
|
287 |
begin
|
288 |
Result := 'Test 11: Services API';
|
289 |
end;
|
290 |
|
291 |
procedure TTest11.RunTest(CharSet: AnsiString; SQLDialect: integer);
|
292 |
var SPB: ISPB;
|
293 |
Service: IServiceManager;
|
294 |
ServerName: AnsiString;
|
295 |
DBName: AnsiString;
|
296 |
begin
|
297 |
if not FirebirdAPI.HasServiceAPI then Exit;
|
298 |
|
299 |
DBName := ExtractDBName(Owner.GetEmployeeDatabaseName);
|
300 |
ServerName := Owner.Server;
|
301 |
|
302 |
SPB := FirebirdAPI.AllocateSPB;
|
303 |
SPB.Add(isc_spb_user_name).setAsString(Owner.GetUserName);
|
304 |
SPB.Add(isc_spb_password).setAsString(Owner.GetPassword);
|
305 |
Service := FirebirdAPI.GetServiceManager(ServerName,TCP,SPB);
|
306 |
PrintSPB(Service.getSPB);
|
307 |
|
308 |
GetStatistics(Service,DBName);
|
309 |
BackupRestore(Service,DBName);
|
310 |
|
311 |
end;
|
312 |
|
313 |
initialization
|
314 |
RegisterTest(TTest11);
|
315 |
|
316 |
end.
|
317 |
|