VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/tests/additions/tdAddGuestCtrl.py@ 55338

Last change on this file since 55338 was 52776, checked in by vboxsync, 10 years ago

fix OSE

  • Property svn:eol-style set to LF
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 163.6 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# pylint: disable=C0302
4
5"""
6VirtualBox Validation Kit - Guest Control Tests.
7"""
8
9__copyright__ = \
10"""
11Copyright (C) 2010-2014 Oracle Corporation
12
13This file is part of VirtualBox Open Source Edition (OSE), as
14available from http://www.virtualbox.org. This file is free software;
15you can redistribute it and/or modify it under the terms of the GNU
16General Public License (GPL) as published by the Free Software
17Foundation, in version 2 as it comes in the "COPYING" file of the
18VirtualBox OSE distribution. VirtualBox OSE is distributed in the
19hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
20
21The contents of this file may alternatively be used under the terms
22of the Common Development and Distribution License Version 1.0
23(CDDL) only, as it comes in the "COPYING.CDDL" file of the
24VirtualBox OSE distribution, in which case the provisions of the
25CDDL are applicable instead of those of the GPL.
26
27You may elect to license modified versions of this file under the
28terms and conditions of either the GPL or the CDDL or both.
29"""
30__version__ = "$Revision: 52776 $"
31
32# Disable bitching about too many arguments per function.
33# pylint: disable=R0913
34
35## @todo Convert map() usage to a cleaner alternative Python now offers.
36# pylint: disable=W0141
37
38## @todo Convert the context/test classes into named tuples. Not in the mood right now, so
39# disabling it.
40# pylint: disable=R0903
41
42# Standard Python imports.
43from array import array
44import errno
45import os
46import random
47import string # pylint: disable=W0402
48import struct
49import sys
50import time
51
52# Only the main script needs to modify the path.
53try: __file__
54except: __file__ = sys.argv[0];
55g_ksValidationKitDir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))));
56sys.path.append(g_ksValidationKitDir);
57
58# Validation Kit imports.
59from testdriver import reporter;
60from testdriver import base;
61from testdriver import vbox;
62from testdriver import vboxcon;
63from testdriver import vboxwrappers;
64
65
66class GuestStream(bytearray):
67 """
68 Class for handling a guest process input/output stream.
69 """
70 def appendStream(self, stream, convertTo='<b'):
71 """
72 Appends and converts a byte sequence to this object;
73 handy for displaying a guest stream.
74 """
75 self.extend(struct.pack(convertTo, stream));
76
77class tdCtxTest(object):
78 """
79 Provides the actual test environment. Should be kept
80 as generic as possible.
81 """
82 def __init__(self, oSession, oTxsSession, oTestVm): # pylint: disable=W0613
83 ## The desired Main API result.
84 self.fRc = False;
85 ## IGuest reference.
86 self.oGuest = oSession.o.console.guest;
87 # Rest not used (yet).
88
89class tdCtxCreds(object):
90 """
91 Provides credentials to pass to the guest.
92 """
93 def __init__(self, sUser, sPassword, sDomain):
94 self.sUser = sUser;
95 self.sPassword = sPassword;
96 self.sDomain = sDomain;
97
98class tdTestGuestCtrlBase(object):
99 """
100 Base class for all guest control tests.
101 Note: This test ASSUMES that working Guest Additions
102 were installed and running on the guest to be tested.
103 """
104 def __init__(self):
105 self.oTest = None;
106 self.oCreds = None;
107 self.timeoutMS = 30 * 1000; # 30s timeout
108 ## IGuestSession reference or None.
109 self.oGuestSession = None;
110
111 def setEnvironment(self, oSession, oTxsSession, oTestVm):
112 """
113 Sets the test environment required for this test.
114 """
115 self.oTest = tdCtxTest(oSession, oTxsSession, oTestVm);
116 return self.oTest;
117
118 def createSession(self, sName):
119 """
120 Creates (opens) a guest session.
121 Returns (True, IGuestSession) on success or (False, None) on failure.
122 """
123 if self.oGuestSession is None:
124 if sName is None:
125 sName = "<untitled>";
126 try:
127 reporter.log('Creating session "%s" ...' % (sName,));
128 self.oGuestSession = self.oTest.oGuest.createSession(self.oCreds.sUser,
129 self.oCreds.sPassword,
130 self.oCreds.sDomain,
131 sName);
132 except:
133 # Just log, don't assume an error here (will be done in the main loop then).
134 reporter.logXcpt('Creating a guest session "%s" failed; sUser="%s", pw="%s", sDomain="%s":'
135 % (sName, self.oCreds.sUser, self.oCreds.sPassword, self.oCreds.sDomain));
136 return (False, None);
137
138 try:
139 reporter.log('Waiting for session "%s" to start within %ldms...' % (sName, self.timeoutMS));
140 fWaitFor = [ vboxcon.GuestSessionWaitForFlag_Start ];
141 waitResult = self.oGuestSession.waitForArray(fWaitFor, self.timeoutMS);
142 #
143 # Be nice to Guest Additions < 4.3: They don't support session handling and
144 # therefore return WaitFlagNotSupported.
145 #
146 if waitResult != vboxcon.GuestSessionWaitResult_Start \
147 and waitResult != vboxcon.GuestSessionWaitResult_WaitFlagNotSupported:
148 # Just log, don't assume an error here (will be done in the main loop then).
149 reporter.log('Session did not start successfully, returned wait result: %ld' \
150 % (waitResult));
151 return (False, None);
152 reporter.log('Session "%s" successfully started' % (sName,));
153 except:
154 # Just log, don't assume an error here (will be done in the main loop then).
155 reporter.logXcpt('Waiting for guest session "%s" to start failed:' % (sName));
156 return (False, None);
157 else:
158 reporter.log('Warning: Session already set; this is probably not what you want');
159 return (True, self.oGuestSession);
160
161 def setSession(self, oGuestSession):
162 """
163 Sets the current guest session and closes
164 an old one if necessary.
165 """
166 if self.oGuestSession is not None:
167 self.closeSession();
168 self.oGuestSession = oGuestSession;
169 return self.oGuestSession;
170
171 def closeSession(self):
172 """
173 Closes the guest session.
174 """
175 if self.oGuestSession is not None:
176 sName = self.oGuestSession.name;
177 try:
178 reporter.log('Closing session "%s" ...' % (sName,));
179 self.oGuestSession.close();
180 self.oGuestSession = None;
181 except:
182 # Just log, don't assume an error here (will be done in the main loop then).
183 reporter.logXcpt('Closing guest session "%s" failed:' % (sName,));
184 return False;
185 return True;
186
187class tdTestCopyFrom(tdTestGuestCtrlBase):
188 """
189 Test for copying files from the guest to the host.
190 """
191 def __init__(self, sSrc = "", sDst = "", sUser = "", sPassword = "", aFlags = None):
192 tdTestGuestCtrlBase.__init__(self);
193 self.oCreds = tdCtxCreds(sUser, sPassword, sDomain = "");
194 self.sSrc = sSrc;
195 self.sDst = sDst;
196 self.aFlags = aFlags;
197
198class tdTestCopyTo(tdTestGuestCtrlBase):
199 """
200 Test for copying files from the host to the guest.
201 """
202 def __init__(self, sSrc = "", sDst = "", sUser = "", sPassword = "", aFlags = None):
203 tdTestGuestCtrlBase.__init__(self);
204 self.oCreds = tdCtxCreds(sUser, sPassword, sDomain = "");
205 self.sSrc = sSrc;
206 self.sDst = sDst;
207 self.aFlags = aFlags;
208
209class tdTestDirCreate(tdTestGuestCtrlBase):
210 """
211 Test for directoryCreate call.
212 """
213 def __init__(self, sDirectory = "", sUser = "", sPassword = "", fMode = 0, aFlags = None):
214 tdTestGuestCtrlBase.__init__(self);
215 self.oCreds = tdCtxCreds(sUser, sPassword, sDomain = "");
216 self.sDirectory = sDirectory;
217 self.fMode = fMode;
218 self.aFlags = aFlags;
219
220class tdTestDirCreateTemp(tdTestGuestCtrlBase):
221 """
222 Test for the directoryCreateTemp call.
223 """
224 def __init__(self, sDirectory = "", sTemplate = "", sUser = "", sPassword = "", fMode = 0, fSecure = False):
225 tdTestGuestCtrlBase.__init__(self);
226 self.oCreds = tdCtxCreds(sUser, sPassword, sDomain = "");
227 self.sDirectory = sDirectory;
228 self.sTemplate = sTemplate;
229 self.fMode = fMode;
230 self.fSecure = fSecure;
231
232class tdTestDirOpen(tdTestGuestCtrlBase):
233 """
234 Test for the directoryOpen call.
235 """
236 def __init__(self, sDirectory = "", sUser = "", sPassword = "",
237 sFilter = "", aFlags = None):
238 tdTestGuestCtrlBase.__init__(self);
239 self.oCreds = tdCtxCreds(sUser, sPassword, sDomain = "");
240 self.sDirectory = sDirectory;
241 self.sFilter = sFilter;
242 self.aFlags = aFlags or [];
243
244class tdTestDirRead(tdTestDirOpen):
245 """
246 Test for the opening, reading and closing a certain directory.
247 """
248 def __init__(self, sDirectory = "", sUser = "", sPassword = "",
249 sFilter = "", aFlags = None):
250 tdTestDirOpen.__init__(self, sDirectory, sUser, sPassword, sFilter, aFlags);
251
252class tdTestExec(tdTestGuestCtrlBase):
253 """
254 Specifies exactly one guest control execution test.
255 Has a default timeout of 5 minutes (for safety).
256 """
257 def __init__(self, sCmd = "", aArgs = None, aEnv = None, \
258 aFlags = None, timeoutMS = 5 * 60 * 1000, \
259 sUser = "", sPassword = "", sDomain = "", \
260 fWaitForExit = True):
261 tdTestGuestCtrlBase.__init__(self);
262 self.oCreds = tdCtxCreds(sUser, sPassword, sDomain);
263 self.sCmd = sCmd;
264 self.aArgs = aArgs;
265 self.aEnv = aEnv;
266 self.aFlags = aFlags or [];
267 self.timeoutMS = timeoutMS;
268 self.fWaitForExit = fWaitForExit;
269 self.uExitStatus = 0;
270 self.iExitCode = 0;
271 self.cbStdOut = 0;
272 self.cbStdErr = 0;
273 self.sBuf = '';
274
275class tdTestFileExists(tdTestGuestCtrlBase):
276 """
277 Test for the file exists API call (fileExists).
278 """
279 def __init__(self, sFile = "", sUser = "", sPassword = ""):
280 tdTestGuestCtrlBase.__init__(self);
281 self.oCreds = tdCtxCreds(sUser, sPassword, sDomain = "");
282 self.sFile = sFile;
283
284class tdTestFileRemove(tdTestGuestCtrlBase):
285 """
286 Test querying guest file information.
287 """
288 def __init__(self, sFile = "", sUser = "", sPassword = ""):
289 tdTestGuestCtrlBase.__init__(self);
290 self.oCreds = tdCtxCreds(sUser, sPassword, sDomain = "");
291 self.sFile = sFile;
292
293class tdTestFileStat(tdTestGuestCtrlBase):
294 """
295 Test querying guest file information.
296 """
297 def __init__(self, sFile = "", sUser = "", sPassword = "", cbSize = 0, eFileType = 0):
298 tdTestGuestCtrlBase.__init__(self);
299 self.oCreds = tdCtxCreds(sUser, sPassword, sDomain = "");
300 self.sFile = sFile;
301 self.cbSize = cbSize;
302 self.eFileType = eFileType;
303
304class tdTestFileIO(tdTestGuestCtrlBase):
305 """
306 Test for the IGuestFile object.
307 """
308 def __init__(self, sFile = "", sUser = "", sPassword = ""):
309 tdTestGuestCtrlBase.__init__(self);
310 self.oCreds = tdCtxCreds(sUser, sPassword, sDomain = "");
311 self.sFile = sFile;
312
313class tdTestFileQuerySize(tdTestGuestCtrlBase):
314 """
315 Test for the file size query API call (fileQuerySize).
316 """
317 def __init__(self, sFile = "", sUser = "", sPassword = ""):
318 tdTestGuestCtrlBase.__init__(self);
319 self.oCreds = tdCtxCreds(sUser, sPassword, sDomain = "");
320 self.sFile = sFile;
321
322class tdTestFileReadWrite(tdTestGuestCtrlBase):
323 """
324 Tests reading from guest files.
325 """
326 def __init__(self, sFile = "", sUser = "", sPassword = "",
327 sOpenMode = "r", sDisposition = "",
328 sSharingMode = "",
329 lCreationMode = 0, cbOffset = 0, cbToReadWrite = 0,
330 aBuf = None):
331 tdTestGuestCtrlBase.__init__(self);
332 self.oCreds = tdCtxCreds(sUser, sPassword, sDomain = "");
333 self.sFile = sFile;
334 self.sOpenMode = sOpenMode;
335 self.sDisposition = sDisposition;
336 self.sSharingMode = sSharingMode;
337 self.lCreationMode = lCreationMode;
338 self.cbOffset = cbOffset;
339 self.cbToReadWrite = cbToReadWrite;
340 self.aBuf = aBuf;
341
342class tdTestSession(tdTestGuestCtrlBase):
343 """
344 Test the guest session handling.
345 """
346 def __init__(self, sUser = "", sPassword = "", sDomain = "", \
347 sSessionName = ""):
348 tdTestGuestCtrlBase.__init__(self);
349 self.sSessionName = sSessionName;
350 self.oCreds = tdCtxCreds(sUser, sPassword, sDomain);
351
352 def getSessionCount(self, oVBoxMgr):
353 """
354 Helper for returning the number of currently
355 opened guest sessions of a VM.
356 """
357 if self.oTest.oGuest is None:
358 return 0;
359 aoSession = oVBoxMgr.getArray(self.oTest.oGuest, 'sessions')
360 return len(aoSession);
361
362class tdTestSessionEnv(tdTestGuestCtrlBase):
363 """
364 Test the guest session environment.
365 """
366 def __init__(self, sUser = "", sPassword = "", aEnv = None):
367 tdTestGuestCtrlBase.__init__(self);
368 self.oCreds = tdCtxCreds(sUser, sPassword, sDomain = "");
369 self.aEnv = aEnv or [];
370
371class tdTestSessionFileRefs(tdTestGuestCtrlBase):
372 """
373 Tests session file (IGuestFile) reference counting.
374 """
375 def __init__(self, cRefs = 0):
376 tdTestGuestCtrlBase.__init__(self);
377 self.cRefs = cRefs;
378
379class tdTestSessionDirRefs(tdTestGuestCtrlBase):
380 """
381 Tests session directory (IGuestDirectory) reference counting.
382 """
383 def __init__(self, cRefs = 0):
384 tdTestGuestCtrlBase.__init__(self);
385 self.cRefs = cRefs;
386
387class tdTestSessionProcRefs(tdTestGuestCtrlBase):
388 """
389 Tests session process (IGuestProcess) reference counting.
390 """
391 def __init__(self, cRefs = 0):
392 tdTestGuestCtrlBase.__init__(self);
393 self.cRefs = cRefs;
394
395class tdTestUpdateAdditions(tdTestGuestCtrlBase):
396 """
397 Test updating the Guest Additions inside the guest.
398 """
399 def __init__(self, sSrc = "", aArgs = None, aFlags = None,
400 sUser = "", sPassword = "", sDomain = ""):
401 tdTestGuestCtrlBase.__init__(self);
402 self.oCreds = tdCtxCreds(sUser, sPassword, sDomain);
403 self.sSrc = sSrc;
404 self.aArgs = aArgs;
405 self.aFlags = aFlags;
406
407class tdTestResult(object):
408 """
409 Base class for test results.
410 """
411 def __init__(self, fRc = False):
412 ## The overall test result.
413 self.fRc = fRc;
414
415class tdTestResultDirRead(tdTestResult):
416 """
417 Test result for reading guest directories.
418 """
419 def __init__(self, fRc = False,
420 numFiles = 0, numDirs = 0):
421 tdTestResult.__init__(self, fRc = fRc);
422 self.numFiles = numFiles;
423 self.numDirs = numDirs;
424
425class tdTestResultExec(tdTestResult):
426 """
427 Holds a guest process execution test result,
428 including the exit code, status + aFlags.
429 """
430 def __init__(self, fRc = False, \
431 uExitStatus = 500, iExitCode = 0, \
432 sBuf = None, cbBuf = 0, \
433 cbStdOut = 0, cbStdErr = 0):
434 tdTestResult.__init__(self);
435 ## The overall test result.
436 self.fRc = fRc;
437 ## Process exit stuff.
438 self.uExitStatus = uExitStatus;
439 self.iExitCode = iExitCode;
440 ## Desired buffer length returned back from stdout/stderr.
441 self.cbBuf = cbBuf;
442 ## Desired buffer result from stdout/stderr. Use with caution!
443 self.sBuf = sBuf;
444 self.cbStdOut = cbStdOut;
445 self.cbStdErr = cbStdErr;
446
447class tdTestResultFileStat(tdTestResult):
448 """
449 Test result for stat'ing guest files.
450 """
451 def __init__(self, fRc = False,
452 cbSize = 0, eFileType = 0):
453 tdTestResult.__init__(self, fRc = fRc);
454 self.cbSize = cbSize;
455 self.eFileType = eFileType;
456 ## @todo Add more information.
457
458class tdTestResultFileReadWrite(tdTestResult):
459 """
460 Test result for reading + writing guest directories.
461 """
462 def __init__(self, fRc = False,
463 cbProcessed = 0, cbOffset = 0, aBuf = None):
464 tdTestResult.__init__(self, fRc = fRc);
465 self.cbProcessed = cbProcessed;
466 self.cbOffset = cbOffset;
467 self.aBuf = aBuf;
468
469class tdTestResultSession(tdTestResult):
470 """
471 Test result for guest session counts.
472 """
473 def __init__(self, fRc = False, cNumSessions = 0):
474 tdTestResult.__init__(self, fRc = fRc);
475 self.cNumSessions = cNumSessions;
476
477class tdTestResultSessionEnv(tdTestResult):
478 """
479 Test result for guest session environment tests.
480 """
481 def __init__(self, fRc = False, cNumVars = 0):
482 tdTestResult.__init__(self, fRc = fRc);
483 self.cNumVars = cNumVars;
484
485
486class SubTstDrvAddGuestCtrl(base.SubTestDriverBase):
487 """
488 Sub-test driver for executing guest control (VBoxService, IGuest) tests.
489 """
490
491 def __init__(self, oTstDrv):
492 base.SubTestDriverBase.__init__(self, 'add-guest-ctrl', oTstDrv);
493
494 ## @todo base.TestBase.
495 self.asTestsDef = \
496 [
497 'session_basic', 'session_env', 'session_file_ref', 'session_dir_ref', 'session_proc_ref', \
498 'exec_basic', 'exec_errorlevel', 'exec_timeout', \
499 'dir_create', 'dir_create_temp', 'dir_read', \
500 'file_remove', 'file_stat', 'file_read', 'file_write', \
501 'copy_to', 'copy_from', \
502 'update_additions'
503 ];
504 self.asTests = self.asTestsDef;
505
506 def parseOption(self, asArgs, iArg): # pylint: disable=R0912,R0915
507 if asArgs[iArg] == '--add-guest-ctrl-tests':
508 iArg += 1;
509 if asArgs[iArg] == 'all': # Nice for debugging scripts.
510 self.asTests = self.asTestsDef;
511 return iArg + 1;
512
513 iNext = self.oTstDrv.requireMoreArgs(1, asArgs, iArg);
514 self.asTests = asArgs[iArg].split(':');
515 for s in self.asTests:
516 if s not in self.asTestsDef:
517 raise base.InvalidOption('The "--add-guest-ctrl-tests" value "%s" is not valid; valid values are: %s' \
518 % (s, ' '.join(self.asTestsDef)));
519 return iNext;
520 return iArg;
521
522 def showUsage(self):
523 base.SubTestDriverBase.showUsage(self);
524 reporter.log(' --add-guest-ctrl-tests <s1[:s2[:]]>');
525 reporter.log(' Default: %s (all)' % (':'.join(self.asTestsDef)));
526 return True;
527
528 def testIt(self, oTestVm, oSession, oTxsSession):
529 """
530 Executes the test.
531
532 Returns fRc, oTxsSession. The latter may have changed.
533 """
534 reporter.log("Active tests: %s" % (self.asTests,));
535
536 # Do the testing.
537 reporter.testStart('Session Basics');
538 fSkip = 'session_basic' not in self.asTests;
539 if fSkip == False:
540 fRc, oTxsSession = self.testGuestCtrlSession(oSession, oTxsSession, oTestVm);
541 reporter.testDone(fSkip);
542
543 reporter.testStart('Session Environment');
544 fSkip = 'session_env' not in self.asTests or fRc is False;
545 if fSkip == False:
546 fRc, oTxsSession = self.testGuestCtrlSessionEnvironment(oSession, oTxsSession, oTestVm);
547 reporter.testDone(fSkip);
548
549 reporter.testStart('Session File References');
550 fSkip = 'session_file_ref' not in self.asTests;
551 if fSkip == False:
552 fRc, oTxsSession = self.testGuestCtrlSessionFileRefs(oSession, oTxsSession, oTestVm);
553 reporter.testDone(fSkip);
554
555 ## @todo Implement this.
556 #reporter.testStart('Session Directory References');
557 #fSkip = 'session_dir_ref' not in self.asTests;
558 #if fSkip == False:
559 # fRc, oTxsSession = self.testGuestCtrlSessionDirRefs(oSession, oTxsSession, oTestVm);
560 #reporter.testDone(fSkip);
561
562 reporter.testStart('Session Process References');
563 fSkip = 'session_proc_ref' not in self.asTests or fRc is False;
564 if fSkip == False:
565 fRc, oTxsSession = self.testGuestCtrlSessionProcRefs(oSession, oTxsSession, oTestVm);
566 reporter.testDone(fSkip);
567
568 reporter.testStart('Execution');
569 fSkip = 'exec_basic' not in self.asTests or fRc is False;
570 if fSkip == False:
571 fRc, oTxsSession = self.testGuestCtrlExec(oSession, oTxsSession, oTestVm);
572 reporter.testDone(fSkip);
573
574 reporter.testStart('Execution Error Levels');
575 fSkip = 'exec_errorlevel' not in self.asTests or fRc is False;
576 if fSkip == False:
577 fRc, oTxsSession = self.testGuestCtrlExecErrorLevel(oSession, oTxsSession, oTestVm);
578 reporter.testDone(fSkip);
579
580 reporter.testStart('Execution Timeouts');
581 fSkip = 'exec_timeout' not in self.asTests or fRc is False;
582 if fSkip == False:
583 fRc, oTxsSession = self.testGuestCtrlExecTimeout(oSession, oTxsSession, oTestVm);
584 reporter.testDone(fSkip);
585
586 reporter.testStart('Creating directories');
587 fSkip = 'dir_create' not in self.asTests or fRc is False;
588 if fSkip == False:
589 fRc, oTxsSession = self.testGuestCtrlDirCreate(oSession, oTxsSession, oTestVm);
590 reporter.testDone(fSkip);
591
592 reporter.testStart('Creating temporary directories');
593 fSkip = 'dir_create_temp' not in self.asTests or fRc is False;
594 if fSkip == False:
595 fRc, oTxsSession = self.testGuestCtrlDirCreateTemp(oSession, oTxsSession, oTestVm);
596 reporter.testDone(fSkip);
597
598 reporter.testStart('Reading directories');
599 fSkip = 'dir_read' not in self.asTests or fRc is False;
600 if fSkip == False:
601 fRc, oTxsSession = self.testGuestCtrlDirRead(oSession, oTxsSession, oTestVm);
602 reporter.testDone(fSkip);
603
604 # FIXME: Failing test.
605 # reporter.testStart('Copy to guest');
606 # fSkip = 'copy_to' not in self.asTests or fRc is False;
607 # if fSkip == False:
608 # fRc, oTxsSession = self.testGuestCtrlCopyTo(oSession, oTxsSession, oTestVm);
609 # reporter.testDone(fSkip);
610
611 reporter.testStart('Copy from guest');
612 fSkip = 'copy_from' not in self.asTests or fRc is False;
613 if fSkip == False:
614 fRc, oTxsSession = self.testGuestCtrlCopyFrom(oSession, oTxsSession, oTestVm);
615 reporter.testDone(fSkip);
616
617 reporter.testStart('Removing files');
618 fSkip = 'file_remove' not in self.asTests or fRc is False;
619 if fSkip == False:
620 fRc, oTxsSession = self.testGuestCtrlFileRemove(oSession, oTxsSession, oTestVm);
621 reporter.testDone(fSkip);
622
623 reporter.testStart('Querying file information (stat)');
624 fSkip = 'file_stat' not in self.asTests or fRc is False;
625 if fSkip == False:
626 fRc, oTxsSession = self.testGuestCtrlFileStat(oSession, oTxsSession, oTestVm);
627 reporter.testDone(fSkip);
628
629 # FIXME: Failing tests.
630 # reporter.testStart('File read');
631 # fSkip = 'file_read' not in self.asTests or fRc is False;
632 # if fSkip == False:
633 # fRc, oTxsSession = self.testGuestCtrlFileRead(oSession, oTxsSession, oTestVm);
634 # reporter.testDone(fSkip);
635
636 # reporter.testStart('File write');
637 # fSkip = 'file_write' not in self.asTests or fRc is False;
638 # if fSkip == False:
639 # fRc, oTxsSession = self.testGuestCtrlFileWrite(oSession, oTxsSession, oTestVm);
640 # reporter.testDone(fSkip);
641
642 reporter.testStart('Updating Guest Additions');
643 fSkip = 'update_additions' not in self.asTests or fRc is False;
644 # Skip test for updating Guest Additions if we run on a too old (Windows) guest.
645 fSkip = oTestVm.sKind in ('WindowsNT4', 'Windows2000', 'WindowsXP', 'Windows2003');
646 if fSkip == False:
647 fRc, oTxsSession = self.testGuestCtrlUpdateAdditions(oSession, oTxsSession, oTestVm);
648 reporter.testDone(fSkip);
649
650 return (fRc, oTxsSession);
651
652 def gctrlCopyFileFrom(self, oGuestSession, sSrc, sDst, aFlags):
653 """
654 Helper function to copy a single file from the guest to the host.
655 """
656 fRc = True; # Be optimistic.
657 try:
658 reporter.log2('Copying guest file "%s" to host "%s"' % (sSrc, sDst));
659 curProgress = oGuestSession.copyFrom(sSrc, sDst, aFlags);
660 if curProgress is not None:
661 oProgress = vboxwrappers.ProgressWrapper(curProgress, self.oTstDrv.oVBoxMgr, self, "gctrlCopyFrm");
662 try:
663 iRc = oProgress.waitForOperation(0, fIgnoreErrors = True);
664 if iRc != 0:
665 reporter.log('Waiting for copyFrom failed');
666 fRc = False;
667 except:
668 reporter.logXcpt('Copy from waiting exception for sSrc="%s", sDst="%s":' \
669 % (sSrc, sDst));
670 fRc = False;
671 except:
672 # Just log, don't assume an error here (will be done in the main loop then).
673 reporter.logXcpt('Copy from exception for sSrc="%s", sDst="%s":' \
674 % (sSrc, sDst));
675 fRc = False;
676
677 return fRc;
678
679 def gctrlCopyFileTo(self, oGuestSession, sSrc, sDst, aFlags):
680 """
681 Helper function to copy a single file from host to the guest.
682 """
683 fRc = True; # Be optimistic.
684 try:
685 reporter.log2('Copying host file "%s" to guest "%s"' % (sSrc, sDst));
686 curProgress = oGuestSession.copyTo(sSrc, sDst, aFlags);
687 if curProgress is not None:
688 oProgress = vboxwrappers.ProgressWrapper(curProgress, self.oTstDrv.oVBoxMgr, self, "gctrlCopyTo");
689 try:
690 iRc = oProgress.waitForOperation(0, fIgnoreErrors = True);
691 if iRc != 0:
692 reporter.log('Waiting for copyTo failed');
693 fRc = False;
694 except:
695 reporter.logXcpt('Copy to waiting exception for sSrc="%s", sDst="%s":' \
696 % (sSrc, sDst));
697 fRc = False;
698 else:
699 reporter.error('No progress object returned');
700 except:
701 # Just log, don't assume an error here (will be done in the main loop then).
702 reporter.logXcpt('Copy to exception for sSrc="%s", sDst="%s":' \
703 % (sSrc, sDst));
704 fRc = False;
705
706 return fRc;
707
708 def gctrlCopyFrom(self, oTest, oRes, oGuestSession, subDir = ''): # pylint: disable=R0914
709 """
710 Copies files / directories to the host.
711 Source always contains the absolute path, subDir all paths
712 below it.
713 """
714 ## @todo r=bird: The use subDir and sSubDir in this method is extremely confusing!!
715 # Just follow the coding guidelines and ALWAYS use prefixes, please!
716 fRc = True; # Be optimistic.
717
718 sSrc = oTest.sSrc;
719 sDst = oTest.sDst;
720 aFlags = oTest.aFlags;
721
722 if sSrc == "" \
723 or os.path.isfile(sSrc):
724 return self.gctrlCopyFileFrom(oGuestSession, \
725 sSrc, sDst, aFlags);
726 sSrcAbs = sSrc;
727 if subDir != "":
728 sSrcAbs = os.path.join(sSrc, subDir);
729 sFilter = ""; # No filter.
730
731 sDstAbs = os.path.join(sDst, subDir);
732 reporter.log2('Copying guest directory "%s" to host "%s"' % (sSrcAbs, sDstAbs));
733
734 try:
735 #reporter.log2('Directory="%s", filter="%s", aFlags="%s"' % (sCurDir, sFilter, aFlags));
736 oCurDir = oGuestSession.directoryOpen(sSrcAbs, sFilter, aFlags);
737 while fRc:
738 try:
739 oFsObjInfo = oCurDir.read();
740 if oFsObjInfo.name == "." \
741 or oFsObjInfo.name == "..":
742 #reporter.log2('\tSkipping "%s"' % (oFsObjInfo.name,));
743 continue; # Skip "." and ".." entries.
744 if oFsObjInfo.type is vboxcon.FsObjType_Directory:
745 #reporter.log2('\tDirectory "%s"' % (oFsObjInfo.name,));
746 sDirCreate = sDst;
747 if subDir != "":
748 sDirCreate = os.path.join(sDirCreate, subDir);
749 sDirCreate = os.path.join(sDirCreate, oFsObjInfo.name);
750 try:
751 reporter.log2('\tCreating directory "%s"' % (sDirCreate,));
752 os.makedirs(sDirCreate);
753 sSubDir = oFsObjInfo.name;
754 if subDir != "":
755 sSubDir = os.path.join(subDir, oFsObjInfo.name);
756 self.gctrlCopyFrom(oTest, oRes, oGuestSession, sSubDir);
757 except (OSError) as e:
758 if e.errno == errno.EEXIST:
759 pass;
760 else:
761 # Just log, don't assume an error here (will be done in the main loop then).
762 reporter.logXcpt('\tDirectory creation exception for directory="%s":' % (sSubDir,));
763 raise;
764 elif oFsObjInfo.type is vboxcon.FsObjType_File:
765 #reporter.log2('\tFile "%s"' % (oFsObjInfo.name,));
766 sSourceFile = os.path.join(sSrcAbs, oFsObjInfo.name);
767 sDestFile = os.path.join(sDstAbs, oFsObjInfo.name);
768 self.gctrlCopyFileFrom(oGuestSession, sSourceFile, sDestFile, aFlags);
769 elif oFsObjInfo.type is vboxcon.FsObjType_Symlink:
770 #reporter.log2('\tSymlink "%s" -- not tested yet' % oFsObjInfo.name);
771 pass;
772 else:
773 reporter.error('\tDirectory "%s" contains invalid directory entry "%s" (%d)' \
774 % (sSubDir, oFsObjInfo.name, oFsObjInfo.type));
775 fRc = False;
776 except Exception, oXcpt:
777 # No necessarily an error -- could be VBOX_E_OBJECT_NOT_FOUND. See reference.
778 if vbox.ComError.equal(oXcpt, vbox.ComError.VBOX_E_OBJECT_NOT_FOUND):
779 #reporter.log2('\tNo more directory entries for "%s"' % (sCurDir,));
780 break
781 # Just log, don't assume an error here (will be done in the main loop then).
782 reporter.logXcpt('\tDirectory read exception for directory="%s":' % (sSrcAbs,));
783 fRc = False;
784 break;
785 oCurDir.close();
786 except:
787 # Just log, don't assume an error here (will be done in the main loop then).
788 reporter.logXcpt('\tDirectory open exception for directory="%s":' % (sSrcAbs,));
789 fRc = False;
790
791 return fRc;
792
793 def gctrlCopyTo(self, oTest, oGuestSession, subDir = ''): # pylint: disable=R0914
794 """
795 Copies files / directories to the guest.
796 Source always contains the absolute path,
797 subDir all paths below it.
798 """
799 fRc = True; # Be optimistic.
800
801 sSrc = oTest.sSrc;
802 sDst = oTest.sDst;
803 aFlags = oTest.aFlags;
804
805 reporter.log2('sSrc=%s, sDst=%s, aFlags=%s' % (sSrc, sDst, aFlags));
806
807 sSrcAbs = sSrc;
808 if subDir != "":
809 sSrcAbs = os.path.join(sSrc, subDir);
810
811 # Note: Current test might want to test copying empty sources
812 if not os.path.exists(sSrcAbs) \
813 or os.path.isfile(sSrcAbs):
814 return self.gctrlCopyFileTo(oGuestSession, \
815 sSrcAbs, sDst, aFlags);
816
817 sDstAbs = os.path.join(sDst, subDir);
818 reporter.log2('Copying host directory "%s" to guest "%s"' % (sSrcAbs, sDstAbs));
819
820 try:
821 # Note: Symlinks intentionally not considered here.
822 for (sDirCurrent, oDirs, oFiles) in os.walk(sSrcAbs, topdown=True):
823 for sFile in oFiles:
824 sCurFile = os.path.join(sDirCurrent, sFile);
825 reporter.log2('Processing host file "%s"' % (sCurFile,));
826 sFileDest = os.path.join(sDstAbs, os.path.relpath(sCurFile, sSrcAbs));
827 reporter.log2('Copying file to "%s"' % (sFileDest,));
828 fRc = self.gctrlCopyFileTo(oGuestSession, \
829 sCurFile, sFileDest, aFlags);
830 if fRc is False:
831 break;
832
833 if fRc is False:
834 break;
835
836 for sSubDir in oDirs:
837 sCurDir = os.path.join(sDirCurrent, sSubDir);
838 reporter.log2('Processing host dir "%s"' % (sCurDir,));
839 sDirDest = os.path.join(sDstAbs, os.path.relpath(sCurDir, sSrcAbs));
840 reporter.log2('Creating guest dir "%s"' % (sDirDest,));
841 oGuestSession.directoryCreate(sDirDest, \
842 700, [ vboxcon.DirectoryCreateFlag_Parents ]);
843 if fRc is False:
844 break;
845
846 if fRc is False:
847 break;
848 except:
849 # Just log, don't assume an error here (will be done in the main loop then).
850 reporter.logXcpt('Copy to exception for sSrc="%s", sDst="%s":' \
851 % (sSrcAbs, sDst));
852 return False;
853
854 return fRc;
855
856 def gctrlCreateDir(self, oTest, oRes, oGuestSession):
857 """
858 Helper function to create a guest directory specified in
859 the current test.
860 """
861 fRc = True; # Be optimistic.
862 reporter.log2('Creating directory "%s"' % (oTest.sDirectory,));
863
864 try:
865 oGuestSession.directoryCreate(oTest.sDirectory, \
866 oTest.fMode, oTest.aFlags);
867 fDirExists = oGuestSession.directoryExists(oTest.sDirectory);
868 if fDirExists is False \
869 and oRes.fRc is True:
870 # Directory does not exist but we want it to.
871 fRc = False;
872 except:
873 reporter.logXcpt('Directory create exception for directory "%s":' % (oTest.sDirectory,));
874 if oRes.fRc is True:
875 # Just log, don't assume an error here (will be done in the main loop then).
876 fRc = False;
877 # Directory creation failed, which was the expected result.
878
879 return fRc;
880
881 def gctrlReadDir(self, oTest, oRes, oGuestSession, subDir = ''): # pylint: disable=R0914
882 """
883 Helper function to read a guest directory specified in
884 the current test.
885 """
886 sDir = oTest.sDirectory;
887 sFilter = oTest.sFilter;
888 aFlags = oTest.aFlags;
889
890 fRc = True; # Be optimistic.
891 cDirs = 0; # Number of directories read.
892 cFiles = 0; # Number of files read.
893
894 try:
895 sCurDir = os.path.join(sDir, subDir);
896 #reporter.log2('Directory="%s", filter="%s", aFlags="%s"' % (sCurDir, sFilter, aFlags));
897 oCurDir = oGuestSession.directoryOpen(sCurDir, sFilter, aFlags);
898 while fRc:
899 try:
900 oFsObjInfo = oCurDir.read();
901 if oFsObjInfo.name == "." \
902 or oFsObjInfo.name == "..":
903 #reporter.log2('\tSkipping "%s"' % oFsObjInfo.name);
904 continue; # Skip "." and ".." entries.
905 if oFsObjInfo.type is vboxcon.FsObjType_Directory:
906 #reporter.log2('\tDirectory "%s"' % oFsObjInfo.name);
907 cDirs += 1;
908 sSubDir = oFsObjInfo.name;
909 if subDir != "":
910 sSubDir = os.path.join(subDir, oFsObjInfo.name);
911 fRc, cSubDirs, cSubFiles = self.gctrlReadDir(oTest, oRes, oGuestSession, sSubDir);
912 cDirs += cSubDirs;
913 cFiles += cSubFiles;
914 elif oFsObjInfo.type is vboxcon.FsObjType_File:
915 #reporter.log2('\tFile "%s"' % oFsObjInfo.name);
916 cFiles += 1;
917 elif oFsObjInfo.type is vboxcon.FsObjType_Symlink:
918 #reporter.log2('\tSymlink "%s" -- not tested yet' % oFsObjInfo.name);
919 pass;
920 else:
921 reporter.error('\tDirectory "%s" contains invalid directory entry "%s" (type %d)' % \
922 (sCurDir, oFsObjInfo.name, oFsObjInfo.type));
923 fRc = False;
924 except Exception, oXcpt:
925 # No necessarily an error -- could be VBOX_E_OBJECT_NOT_FOUND. See reference.
926 if vbox.ComError.equal(oXcpt, vbox.ComError.VBOX_E_OBJECT_NOT_FOUND):
927 #reporter.log2('\tNo more directory entries for "%s"' % (sCurDir,));
928 break
929 # Just log, don't assume an error here (will be done in the main loop then).
930 reporter.logXcpt('\tDirectory open exception for directory="%s":' % (sCurDir,));
931 fRc = False;
932 break;
933 oCurDir.close();
934 except:
935 # Just log, don't assume an error here (will be done in the main loop then).
936 reporter.logXcpt('\tDirectory open exception for directory="%s":' % (sCurDir,));
937 fRc = False;
938
939 return (fRc, cDirs, cFiles);
940
941 def gctrlExecDoTest(self, i, oTest, oRes, oGuestSession):
942 """
943 Wrapper function around gctrlExecute to provide more sanity checking
944 when needed in actual execution tests.
945 """
946 reporter.log('Testing #%d, cmd="%s" ...' % (i, oTest.sCmd));
947 fRc = self.gctrlExecute(oTest, oGuestSession);
948 if fRc is oRes.fRc:
949 if fRc is True:
950 # Compare exit status / code on successful process execution.
951 if oTest.uExitStatus != oRes.uExitStatus \
952 or oTest.iExitCode != oRes.iExitCode:
953 reporter.error('Test #%d failed: Got exit status + code %d,%d, expected %d,%d' % \
954 (i, oTest.uExitStatus, oTest.iExitCode, \
955 oRes.uExitStatus, oRes.iExitCode));
956 return False;
957 if fRc is True:
958 # Compare test / result buffers on successful process execution.
959 if oTest.sBuf is not None \
960 and oRes.sBuf is not None:
961 if bytes(oTest.sBuf) != bytes(oRes.sBuf):
962 reporter.error('Test #%d failed: Got buffer\n%s (%ld bytes), expected\n%s (%ld bytes)' %
963 (i, map(hex, map(ord, oTest.sBuf)), len(oTest.sBuf), \
964 map(hex, map(ord, oRes.sBuf)), len(oRes.sBuf)));
965 return False;
966 else:
967 reporter.log2('Test #%d passed: Buffers match (%ld bytes)' % (i, len(oRes.sBuf)));
968 elif oRes.sBuf is not None \
969 and len(oRes.sBuf):
970 reporter.error('Test #%d failed: Got no buffer data, expected\n%s (%dbytes)' %
971 (i, map(hex, map(ord, oRes.sBuf)), len(oRes.sBuf)));
972 return False;
973 elif oRes.cbStdOut > 0 \
974 and oRes.cbStdOut != oTest.cbStdOut:
975 reporter.error('Test #%d failed: Got %ld stdout data, expected %ld'
976 % (i, oTest.cbStdOut, oRes.cbStdOut));
977 return False;
978 else:
979 reporter.error('Test #%d failed: Got %s, expected %s' % (i, fRc, oRes.fRc));
980 return False;
981 return True;
982
983 def gctrlExecute(self, oTest, oGuestSession):
984 """
985 Helper function to execute a program on a guest, specified in
986 the current test.
987 """
988 fRc = True; # Be optimistic.
989
990 ## @todo Compare execution timeouts!
991 #tsStart = base.timestampMilli();
992
993 reporter.log2('Using session user=%s, sDomain=%s, session name=%s, session timeout=%ld' \
994 % (oGuestSession.user, oGuestSession.domain, \
995 oGuestSession.name, oGuestSession.timeout));
996 reporter.log2('Executing cmd=%s, aFlags=%s, timeout=%ld, args=%s, env=%s' \
997 % (oTest.sCmd, oTest.aFlags, oTest.timeoutMS, \
998 oTest.aArgs, oTest.aEnv));
999 try:
1000 curProc = oGuestSession.processCreate(oTest.sCmd, \
1001 oTest.aArgs, oTest.aEnv, \
1002 oTest.aFlags, oTest.timeoutMS);
1003 if curProc is not None:
1004 reporter.log2('Process start requested, waiting for start (%ldms) ...' % (oTest.timeoutMS,));
1005 fWaitFor = [ vboxcon.ProcessWaitForFlag_Start ];
1006 waitResult = curProc.waitForArray(fWaitFor, oTest.timeoutMS);
1007 reporter.log2('Wait result returned: %d, current process status is: %ld' % (waitResult, curProc.status));
1008
1009 if curProc.status == vboxcon.ProcessStatus_Started:
1010 fWaitFor = [ vboxcon.ProcessWaitForFlag_Terminate ];
1011 if vboxcon.ProcessCreateFlag_WaitForStdOut in oTest.aFlags:
1012 fWaitFor.append(vboxcon.ProcessWaitForFlag_StdOut);
1013 if vboxcon.ProcessCreateFlag_WaitForStdErr in oTest.aFlags:
1014 fWaitFor.append(vboxcon.ProcessWaitForFlag_StdErr);
1015 ## @todo Add vboxcon.ProcessWaitForFlag_StdIn.
1016 reporter.log2('Process (PID %ld) started, waiting for termination (%dms), waitFlags=%s ...' \
1017 % (curProc.PID, oTest.timeoutMS, fWaitFor));
1018 while True:
1019 waitResult = curProc.waitForArray(fWaitFor, oTest.timeoutMS);
1020 reporter.log2('Wait returned: %d' % (waitResult,));
1021 try:
1022 # Try stdout.
1023 if waitResult == vboxcon.ProcessWaitResult_StdOut \
1024 or waitResult == vboxcon.ProcessWaitResult_WaitFlagNotSupported:
1025 reporter.log2('Reading stdout ...');
1026 buf = curProc.Read(1, 64 * 1024, oTest.timeoutMS);
1027 if len(buf):
1028 reporter.log2('Process (PID %ld) got %ld bytes of stdout data' % (curProc.PID, len(buf)));
1029 oTest.cbStdOut += len(buf);
1030 oTest.sBuf = buf; # Appending does *not* work atm, so just assign it. No time now.
1031 # Try stderr.
1032 if waitResult == vboxcon.ProcessWaitResult_StdErr \
1033 or waitResult == vboxcon.ProcessWaitResult_WaitFlagNotSupported:
1034 reporter.log2('Reading stderr ...');
1035 buf = curProc.Read(2, 64 * 1024, oTest.timeoutMS);
1036 if len(buf):
1037 reporter.log2('Process (PID %ld) got %ld bytes of stderr data' % (curProc.PID, len(buf)));
1038 oTest.cbStdErr += len(buf);
1039 oTest.sBuf = buf; # Appending does *not* work atm, so just assign it. No time now.
1040 # Use stdin.
1041 if waitResult == vboxcon.ProcessWaitResult_StdIn \
1042 or waitResult == vboxcon.ProcessWaitResult_WaitFlagNotSupported:
1043 pass; #reporter.log2('Process (PID %ld) needs stdin data' % (curProc.pid,));
1044 # Termination or error?
1045 if waitResult == vboxcon.ProcessWaitResult_Terminate \
1046 or waitResult == vboxcon.ProcessWaitResult_Error \
1047 or waitResult == vboxcon.ProcessWaitResult_Timeout:
1048 reporter.log2('Process (PID %ld) reported terminate/error/timeout: %ld, status: %ld' \
1049 % (curProc.PID, waitResult, curProc.status));
1050 break;
1051 except:
1052 # Just skip reads which returned nothing.
1053 pass;
1054 reporter.log2('Final process status (PID %ld) is: %ld' % (curProc.PID, curProc.status));
1055 reporter.log2('Process (PID %ld) %ld stdout, %ld stderr' % (curProc.PID, oTest.cbStdOut, oTest.cbStdErr));
1056 oTest.uExitStatus = curProc.status;
1057 oTest.iExitCode = curProc.exitCode;
1058 reporter.log2('Process (PID %ld) has exit code: %ld' % (curProc.PID, oTest.iExitCode));
1059 except KeyboardInterrupt:
1060 reporter.error('Process (PID %ld) execution interrupted' % (curProc.PID,));
1061 if curProc is not None:
1062 curProc.close();
1063 except:
1064 # Just log, don't assume an error here (will be done in the main loop then).
1065 reporter.logXcpt('Execution exception for command "%s":' % (oTest.sCmd,));
1066 fRc = False;
1067
1068 return fRc;
1069
1070 def testGuestCtrlSessionEnvironment(self, oSession, oTxsSession, oTestVm): # pylint: disable=R0914
1071 """
1072 Tests the guest session environment.
1073 """
1074
1075 if oTestVm.isWindows():
1076 sUser = "Administrator";
1077 else:
1078 sUser = "vbox";
1079 sPassword = "password";
1080
1081 aaTests = [
1082 # No environment set.
1083 [ tdTestSessionEnv(sUser = sUser, sPassword = sPassword),
1084 tdTestResultSessionEnv(fRc = False) ],
1085 # Invalid stuff.
1086 [ tdTestSessionEnv(sUser = sUser, sPassword = sPassword, aEnv = [ '=FOO' ]),
1087 tdTestResultSessionEnv(fRc = False) ],
1088 [ tdTestSessionEnv(sUser = sUser, sPassword = sPassword, aEnv = [ '====' ]),
1089 tdTestResultSessionEnv(fRc = False) ],
1090 [ tdTestSessionEnv(sUser = sUser, sPassword = sPassword, aEnv = [ '=BAR' ]),
1091 tdTestResultSessionEnv(fRc = False) ],
1092 [ tdTestSessionEnv(sUser = sUser, sPassword = sPassword, aEnv = [ u'ß$%ß&' ]),
1093 tdTestResultSessionEnv(fRc = False) ],
1094 # Key only.
1095 [ tdTestSessionEnv(sUser = sUser, sPassword = sPassword, aEnv = [ 'FOO=' ]),
1096 tdTestResultSessionEnv(fRc = True, cNumVars = 1) ],
1097 # Values.
1098 [ tdTestSessionEnv(sUser = sUser, sPassword = sPassword, aEnv = [ 'FOO' ]),
1099 tdTestResultSessionEnv(fRc = True, cNumVars = 1) ],
1100 [ tdTestSessionEnv(sUser = sUser, sPassword = sPassword, aEnv = [ 'FOO=BAR' ]),
1101 tdTestResultSessionEnv(fRc = True, cNumVars = 1) ],
1102 [ tdTestSessionEnv(sUser = sUser, sPassword = sPassword, aEnv = [ 'FOO=BAR', 'BAR=BAZ' ]),
1103 tdTestResultSessionEnv(fRc = True, cNumVars = 2) ],
1104 # A bit more weird keys/values.
1105 [ tdTestSessionEnv(sUser = sUser, sPassword = sPassword, aEnv = [ '$$$=' ]),
1106 tdTestResultSessionEnv(fRc = True, cNumVars = 1) ],
1107 [ tdTestSessionEnv(sUser = sUser, sPassword = sPassword, aEnv = [ '$$$=%%%' ]),
1108 tdTestResultSessionEnv(fRc = True, cNumVars = 1) ],
1109 # Same stuff.
1110 [ tdTestSessionEnv(sUser = sUser, sPassword = sPassword, aEnv = [ 'FOO=BAR', 'FOO=BAR' ]),
1111 tdTestResultSessionEnv(fRc = True, cNumVars = 1) ]
1112 ];
1113
1114 # Parameters.
1115 fRc = True;
1116 for (i, aTest) in enumerate(aaTests):
1117 curTest = aTest[0]; # tdTestExec, use an index, later.
1118 curRes = aTest[1]; # tdTestResult
1119 curTest.setEnvironment(oSession, oTxsSession, oTestVm);
1120 reporter.log('Testing #%d, user="%s", sPassword="%s", env="%s" (%d)...' \
1121 % (i, curTest.oCreds.sUser, curTest.oCreds.sPassword, curTest.aEnv, len(curTest.aEnv)));
1122 curGuestSessionName = 'testGuestCtrlSessionEnvironment: Test #%d' % (i,);
1123 fRc2, curGuestSession = curTest.createSession(curGuestSessionName);
1124 if fRc2 is not True:
1125 reporter.error('Test #%d failed: Session creation failed: Got %s, expected True' % (i, fRc2));
1126 fRc = False;
1127 break;
1128 # Make sure environment is empty.
1129 curEnv = self.oTstDrv.oVBoxMgr.getArray(curGuestSession, 'environment');
1130 reporter.log2('Test #%d: Environment initially has %d elements' % (i, len(curEnv)));
1131 if len(curEnv) != 0:
1132 reporter.error('Test #%d failed: Initial session environment has %d vars, expected 0' % (i, len(curEnv)));
1133 fRc = False;
1134 break;
1135 try:
1136 for (_, aEnv) in enumerate(curTest.aEnv): # Enumerate only will work with a sequence (e.g > 1 entries).
1137 aElems = aEnv.split('=');
1138 strKey = '';
1139 strValue = '';
1140 if len(aElems) > 0:
1141 strKey = aElems[0];
1142 if len(aElems) == 2:
1143 strValue = aElems[1];
1144 reporter.log2('Test #%d: Single key="%s", value="%s" (%d) ...' \
1145 % (i, strKey, strValue, len(aElems)));
1146 try:
1147 curGuestSession.environmentSet(strKey, strValue); # No return (e.g. boolean) value available thru wrapper.
1148 except:
1149 # Setting environment variables might fail (e.g. if empty name specified). Check.
1150 reporter.logXcpt('Test #%d failed: Setting environment variable failed:' % (i,));
1151 curEnv = self.oTstDrv.oVBoxMgr.getArray(curGuestSession, 'environment');
1152 if len(curEnv) is not curRes.cNumVars:
1153 reporter.error('Test #%d failed: Session environment has %d vars, expected %d' \
1154 % (i, len(curEnv), curRes.cNumVars));
1155 fRc = False;
1156 break;
1157 else:
1158 reporter.log('Test #%d: API reported an error (single), good' % (i,));
1159 reporter.log2('Getting key="%s" ...' % (strKey,));
1160 try:
1161 strValue2 = curGuestSession.environmentGet(strKey);
1162 if strKey.isalnum() \
1163 and strValue != strValue2:
1164 reporter.error('Test #%d failed: Got environment variable "%s", expected "%s" (key: "%s")' \
1165 % (i, strValue2, strValue, strKey));
1166 fRc = False;
1167 break;
1168 # Getting back an empty value when specifying an invalid key is fine.
1169 reporter.log2('Got key "%s=%s"' % (strKey, strValue2));
1170 except UnicodeDecodeError: # Might happen on unusal values, fine.
1171 if strValue != strValue2:
1172 reporter.error('Test #%d failed: Got (undecoded) environment variable "%s", ' \
1173 'expected "%s" (key: "%s")' \
1174 % (i, strValue2, strValue, strKey));
1175 fRc = False;
1176 break;
1177 except:
1178 if strKey == "" \
1179 or not strKey.isalnum():
1180 reporter.log('Test #%d: API reported an error (invalid key "%s"), good' % (i, strKey));
1181 else:
1182 reporter.errorXcpt('Test #%d failed: Getting environment variable:' % (i));
1183 if fRc is False:
1184 continue;
1185 # Set the same stuff again, this time all at once using the array.
1186 if len(curTest.aEnv):
1187 reporter.log('Test #%d: Array %s (%d)' % (i, curTest.aEnv, len(curTest.aEnv)));
1188 try:
1189 ## @todo No return (e.g. boolean) value available thru wrapper.
1190 #curGuestSession.environmentSetArray(curTest.aEnv);
1191 pass;
1192 except:
1193 # Setting environment variables might fail (e.g. if empty name specified). Check.
1194 curEnv = self.oTstDrv.oVBoxMgr.getArray(curGuestSession, 'environment');
1195 if len(curEnv) is not curRes.cNumVars:
1196 reporter.error('Test #%d failed: Session environment has %d vars, expected %d (array)' \
1197 % (i, len(curEnv), curRes.cNumVars));
1198 fRc = False;
1199 break;
1200 else:
1201 reporter.log('Test #%d: API reported an error (array), good' % (i,));
1202 ## @todo Get current system environment and add it to curRes.cNumVars before comparing!
1203 reporter.log('Test #%d: Environment size' % (i,));
1204 curEnv = self.oTstDrv.oVBoxMgr.getArray(curGuestSession, 'environment');
1205 reporter.log2('Test #%d: Environment (%d) -> %s' % (i, len(curEnv), curEnv));
1206 if len(curEnv) != curRes.cNumVars:
1207 reporter.error('Test #%d failed: Session environment has %d vars (%s), expected %d' \
1208 % (i, len(curEnv), curEnv, curRes.cNumVars));
1209 fRc = False;
1210 break;
1211 curGuestSession.environmentClear(); # No return (e.g. boolean) value available thru wrapper.
1212 curEnv = self.oTstDrv.oVBoxMgr.getArray(curGuestSession, 'environment');
1213 if len(curEnv) is not 0:
1214 reporter.error('Test #%d failed: Session environment has %d vars, expected 0');
1215 fRc = False;
1216 break;
1217 except:
1218 reporter.errorXcpt('Test #%d failed:' % (i,));
1219
1220 fRc2 = curTest.closeSession();
1221 if fRc2 is False:
1222 reporter.error('Test #%d failed: Session could not be closed' % (i,));
1223 fRc = False;
1224 break;
1225
1226 return (fRc, oTxsSession);
1227
1228 def testGuestCtrlSession(self, oSession, oTxsSession, oTestVm): # pylint: disable=R0914
1229 """
1230 Tests the guest session handling.
1231 """
1232
1233 if oTestVm.isWindows():
1234 sUser = "Administrator";
1235 else:
1236 sUser = "vbox";
1237 sPassword = "password";
1238
1239 aaTests = [
1240 # Invalid parameters.
1241 [ tdTestSession(),
1242 tdTestResultSession(fRc = False) ],
1243 [ tdTestSession(sUser = ''),
1244 tdTestResultSession(fRc = False) ],
1245 [ tdTestSession(sPassword = 'bar'),
1246 tdTestResultSession(fRc = False) ],
1247 [ tdTestSession(sDomain = 'boo'),
1248 tdTestResultSession(fRc = False) ],
1249 [ tdTestSession(sPassword = 'bar', sDomain = 'boo'),
1250 tdTestResultSession(fRc = False) ],
1251 # User account without a passwort - forbidden.
1252 [ tdTestSession(sUser = sUser),
1253 tdTestResultSession(fRc = False) ],
1254 # Wrong credentials.
1255 # Note: On Guest Additions < 4.3 this always succeeds because these don't
1256 # support creating dedicated sessions. Instead, guest process creation
1257 # then will fail. See note below.
1258 [ tdTestSession(sUser = 'foo', sPassword = 'bar', sDomain = 'boo'),
1259 tdTestResultSession(fRc = False) ],
1260 # Correct credentials.
1261 [ tdTestSession(sUser = sUser, sPassword = sPassword),
1262 tdTestResultSession(fRc = True, cNumSessions = 1) ]
1263 ];
1264
1265 # Parameters.
1266 fRc = True;
1267 for (i, aTest) in enumerate(aaTests):
1268 curTest = aTest[0]; # tdTestSession, use an index, later.
1269 curRes = aTest[1]; # tdTestResult
1270 curTest.setEnvironment(oSession, oTxsSession, oTestVm);
1271 reporter.log('Testing #%d, user="%s", sPassword="%s", sDomain="%s" ...' \
1272 % (i, curTest.oCreds.sUser, curTest.oCreds.sPassword, curTest.oCreds.sDomain));
1273 curGuestSessionName = 'testGuestCtrlSession: Test #%d' % (i);
1274 fRc2, curGuestSession = curTest.createSession(curGuestSessionName);
1275 # See note about < 4.3 Guest Additions above.
1276 if curGuestSession is not None \
1277 and curGuestSession.protocolVersion >= 2 \
1278 and fRc2 is not curRes.fRc:
1279 reporter.error('Test #%d failed: Session creation failed: Got %s, expected %s' \
1280 % (i, fRc2, curRes.fRc));
1281 fRc = False;
1282 if fRc2:
1283 # On Guest Additions < 4.3 getSessionCount() always will return 1, so skip the
1284 # check then.
1285 if curGuestSession.protocolVersion >= 2:
1286 curSessionCount = curTest.getSessionCount(self.oTstDrv.oVBoxMgr);
1287 if curSessionCount is not curRes.cNumSessions:
1288 reporter.error('Test #%d failed: Session count does not match: Got %d, expected %d' \
1289 % (i, curSessionCount, curRes.cNumSessions));
1290 fRc = False;
1291 break;
1292 if curGuestSession is not None \
1293 and curGuestSession.name != curGuestSessionName:
1294 reporter.error('Test #%d failed: Session name does not match: Got "%s", expected "%s"' \
1295 % (i, curGuestSession.name, curGuestSessionName));
1296 fRc = False;
1297 break;
1298 fRc2 = curTest.closeSession();
1299 if fRc2 is False:
1300 reporter.error('Test #%d failed: Session could not be closed' % (i,));
1301 fRc = False;
1302 break;
1303
1304 if fRc is False:
1305 return (False, oTxsSession);
1306
1307 # Multiple sessions.
1308 iMaxGuestSessions = 31; # Maximum number of concurrent guest session allowed.
1309 # Actually, this is 32, but we don't test session 0.
1310 multiSession = {};
1311 reporter.log2('Opening multiple guest tsessions at once ...');
1312 for i in range(iMaxGuestSessions + 1):
1313 multiSession[i] = tdTestSession(sUser = sUser, sPassword = sPassword, sSessionName = 'MultiSession #%d' % (i,));
1314 multiSession[i].setEnvironment(oSession, oTxsSession, oTestVm);
1315 curSessionCount = multiSession[i].getSessionCount(self.oTstDrv.oVBoxMgr);
1316 reporter.log2('MultiSession test #%d count is %d' % (i, curSessionCount));
1317 if curSessionCount is not i:
1318 reporter.error('MultiSession count #%d must be %d, got %d' % (i, i, curSessionCount));
1319 fRc = False;
1320 break;
1321 fRc2, _ = multiSession[i].createSession('MultiSession #%d' % (i,));
1322 if fRc2 is not True:
1323 if i < iMaxGuestSessions:
1324 reporter.error('MultiSession #%d test failed' % (i,));
1325 fRc = False;
1326 else:
1327 reporter.log('MultiSession #%d exceeded concurrent guest session count, good' % (i,));
1328 break;
1329
1330 curSessionCount = multiSession[i].getSessionCount(self.oTstDrv.oVBoxMgr);
1331 if curSessionCount is not iMaxGuestSessions:
1332 reporter.error('Final MultiSession count must be %d, got %d'
1333 % (iMaxGuestSessions, curSessionCount));
1334 return (False, oTxsSession);
1335
1336 reporter.log2('Closing MultiSessions ...');
1337 iLastSession = iMaxGuestSessions - 1;
1338 for i in range(iLastSession): # Close all but the last opened session.
1339 fRc2 = multiSession[i].closeSession();
1340 reporter.log2('MultiSession #%d count is %d' % (i, multiSession[i].getSessionCount(self.oTstDrv.oVBoxMgr),));
1341 if fRc2 is False:
1342 reporter.error('Closing MultiSession #%d failed' % (i,));
1343 fRc = False;
1344 break;
1345 curSessionCount = multiSession[i].getSessionCount(self.oTstDrv.oVBoxMgr);
1346 if curSessionCount is not 1:
1347 reporter.error('Final MultiSession count #2 must be 1, got %d' % (curSessionCount,));
1348 fRc = False;
1349
1350 try:
1351 # Make sure that accessing the first opened guest session does not work anymore because we just removed (closed) it.
1352 curSessionName = multiSession[0].oGuestSession.name;
1353 reporter.error('Accessing first removed MultiSession should not be possible, got name="%s"' % (curSessionName,));
1354 fRc = False;
1355 except:
1356 reporter.logXcpt('Could not access first removed MultiSession object, good:');
1357
1358 try:
1359 # Try Accessing last opened session which did not get removed yet.
1360 curSessionName = multiSession[iLastSession].oGuestSession.name;
1361 reporter.log('Accessing last standing MultiSession worked, got name="%s"' % (curSessionName,));
1362 multiSession[iLastSession].closeSession();
1363 curSessionCount = multiSession[i].getSessionCount(self.oTstDrv.oVBoxMgr);
1364 if curSessionCount is not 0:
1365 reporter.error('Final MultiSession count #3 must be 0, got %d' % (curSessionCount,));
1366 fRc = False;
1367 except:
1368 reporter.logXcpt('Could not access last standing MultiSession object:');
1369 fRc = False;
1370
1371 ## @todo Test session timeouts.
1372
1373 return (fRc, oTxsSession);
1374
1375 def testGuestCtrlSessionFileRefs(self, oSession, oTxsSession, oTestVm): # pylint: disable=R0914
1376 """
1377 Tests the guest session file reference handling.
1378 """
1379
1380 if oTestVm.isWindows():
1381 sUser = "Administrator";
1382 sPassword = "password";
1383 sDomain = "";
1384 sFile = "C:\\windows\\system32\\kernel32.dll";
1385
1386 # Number of stale guest files to create.
1387 cStaleFiles = 10;
1388
1389 fRc = True;
1390 try:
1391 oGuest = oSession.o.console.guest;
1392 oGuestSession = oGuest.createSession(sUser, sPassword, sDomain, \
1393 "testGuestCtrlSessionFileRefs");
1394 fWaitFor = [ vboxcon.GuestSessionWaitForFlag_Start ];
1395 waitResult = oGuestSession.waitForArray(fWaitFor, 30 * 1000);
1396 #
1397 # Be nice to Guest Additions < 4.3: They don't support session handling and
1398 # therefore return WaitFlagNotSupported.
1399 #
1400 if waitResult != vboxcon.GuestSessionWaitResult_Start \
1401 and waitResult != vboxcon.GuestSessionWaitResult_WaitFlagNotSupported:
1402 # Just log, don't assume an error here (will be done in the main loop then).
1403 reporter.log('Session did not start successfully, returned wait result: %ld' \
1404 % (waitResult));
1405 return (False, oTxsSession);
1406 reporter.log('Session successfully started');
1407
1408 #
1409 # Open guest files and "forget" them (stale entries).
1410 # For them we don't have any references anymore intentionally.
1411 #
1412 reporter.log2('Opening stale files');
1413 for i in range(0, cStaleFiles):
1414 try:
1415 oGuestSession.fileOpen(sFile, "r", "oe", 0);
1416 # Note: Use a timeout in the call above for not letting the stale processes
1417 # hanging around forever. This can happen if the installed Guest Additions
1418 # do not support terminating guest processes.
1419 except:
1420 reporter.errorXcpt('Opening stale file #%ld failed:' % (i,));
1421 fRc = False;
1422 break;
1423
1424 if fRc:
1425 cFiles = len(self.oTstDrv.oVBoxMgr.getArray(oGuestSession, 'files'));
1426 if cFiles != cStaleFiles:
1427 reporter.error('Test failed: Got %ld stale files, expected %ld' % (cFiles, cStaleFiles));
1428 fRc = False;
1429
1430 if fRc:
1431 #
1432 # Open non-stale files and close them again.
1433 #
1434 reporter.log2('Opening non-stale files');
1435 aaFiles = [];
1436 for i in range(0, cStaleFiles):
1437 try:
1438 oCurFile = oGuestSession.fileOpen(sFile, "r", "oe", 0);
1439 aaFiles.append(oCurFile);
1440 except:
1441 reporter.errorXcpt('Opening non-stale file #%ld failed:' % (i,));
1442 fRc = False;
1443 break;
1444 if fRc:
1445 cFiles = len(self.oTstDrv.oVBoxMgr.getArray(oGuestSession, 'files'));
1446 if cFiles != cStaleFiles * 2:
1447 reporter.error('Test failed: Got %ld total files, expected %ld' % (cFiles, cStaleFiles * 2));
1448 fRc = False;
1449 if fRc:
1450 reporter.log2('Closing all non-stale files again ...');
1451 for i in range(0, cStaleFiles):
1452 try:
1453 aaFiles[i].close();
1454 except:
1455 reporter.errorXcpt('Waiting for non-stale file #%ld failed:' % (i,));
1456 fRc = False;
1457 break;
1458 cFiles = len(self.oTstDrv.oVBoxMgr.getArray(oGuestSession, 'files'));
1459 # Here we count the stale files (that is, files we don't have a reference
1460 # anymore for) and the opened and then closed non-stale files (that we still keep
1461 # a reference in aaFiles[] for).
1462 if cFiles != cStaleFiles:
1463 reporter.error('Test failed: Got %ld total files, expected %ld' \
1464 % (cFiles, cStaleFiles));
1465 fRc = False;
1466 if fRc:
1467 #
1468 # Check if all (referenced) non-stale files now are in "closed" state.
1469 #
1470 reporter.log2('Checking statuses of all non-stale files ...');
1471 for i in range(0, cStaleFiles):
1472 try:
1473 curFilesStatus = aaFiles[i].status;
1474 if curFilesStatus != vboxcon.FileStatus_Closed:
1475 reporter.error('Test failed: Non-stale file #%ld has status %ld, expected %ld' \
1476 % (i, curFilesStatus, vboxcon.FileStatus_Closed));
1477 fRc = False;
1478 except:
1479 reporter.errorXcpt('Checking status of file #%ld failed:' % (i,));
1480 fRc = False;
1481 break;
1482 if fRc:
1483 reporter.log2('All non-stale files closed');
1484 cFiles = len(self.oTstDrv.oVBoxMgr.getArray(oGuestSession, 'files'));
1485 reporter.log2('Final guest session file count: %ld' % (cFiles,));
1486 # Now try to close the session and see what happens.
1487 reporter.log2('Closing guest session ...');
1488 oGuestSession.close();
1489 except:
1490 reporter.errorXcpt('Testing for stale processes failed:');
1491 fRc = False;
1492
1493 return (fRc, oTxsSession);
1494
1495 #def testGuestCtrlSessionDirRefs(self, oSession, oTxsSession, oTestVm):
1496 # """
1497 # Tests the guest session directory reference handling.
1498 # """
1499
1500 # fRc = True;
1501 # return (fRc, oTxsSession);
1502
1503 def testGuestCtrlSessionProcRefs(self, oSession, oTxsSession, oTestVm): # pylint: disable=R0914
1504 """
1505 Tests the guest session process reference handling.
1506 """
1507
1508 if oTestVm.isWindows():
1509 sUser = "Administrator";
1510 sPassword = "password";
1511 sDomain = "";
1512 sCmd = "C:\\windows\\system32\\cmd.exe";
1513 sArgs = [];
1514
1515 # Number of stale guest processes to create.
1516 cStaleProcs = 10;
1517
1518 fRc = True;
1519 try:
1520 oGuest = oSession.o.console.guest;
1521 oGuestSession = oGuest.createSession(sUser, sPassword, sDomain, \
1522 "testGuestCtrlSessionProcRefs");
1523 fWaitFor = [ vboxcon.GuestSessionWaitForFlag_Start ];
1524 waitResult = oGuestSession.waitForArray(fWaitFor, 30 * 1000);
1525 #
1526 # Be nice to Guest Additions < 4.3: They don't support session handling and
1527 # therefore return WaitFlagNotSupported.
1528 #
1529 if waitResult != vboxcon.GuestSessionWaitResult_Start \
1530 and waitResult != vboxcon.GuestSessionWaitResult_WaitFlagNotSupported:
1531 # Just log, don't assume an error here (will be done in the main loop then).
1532 reporter.log('Session did not start successfully, returned wait result: %ld' \
1533 % (waitResult));
1534 return (False, oTxsSession);
1535 reporter.log('Session successfully started');
1536
1537 #
1538 # Fire off forever-running processes and "forget" them (stale entries).
1539 # For them we don't have any references anymore intentionally.
1540 #
1541 reporter.log2('Starting stale processes');
1542 for i in range(0, cStaleProcs):
1543 try:
1544 oGuestSession.processCreate(sCmd, \
1545 sArgs, [], \
1546 [ vboxcon.ProcessCreateFlag_WaitForStdOut ], \
1547 30 * 1000);
1548 # Note: Use a timeout in the call above for not letting the stale processes
1549 # hanging around forever. This can happen if the installed Guest Additions
1550 # do not support terminating guest processes.
1551 except:
1552 reporter.logXcpt('Creating stale process #%ld failed:' % (i,));
1553 fRc = False;
1554 break;
1555
1556 if fRc:
1557 cProcs = len(self.oTstDrv.oVBoxMgr.getArray(oGuestSession, 'processes'));
1558 if cProcs != cStaleProcs:
1559 reporter.error('Test failed: Got %ld stale processes, expected %ld' % (cProcs, cStaleProcs));
1560 fRc = False;
1561
1562 if fRc:
1563 #
1564 # Fire off non-stale processes and wait for termination.
1565 #
1566 if oTestVm.isWindows():
1567 sArgs = [ '/C', 'dir', '/S', 'C:\\Windows\\system'];
1568 reporter.log2('Starting non-stale processes');
1569 aaProcs = [];
1570 for i in range(0, cStaleProcs):
1571 try:
1572 oCurProc = oGuestSession.processCreate(sCmd, \
1573 sArgs, [], \
1574 [], \
1575 0); # Infinite timeout.
1576 aaProcs.append(oCurProc);
1577 except:
1578 reporter.logXcpt('Creating non-stale process #%ld failed:' % (i,));
1579 fRc = False;
1580 break;
1581 if fRc:
1582 reporter.log2('Waiting for non-stale processes to terminate');
1583 for i in range(0, cStaleProcs):
1584 try:
1585 aaProcs[i].waitForArray([ vboxcon.ProcessWaitForFlag_Terminate ], 30 * 1000);
1586 curProcStatus = aaProcs[i].status;
1587 if aaProcs[i].status != vboxcon.ProcessStatus_TerminatedNormally:
1588 reporter.error('Test failed: Waiting for non-stale processes #%ld'
1589 ' resulted in status %ld, expected %ld' \
1590 % (i, curProcStatus, vboxcon.ProcessStatus_TerminatedNormally));
1591 fRc = False;
1592 except:
1593 reporter.logXcpt('Waiting for non-stale process #%ld failed:' % (i,));
1594 fRc = False;
1595 break;
1596 cProcs = len(self.oTstDrv.oVBoxMgr.getArray(oGuestSession, 'processes'));
1597 # Here we count the stale processes (that is, processes we don't have a reference
1598 # anymore for) and the started + terminated non-stale processes (that we still keep
1599 # a reference in aaProcs[] for).
1600 if cProcs != (cStaleProcs * 2):
1601 reporter.error('Test failed: Got %ld total processes, expected %ld' \
1602 % (cProcs, cStaleProcs));
1603 fRc = False;
1604 if fRc:
1605 #
1606 # Check if all (referenced) non-stale processes now are in "terminated" state.
1607 #
1608 for i in range(0, cStaleProcs):
1609 curProcStatus = aaProcs[i].status;
1610 if aaProcs[i].status != vboxcon.ProcessStatus_TerminatedNormally:
1611 reporter.error('Test failed: Non-stale processes #%ld has status %ld, expected %ld' \
1612 % (i, curProcStatus, vboxcon.ProcessStatus_TerminatedNormally));
1613 fRc = False;
1614 if fRc:
1615 reporter.log2('All non-stale processes terminated');
1616
1617 # Fire off blocking processes which are terminated via terminate().
1618 if oTestVm.isWindows():
1619 sArgs = [ '/C', 'dir', '/S', 'C:\\Windows'];
1620 reporter.log2('Starting blocking processes');
1621 aaProcs = [];
1622 for i in range(0, cStaleProcs):
1623 try:
1624 oCurProc = oGuestSession.processCreate(sCmd, \
1625 sArgs, [], \
1626 [], 30 * 1000);
1627 # Note: Use a timeout in the call above for not letting the stale processes
1628 # hanging around forever. This can happen if the installed Guest Additions
1629 # do not support terminating guest processes.
1630 aaProcs.append(oCurProc);
1631 except:
1632 reporter.logXcpt('Creating blocking process failed:');
1633 fRc = False;
1634 break;
1635 if fRc:
1636 reporter.log2('Terminating blocking processes');
1637 for i in range(0, cStaleProcs):
1638 try:
1639 aaProcs[i].terminate();
1640 except: # Termination might not be supported, just skip and log it.
1641 reporter.logXcpt('Termination of blocking process failed, skipped:');
1642 cProcs = len(self.oTstDrv.oVBoxMgr.getArray(oGuestSession, 'processes'));
1643 if cProcs != (cStaleProcs * 2): # Still should be 20 processes because we terminated the 10 newest ones.
1644 reporter.error('Test failed: Got %ld total processes, expected %ld' % (cProcs, cStaleProcs * 2));
1645 fRc = False;
1646 cProcs = len(self.oTstDrv.oVBoxMgr.getArray(oGuestSession, 'processes'));
1647 reporter.log2('Final guest session processes count: %ld' % (cProcs,));
1648 # Now try to close the session and see what happens.
1649 reporter.log2('Closing guest session ...');
1650 oGuestSession.close();
1651 except:
1652 reporter.logXcpt('Testing for stale processes failed:');
1653 fRc = False;
1654
1655 return (fRc, oTxsSession);
1656
1657 def testGuestCtrlExec(self, oSession, oTxsSession, oTestVm): # pylint: disable=R0914,R0915
1658 """
1659 Tests the basic execution feature.
1660 """
1661
1662 if oTestVm.isWindows():
1663 sUser = "Administrator";
1664 else:
1665 sUser = "vbox";
1666 sPassword = "password";
1667
1668 if oTestVm.isWindows():
1669 # Outputting stuff.
1670 sImageOut = "C:\\windows\\system32\\cmd.exe";
1671 else:
1672 reporter.error('Implement me!'); ## @todo Implement non-Windows bits.
1673 return (False, oTxsSession);
1674
1675 aaInvalid = [
1676 # Invalid parameters.
1677 [ tdTestExec(sUser = sUser, sPassword = sPassword),
1678 tdTestResultExec(fRc = False) ],
1679 # Non-existent / invalid image.
1680 [ tdTestExec(sCmd = "non-existent", sUser = sUser, sPassword = sPassword),
1681 tdTestResultExec(fRc = False) ],
1682 [ tdTestExec(sCmd = "non-existent2", sUser = sUser, sPassword = sPassword, fWaitForExit = True),
1683 tdTestResultExec(fRc = False) ],
1684 # Use an invalid format string.
1685 [ tdTestExec(sCmd = "%$%%%&", sUser = sUser, sPassword = sPassword),
1686 tdTestResultExec(fRc = False) ],
1687 # More stuff.
1688 [ tdTestExec(sCmd = "ƒ‰‹ˆ÷‹¸", sUser = sUser, sPassword = sPassword),
1689 tdTestResultExec(fRc = False) ],
1690 [ tdTestExec(sCmd = "???://!!!", sUser = sUser, sPassword = sPassword),
1691 tdTestResultExec(fRc = False) ],
1692 [ tdTestExec(sCmd = "<>!\\", sUser = sUser, sPassword = sPassword),
1693 tdTestResultExec(fRc = False) ]
1694 # Enable as soon as ERROR_BAD_DEVICE is implemented.
1695 #[ tdTestExec(sCmd = "CON", sUser = sUser, sPassword = sPassword),
1696 # tdTestResultExec(fRc = False) ]
1697 ];
1698
1699 if oTestVm.isWindows():
1700 aaExec = [
1701 # Basic executon.
1702 [ tdTestExec(sCmd = sImageOut, aArgs = [ '/C', 'dir', '/S', 'c:\\windows\\system32' ],
1703 sUser = sUser, sPassword = sPassword),
1704 tdTestResultExec(fRc = True) ],
1705 [ tdTestExec(sCmd = sImageOut, aArgs = [ '/C', 'dir', '/S', 'c:\\windows\\system32\\kernel32.dll' ],
1706 sUser = sUser, sPassword = sPassword),
1707 tdTestResultExec(fRc = True) ],
1708 [ tdTestExec(sCmd = sImageOut, aArgs = [ '/C', 'dir', '/S', 'c:\\windows\\system32\\nonexist.dll' ],
1709 sUser = sUser, sPassword = sPassword),
1710 tdTestResultExec(fRc = True, iExitCode = 1) ],
1711 [ tdTestExec(sCmd = sImageOut, aArgs = [ '/C', 'dir', '/S', '/wrongparam' ],
1712 sUser = sUser, sPassword = sPassword),
1713 tdTestResultExec(fRc = True, iExitCode = 1) ],
1714 # Paths with spaces.
1715 ## @todo Get path of installed Guest Additions. Later.
1716 [ tdTestExec(sCmd = "C:\\Program Files\\Oracle\\VirtualBox Guest Additions\\VBoxControl.exe",
1717 aArgs = [ 'version' ],
1718 sUser = sUser, sPassword = sPassword),
1719 tdTestResultExec(fRc = True) ],
1720 # StdOut.
1721 [ tdTestExec(sCmd = sImageOut, aArgs = [ '/C', 'dir', '/S', 'c:\\windows\\system32' ],
1722 sUser = sUser, sPassword = sPassword),
1723 tdTestResultExec(fRc = True) ],
1724 [ tdTestExec(sCmd = sImageOut, aArgs = [ '/C', 'dir', '/S', 'stdout-non-existing' ],
1725 sUser = sUser, sPassword = sPassword),
1726 tdTestResultExec(fRc = True, iExitCode = 1) ],
1727 # StdErr.
1728 [ tdTestExec(sCmd = sImageOut, aArgs = [ '/C', 'dir', '/S', 'c:\\windows\\system32' ],
1729 sUser = sUser, sPassword = sPassword),
1730 tdTestResultExec(fRc = True) ],
1731 [ tdTestExec(sCmd = sImageOut, aArgs = [ '/C', 'dir', '/S', 'stderr-non-existing' ],
1732 sUser = sUser, sPassword = sPassword),
1733 tdTestResultExec(fRc = True, iExitCode = 1) ],
1734 # StdOut + StdErr.
1735 [ tdTestExec(sCmd = sImageOut, aArgs = [ '/C', 'dir', '/S', 'c:\\windows\\system32' ],
1736 sUser = sUser, sPassword = sPassword),
1737 tdTestResultExec(fRc = True) ],
1738 [ tdTestExec(sCmd = sImageOut, aArgs = [ '/C', 'dir', '/S', 'stdouterr-non-existing' ],
1739 sUser = sUser, sPassword = sPassword),
1740 tdTestResultExec(fRc = True, iExitCode = 1) ]
1741 # FIXME: Failing tests.
1742 # Environment variables.
1743 # [ tdTestExec(sCmd = sImageOut, aArgs = [ '/C', 'set', 'TEST_NONEXIST' ],
1744 # sUser = sUser, sPassword = sPassword),
1745 # tdTestResultExec(fRc = True, iExitCode = 1) ]
1746 # [ tdTestExec(sCmd = sImageOut, aArgs = [ '/C', 'set', 'windir' ],
1747 # sUser = sUser, sPassword = sPassword,
1748 # aFlags = [ vboxcon.ProcessCreateFlag_WaitForStdOut, vboxcon.ProcessCreateFlag_WaitForStdErr ]),
1749 # tdTestResultExec(fRc = True, sBuf = 'windir=C:\\WINDOWS\r\n') ],
1750 # [ tdTestExec(sCmd = sImageOut, aArgs = [ '/C', 'set', 'TEST_FOO' ],
1751 # sUser = sUser, sPassword = sPassword,
1752 # aEnv = [ 'TEST_FOO=BAR' ],
1753 # aFlags = [ vboxcon.ProcessCreateFlag_WaitForStdOut, vboxcon.ProcessCreateFlag_WaitForStdErr ]),
1754 # tdTestResultExec(fRc = True, sBuf = 'TEST_FOO=BAR\r\n') ],
1755 # [ tdTestExec(sCmd = sImageOut, aArgs = [ '/C', 'set', 'TEST_FOO' ],
1756 # sUser = sUser, sPassword = sPassword,
1757 # aEnv = [ 'TEST_FOO=BAR', 'TEST_BAZ=BAR' ],
1758 # aFlags = [ vboxcon.ProcessCreateFlag_WaitForStdOut, vboxcon.ProcessCreateFlag_WaitForStdErr ]),
1759 # tdTestResultExec(fRc = True, sBuf = 'TEST_FOO=BAR\r\n') ]
1760
1761 ## @todo Create some files (or get files) we know the output size of to validate output length!
1762 ## @todo Add task which gets killed at some random time while letting the guest output something.
1763 ];
1764
1765 # Manual test, not executed automatically.
1766 aaManual = [
1767 [ tdTestExec(sCmd = sImageOut, aArgs = [ '/C', 'dir /S C:\\Windows' ],
1768 sUser = sUser, sPassword = sPassword,
1769 aFlags = [ vboxcon.ProcessCreateFlag_WaitForStdOut, vboxcon.ProcessCreateFlag_WaitForStdErr ]),
1770 tdTestResultExec(fRc = True, cbStdOut = 497917) ] ];
1771 else:
1772 reporter.log('No OS-specific tests for non-Windows yet!');
1773
1774 # Build up the final test array for the first batch.
1775 aaTests = [];
1776 aaTests.extend(aaInvalid);
1777 if aaExec is not None:
1778 aaTests.extend(aaExec);
1779 fRc = True;
1780
1781 #
1782 # Single execution stuff. Nice for debugging.
1783 #
1784 fManual = False;
1785 if fManual:
1786 curTest = aaTests[1][0]; # tdTestExec, use an index, later.
1787 curRes = aaTests[1][1]; # tdTestResultExec
1788 curTest.setEnvironment(oSession, oTxsSession, oTestVm);
1789 fRc, curGuestSession = curTest.createSession('testGuestCtrlExec: Single test 1');
1790 if fRc is False:
1791 reporter.error('Single test failed: Could not create session');
1792 else:
1793 fRc = self.gctrlExecDoTest(0, curTest, curRes, curGuestSession);
1794 curTest.closeSession();
1795
1796 curTest = aaTests[2][0]; # tdTestExec, use an index, later.
1797 curRes = aaTests[2][1]; # tdTestResultExec
1798 curTest.setEnvironment(oSession, oTxsSession, oTestVm);
1799 fRc, curGuestSession = curTest.createSession('testGuestCtrlExec: Single test 2');
1800 if fRc is False:
1801 reporter.error('Single test failed: Could not create session');
1802 else:
1803 fRc = self.gctrlExecDoTest(0, curTest, curRes, curGuestSession);
1804 curTest.closeSession();
1805
1806 curTest = aaTests[3][0]; # tdTestExec, use an index, later.
1807 curRes = aaTests[3][1]; # tdTestResultExec
1808 curTest.setEnvironment(oSession, oTxsSession, oTestVm);
1809 fRc, curGuestSession = curTest.createSession('testGuestCtrlExec: Single test 3');
1810 if fRc is False:
1811 reporter.error('Single test failed: Could not create session');
1812 else:
1813 fRc = self.gctrlExecDoTest(0, curTest, curRes, curGuestSession);
1814 curTest.closeSession();
1815 return (fRc, oTxsSession);
1816 else:
1817 aaManual = aaManual; # Workaround for pylint #W0612.
1818
1819 if fRc is False:
1820 return (fRc, oTxsSession);
1821
1822 #
1823 # First batch: One session per guest process.
1824 #
1825 for (i, aTest) in enumerate(aaTests):
1826 curTest = aTest[0]; # tdTestExec, use an index, later.
1827 curRes = aTest[1]; # tdTestResultExec
1828 curTest.setEnvironment(oSession, oTxsSession, oTestVm);
1829 fRc, curGuestSession = curTest.createSession('testGuestCtrlExec: Test #%d' % (i,));
1830 if fRc is False:
1831 reporter.error('Test #%d failed: Could not create session' % (i,));
1832 break;
1833 fRc = self.gctrlExecDoTest(i, curTest, curRes, curGuestSession);
1834 if fRc is False:
1835 break;
1836 fRc = curTest.closeSession();
1837 if fRc is False:
1838 break;
1839
1840 # No sessions left?
1841 if fRc is True:
1842 cSessions = len(self.oTstDrv.oVBoxMgr.getArray(oSession.o.console.guest, 'sessions'));
1843 if cSessions is not 0:
1844 reporter.error('Found %d stale session(s), expected 0' % (cSessions,));
1845 fRc = False;
1846
1847 if fRc is False:
1848 return (fRc, oTxsSession);
1849
1850 reporter.log('Now using one guest session for all tests ...');
1851
1852 #
1853 # Second batch: One session for *all* guest processes.
1854 #
1855 oGuest = oSession.o.console.guest;
1856 try:
1857 reporter.log('Creating session for all tests ...');
1858 curGuestSession = oGuest.createSession(sUser, sPassword, '', 'testGuestCtrlExec: One session for all tests');
1859 try:
1860 fWaitFor = [ vboxcon.GuestSessionWaitForFlag_Start ];
1861 waitResult = curGuestSession.waitForArray(fWaitFor, 30 * 1000);
1862 if waitResult != vboxcon.GuestSessionWaitResult_Start \
1863 and waitResult != vboxcon.GuestSessionWaitResult_WaitFlagNotSupported:
1864 reporter.error('Session did not start successfully, returned wait result: %ld' \
1865 % (waitResult));
1866 return (False, oTxsSession);
1867 reporter.log('Session successfully started');
1868 except:
1869 # Just log, don't assume an error here (will be done in the main loop then).
1870 reporter.logXcpt('Waiting for guest session to start failed:');
1871 return (False, oTxsSession);
1872 # Note: Not waiting for the guest session to start here
1873 # is intentional. This must be handled by the process execution
1874 # call then.
1875 for (i, aTest) in enumerate(aaTests):
1876 curTest = aTest[0]; # tdTestExec, use an index, later.
1877 curRes = aTest[1]; # tdTestResultExec
1878 curTest.setEnvironment(oSession, oTxsSession, oTestVm);
1879 fRc = self.gctrlExecDoTest(i, curTest, curRes, curGuestSession);
1880 if fRc is False:
1881 break;
1882 try:
1883 reporter.log2('Closing guest session ...');
1884 curGuestSession.close();
1885 curGuestSession = None;
1886 except:
1887 # Just log, don't assume an error here (will be done in the main loop then).
1888 reporter.logXcpt('Closing guest session failed:');
1889 fRc = False;
1890 except:
1891 reporter.logXcpt('Could not create one session:');
1892
1893 # No sessions left?
1894 if fRc is True:
1895 cSessions = len(self.oTstDrv.oVBoxMgr.getArray(oSession.o.console.guest, 'sessions'));
1896 if cSessions is not 0:
1897 reporter.error('Found %d stale session(s), expected 0' % (cSessions,));
1898 fRc = False;
1899
1900 return (fRc, oTxsSession);
1901
1902 def testGuestCtrlExecErrorLevel(self, oSession, oTxsSession, oTestVm):
1903 """
1904 Tests handling of error levels from started guest processes.
1905 """
1906
1907 if oTestVm.isWindows():
1908 sUser = "Administrator";
1909 else:
1910 sUser = "vbox";
1911 sPassword = "password";
1912
1913 if oTestVm.isWindows():
1914 # Outputting stuff.
1915 sImage = "C:\\windows\\system32\\cmd.exe";
1916 else:
1917 reporter.error('Implement me!'); ## @todo Implement non-Windows bits.
1918 return (False, oTxsSession);
1919
1920 aaTests = [];
1921 if oTestVm.isWindows():
1922 aaTests.extend([
1923 # Simple.
1924 [ tdTestExec(sCmd = sImage, aArgs = [ '/C', 'wrongcommand' ],
1925 sUser = sUser, sPassword = sPassword),
1926 tdTestResultExec(fRc = True, iExitCode = 1) ],
1927 [ tdTestExec(sCmd = sImage, aArgs = [ '/C', 'exit', '22' ],
1928 sUser = sUser, sPassword = sPassword),
1929 tdTestResultExec(fRc = True, iExitCode = 22) ],
1930 [ tdTestExec(sCmd = sImage, aArgs = [ '/C', 'set', 'ERRORLEVEL=234' ],
1931 sUser = sUser, sPassword = sPassword),
1932 tdTestResultExec(fRc = True, iExitCode = 0) ],
1933 [ tdTestExec(sCmd = sImage, aArgs = [ '/C', 'echo', '%WINDIR%' ],
1934 sUser = sUser, sPassword = sPassword),
1935 tdTestResultExec(fRc = True, iExitCode = 0) ],
1936 [ tdTestExec(sCmd = sImage, aArgs = [ '/C', 'set', 'ERRORLEVEL=0' ],
1937 sUser = sUser, sPassword = sPassword),
1938 tdTestResultExec(fRc = True, iExitCode = 0) ],
1939 [ tdTestExec(sCmd = sImage, aArgs = [ '/C', 'dir', 'c:\\windows\\system32' ],
1940 sUser = sUser, sPassword = sPassword),
1941 tdTestResultExec(fRc = True, iExitCode = 0) ],
1942 [ tdTestExec(sCmd = sImage, aArgs = [ '/C', 'dir', 'c:\\windows\\system32\\kernel32.dll' ],
1943 sUser = sUser, sPassword = sPassword),
1944 tdTestResultExec(fRc = True, iExitCode = 0) ],
1945 [ tdTestExec(sCmd = sImage, aArgs = [ '/C', 'dir', 'c:\\nonexisting-file' ],
1946 sUser = sUser, sPassword = sPassword),
1947 tdTestResultExec(fRc = True, iExitCode = 1) ],
1948 [ tdTestExec(sCmd = sImage, aArgs = [ '/C', 'dir', 'c:\\nonexisting-dir\\' ],
1949 sUser = sUser, sPassword = sPassword),
1950 tdTestResultExec(fRc = True, iExitCode = 1) ]
1951 # FIXME: Failing tests.
1952 # With stdout.
1953 # [ tdTestExec(sCmd = sImage, aArgs = [ '/C', 'dir', 'c:\\windows\\system32' ],
1954 # sUser = sUser, sPassword = sPassword, aFlags = [ vboxcon.ProcessCreateFlag_WaitForStdOut ]),
1955 # tdTestResultExec(fRc = True, iExitCode = 0) ],
1956 # [ tdTestExec(sCmd = sImage, aArgs = [ '/C', 'dir', 'c:\\nonexisting-file' ],
1957 # sUser = sUser, sPassword = sPassword, aFlags = [ vboxcon.ProcessCreateFlag_WaitForStdOut ]),
1958 # tdTestResultExec(fRc = True, iExitCode = 1) ],
1959 # [ tdTestExec(sCmd = sImage, aArgs = [ '/C', 'dir', 'c:\\nonexisting-dir\\' ],
1960 # sUser = sUser, sPassword = sPassword, aFlags = [ vboxcon.ProcessCreateFlag_WaitForStdOut ]),
1961 # tdTestResultExec(fRc = True, iExitCode = 1) ],
1962 # With stderr.
1963 # [ tdTestExec(sCmd = sImage, aArgs = [ '/C', 'dir', 'c:\\windows\\system32' ],
1964 # sUser = sUser, sPassword = sPassword, aFlags = [ vboxcon.ProcessCreateFlag_WaitForStdErr ]),
1965 # tdTestResultExec(fRc = True, iExitCode = 0) ],
1966 # [ tdTestExec(sCmd = sImage, aArgs = [ '/C', 'dir', 'c:\\nonexisting-file' ],
1967 # sUser = sUser, sPassword = sPassword, aFlags = [ vboxcon.ProcessCreateFlag_WaitForStdErr ]),
1968 # tdTestResultExec(fRc = True, iExitCode = 1) ],
1969 # [ tdTestExec(sCmd = sImage, aArgs = [ '/C', 'dir', 'c:\\nonexisting-dir\\' ],
1970 # sUser = sUser, sPassword = sPassword, aFlags = [ vboxcon.ProcessCreateFlag_WaitForStdErr ]),
1971 # tdTestResultExec(fRc = True, iExitCode = 1) ],
1972 # With stdout/stderr.
1973 # [ tdTestExec(sCmd = sImage, aArgs = [ '/C', 'dir', 'c:\\windows\\system32' ],
1974 # sUser = sUser, sPassword = sPassword,
1975 # aFlags = [ vboxcon.ProcessCreateFlag_WaitForStdOut, vboxcon.ProcessCreateFlag_WaitForStdErr ]),
1976 # tdTestResultExec(fRc = True, iExitCode = 0) ],
1977 # [ tdTestExec(sCmd = sImage, aArgs = [ '/C', 'dir', 'c:\\nonexisting-file' ],
1978 # sUser = sUser, sPassword = sPassword,
1979 # aFlags = [ vboxcon.ProcessCreateFlag_WaitForStdOut, vboxcon.ProcessCreateFlag_WaitForStdErr ]),
1980 # tdTestResultExec(fRc = True, iExitCode = 1) ],
1981 # [ tdTestExec(sCmd = sImage, aArgs = [ '/C', 'dir', 'c:\\nonexisting-dir\\' ],
1982 # sUser = sUser, sPassword = sPassword,
1983 # aFlags = [ vboxcon.ProcessCreateFlag_WaitForStdOut, vboxcon.ProcessCreateFlag_WaitForStdErr ]),
1984 # tdTestResultExec(fRc = True, iExitCode = 1) ]
1985 ## @todo Test stdin!
1986 ]);
1987 else:
1988 reporter.log('No OS-specific tests for non-Windows yet!');
1989
1990 fRc = True;
1991 for (i, aTest) in enumerate(aaTests):
1992 curTest = aTest[0]; # tdTestExec, use an index, later.
1993 curRes = aTest[1]; # tdTestResult
1994 curTest.setEnvironment(oSession, oTxsSession, oTestVm);
1995 fRc, curGuestSession = curTest.createSession('testGuestCtrlExecErrorLevel: Test #%d' % (i,));
1996 if fRc is False:
1997 reporter.error('Test #%d failed: Could not create session' % (i,));
1998 break;
1999 fRc = self.gctrlExecDoTest(i, curTest, curRes, curGuestSession);
2000 curTest.closeSession();
2001 if fRc is False:
2002 break;
2003
2004 return (fRc, oTxsSession);
2005
2006 def testGuestCtrlExecTimeout(self, oSession, oTxsSession, oTestVm):
2007 """
2008 Tests handling of timeouts of started guest processes.
2009 """
2010
2011 if oTestVm.isWindows():
2012 sUser = "Administrator";
2013 else:
2014 sUser = "vbox";
2015 sPassword = "password";
2016 sDomain = "";
2017
2018 if oTestVm.isWindows():
2019 # Outputting stuff.
2020 sImage = "C:\\windows\\system32\\cmd.exe";
2021 else:
2022 reporter.error('Implement me!'); ## @todo Implement non-Windows bits.
2023 return (False, oTxsSession);
2024
2025 fRc = True;
2026 try:
2027 oGuest = oSession.o.console.guest;
2028 oGuestSession = oGuest.createSession(sUser, sPassword, sDomain, "testGuestCtrlExecTimeout");
2029 oGuestSession.waitForArray([ vboxcon.GuestSessionWaitForFlag_Start ], 30 * 1000);
2030 # Create a process which never terminates and should timeout when
2031 # waiting for termination.
2032 try:
2033 curProc = oGuestSession.processCreate(sImage, [], \
2034 [], [], 30 * 1000);
2035 reporter.log('Waiting for process 1 being started ...');
2036 waitRes = curProc.waitForArray([ vboxcon.ProcessWaitForFlag_Start ], 30 * 1000);
2037 if waitRes != vboxcon.ProcessWaitResult_Start:
2038 reporter.error('Waiting for process 1 to start failed, got status %ld');
2039 fRc = False;
2040 if fRc:
2041 reporter.log('Waiting for process 1 to time out within 1ms ...');
2042 waitRes = curProc.waitForArray([ vboxcon.ProcessWaitForFlag_Terminate ], 1);
2043 if waitRes != vboxcon.ProcessWaitResult_Timeout:
2044 reporter.error('Waiting for process 1 did not time out when it should (1)');
2045 fRc = False;
2046 else:
2047 reporter.log('Waiting for process 1 timed out (1), good');
2048 if fRc:
2049 reporter.log('Waiting for process 1 to time out within 5000ms ...');
2050 waitRes = curProc.waitForArray([ vboxcon.ProcessWaitForFlag_Terminate ], 5000);
2051 if waitRes != vboxcon.ProcessWaitResult_Timeout:
2052 reporter.error('Waiting for process 1 did not time out when it should, got wait result %ld' % (waitRes,));
2053 fRc = False;
2054 else:
2055 reporter.log('Waiting for process 1 timed out (5000), good');
2056 ## @todo Add curProc.terminate() as soon as it's implemented.
2057 except:
2058 reporter.errorXcpt('Exception for process 1:');
2059 fRc = False;
2060 # Create a lengthly running guest process which will be killed by VBoxService on the
2061 # guest because it ran out of execution time (5 seconds).
2062 if fRc:
2063 try:
2064 curProc = oGuestSession.processCreate(sImage, [], \
2065 [], [], 5 * 1000);
2066 reporter.log('Waiting for process 2 being started ...');
2067 waitRes = curProc.waitForArray([ vboxcon.ProcessWaitForFlag_Start ], 30 * 1000);
2068 if waitRes != vboxcon.ProcessWaitResult_Start:
2069 reporter.error('Waiting for process 1 to start failed, got status %ld');
2070 fRc = False;
2071 if fRc:
2072 reporter.log('Waiting for process 2 to get killed because it ran out of execution time ...');
2073 waitRes = curProc.waitForArray([ vboxcon.ProcessWaitForFlag_Terminate ], 30 * 1000);
2074 if waitRes != vboxcon.ProcessWaitResult_Timeout:
2075 reporter.error('Waiting for process 2 did not time out when it should, got wait result %ld' \
2076 % (waitRes,));
2077 fRc = False;
2078 if fRc:
2079 reporter.log('Waiting for process 2 indicated an error, good');
2080 if curProc.status != vboxcon.ProcessStatus_TimedOutKilled:
2081 reporter.error('Status of process 2 wrong; excepted %ld, got %ld' \
2082 % (vboxcon.ProcessStatus_TimedOutKilled, curProc.status));
2083 fRc = False;
2084 else:
2085 reporter.log('Status of process 2 correct (%ld)' % (vboxcon.ProcessStatus_TimedOutKilled,));
2086 ## @todo Add curProc.terminate() as soon as it's implemented.
2087 except:
2088 reporter.errorXcpt('Exception for process 2:');
2089 fRc = False;
2090 oGuestSession.close();
2091 except:
2092 reporter.errorXcpt('Could not handle session:');
2093 fRc = False;
2094
2095 return (fRc, oTxsSession);
2096
2097 def testGuestCtrlDirCreate(self, oSession, oTxsSession, oTestVm):
2098 """
2099 Tests creation of guest directories.
2100 """
2101
2102 if oTestVm.isWindows():
2103 sUser = "Administrator";
2104 else:
2105 sUser = "vbox";
2106 sPassword = "password";
2107
2108 if oTestVm.isWindows():
2109 sScratch = "C:\\Temp\\vboxtest\\testGuestCtrlDirCreate\\";
2110
2111 aaTests = [];
2112 if oTestVm.isWindows():
2113 aaTests.extend([
2114 # Invalid stuff.
2115 [ tdTestDirCreate(sUser = sUser, sPassword = sPassword, sDirectory = '' ),
2116 tdTestResult(fRc = False) ],
2117 # More unusual stuff.
2118 [ tdTestDirCreate(sUser = sUser, sPassword = sPassword, sDirectory = '..\\..\\' ),
2119 tdTestResult(fRc = False) ],
2120 [ tdTestDirCreate(sUser = sUser, sPassword = sPassword, sDirectory = '../../' ),
2121 tdTestResult(fRc = False) ],
2122 [ tdTestDirCreate(sUser = sUser, sPassword = sPassword, sDirectory = 'z:\\' ),
2123 tdTestResult(fRc = False) ],
2124 [ tdTestDirCreate(sUser = sUser, sPassword = sPassword, sDirectory = '\\\\uncrulez\\foo' ),
2125 tdTestResult(fRc = False) ],
2126 # Creating directories.
2127 [ tdTestDirCreate(sUser = sUser, sPassword = sPassword, sDirectory = sScratch ),
2128 tdTestResult(fRc = False) ],
2129 [ tdTestDirCreate(sUser = sUser, sPassword = sPassword, sDirectory = os.path.join(sScratch, 'foo\\bar\\baz'),
2130 aFlags = [ vboxcon.DirectoryCreateFlag_Parents ] ),
2131 tdTestResult(fRc = True) ],
2132 [ tdTestDirCreate(sUser = sUser, sPassword = sPassword, sDirectory = os.path.join(sScratch, 'foo\\bar\\baz'),
2133 aFlags = [ vboxcon.DirectoryCreateFlag_Parents ] ),
2134 tdTestResult(fRc = True) ],
2135 # Long (+ random) stuff.
2136 [ tdTestDirCreate(sUser = sUser, sPassword = sPassword,
2137 sDirectory = os.path.join(sScratch,
2138 "".join(random.choice(string.ascii_lowercase) for i in range(32))) ),
2139 tdTestResult(fRc = True) ],
2140 [ tdTestDirCreate(sUser = sUser, sPassword = sPassword,
2141 sDirectory = os.path.join(sScratch,
2142 "".join(random.choice(string.ascii_lowercase) for i in range(128))) ),
2143 tdTestResult(fRc = True) ],
2144 # Following two should fail on Windows (paths too long). Both should timeout.
2145 [ tdTestDirCreate(sUser = sUser, sPassword = sPassword,
2146 sDirectory = os.path.join(sScratch,
2147 "".join(random.choice(string.ascii_lowercase) for i in range(255))) ),
2148 tdTestResult(fRc = False) ],
2149 [ tdTestDirCreate(sUser = sUser, sPassword = sPassword,
2150 sDirectory = os.path.join(sScratch,
2151 "".join(random.choice(string.ascii_lowercase) for i in range(1024))) ),
2152 tdTestResult(fRc = False) ]
2153 ]);
2154 else:
2155 reporter.log('No OS-specific tests for non-Windows yet!');
2156
2157 fRc = True;
2158 for (i, aTest) in enumerate(aaTests):
2159 curTest = aTest[0]; # tdTestExec, use an index, later.
2160 curRes = aTest[1]; # tdTestResult
2161 reporter.log('Testing #%d, sDirectory="%s" ...' % (i, curTest.sDirectory));
2162 curTest.setEnvironment(oSession, oTxsSession, oTestVm);
2163 fRc, curGuestSession = curTest.createSession('testGuestCtrlDirCreate: Test #%d' % (i,));
2164 if fRc is False:
2165 reporter.error('Test #%d failed: Could not create session' % (i,));
2166 break;
2167 fRc = self.gctrlCreateDir(curTest, curRes, curGuestSession);
2168 curTest.closeSession();
2169 if fRc is False:
2170 reporter.error('Test #%d failed' % (i,));
2171 fRc = False;
2172 break;
2173
2174 return (fRc, oTxsSession);
2175
2176 def testGuestCtrlDirCreateTemp(self, oSession, oTxsSession, oTestVm): # pylint: disable=R0914
2177 """
2178 Tests creation of temporary directories.
2179 """
2180
2181 if oTestVm.isWindows():
2182 sUser = "Administrator";
2183 else:
2184 sUser = "vbox";
2185 sPassword = "password";
2186
2187 # if oTestVm.isWindows():
2188 # sScratch = "C:\\Temp\\vboxtest\\testGuestCtrlDirCreateTemp\\";
2189
2190 aaTests = [];
2191 if oTestVm.isWindows():
2192 aaTests.extend([
2193 # Invalid stuff.
2194 [ tdTestDirCreateTemp(sUser = sUser, sPassword = sPassword, sDirectory = ''),
2195 tdTestResult(fRc = False) ],
2196 [ tdTestDirCreateTemp(sUser = sUser, sPassword = sPassword, sDirectory = 'C:\\Windows',
2197 fMode = 1234),
2198 tdTestResult(fRc = False) ],
2199 [ tdTestDirCreateTemp(sUser = sUser, sPassword = sPassword, sTemplate = '',
2200 sDirectory = 'C:\\Windows', fMode = 1234),
2201 tdTestResult(fRc = False) ],
2202 [ tdTestDirCreateTemp(sUser = sUser, sPassword = sPassword, sTemplate = 'xXx',
2203 sDirectory = 'C:\\Windows', fMode = 0700),
2204 tdTestResult(fRc = False) ],
2205 [ tdTestDirCreateTemp(sUser = sUser, sPassword = sPassword, sTemplate = 'xxx',
2206 sDirectory = 'C:\\Windows', fMode = 0700),
2207 tdTestResult(fRc = False) ],
2208 # More unusual stuff.
2209 [ tdTestDirCreateTemp(sUser = sUser, sPassword = sPassword, sTemplate = 'foo',
2210 sDirectory = 'z:\\'),
2211 tdTestResult(fRc = False) ],
2212 [ tdTestDirCreateTemp(sUser = sUser, sPassword = sPassword, sTemplate = 'foo',
2213 sDirectory = '\\\\uncrulez\\foo'),
2214 tdTestResult(fRc = False) ],
2215 # Non-existing stuff.
2216 [ tdTestDirCreateTemp(sUser = sUser, sPassword = sPassword, sTemplate = 'bar',
2217 sDirectory = 'c:\\Apps\\nonexisting\\foo'),
2218 tdTestResult(fRc = False) ],
2219 # FIXME: Failing test. Non Windows path
2220 # [ tdTestDirCreateTemp(sUser = sUser, sPassword = sPassword, sTemplate = 'bar',
2221 # sDirectory = '/tmp/non/existing'),
2222 # tdTestResult(fRc = False) ]
2223 ]);
2224 else:
2225 reporter.log('No OS-specific tests for non-Windows yet!');
2226
2227 # FIXME: Failing tests.
2228 # aaTests.extend([
2229 # Non-secure variants.
2230 # [ tdTestDirCreateTemp(sUser = sUser, sPassword = sPassword, sTemplate = 'XXX',
2231 # sDirectory = sScratch),
2232 # tdTestResult(fRc = True) ],
2233 # [ tdTestDirCreateTemp(sUser = sUser, sPassword = sPassword, sTemplate = 'XXX',
2234 # sDirectory = sScratch),
2235 # tdTestResult(fRc = True) ],
2236 # [ tdTestDirCreateTemp(sUser = sUser, sPassword = sPassword, sTemplate = 'X',
2237 # sDirectory = sScratch),
2238 # tdTestResult(fRc = True) ],
2239 # [ tdTestDirCreateTemp(sUser = sUser, sPassword = sPassword, sTemplate = 'X',
2240 # sDirectory = sScratch),
2241 # tdTestResult(fRc = True) ],
2242 # [ tdTestDirCreateTemp(sUser = sUser, sPassword = sPassword, sTemplate = 'XXX',
2243 # sDirectory = sScratch,
2244 # fMode = 0700),
2245 # tdTestResult(fRc = True) ],
2246 # [ tdTestDirCreateTemp(sUser = sUser, sPassword = sPassword, sTemplate = 'XXX',
2247 # sDirectory = sScratch,
2248 # fMode = 0700),
2249 # tdTestResult(fRc = True) ],
2250 # [ tdTestDirCreateTemp(sUser = sUser, sPassword = sPassword, sTemplate = 'XXX',
2251 # sDirectory = sScratch,
2252 # fMode = 0755),
2253 # tdTestResult(fRc = True) ],
2254 # [ tdTestDirCreateTemp(sUser = sUser, sPassword = sPassword, sTemplate = 'XXX',
2255 # sDirectory = sScratch,
2256 # fMode = 0755),
2257 # tdTestResult(fRc = True) ],
2258 # Secure variants.
2259 # [ tdTestDirCreateTemp(sUser = sUser, sPassword = sPassword, sTemplate = 'XXX',
2260 # sDirectory = sScratch, fSecure = True),
2261 # tdTestResult(fRc = True) ],
2262 # [ tdTestDirCreateTemp(sUser = sUser, sPassword = sPassword, sTemplate = 'XXX',
2263 # sDirectory = sScratch, fSecure = True),
2264 # tdTestResult(fRc = True) ],
2265 # [ tdTestDirCreateTemp(sUser = sUser, sPassword = sPassword, sTemplate = 'XXX',
2266 # sDirectory = sScratch, fSecure = True),
2267 # tdTestResult(fRc = True) ],
2268 # [ tdTestDirCreateTemp(sUser = sUser, sPassword = sPassword, sTemplate = 'XXX',
2269 # sDirectory = sScratch, fSecure = True),
2270 # tdTestResult(fRc = True) ],
2271 # [ tdTestDirCreateTemp(sUser = sUser, sPassword = sPassword, sTemplate = 'XXX',
2272 # sDirectory = sScratch,
2273 # fSecure = True, fMode = 0700),
2274 # tdTestResult(fRc = True) ],
2275 # [ tdTestDirCreateTemp(sUser = sUser, sPassword = sPassword, sTemplate = 'XXX',
2276 # sDirectory = sScratch,
2277 # fSecure = True, fMode = 0700),
2278 # tdTestResult(fRc = True) ],
2279 # [ tdTestDirCreateTemp(sUser = sUser, sPassword = sPassword, sTemplate = 'XXX',
2280 # sDirectory = sScratch,
2281 # fSecure = True, fMode = 0755),
2282 # tdTestResult(fRc = True) ],
2283 # [ tdTestDirCreateTemp(sUser = sUser, sPassword = sPassword, sTemplate = 'XXX',
2284 # sDirectory = sScratch,
2285 # fSecure = True, fMode = 0755),
2286 # tdTestResult(fRc = True) ],
2287 # Random stuff.
2288 # [ tdTestDirCreateTemp(sUser = sUser, sPassword = sPassword,
2289 # sTemplate = "XXX-".join(random.choice(string.ascii_lowercase) for i in range(32)),
2290 # sDirectory = sScratch,
2291 # fSecure = True, fMode = 0755),
2292 # tdTestResult(fRc = True) ],
2293 # [ tdTestDirCreateTemp(sUser = sUser, sPassword = sPassword, sTemplate = "".join('X' for i in range(32)),
2294 # sDirectory = sScratch,
2295 # fSecure = True, fMode = 0755),
2296 # tdTestResult(fRc = True) ],
2297 # [ tdTestDirCreateTemp(sUser = sUser, sPassword = sPassword, sTemplate = "".join('X' for i in range(128)),
2298 # sDirectory = sScratch,
2299 # fSecure = True, fMode = 0755),
2300 # tdTestResult(fRc = True) ]
2301 # ]);
2302
2303 fRc = True;
2304 for (i, aTest) in enumerate(aaTests):
2305 curTest = aTest[0]; # tdTestExec, use an index, later.
2306 curRes = aTest[1]; # tdTestResult
2307 reporter.log('Testing #%d, sTemplate="%s", fMode=%#o, path="%s", secure="%s" ...' %
2308 (i, curTest.sTemplate, curTest.fMode, curTest.sDirectory, curTest.fSecure));
2309 curTest.setEnvironment(oSession, oTxsSession, oTestVm);
2310 fRc, curGuestSession = curTest.createSession('testGuestCtrlDirCreateTemp: Test #%d' % (i,));
2311 if fRc is False:
2312 reporter.error('Test #%d failed: Could not create session' % (i,));
2313 break;
2314 sDirTemp = "";
2315 try:
2316 sDirTemp = curGuestSession.directoryCreateTemp(curTest.sTemplate, curTest.fMode,
2317 curTest.sDirectory, curTest.fSecure);
2318 except:
2319 if curRes.fRc is True:
2320 reporter.errorXcpt('Creating temp directory "%s" failed:' % (curTest.sDirectory,));
2321 fRc = False;
2322 break;
2323 else:
2324 reporter.logXcpt('Creating temp directory "%s" failed expectedly, skipping:' % (curTest.sDirectory,));
2325 curTest.closeSession();
2326 if sDirTemp != "":
2327 reporter.log2('Temporary directory is: %s' % (sDirTemp,));
2328 fExists = curGuestSession.directoryExists(sDirTemp);
2329 if fExists is False:
2330 reporter.error('Test #%d failed: Temporary directory "%s" does not exists' % (i, sDirTemp));
2331 fRc = False;
2332 break;
2333 return (fRc, oTxsSession);
2334
2335 def testGuestCtrlDirRead(self, oSession, oTxsSession, oTestVm): # pylint: disable=R0914
2336 """
2337 Tests opening and reading (enumerating) guest directories.
2338 """
2339
2340 if oTestVm.isWindows():
2341 sUser = "Administrator";
2342 else:
2343 sUser = "vbox";
2344 sPassword = "password";
2345
2346 aaTests = [];
2347 if oTestVm.isWindows():
2348 aaTests.extend([
2349 # Invalid stuff.
2350 [ tdTestDirRead(sUser = sUser, sPassword = sPassword, sDirectory = ''),
2351 tdTestResultDirRead(fRc = False) ],
2352 [ tdTestDirRead(sUser = sUser, sPassword = sPassword, sDirectory = 'C:\\Windows', aFlags = [ 1234 ]),
2353 tdTestResultDirRead(fRc = False) ],
2354 [ tdTestDirRead(sUser = sUser, sPassword = sPassword, sDirectory = 'C:\\Windows', sFilter = '*.foo'),
2355 tdTestResultDirRead(fRc = False) ],
2356 # More unusual stuff.
2357 [ tdTestDirRead(sUser = sUser, sPassword = sPassword, sDirectory = 'z:\\'),
2358 tdTestResultDirRead(fRc = False) ],
2359 [ tdTestDirRead(sUser = sUser, sPassword = sPassword, sDirectory = '\\\\uncrulez\\foo'),
2360 tdTestResultDirRead(fRc = False) ],
2361 # Non-existing stuff.
2362 [ tdTestDirRead(sUser = sUser, sPassword = sPassword, sDirectory = 'c:\\Apps\\nonexisting'),
2363 tdTestResultDirRead(fRc = False) ],
2364 [ tdTestDirRead(sUser = sUser, sPassword = sPassword, sDirectory = 'c:\\Apps\\testDirRead'),
2365 tdTestResultDirRead(fRc = False) ]
2366 ]);
2367
2368 if oTestVm.sVmName == 'tst-xppro':
2369 aaTests.extend([
2370 # Reading directories.
2371 [ tdTestDirRead(sUser = sUser, sPassword = sPassword, sDirectory = '../../Windows/Fonts'),
2372 tdTestResultDirRead(fRc = True, numFiles = 191) ],
2373 [ tdTestDirRead(sUser = sUser, sPassword = sPassword, sDirectory = 'c:\\Windows\\Help'),
2374 tdTestResultDirRead(fRc = True, numDirs = 13, numFiles = 569) ],
2375 [ tdTestDirRead(sUser = sUser, sPassword = sPassword, sDirectory = 'c:\\Windows\\Web'),
2376 tdTestResultDirRead(fRc = True, numDirs = 3, numFiles = 55) ]
2377 ]);
2378 else:
2379 reporter.log('No OS-specific tests for non-Windows yet!');
2380
2381 fRc = True;
2382 for (i, aTest) in enumerate(aaTests):
2383 curTest = aTest[0]; # tdTestExec, use an index, later.
2384 curRes = aTest[1]; # tdTestResult
2385 reporter.log('Testing #%d, dir="%s" ...' % (i, curTest.sDirectory));
2386 curTest.setEnvironment(oSession, oTxsSession, oTestVm);
2387 fRc, curGuestSession = curTest.createSession('testGuestCtrlDirRead: Test #%d' % (i,));
2388 if fRc is False:
2389 reporter.error('Test #%d failed: Could not create session' % (i,));
2390 break;
2391 (fRc2, cDirs, cFiles) = self.gctrlReadDir(curTest, curRes, curGuestSession);
2392 curTest.closeSession();
2393 reporter.log2('Test #%d: Returned %ld directories, %ld files total' % (i, cDirs, cFiles));
2394 if fRc2 is curRes.fRc:
2395 if fRc2 is True:
2396 if curRes.numFiles != cFiles:
2397 reporter.error('Test #%d failed: Got %ld files, expected %ld' % (i, cFiles, curRes.numFiles));
2398 fRc = False;
2399 break;
2400 if curRes.numDirs != cDirs:
2401 reporter.error('Test #%d failed: Got %ld directories, expected %ld' % (i, cDirs, curRes.numDirs));
2402 fRc = False;
2403 break;
2404 else:
2405 reporter.error('Test #%d failed: Got %s, expected %s' % (i, fRc2, curRes.fRc));
2406 fRc = False;
2407 break;
2408
2409 return (fRc, oTxsSession);
2410
2411 def testGuestCtrlFileRemove(self, oSession, oTxsSession, oTestVm):
2412 """
2413 Tests removing guest files.
2414 """
2415
2416 if oTestVm.isWindows():
2417 sUser = "Administrator";
2418 else:
2419 sUser = "vbox";
2420 sPassword = "password";
2421
2422 aaTests = [];
2423 if oTestVm.isWindows():
2424 aaTests.extend([
2425 # Invalid stuff.
2426 [ tdTestFileRemove(sUser = sUser, sPassword = sPassword, sFile = ''), tdTestResult(fRc = False) ],
2427 [ tdTestFileRemove(sUser = sUser, sPassword = sPassword, sFile = 'C:\\Windows'), tdTestResult(fRc = False) ],
2428 [ tdTestFileRemove(sUser = sUser, sPassword = sPassword, sFile = 'C:\\Windows'), tdTestResult(fRc = False) ],
2429 # More unusual stuff.
2430 [ tdTestFileRemove(sUser = sUser, sPassword = sPassword, sFile = 'z:\\'), tdTestResult(fRc = False) ],
2431 [ tdTestFileRemove(sUser = sUser, sPassword = sPassword, sFile = '\\\\uncrulez\\foo'),
2432 tdTestResult(fRc = False) ],
2433 # Non-existing stuff.
2434 [ tdTestFileRemove(sUser = sUser, sPassword = sPassword, sFile = 'c:\\Apps\\nonexisting'),
2435 tdTestResult(fRc = False) ],
2436 [ tdTestFileRemove(sUser = sUser, sPassword = sPassword, sFile = 'c:\\Apps\\testFileRemove'),
2437 tdTestResult(fRc = False) ],
2438 # Try to delete system files.
2439 [ tdTestFileRemove(sUser = sUser, sPassword = sPassword, sFile = 'c:\\pagefile.sys'),
2440 tdTestResult(fRc = False) ],
2441 [ tdTestFileRemove(sUser = sUser, sPassword = sPassword, sFile = 'c:\\Windows\\kernel32.sys'),
2442 tdTestResult(fRc = False) ]
2443 ]);
2444
2445 if oTestVm.sVmName == 'tst-xppro':
2446 aaTests.extend([
2447 # Try delete some unimportant media stuff.
2448 [ tdTestFileRemove(sUser = sUser, sPassword = sPassword, sFile = 'c:\\Windows\\Media\\chimes.wav'),
2449 tdTestResult(fRc = True) ],
2450 # Second attempt should fail.
2451 [ tdTestFileRemove(sUser = sUser, sPassword = sPassword, sFile = 'c:\\Windows\\Media\\chimes.wav'),
2452 tdTestResult(fRc = False) ],
2453 [ tdTestFileRemove(sUser = sUser, sPassword = sPassword, sFile = 'c:\\Windows\\Media\\chord.wav'),
2454 tdTestResult(fRc = True) ],
2455 [ tdTestFileRemove(sUser = sUser, sPassword = sPassword, sFile = 'c:\\Windows\\Media\\chord.wav'),
2456 tdTestResult(fRc = False) ]
2457 ]);
2458 else:
2459 reporter.log('No OS-specific tests for non-Windows yet!');
2460
2461 fRc = True;
2462 for (i, aTest) in enumerate(aaTests):
2463 curTest = aTest[0]; # tdTestExec, use an index, later.
2464 curRes = aTest[1]; # tdTestResult
2465 reporter.log('Testing #%d, file="%s" ...' % (i, curTest.sFile));
2466 curTest.setEnvironment(oSession, oTxsSession, oTestVm);
2467 fRc, curGuestSession = curTest.createSession('testGuestCtrlFileRemove: Test #%d' % (i,));
2468 if fRc is False:
2469 reporter.error('Test #%d failed: Could not create session' % (i,));
2470 break;
2471 try:
2472 curGuestSession.fileRemove(curTest.sFile);
2473 except:
2474 if curRes.fRc is True:
2475 reporter.errorXcpt('Removing file "%s" failed:' % (curTest.sFile,));
2476 fRc = False;
2477 break;
2478 else:
2479 reporter.logXcpt('Removing file "%s" failed expectedly, skipping:' % (curTest.sFile,));
2480 curTest.closeSession();
2481 return (fRc, oTxsSession);
2482
2483 def testGuestCtrlFileStat(self, oSession, oTxsSession, oTestVm): # pylint: disable=R0914
2484 """
2485 Tests querying file information through stat.
2486 """
2487
2488 if oTestVm.isWindows():
2489 sUser = "Administrator";
2490 else:
2491 sUser = "vbox";
2492 sPassword = "password";
2493
2494 aaTests = [];
2495 if oTestVm.isWindows():
2496 aaTests.extend([
2497 # Invalid stuff.
2498 [ tdTestFileStat(sUser = sUser, sPassword = sPassword, sFile = ''),
2499 tdTestResultFileStat(fRc = False) ],
2500 [ tdTestFileStat(sUser = sUser, sPassword = sPassword, sFile = 'C:\\Windows'),
2501 tdTestResultFileStat(fRc = False) ],
2502 [ tdTestFileStat(sUser = sUser, sPassword = sPassword, sFile = 'C:\\Windows'),
2503 tdTestResultFileStat(fRc = False) ],
2504 # More unusual stuff.
2505 [ tdTestFileStat(sUser = sUser, sPassword = sPassword, sFile = 'z:\\'),
2506 tdTestResultFileStat(fRc = False) ],
2507 [ tdTestFileStat(sUser = sUser, sPassword = sPassword, sFile = '\\\\uncrulez\\foo'),
2508 tdTestResultFileStat(fRc = False) ],
2509 # Non-existing stuff.
2510 [ tdTestFileStat(sUser = sUser, sPassword = sPassword, sFile = 'c:\\Apps\\nonexisting'),
2511 tdTestResultFileStat(fRc = False) ],
2512 [ tdTestFileStat(sUser = sUser, sPassword = sPassword, sFile = 'c:\\Apps\\testDirRead'),
2513 tdTestResultFileStat(fRc = False) ]
2514 ]);
2515
2516 if oTestVm.sVmName == 'tst-xppro':
2517 aaTests.extend([
2518 # Directories; should fail.
2519 [ tdTestFileStat(sUser = sUser, sPassword = sPassword, sFile = '../../Windows/Fonts'),
2520 tdTestResultFileStat(fRc = False) ],
2521 [ tdTestFileStat(sUser = sUser, sPassword = sPassword, sFile = 'c:\\Windows\\Help'),
2522 tdTestResultFileStat(fRc = False) ],
2523 [ tdTestFileStat(sUser = sUser, sPassword = sPassword, sFile = 'c:\\Windows\\system32'),
2524 tdTestResultFileStat(fRc = False) ],
2525 # Regular files.
2526 [ tdTestFileStat(sUser = sUser, sPassword = sPassword, sFile = 'c:\\Windows\\system32\\kernel32.dll'),
2527 tdTestResultFileStat(fRc = False, cbSize = 926720, eFileType = vboxcon.FsObjType_File) ]
2528 ]);
2529 else:
2530 reporter.log('No OS-specific tests for non-Windows yet!');
2531
2532 fRc = True;
2533 for (i, aTest) in enumerate(aaTests):
2534 curTest = aTest[0]; # tdTestExec, use an index, later.
2535 curRes = aTest[1]; # tdTestResult
2536 reporter.log('Testing #%d, sFile="%s" ...' % (i, curTest.sFile));
2537 curTest.setEnvironment(oSession, oTxsSession, oTestVm);
2538 fRc, curGuestSession = curTest.createSession('testGuestCtrlFileStat: Test #%d' % (i,));
2539 if fRc is False:
2540 reporter.error('Test #%d failed: Could not create session' % (i,));
2541 break;
2542 fileObjInfo = None;
2543 try:
2544 fileObjInfo = curGuestSession.fileQueryInfo(curTest.sFile);
2545 except:
2546 if curRes.fRc is True:
2547 reporter.errorXcpt('Querying file information for "%s" failed:' % (curTest.sFile,));
2548 fRc = False;
2549 break;
2550 else:
2551 reporter.logXcpt('Querying file information for "%s" failed expectedly, skipping:' % (curTest.sFile,));
2552 curTest.closeSession();
2553 if fileObjInfo is not None:
2554 eFileType = fileObjInfo.type;
2555 if curRes.eFileType != eFileType:
2556 reporter.error('Test #%d failed: Got file type %ld, expected %ld' % (i, eFileType, curRes.eFileType));
2557 fRc = False;
2558 break;
2559 cbFile = long(fileObjInfo.objectSize);
2560 if curRes.cbSize != cbFile:
2561 reporter.error('Test #%d failed: Got %ld bytes size, expected %ld bytes' % (i, cbFile, curRes.cbSize));
2562 fRc = False;
2563 break;
2564 ## @todo Add more checks later.
2565
2566 return (fRc, oTxsSession);
2567
2568 def testGuestCtrlFileRead(self, oSession, oTxsSession, oTestVm): # pylint: disable=R0914
2569 """
2570 Tests reading from guest files.
2571 """
2572
2573 if oTestVm.isWindows():
2574 sUser = "Administrator";
2575 else:
2576 sUser = "vbox";
2577 sPassword = "password";
2578
2579 if oTxsSession.syncMkDir('${SCRATCH}/testGuestCtrlFileRead') is False:
2580 reporter.error('Could not create scratch directory on guest');
2581 return (False, oTxsSession);
2582
2583 aaTests = [];
2584 aaTests.extend([
2585 # Invalid stuff.
2586 [ tdTestFileReadWrite(sUser = sUser, sPassword = sPassword, cbToReadWrite = 0),
2587 tdTestResultFileReadWrite(fRc = False) ],
2588 [ tdTestFileReadWrite(sUser = sUser, sPassword = sPassword, sFile = ''),
2589 tdTestResultFileReadWrite(fRc = False) ],
2590 [ tdTestFileReadWrite(sUser = sUser, sPassword = sPassword, sFile = 'non-existing.file'),
2591 tdTestResultFileReadWrite(fRc = False) ],
2592 # Wrong open mode.
2593 [ tdTestFileReadWrite(sUser = sUser, sPassword = sPassword, sFile = 'non-existing.file', \
2594 sOpenMode = 'rt', sDisposition = 'oe'),
2595 tdTestResultFileReadWrite(fRc = False) ],
2596 [ tdTestFileReadWrite(sUser = sUser, sPassword = sPassword, sFile = '\\\\uncrulez\\non-existing.file', \
2597 sOpenMode = 'tr', sDisposition = 'oe'),
2598 tdTestResultFileReadWrite(fRc = False) ],
2599 [ tdTestFileReadWrite(sUser = sUser, sPassword = sPassword, sFile = '../../non-existing.file', \
2600 sOpenMode = 'wr', sDisposition = 'oe'),
2601 tdTestResultFileReadWrite(fRc = False) ],
2602 # Wrong disposition.
2603 [ tdTestFileReadWrite(sUser = sUser, sPassword = sPassword, sFile = 'non-existing.file', \
2604 sOpenMode = 'r', sDisposition = 'e'),
2605 tdTestResultFileReadWrite(fRc = False) ],
2606 [ tdTestFileReadWrite(sUser = sUser, sPassword = sPassword, sFile = '\\\\uncrulez\\non-existing.file', \
2607 sOpenMode = 'r', sDisposition = 'o'),
2608 tdTestResultFileReadWrite(fRc = False) ],
2609 [ tdTestFileReadWrite(sUser = sUser, sPassword = sPassword, sFile = '../../non-existing.file', \
2610 sOpenMode = 'r', sDisposition = 'c'),
2611 tdTestResultFileReadWrite(fRc = False) ],
2612 # Opening non-existing file when it should exist.
2613 [ tdTestFileReadWrite(sUser = sUser, sPassword = sPassword, sFile = 'non-existing.file', \
2614 sOpenMode = 'r', sDisposition = 'oe'),
2615 tdTestResultFileReadWrite(fRc = False) ],
2616 [ tdTestFileReadWrite(sUser = sUser, sPassword = sPassword, sFile = '\\\\uncrulez\\non-existing.file', \
2617 sOpenMode = 'r', sDisposition = 'oe'),
2618 tdTestResultFileReadWrite(fRc = False) ],
2619 [ tdTestFileReadWrite(sUser = sUser, sPassword = sPassword, sFile = '../../non-existing.file', \
2620 sOpenMode = 'r', sDisposition = 'oe'),
2621 tdTestResultFileReadWrite(fRc = False) ]
2622 ]);
2623
2624 if oTestVm.isWindows():
2625 aaTests.extend([
2626 # Create a file which must not exist (but it hopefully does).
2627 [ tdTestFileReadWrite(sUser = sUser, sPassword = sPassword, sFile = 'C:\\Windows\\System32\\calc.exe', \
2628 sOpenMode = 'w', sDisposition = 'ce'),
2629 tdTestResultFileReadWrite(fRc = False) ],
2630 # Open a file which must exist.
2631 [ tdTestFileReadWrite(sUser = sUser, sPassword = sPassword, sFile = 'C:\\Windows\\System32\\kernel32.dll', \
2632 sOpenMode = 'r', sDisposition = 'oe'),
2633 tdTestResultFileReadWrite(fRc = True) ],
2634 # Try truncating a file which already is opened with a different sharing mode (and thus should fail).
2635 [ tdTestFileReadWrite(sUser = sUser, sPassword = sPassword, sFile = 'C:\\Windows\\System32\\kernel32.dll', \
2636 sOpenMode = 'w', sDisposition = 'ot'),
2637 tdTestResultFileReadWrite(fRc = False) ]
2638 ]);
2639
2640 if oTestVm.sKind == "WindowsXP":
2641 aaTests.extend([
2642 # Reading from beginning.
2643 [ tdTestFileReadWrite(sUser = sUser, sPassword = sPassword, sFile = 'C:\\Windows\\System32\\eula.txt', \
2644 sOpenMode = 'r', sDisposition = 'oe', cbToReadWrite = 33),
2645 tdTestResultFileReadWrite(fRc = True, aBuf = 'Microsoft Windows XP Professional', \
2646 cbProcessed = 33, cbOffset = 33) ],
2647 # Reading from offset.
2648 [ tdTestFileReadWrite(sUser = sUser, sPassword = sPassword, sFile = 'C:\\Windows\\System32\\eula.txt', \
2649 sOpenMode = 'r', sDisposition = 'oe', cbOffset = 17782, cbToReadWrite = 26),
2650 tdTestResultFileReadWrite(fRc = True, aBuf = 'LINKS TO THIRD PARTY SITES', \
2651 cbProcessed = 26, cbOffset = 17782 + 26) ]
2652 ]);
2653
2654 fRc = True;
2655 for (i, aTest) in enumerate(aaTests):
2656 curTest = aTest[0]; # tdTestFileReadWrite, use an index, later.
2657 curRes = aTest[1]; # tdTestResult
2658 reporter.log('Testing #%d, sFile="%s", cbToReadWrite=%d, sOpenMode="%s", sDisposition="%s", cbOffset=%ld ...' % \
2659 (i, curTest.sFile, curTest.cbToReadWrite, curTest.sOpenMode, curTest.sDisposition, curTest.cbOffset));
2660 curTest.setEnvironment(oSession, oTxsSession, oTestVm);
2661 fRc, curGuestSession = curTest.createSession('testGuestCtrlFileRead: Test #%d' % (i,));
2662 if fRc is False:
2663 reporter.error('Test #%d failed: Could not create session' % (i,));
2664 break;
2665 try:
2666 if curTest.cbOffset > 0:
2667 curFile = curGuestSession.fileOpenEx(curTest.sFile, curTest.sOpenMode, curTest.sDisposition, \
2668 curTest.sSharingMode, curTest.lCreationMode, curTest.cbOffset);
2669 curOffset = long(curFile.offset);
2670 resOffset = long(curTest.cbOffset);
2671 if curOffset != resOffset:
2672 reporter.error('Test #%d failed: Initial offset on open does not match: Got %ld, expected %ld' \
2673 % (i, curOffset, resOffset));
2674 fRc = False;
2675 else:
2676 curFile = curGuestSession.fileOpen(curTest.sFile, curTest.sOpenMode, curTest.sDisposition, \
2677 curTest.lCreationMode);
2678 if fRc \
2679 and curTest.cbToReadWrite > 0:
2680 ## @todo Split this up in 64K reads. Later.
2681 ## @todo Test timeouts.
2682 aBufRead = curFile.read(curTest.cbToReadWrite, 30 * 1000);
2683 if curRes.cbProcessed > 0 \
2684 and curRes.cbProcessed is not len(aBufRead):
2685 reporter.error('Test #%d failed: Read buffer length does not match: Got %ld, expected %ld' \
2686 % (i, len(aBufRead), curRes.cbProcessed));
2687 fRc = False;
2688 if fRc:
2689 if curRes.aBuf is not None \
2690 and bytes(curRes.aBuf) != bytes(aBufRead):
2691 reporter.error('Test #%d failed: Got buffer\n%s (%ld bytes), expected\n%s (%ld bytes)' \
2692 % (i, map(hex, map(ord, aBufRead)), len(aBufRead), \
2693 map(hex, map(ord, curRes.aBuf)), len(curRes.aBuf)));
2694 reporter.error('Test #%d failed: Got buffer\n%s, expected\n%s' \
2695 % (i, aBufRead, curRes.aBuf));
2696 fRc = False;
2697 # Test final offset.
2698 curOffset = long(curFile.offset);
2699 resOffset = long(curRes.cbOffset);
2700 if curOffset != resOffset:
2701 reporter.error('Test #%d failed: Final offset does not match: Got %ld, expected %ld' \
2702 % (i, curOffset, resOffset));
2703 fRc = False;
2704 curFile.close();
2705 except:
2706 reporter.logXcpt('Opening "%s" failed:' % (curTest.sFile,));
2707 fRc = False;
2708
2709 curTest.closeSession();
2710
2711 if fRc != curRes.fRc:
2712 reporter.error('Test #%d failed: Got %s, expected %s' % (i, fRc, curRes.fRc));
2713 fRc = False;
2714 break;
2715
2716 return (fRc, oTxsSession);
2717
2718 def testGuestCtrlFileWrite(self, oSession, oTxsSession, oTestVm): # pylint: disable=R0914
2719 """
2720 Tests writing to guest files.
2721 """
2722
2723 if oTestVm.isWindows():
2724 sUser = "Administrator";
2725 else:
2726 sUser = "vbox";
2727 sPassword = "password";
2728
2729 if oTestVm.isWindows():
2730 sScratch = "C:\\Temp\\vboxtest\\testGuestCtrlFileWrite\\";
2731
2732 if oTxsSession.syncMkDir('${SCRATCH}/testGuestCtrlFileWrite') is False:
2733 reporter.error('Could not create scratch directory on guest');
2734 return (False, oTxsSession);
2735
2736 aaTests = [];
2737
2738 cScratchBuf = 512;
2739 aScratchBuf = array('b', [random.randint(-128, 127) for i in range(cScratchBuf)]);
2740 aaTests.extend([
2741 # Write to a non-existing file.
2742 [ tdTestFileReadWrite(sUser = sUser, sPassword = sPassword, sFile = sScratch + 'testGuestCtrlFileWrite.txt', \
2743 sOpenMode = 'w+', sDisposition = 'ce', cbToReadWrite = cScratchBuf,
2744 aBuf = aScratchBuf),
2745 tdTestResultFileReadWrite(fRc = True, aBuf = aScratchBuf, \
2746 cbProcessed = cScratchBuf, cbOffset = cScratchBuf) ]
2747 ]);
2748
2749 aScratchBuf2 = array('b', [random.randint(-128, 127) for i in range(cScratchBuf)]);
2750 aaTests.extend([
2751 # Append the same amount of data to the just created file.
2752 [ tdTestFileReadWrite(sUser = sUser, sPassword = sPassword, sFile = sScratch + 'testGuestCtrlFileWrite.txt', \
2753 sOpenMode = 'w+', sDisposition = 'oa', cbToReadWrite = cScratchBuf,
2754 cbOffset = cScratchBuf, aBuf = aScratchBuf2),
2755 tdTestResultFileReadWrite(fRc = True, aBuf = aScratchBuf2, \
2756 cbProcessed = cScratchBuf, cbOffset = cScratchBuf * 2) ],
2757 ]);
2758
2759 fRc = True;
2760 for (i, aTest) in enumerate(aaTests):
2761 curTest = aTest[0]; # tdTestFileReadWrite, use an index, later.
2762 curRes = aTest[1]; # tdTestResult
2763 reporter.log('Testing #%d, sFile="%s", cbToReadWrite=%d, sOpenMode="%s", sDisposition="%s", cbOffset=%ld ...' % \
2764 (i, curTest.sFile, curTest.cbToReadWrite, curTest.sOpenMode, curTest.sDisposition, curTest.cbOffset));
2765 curTest.setEnvironment(oSession, oTxsSession, oTestVm);
2766 fRc, curGuestSession = curTest.createSession('testGuestCtrlFileWrite: Test #%d' % (i,));
2767 if fRc is False:
2768 reporter.error('Test #%d failed: Could not create session' % (i,));
2769 break;
2770 try:
2771 if curTest.cbOffset > 0:
2772 curFile = curGuestSession.fileOpenEx(curTest.sFile, curTest.sOpenMode, curTest.sDisposition, \
2773 curTest.sSharingMode, curTest.lCreationMode, curTest.cbOffset);
2774 curOffset = long(curFile.offset);
2775 resOffset = long(curTest.cbOffset);
2776 if curOffset != resOffset:
2777 reporter.error('Test #%d failed: Initial offset on open does not match: Got %ld, expected %ld' \
2778 % (i, curOffset, resOffset));
2779 fRc = False;
2780 else:
2781 curFile = curGuestSession.fileOpen(curTest.sFile, curTest.sOpenMode, curTest.sDisposition, \
2782 curTest.lCreationMode);
2783 if fRc \
2784 and curTest.cbToReadWrite > 0:
2785 ## @todo Split this up in 64K writes. Later.
2786 ## @todo Test timeouts.
2787 cBytesWritten = curFile.write(curTest.aBuf, 30 * 1000);
2788 if curRes.cbProcessed > 0 \
2789 and curRes.cbProcessed != cBytesWritten:
2790 reporter.error('Test #%d failed: Written buffer length does not match: Got %ld, expected %ld' \
2791 % (i, cBytesWritten, curRes.cbProcessed));
2792 fRc = False;
2793 if fRc:
2794 # Verify written content by seeking back to the initial offset and
2795 # re-read & compare the written data.
2796 try:
2797 curFile.seek(-(curTest.cbToReadWrite), vboxcon.FileSeekType_Current);
2798 except:
2799 reporter.logXcpt('Seeking back to initial write position failed:');
2800 fRc = False;
2801 if fRc \
2802 and long(curFile.offset) != curTest.cbOffset:
2803 reporter.error('Test #%d failed: Initial write position does not match current position, \
2804 got %ld, expected %ld' \
2805 % (i, long(curFile.offset), curTest.cbOffset));
2806 fRc = False;
2807 if fRc:
2808 aBufRead = curFile.read(curTest.cbToReadWrite, 30 * 1000);
2809 if len(aBufRead) != curTest.cbToReadWrite:
2810 reporter.error('Test #%d failed: Got buffer length %ld, expected %ld' \
2811 % (i, len(aBufRead), curTest.cbToReadWrite));
2812 fRc = False;
2813 if fRc \
2814 and curRes.aBuf is not None \
2815 and buffer(curRes.aBuf) != aBufRead:
2816 reporter.error('Test #%d failed: Got buffer\n%s, expected\n%s' \
2817 % (i, aBufRead, curRes.aBuf));
2818 fRc = False;
2819 # Test final offset.
2820 curOffset = long(curFile.offset);
2821 resOffset = long(curRes.cbOffset);
2822 if curOffset != resOffset:
2823 reporter.error('Test #%d failed: Final offset does not match: Got %ld, expected %ld' \
2824 % (i, curOffset, resOffset));
2825 fRc = False;
2826 curFile.close();
2827 except:
2828 reporter.logXcpt('Opening "%s" failed:' % (curTest.sFile,));
2829 fRc = False;
2830
2831 curTest.closeSession();
2832
2833 if fRc != curRes.fRc:
2834 reporter.error('Test #%d failed: Got %s, expected %s' % (i, fRc, curRes.fRc));
2835 fRc = False;
2836 break;
2837
2838 return (fRc, oTxsSession);
2839
2840 def testGuestCtrlCopyTo(self, oSession, oTxsSession, oTestVm):
2841 """
2842 Tests copying files from host to the guest.
2843 """
2844
2845 if oTestVm.isWindows():
2846 sUser = "Administrator";
2847 else:
2848 sUser = "vbox";
2849 sPassword = "password";
2850
2851 if oTestVm.isWindows():
2852 sScratch = "C:\\Temp\\vboxtest\\testGuestCtrlCopyTo\\";
2853
2854 if oTxsSession.syncMkDir('${SCRATCH}/testGuestCtrlCopyTo') is False:
2855 reporter.error('Could not create scratch directory on guest');
2856 return (False, oTxsSession);
2857
2858 # Some stupid trickery to guess the location of the iso.
2859 sVBoxValidationKitISO = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../VBoxValidationKit.iso'));
2860 if not os.path.isfile(sVBoxValidationKitISO):
2861 sVBoxValidationKitISO = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../VBoxTestSuite.iso'));
2862 if not os.path.isfile(sVBoxValidationKitISO):
2863 sCur = os.getcwd();
2864 for i in range(0, 10):
2865 sVBoxValidationKitISO = os.path.join(sCur, 'validationkit/VBoxValidationKit.iso');
2866 if os.path.isfile(sVBoxValidationKitISO):
2867 break;
2868 sVBoxValidationKitISO = os.path.join(sCur, 'testsuite/VBoxTestSuite.iso');
2869 if os.path.isfile(sVBoxValidationKitISO):
2870 break;
2871 sCur = os.path.abspath(os.path.join(sCur, '..'));
2872 if i is None: pass; # shut up pychecker/pylint.
2873 if os.path.isfile(sVBoxValidationKitISO):
2874 reporter.log('Validation Kit .ISO found at: %s' % (sVBoxValidationKitISO,));
2875 else:
2876 reporter.log('Warning: Validation Kit .ISO not found -- some tests might fail');
2877
2878 aaTests = [];
2879 if oTestVm.isWindows():
2880 aaTests.extend([
2881 # Destination missing.
2882 [ tdTestCopyTo(sUser = sUser, sPassword = sPassword, sSrc = ''), tdTestResult(fRc = False) ],
2883 [ tdTestCopyTo(sUser = sUser, sPassword = sPassword, sSrc = 'C:\\Windows',
2884 aFlags = [ 1234 ] ), tdTestResult(fRc = False) ],
2885 # Source missing.
2886 [ tdTestCopyTo(sUser = sUser, sPassword = sPassword, sDst = ''), tdTestResult(fRc = False) ],
2887 [ tdTestCopyTo(sUser = sUser, sPassword = sPassword, sDst = 'C:\\Windows',
2888 aFlags = [ 1234 ] ), tdTestResult(fRc = False) ],
2889 # Nothing to copy (source and/or destination is empty).
2890 [ tdTestCopyTo(sUser = sUser, sPassword = sPassword, sSrc = 'z:\\'), tdTestResult(fRc = False) ],
2891 [ tdTestCopyTo(sUser = sUser, sPassword = sPassword, sSrc = '\\\\uncrulez\\foo'),
2892 tdTestResult(fRc = False) ],
2893 [ tdTestCopyTo(sUser = sUser, sPassword = sPassword, sSrc = 'non-exist',
2894 sDst = os.path.join(sScratch, 'non-exist.dll')), tdTestResult(fRc = False) ],
2895 # Copying single files.
2896 [ tdTestCopyTo(sUser = sUser, sPassword = sPassword, sSrc = sVBoxValidationKitISO,
2897 sDst = 'C:\\non-exist\\'), tdTestResult(fRc = False) ],
2898 [ tdTestCopyTo(sUser = sUser, sPassword = sPassword, sSrc = sVBoxValidationKitISO,
2899 sDst = 'C:\\non\\exist\\'), tdTestResult(fRc = False) ],
2900 [ tdTestCopyTo(sUser = sUser, sPassword = sPassword, sSrc = sVBoxValidationKitISO,
2901 sDst = 'C:\\non\\exist\\renamedfile.dll'), tdTestResult(fRc = False) ],
2902 [ tdTestCopyTo(sUser = sUser, sPassword = sPassword, sSrc = sVBoxValidationKitISO,
2903 sDst = os.path.join(sScratch, 'HostGuestAdditions.iso')), tdTestResult(fRc = True) ],
2904 [ tdTestCopyTo(sUser = sUser, sPassword = sPassword, sSrc = sVBoxValidationKitISO,
2905 sDst = os.path.join(sScratch, 'HostGuestAdditions.iso')), tdTestResult(fRc = True) ],
2906 # Destination is a directory, should fail.
2907 [ tdTestCopyTo(sUser = sUser, sPassword = sPassword, sSrc = sVBoxValidationKitISO,
2908 sDst = sScratch), tdTestResult(fRc = False) ]
2909 ## @todo Add testing the CopyTo flags here!
2910 ]);
2911
2912 if self.oTstDrv.sHost == 'win':
2913 ## @todo Check for Windows (7) host.
2914 aaTests.extend([
2915 # Copying directories with contain files we don't have read access to.
2916 [ tdTestCopyTo(sUser = sUser, sPassword = sPassword, sSrc = 'C:\\Windows\\security',
2917 sDst = sScratch), tdTestResult(fRc = False) ],
2918 # Copying directories with regular files.
2919 [ tdTestCopyTo(sUser = sUser, sPassword = sPassword, sSrc = 'C:\\Windows\\Help',
2920 sDst = sScratch), tdTestResult(fRc = True) ]
2921 ]);
2922 else:
2923 reporter.log('No OS-specific tests for non-Windows yet!');
2924
2925 fRc = True;
2926 for (i, aTest) in enumerate(aaTests):
2927 curTest = aTest[0]; # tdTestExec, use an index, later.
2928 curRes = aTest[1]; # tdTestResult
2929 reporter.log('Testing #%d, sSrc="%s", sDst="%s", aFlags="%s" ...' % \
2930 (i, curTest.sSrc, curTest.sDst, curTest.aFlags));
2931 curTest.setEnvironment(oSession, oTxsSession, oTestVm);
2932 fRc, curGuestSession = curTest.createSession('testGuestCtrlCopyTo: Test #%d' % (i,));
2933 if fRc is False:
2934 reporter.error('Test #%d failed: Could not create session' % (i,));
2935 break;
2936 fRc2 = self.gctrlCopyTo(curTest, curGuestSession);
2937 curTest.closeSession();
2938 if fRc2 is curRes.fRc:
2939 ## @todo Verify the copied results (size, checksum?).
2940 pass;
2941 else:
2942 reporter.error('Test #%d failed: Got %s, expected %s' % (i, fRc2, curRes.fRc));
2943 fRc = False;
2944 break;
2945
2946 return (fRc, oTxsSession);
2947
2948 def testGuestCtrlCopyFrom(self, oSession, oTxsSession, oTestVm): # pylint: disable=R0914
2949 """
2950 Tests copying files from guest to the host.
2951 """
2952
2953 if oTestVm.isWindows():
2954 sUser = "Administrator";
2955 else:
2956 sUser = "vbox";
2957 sPassword = "password";
2958
2959 sScratch = os.path.join(self.oTstDrv.sScratchPath, "testGctrlCopyFrom");
2960 try:
2961 os.makedirs(sScratch);
2962 except OSError as e:
2963 if e.errno != errno.EEXIST:
2964 reporter.error('Failed: Unable to create scratch directory \"%s\"' % (sScratch,));
2965 return (False, oTxsSession);
2966 reporter.log('Scratch path is: %s' % (sScratch,));
2967
2968 aaTests = [];
2969 if oTestVm.isWindows():
2970 aaTests.extend([
2971 # Destination missing.
2972 [ tdTestCopyFrom(sUser = sUser, sPassword = sPassword, sSrc = ''),
2973 tdTestResult(fRc = False) ],
2974 [ tdTestCopyFrom(sUser = sUser, sPassword = sPassword, sSrc = 'C:\\Windows',
2975 aFlags = [ 1234 ] ),
2976 tdTestResult(fRc = False) ],
2977 # Source missing.
2978 [ tdTestCopyFrom(sUser = sUser, sPassword = sPassword, sDst = ''),
2979 tdTestResult(fRc = False) ],
2980 [ tdTestCopyFrom(sUser = sUser, sPassword = sPassword, sDst = 'C:\\Windows',
2981 aFlags = [ 1234 ] ),
2982 tdTestResult(fRc = False) ],
2983 # Nothing to copy (sDst is empty / unreachable).
2984 [ tdTestCopyFrom(sUser = sUser, sPassword = sPassword, sSrc = 'z:\\'),
2985 tdTestResult(fRc = False) ],
2986 [ tdTestCopyFrom(sUser = sUser, sPassword = sPassword, sSrc = '\\\\uncrulez\\foo'),
2987
2988 tdTestResult(fRc = False) ],
2989 [ tdTestCopyFrom(sUser = sUser, sPassword = sPassword, sSrc = 'non-exist',
2990 sDst = os.path.join(sScratch, 'non-exist.dll')),
2991 tdTestResult(fRc = False) ]
2992 ## @todo Add testing the CopyFrom aFlags here!
2993 ]);
2994
2995 if self.oTstDrv.sHost == 'win':
2996 aaTests.extend([
2997 # FIXME: Failing test.
2998 # Copying single files.
2999 # [ tdTestCopyFrom(sUser = sUser, sPassword = sPassword, sSrc = 'C:\\Windows\\system32\\ole32.dll',
3000 # sDst = 'C:\\non-exist\\'), tdTestResult(fRc = False) ],
3001 # [ tdTestCopyFrom(sUser = sUser, sPassword = sPassword, sSrc = 'C:\\Windows\\system32\\ole32.dll',
3002 # sDst = 'C:\\non\\exist\\'), tdTestResult(fRc = False) ],
3003 # [ tdTestCopyFrom(sUser = sUser, sPassword = sPassword, sSrc = 'C:\\Windows\\system32\\ole32.dll',
3004 # sDst = 'C:\\non\\exist\\renamedfile.dll'), tdTestResult(fRc = False) ],
3005 # [ tdTestCopyFrom(sUser = sUser, sPassword = sPassword, sSrc = 'C:\\Windows\\system32\\ole32.dll',
3006 # sDst = os.path.join(sScratch, 'renamedfile.dll')), tdTestResult(fRc = True) ],
3007 # [ tdTestCopyFrom(sUser = sUser, sPassword = sPassword, sSrc = 'C:\\Windows\\system32\\ole32.dll',
3008 # sDst = os.path.join(sScratch, 'renamedfile.dll')), tdTestResult(fRc = True) ],
3009 # Destination is a directory, should fail.
3010 [ tdTestCopyFrom(sUser = sUser, sPassword = sPassword, sSrc = 'C:\\Windows\\system32\\ole32.dll',
3011 sDst = sScratch), tdTestResult(fRc = False) ],
3012 # Copying directories.
3013 [ tdTestCopyFrom(sUser = sUser, sPassword = sPassword, sSrc = 'C:\\Windows\\Web',
3014 sDst = sScratch), tdTestResult(fRc = True) ]
3015 ## @todo Add testing the CopyFrom aFlags here!
3016 ]);
3017 else:
3018 reporter.log('No OS-specific tests for non-Windows yet!');
3019
3020 fRc = True;
3021 for (i, aTest) in enumerate(aaTests):
3022 curTest = aTest[0]; # tdTestExec, use an index, later.
3023 curRes = aTest[1]; # tdTestResult
3024 reporter.log('Testing #%d, sSrc="%s", sDst="%s", aFlags="%s" ...' % \
3025 (i, curTest.sSrc, curTest.sDst, curTest.aFlags));
3026 curTest.setEnvironment(oSession, oTxsSession, oTestVm);
3027 fRc, curGuestSession = curTest.createSession('testGuestCtrlCopyFrom: Test #%d' % (i,));
3028 if fRc is False:
3029 reporter.error('Test #%d failed: Could not create session' % (i,));
3030 break;
3031 fRc2 = self.gctrlCopyFrom(curTest, curRes, curGuestSession);
3032 curTest.closeSession();
3033 if fRc2 is curRes.fRc:
3034 ## @todo Verify the copied results (size, checksum?).
3035 pass;
3036 else:
3037 reporter.error('Test #%d failed: Got %s, expected %s' % (i, fRc2, curRes.fRc));
3038 fRc = False;
3039 break;
3040
3041 return (fRc, oTxsSession);
3042
3043 def testGuestCtrlUpdateAdditions(self, oSession, oTxsSession, oTestVm): # pylint: disable=R0914
3044 """
3045 Tests updating the Guest Additions inside the guest.
3046 """
3047
3048 if oTestVm.isWindows():
3049 sUser = "Administrator";
3050 else:
3051 sUser = "vbox";
3052 sPassword = "password";
3053
3054 # Some stupid trickery to guess the location of the iso.
3055 sVBoxValidationKitISO = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../VBoxValidationKit.iso'));
3056 if not os.path.isfile(sVBoxValidationKitISO):
3057 sVBoxValidationKitISO = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../VBoxTestSuite.iso'));
3058 if not os.path.isfile(sVBoxValidationKitISO):
3059 sCur = os.getcwd();
3060 for i in range(0, 10):
3061 sVBoxValidationKitISO = os.path.join(sCur, 'validationkit/VBoxValidationKit.iso');
3062 if os.path.isfile(sVBoxValidationKitISO):
3063 break;
3064 sVBoxValidationKitISO = os.path.join(sCur, 'testsuite/VBoxTestSuite.iso');
3065 if os.path.isfile(sVBoxValidationKitISO):
3066 break;
3067 sCur = os.path.abspath(os.path.join(sCur, '..'));
3068 if i is None: pass; # shut up pychecker/pylint.
3069 if os.path.isfile(sVBoxValidationKitISO):
3070 reporter.log('Validation Kit .ISO found at: %s' % (sVBoxValidationKitISO,));
3071 else:
3072 reporter.log('Warning: Validation Kit .ISO not found -- some tests might fail');
3073
3074 sScratch = os.path.join(self.oTstDrv.sScratchPath, "testGctrlUpdateAdditions");
3075 try:
3076 os.makedirs(sScratch);
3077 except OSError as e:
3078 if e.errno != errno.EEXIST:
3079 reporter.error('Failed: Unable to create scratch directory \"%s\"' % (sScratch,));
3080 return (False, oTxsSession);
3081 reporter.log('Scratch path is: %s' % (sScratch,));
3082
3083 aaTests = [];
3084 if oTestVm.isWindows():
3085 aaTests.extend([
3086 # Source is missing.
3087 [ tdTestUpdateAdditions(sUser = sUser, sPassword = sPassword, sSrc = ''),
3088 tdTestResult(fRc = False) ],
3089 # Wrong aFlags.
3090 [ tdTestUpdateAdditions(sUser = sUser, sPassword = sPassword, sSrc = self.oTstDrv.getGuestAdditionsIso(),
3091 aFlags = [ 1234 ]),
3092 tdTestResult(fRc = False) ],
3093 # Non-existing .ISO.
3094 [ tdTestUpdateAdditions(sUser = sUser, sPassword = sPassword, sSrc = "non-existing.iso"),
3095 tdTestResult(fRc = False) ],
3096 # Wrong .ISO.
3097 [ tdTestUpdateAdditions(sUser = sUser, sPassword = sPassword, sSrc = sVBoxValidationKitISO),
3098 tdTestResult(fRc = False) ],
3099 # The real thing.
3100 [ tdTestUpdateAdditions(sUser = sUser, sPassword = sPassword, sSrc = self.oTstDrv.getGuestAdditionsIso()),
3101 tdTestResult(fRc = True) ],
3102 # Test the (optional) installer arguments. This will extract the
3103 # installer into our guest's scratch directory.
3104 [ tdTestUpdateAdditions(sUser = sUser, sPassword = sPassword, sSrc = self.oTstDrv.getGuestAdditionsIso(),
3105 aArgs = [ '/extract', '/D=' + sScratch ]),
3106 tdTestResult(fRc = True) ]
3107 # Some debg ISO. Only enable locally.
3108 #[ tdTestUpdateAdditions(sUser = sUser, sPassword = sPassword,
3109 # sSrc = "V:\\Downloads\\VBoxGuestAdditions-r80354.iso"),
3110 # tdTestResult(fRc = True) ]
3111 ]);
3112 else:
3113 reporter.log('No OS-specific tests for non-Windows yet!');
3114
3115 fRc = True;
3116 for (i, aTest) in enumerate(aaTests):
3117 curTest = aTest[0]; # tdTestExec, use an index, later.
3118 curRes = aTest[1]; # tdTestResult
3119 reporter.log('Testing #%d, sSrc="%s", aFlags="%s" ...' % \
3120 (i, curTest.sSrc, curTest.aFlags));
3121 curTest.setEnvironment(oSession, oTxsSession, oTestVm);
3122 fRc, _ = curTest.createSession('Test #%d' % (i,));
3123 if fRc is False:
3124 reporter.error('Test #%d failed: Could not create session' % (i,));
3125 break;
3126 try:
3127 curProgress = curTest.oTest.oGuest.updateGuestAdditions(curTest.sSrc, curTest.aArgs, curTest.aFlags);
3128 if curProgress is not None:
3129 oProgress = vboxwrappers.ProgressWrapper(curProgress, self.oTstDrv.oVBoxMgr, self, "gctrlUpGA");
3130 try:
3131 iRc = oProgress.waitForOperation(0, fIgnoreErrors = True);
3132 if iRc != 0:
3133 reporter.log('Waiting for updating Guest Additions failed');
3134 fRc = False;
3135 except:
3136 reporter.logXcpt('Updating Guest Additions waiting exception for sSrc="%s", aFlags="%s":' \
3137 % (curTest.sSrc, curTest.aFlags));
3138 fRc = False;
3139 except:
3140 # Just log, don't assume an error here (will be done in the main loop then).
3141 reporter.logXcpt('Updating Guest Additions exception for sSrc="%s", aFlags="%s":' \
3142 % (curTest.sSrc, curTest.aFlags));
3143 fRc = False;
3144 curTest.closeSession();
3145 if fRc is curRes.fRc:
3146 if fRc:
3147 ## @todo Verify if Guest Additions were really updated (build, revision, ...).
3148 pass;
3149 else:
3150 reporter.error('Test #%d failed: Got %s, expected %s' % (i, fRc, curRes.fRc));
3151 fRc = False;
3152 break;
3153
3154 return (fRc, oTxsSession);
3155
3156
3157
3158class tdAddGuestCtrl(vbox.TestDriver): # pylint: disable=R0902,R0904
3159 """
3160 Guest control using VBoxService on the guest.
3161 """
3162
3163 def __init__(self):
3164 vbox.TestDriver.__init__(self);
3165 self.oTestVmSet = self.oTestVmManager.getStandardVmSet('nat');
3166 self.fQuick = False; # Don't skip lengthly tests by default.
3167 self.addSubTestDriver(SubTstDrvAddGuestCtrl(self));
3168
3169 #
3170 # Overridden methods.
3171 #
3172 def showUsage(self):
3173 """
3174 Shows the testdriver usage.
3175 """
3176 rc = vbox.TestDriver.showUsage(self);
3177 reporter.log('');
3178 reporter.log('tdAddGuestCtrl Options:');
3179 reporter.log(' --quick');
3180 reporter.log(' Same as --virt-modes hwvirt --cpu-counts 1.');
3181 return rc;
3182
3183 def parseOption(self, asArgs, iArg): # pylint: disable=R0912,R0915
3184 """
3185 Parses the testdriver arguments from the command line.
3186 """
3187 if asArgs[iArg] == '--quick':
3188 self.parseOption(['--virt-modes', 'hwvirt'], 0);
3189 self.parseOption(['--cpu-counts', '1'], 0);
3190 self.fQuick = True;
3191 else:
3192 return vbox.TestDriver.parseOption(self, asArgs, iArg);
3193 return iArg + 1;
3194
3195 def actionConfig(self):
3196 if not self.importVBoxApi(): # So we can use the constant below.
3197 return False;
3198
3199 eNic0AttachType = vboxcon.NetworkAttachmentType_NAT;
3200 sGaIso = self.getGuestAdditionsIso();
3201 return self.oTestVmSet.actionConfig(self, eNic0AttachType = eNic0AttachType, sDvdImage = sGaIso);
3202
3203 def actionExecute(self):
3204 return self.oTestVmSet.actionExecute(self, self.testOneCfg);
3205
3206 #
3207 # Test execution helpers.
3208 #
3209 def testOneCfg(self, oVM, oTestVm): # pylint: disable=R0915
3210 """
3211 Runs the specified VM thru the tests.
3212
3213 Returns a success indicator on the general test execution. This is not
3214 the actual test result.
3215 """
3216
3217 self.logVmInfo(oVM);
3218
3219 fRc = True;
3220 oSession, oTxsSession = self.startVmAndConnectToTxsViaTcp(oTestVm.sVmName, fCdWait = False);
3221 reporter.log("TxsSession: %s" % (oTxsSession,));
3222 if oSession is not None:
3223 self.addTask(oSession);
3224
3225 fManual = False; # Manual override for local testing. (Committed version shall be False.)
3226 if not fManual:
3227 fRc, oTxsSession = self.aoSubTstDrvs[0].testIt(oTestVm, oSession, oTxsSession);
3228 else:
3229 fRc, oTxsSession = self.testGuestCtrlManual(oSession, oTxsSession, oTestVm);
3230
3231 # Cleanup.
3232 self.removeTask(oTxsSession);
3233 if not fManual:
3234 self.terminateVmBySession(oSession);
3235 else:
3236 fRc = False;
3237 return fRc;
3238
3239 def gctrlReportError(self, progress):
3240 """
3241 Helper function to report an error of a
3242 given progress object.
3243 """
3244 if progress is None:
3245 reporter.log('No progress object to print error for');
3246 else:
3247 errInfo = progress.errorInfo;
3248 if errInfo:
3249 reporter.log('%s' % (errInfo.text,));
3250 return False;
3251
3252 def gctrlGetRemainingTime(self, msTimeout, msStart):
3253 """
3254 Helper function to return the remaining time (in ms)
3255 based from a timeout value and the start time (both in ms).
3256 """
3257 if msTimeout is 0:
3258 return 0xFFFFFFFE; # Wait forever.
3259 msElapsed = base.timestampMilli() - msStart;
3260 if msElapsed > msTimeout:
3261 return 0; # No time left.
3262 return msTimeout - msElapsed;
3263
3264 def testGuestCtrlManual(self, oSession, oTxsSession, oTestVm): # pylint: disable=R0914,R0915,W0613,W0612
3265 """
3266 For manually testing certain bits.
3267 """
3268
3269 reporter.log('Manual testing ...');
3270 fRc = True;
3271
3272 sUser = 'Administrator';
3273 sPassword = 'password';
3274
3275 oGuest = oSession.o.console.guest;
3276 oGuestSession = oGuest.createSession(sUser,
3277 sPassword,
3278 "", "Manual Test");
3279
3280 aWaitFor = [ vboxcon.GuestSessionWaitForFlag_Start ];
3281 _ = oGuestSession.waitForArray(aWaitFor, 30 * 1000);
3282
3283 sCmd = 'c:\\windows\\system32\\cmd.exe';
3284 aArgs = [ '/C', 'dir', '/S', 'c:\\windows' ];
3285 aEnv = [];
3286 aFlags = [];
3287
3288 for _ in range(100):
3289 oProc = oGuestSession.processCreate(sCmd,
3290 aArgs, aEnv,
3291 aFlags, 30 * 1000);
3292
3293 aWaitFor = [ vboxcon.ProcessWaitForFlag_Terminate ];
3294 _ = oProc.waitForArray(aWaitFor, 30 * 1000);
3295
3296 oGuestSession.close();
3297 oGuestSession = None;
3298
3299 time.sleep(5);
3300
3301 oSession.o.console.PowerDown();
3302
3303 return (fRc, oTxsSession);
3304
3305if __name__ == '__main__':
3306 sys.exit(tdAddGuestCtrl().main(sys.argv));
3307
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette