1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
|
{
This program is a local test runner for win32
Copyright (c) 2006 Free Pascal development team.
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**********************************************************************}
{
Changes :
02-16-2006 : orinaudo@gmail.com, First release
}
program wcetemu;
{$mode objfpc}
uses Sysutils, w32rapi, windows, IniFiles;
{* const **********************************************************************}
const
SREMOTERUNPROG = 'wcetrun.exe';
IBLOCKBUFFERSIZE = 256*1024;
ISEEKWAITTEMPOMS = 200;
ISEEKRETRYMAX = 180000; // 180 seconds
{* var ************************************************************************}
var
SREMOTEEXEPATH : widestring = '\fpctests';
SLOCALLOGPATHFILE: string;
flog : Text;
sTestExeName : String;
lRes,
WaitTime : cardinal;
bExitCode : Byte;
RBLOCKBUFFER : Array[1..IBLOCKBUFFERSIZE] of Byte;
bFileFound : boolean;
CopyFileToDevice : boolean = True;
LocalTestsRoot : string;
RemoteRunner : string;
{* log ************************************************************************}
procedure log(const csString: String; Error: boolean = False);
begin
if Error then
writeln(csString);
if SLOCALLOGPATHFILE = '' then exit;
system.writeln(flog,FormatDateTime('yy-mm-dd hh:nn:ss',Now)+' : '+csString);
system.Flush(flog);
end;
{* sub RAPI *******************************************************************}
function remotecopyto( const csSource, csTarget : String): Longint;
var wsTarget : WideString;
lRead,
lWritten,res,i : Longint;
bError : Boolean;
hFileTarget : HANDLE;
AFileSource : File of byte;
begin
// open local file
{$I-}
AssignFile(AFileSource,csSource);
system.FileMode := $40;
system.Reset(AFileSource);
{$I+}
res:=system.ioresult;
bError:=(res>0);
if bError then begin
Log('error opening local file "'+csSource+'" ioresult='+IntToStr(res), True);
Result:=res;
exit;
end;
try
//create target file
wsTarget:=csTarget;
for i:=1 to 10 do begin
hFileTarget:=CeCreateFile( PWideChar(wsTarget), GENERIC_WRITE, 0, NIL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,0);
bError:=(hFileTarget=INVALID_HANDLE_VALUE);
if not bError then
break;
Sleep(1000);
end;
if bError then begin
Result:=CEGetLastError;
Log('error creating remote file "'+csTarget+'" ce('+IntToStr(CEGetLastError)+') rapi('+IntToStr(ceRapiGetError)+')', True);
exit;
end;
try
//copy local file by block
repeat
{$I-}
system.BlockRead( AFileSource, RBLOCKBUFFER, IBLOCKBUFFERSIZE, lRead);
{$I+}
res:=system.ioresult;
bError:=res>0;
if bError then begin
Log('error readblock local file "'+csTarget+'" toread='+IntToStr(IBLOCKBUFFERSIZE)+' read='+IntToStr(lRead), True);
Result:=res;
end
else begin
bError:=not CeWriteFile( hFileTarget, @RBLOCKBUFFER, lRead, @lWritten, nil) or (lRead<>lWritten);
if bError then begin
Log('error writing remote file "'+csTarget+'" ce(+'+IntToStr(CEGetLastError)+') written('+IntToStr(lWritten)+')', True);
Result:=CEGetLastError;
end;
end;
until (lRead<IBLOCKBUFFERSIZE) or (bError);
if not bError then
Result:=0;
finally
CeCloseHandle(hFileTarget);
end;
finally
CloseFile(AFileSource);
end;
end;
function remoterun( const csApplication, csCmdLine : String): Longint;
var RPI : PROCESS_INFORMATION;
wsCmdLine,
wsApplication : WideString;
bError : boolean;
begin
wsApplication:=csApplication;
wsCmdLine:=csCmdLine;
bError:=not CeCreateProcess(PWideChar(wsApplication), PWideChar(wsCmdLine),nil,nil,FALSE, 0, nil, nil, nil, @RPI );
Log('remote run "'+csApplication+' '+csCmdLine+'" exitcode='+IntToStr(CeGetLastError)+' bError='+BoolToStr(bError));
if bError
then Result:=CeGetLastError
else begin
CeCloseHandle (RPI.hThread);
CeCloseHandle (RPI.hProcess);
Result:=0;
end;
end;
function remotedelete( const csTarget : String): Longint;
var wsTarget : WideString;
bError : boolean;
begin
wsTarget:=csTarget;
bError:=not CeDeleteFile(PWideChar(wsTarget));
Log('remote delete "'+csTarget+'" exitcode='+IntToStr(CeGetLastError)+' bError='+BoolToStr(bError));
if bError
then Result:=CeGetLastError
else Result:=0;
end;
function remotereadexitcode( const csRemote : String; var vbExitCode: Byte): Longint;
var wsRemote : WideString;
lRead : Longint;
hFileRemote : HANDLE;
bError : Boolean;
begin
vbExitCode:=255;
//create target file
//change extention to .ext
wsRemote:=ChangeFileExt(csRemote,'.ext');
hFileRemote:=CeCreateFile( PWideChar(wsRemote), GENERIC_READ, 0, NIL, OPEN_EXISTING, 0, 0);
bError:=(hFileRemote=INVALID_HANDLE_VALUE);
if bError then begin
Result:=CEGetLastError;
Log('error opening remote file "'+wsRemote+'" ce('+IntToStr(CEGetLastError)+') rapi('+IntToStr(ceRapiGetError)+')', True);
end else begin
try
//read remote file
bError:=not (CeReadFile( hFileRemote, @RBLOCKBUFFER, 1, @lRead, nil) and (lRead = 1));
if (bError) then begin
Log('error reading remote file "'+csRemote+'" ce('+IntToStr(CEGetLastError)+') read('+IntToStr(lRead)+')', True);
Result:=CEGetLastError;
if Result = 0 then
Result:=255;
end else begin
vbExitCode:=RBLOCKBUFFER[1];
Log('exit code read ='+IntToStr(vbExitCode));
Result:=0;
end;
finally
CeCloseHandle(hFileRemote);
end;
end;
end;
function InitRapi: boolean;
var
ri: TRapiInit;
res: cardinal;
begin
ri.cbSize:=sizeof(ri);
res:=CeRapiInitEx(ri);
if (res = S_OK) then begin
if (WaitForSingleObject(ri.heRapiInit, 10*1000) = WAIT_OBJECT_0) then
res:=ri.hrRapiInit
else
res:=cardinal(E_FAIL);
end;
Result:=res = S_OK;
if not Result then begin
CeRapiUninit;
Log('Can''''t initialize connection to remote device.', True);
end;
end;
{******************************************************************************}
procedure ProcessCmdLine;
var
i: integer;
s: string;
function GetNextParam(HaltOnEnd: boolean): string;
begin
Inc(i);
if i > ParamCount then begin
if HaltOnEnd then begin
writeln('Incorrect parameters.');
Halt(2);
end;
Result:='';
end
else
Result:=ParamStr(i);
end;
var
ini: TIniFile;
begin
if ParamCount = 0 then begin
writeln('Usage: ' + ExtractFileName(ParamStr(0)) + ' [-L <log_file>] [-R <remote_path>] <test_to_run>');
writeln('-L - write log to <log_file>.');
writeln('-R - path in Pocket PC device where <test_to_run> program will be copied.');
writeln(' default path: ' + SREMOTEEXEPATH);
Halt(1);
end;
s:=ChangeFileExt(ParamStr(0), '.ini');
if FileExists(s) then begin
ini:=TIniFile.Create(s);
try
SLOCALLOGPATHFILE:=ini.ReadString('Options', 'LogToFile', SLOCALLOGPATHFILE);
SREMOTEEXEPATH:=ini.ReadString('Options', 'RemotePath', SREMOTEEXEPATH);
CopyFileToDevice:=ini.ReadBool('Options', 'CopyFile', CopyFileToDevice);
LocalTestsRoot:=ini.ReadString('Options', 'LocalTestsRoot', LocalTestsRoot);
finally
ini.Free;
end;
end;
i:=0;
while True do begin
s:=GetNextParam(False);
if s = '' then
break;
if Copy(s, 1, 2) = '-L' then
SLOCALLOGPATHFILE:=GetNextParam(True)
else
if Copy(s, 1, 2) = '-R' then
SREMOTEEXEPATH:=GetNextParam(True)
else
if sTestExeName = '' then
sTestExeName:=s
else
begin
writeln('Incorrect parameters.');
Halt(2);
end;
end;
SREMOTEEXEPATH:=IncludeTrailingPathDelimiter(SREMOTEEXEPATH);
RemoteRunner:=SREMOTEEXEPATH + SREMOTERUNPROG;
if not CopyFileToDevice and (LocalTestsRoot<>'') then begin
s:=IncludeTrailingPathDelimiter(GetCurrentDir);
LocalTestsRoot:=IncludeTrailingPathDelimiter(LocalTestsRoot);
if AnsiCompareText(LocalTestsRoot, Copy(s, 1, Length(LocalTestsRoot))) = 0 then
SREMOTEEXEPATH:=IncludeTrailingPathDelimiter(SREMOTEEXEPATH + Copy(s, Length(LocalTestsRoot)+1, MaxInt));
end;
end;
{******************************************************************************}
begin
ProcessCmdLine;
if SLOCALLOGPATHFILE <> '' then begin
system.FileMode := $42;
system.Assign(flog,SLOCALLOGPATHFILE);
{$I-}
system.Append(flog);
{$I+}
if (system.ioresult>0) then begin
system.Rewrite(flog);
log('log rewrite');
end else log('log append');
end;
try
if not InitRapi then
Halt(255);
if CopyFileToDevice then begin
CeCreateDirectory(PWideChar(widestring(ExcludeTrailingPathDelimiter(SREMOTEEXEPATH))), nil);
remotedelete(ChangeFileExt(SREMOTEEXEPATH+sTestExeName, '.ext'));
//copy file
log('remote copy "'+sTestExeName+'" to "'+SREMOTEEXEPATH+sTestExeName+'"');
lRes:=remotecopyto(sTestExeName, SREMOTEEXEPATH+sTestExeName);
if lRes<>0 then
Halt(255); // source file not found
end;
while True do begin
lRes:=remoterun(RemoteRunner,'"'+SREMOTEEXEPATH+sTestExeName+'"');
if lRes<>0 then begin
// check exec stub file
if CeGetFileAttributes(PWideChar(widestring(RemoteRunner))) <> -1 then begin
log('Unable to run "'+RemoteRunner+'"', True);
Halt(255);
end;
if remotecopyto(ExtractFilePath(ParamStr(0)) + SREMOTERUNPROG, RemoteRunner) <> 0 then
Halt(255); // exec stub file not found
end
else
break;
end;
// waiting for result file creation (waitforsingleobject and getexitcodprocess not available with rapi)
log('Waiting for result file...');
WaitTime:=GetTickCount;
bFileFound:=False;
repeat
if CeGetFileAttributes(PWideChar(widestring(ChangeFileExt(SREMOTEEXEPATH+sTestExeName, '.ext')))) <> -1 then begin
bFileFound:=True;
break;
end
else
Sleep(ISEEKWAITTEMPOMS);
until GetTickCount - WaitTime >= ISEEKRETRYMAX;
if bFileFound then begin
// reading result file
WaitTime:=GetTickCount;
bFileFound:=False;
repeat
log('remote read exitcode"'+SREMOTEEXEPATH+sTestExeName+'"');
lRes:=remotereadexitcode(SREMOTEEXEPATH+sTestExeName, bExitCode);
bFileFound:=(lRes=0);
if bFileFound then begin
if CopyFileToDevice then
remotedelete(ChangeFileExt(SREMOTEEXEPATH+sTestExeName, '.ext'));
break;
end;
Sleep(ISEEKWAITTEMPOMS);
until GetTickCount - WaitTime >= 30000;
end;
// deleting remote file
if CopyFileToDevice then
remotedelete(SREMOTEEXEPATH+sTestExeName);
CeRapiUninit;
if (not bFileFound) then begin
log('* error no exitcode for "'+sTestExeName+'"', True);
bExitCode:=255; //error code, result file not found
end;
finally
if SLOCALLOGPATHFILE <> '' then begin
log('log closed');
Close(flog);
end;
end;
if bExitCode>0 then
Halt(bExitCode);
end.
|