VirtualBox

Ignore:
Timestamp:
Jun 17, 2019 5:41:18 PM (6 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
131366
Message:

vboxtestfileset.py: Validate the files copied to the host from the guest. bugref:9151 bugref:9320

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/ValidationKit/tests/additions/tdAddGuestCtrl.py

    r79185 r79193  
    239239
    240240class tdTestCopyFromDir(tdTestCopyFrom):
    241     pass;
     241
     242    def __init__(self, sSrc = "", sDst = "", sUser = None, sPassword = None, fFlags = None, oSrc = None, fIntoDst = False):
     243        tdTestCopyFrom.__init__(self, sSrc, sDst, sUser, sPassword, fFlags, oSrc);
     244        self.fIntoDst = fIntoDst; # hint to the verification code that sDst == oSrc, rather than sDst+oSrc.sNAme == oSrc.
    242245
    243246class tdTestCopyFromFile(tdTestCopyFrom):
     
    259262                os.rmdir(self.sDir);
    260263            except:
    261                 reporter.errorXcpt('%s: sDir=%s' % (sMsgPrefix, self.sDir,));
     264                return reporter.errorXcpt('%s: sDir=%s' % (sMsgPrefix, self.sDir,));
    262265        return True;
    263266
     
    11241127        _ = oSession;
    11251128
    1126         #reporter.log('Take snapshot now!');
    1127         #self.oTstDrv.sleep(22);
    1128         #return False;
    1129 
    11301129        # Make sure the temporary directory exists.
    11311130        for sDir in [self.getGuestTempDir(oTestVm), ]:
     
    11421141    #
    11431142
    1144     def gctrlCopyFileFrom(self, oGuestSession, sSrc, sDst, fFlags, fExpected):
     1143    def gctrlCopyFileFrom(self, oGuestSession, oTest, fExpected):
    11451144        """
    11461145        Helper function to copy a single file from the guest to the host.
    11471146        """
    1148         reporter.log2('Copying guest file "%s" to host "%s"' % (sSrc, sDst));
     1147        #
     1148        # Do the copying.
     1149        #
     1150        reporter.log2('Copying guest file "%s" to host "%s"' % (oTest.sSrc, oTest.sDst));
    11491151        try:
    11501152            if self.oTstDrv.fpApiVer >= 5.0:
    1151                 oCurProgress = oGuestSession.fileCopyFromGuest(sSrc, sDst, fFlags);
     1153                oCurProgress = oGuestSession.fileCopyFromGuest(oTest.sSrc, oTest.sDst, oTest.fFlags);
    11521154            else:
    1153                 oCurProgress = oGuestSession.copyFrom(sSrc, sDst, fFlags);
     1155                oCurProgress = oGuestSession.copyFrom(oTest.sSrc, oTest.sDst, oTest.fFlags);
    11541156        except:
    1155             reporter.maybeErrXcpt(fExpected, 'Copy from exception for sSrc="%s", sDst="%s":' % (sSrc, sDst,));
     1157            reporter.maybeErrXcpt(fExpected, 'Copy from exception for sSrc="%s", sDst="%s":' % (oTest.sSrc, oTest.sDst,));
    11561158            return False;
    11571159        if oCurProgress is None:
     
    11621164            oProgress.logResult(fIgnoreErrors = not fExpected);
    11631165            return False;
     1166
     1167        #
     1168        # Check the result if we can.
     1169        #
     1170        if oTest.oSrc:
     1171            assert isinstance(oTest.oSrc, testfileset.TestFile);
     1172            sDst = oTest.sDst;
     1173            if os.path.isdir(sDst):
     1174                sDst = os.path.join(sDst, oTest.oSrc.sName);
     1175            try:
     1176                oFile = open(sDst, 'rb');
     1177            except:
     1178                return reporter.errorXcpt('open(%s) failed during verfication' % (sDst,));
     1179            fEqual = oTest.oSrc.equalFile(oFile);
     1180            oFile.close();
     1181            if not fEqual:
     1182                return reporter.error('Content differs for "%s"' % (sDst,));
     1183
    11641184        return True;
    11651185
    1166     def gctrlCopyDirFrom(self, oGuestSession, sSrc, sDst, fFlags, fExpected):
     1186    def __compareTestDir(self, oDir, sHostPath): # type: (testfileset.TestDir, str) -> bool
     1187        """
     1188        Recursively compare the content of oDir and sHostPath.
     1189
     1190        Returns True on success, False + error logging on failure.
     1191
     1192        Note! This ASSUMES that nothing else was copied to sHostPath!
     1193        """
     1194        #
     1195        # First check out all the entries and files in the directory.
     1196        #
     1197        dLeftUpper = dict(oDir.dChildrenUpper);
     1198        try:
     1199            asEntries = os.listdir(sHostPath);
     1200        except:
     1201            return reporter.errorXcpt('os.listdir(%s) failed' % (sHostPath,));
     1202
     1203        fRc = True;
     1204        for sEntry in asEntries:
     1205            sEntryUpper = sEntry.upper();
     1206            if sEntryUpper not in dLeftUpper:
     1207                fRc = reporter.error('Unexpected entry "%s" in "%s"' % (sEntry, sHostPath,));
     1208            else:
     1209                oFsObj = dLeftUpper[sEntryUpper];
     1210                del dLeftUpper[sEntryUpper];
     1211
     1212                if isinstance(oFsObj, testfileset.TestFile):
     1213                    sFilePath = os.path.join(sHostPath, oFsObj.sName);
     1214                    try:
     1215                        oFile = open(sFilePath, 'rb');
     1216                    except:
     1217                        fRc = reporter.errorXcpt('open(%s) failed during verfication' % (sFilePath,));
     1218                    else:
     1219                        fEqual = oFsObj.equalFile(oFile);
     1220                        oFile.close();
     1221                        if not fEqual:
     1222                            fRc = reporter.error('Content differs for "%s"' % (sFilePath,));
     1223
     1224        # List missing entries:
     1225        for sKey in dLeftUpper:
     1226            oEntry = dLeftUpper[sKey];
     1227            fRc = reporter.error('%s: Missing %s "%s" (src path: %s)'
     1228                                 % (sHostPath, oEntry.sName,
     1229                                    'file' if isinstance(oEntry, testfileset.TestFile) else 'directory', oEntry.sPath));
     1230
     1231        #
     1232        # Recurse into subdirectories.
     1233        #
     1234        for oFsObj in oDir.aoChildren:
     1235            if isinstance(oFsObj, testfileset.TestDir):
     1236                fRc = self.__compareTestDir(oFsObj, os.path.join(sHostPath, oFsObj.sName)) and fRc;
     1237        return fRc;
     1238
     1239    def gctrlCopyDirFrom(self, oGuestSession, oTest, fExpected):
    11671240        """
    11681241        Helper function to copy a directory from the guest to the host.
    11691242        """
    1170         reporter.log2('Copying guest dir "%s" to host "%s"' % (sSrc, sDst));
     1243        #
     1244        # Do the copying.
     1245        #
     1246        reporter.log2('Copying guest dir "%s" to host "%s"' % (oTest.sSrc, oTest.sDst));
    11711247        try:
    1172             oCurProgress = oGuestSession.directoryCopyFromGuest(sSrc, sDst, fFlags);
     1248            oCurProgress = oGuestSession.directoryCopyFromGuest(oTest.sSrc, oTest.sDst, oTest.fFlags);
    11731249        except:
    1174             reporter.maybeErrXcpt(fExpected, 'Copy dir from exception for sSrc="%s", sDst="%s":' % (sSrc, sDst,));
     1250            reporter.maybeErrXcpt(fExpected, 'Copy dir from exception for sSrc="%s", sDst="%s":' % (oTest.sSrc, oTest.sDst,));
    11751251            return False;
    11761252        if oCurProgress is None:
     
    11821258            oProgress.logResult(fIgnoreErrors = not fExpected);
    11831259            return False;
     1260
     1261        #
     1262        # Check the result if we can.
     1263        #
     1264        if oTest.oSrc:
     1265            assert isinstance(oTest.oSrc, testfileset.TestDir);
     1266            sDst = oTest.sDst;
     1267            if oTest.fIntoDst:
     1268                return self.__compareTestDir(oTest.oSrc, os.path.join(sDst, oTest.oSrc.sName));
     1269            oDummy = testfileset.TestDir(None, 'dummy');
     1270            oDummy.aoChildren = [oTest.oSrc,]
     1271            oDummy.dChildrenUpper = { oTest.oSrc.sName.upper(): oTest.oSrc, };
     1272            return self.__compareTestDir(oDummy, sDst);
    11841273        return True;
    11851274
     
    35763665        #
    35773666        sScratchHst             = os.path.join(self.oTstDrv.sScratchPath, "testGctrlCopyFrom");
     3667        sScratchDstDir1Hst      = os.path.join(sScratchHst, "dstdir1");
     3668        sScratchDstDir2Hst      = os.path.join(sScratchHst, "dstdir2");
     3669        sScratchDstDir3Hst      = os.path.join(sScratchHst, "dstdir3");
    35783670        oExistingFileGst        = self.oTestFiles.chooseRandomFile();
    35793671        oNonEmptyDirGst         = self.oTestFiles.chooseRandomDirFromTree(fNonEmpty = True);
     
    35943686        else:
    35953687            try:
    3596                 os.mkdir(sScratchHst)
     3688                os.mkdir(sScratchHst);
    35973689            except:
    3598                 return reporter.errorXcpt('sScratchHst=%s' % (sScratchHst, ));
     3690                return reporter.errorXcpt('os.mkdir(%s)' % (sScratchHst, ));
     3691
     3692        for sSubDir in (sScratchDstDir1Hst, sScratchDstDir2Hst, sScratchDstDir3Hst):
     3693            try:
     3694                os.mkdir(sSubDir);
     3695            except:
     3696                return reporter.errorXcpt('os.mkdir(%s)' % (sSubDir, ));
    35993697
    36003698        #
     
    36793777        atTests.extend([
    36803778            # Copy the empty guest directory (should end up as sScratchHst/empty):
    3681             [ tdTestCopyFromDir(oSrc = oEmptyDirGst, sDst = sScratchHst), tdTestResultSuccess() ],
     3779            [ tdTestCopyFromDir(oSrc = oEmptyDirGst, sDst = sScratchDstDir1Hst), tdTestResultSuccess() ],
    36823780            # Repeat -- this time it should fail, as the destination directory already exists (and
    36833781            # DirectoryCopyFlag_CopyIntoExisting is not specified):
    3684             [ tdTestCopyFromDir(oSrc = oEmptyDirGst, sDst = sScratchHst), tdTestResultFailure() ],
     3782            [ tdTestCopyFromDir(oSrc = oEmptyDirGst, sDst = sScratchDstDir1Hst), tdTestResultFailure() ],
    36853783            # Add the DirectoryCopyFlag_CopyIntoExisting flag being set and it should work.
    3686             [ tdTestCopyFromDir(oSrc = oEmptyDirGst, sDst = sScratchHst,
     3784            [ tdTestCopyFromDir(oSrc = oEmptyDirGst, sDst = sScratchDstDir1Hst,
    36873785                                fFlags = [ vboxcon.DirectoryCopyFlag_CopyIntoExisting, ]), tdTestResultSuccess() ],
    3688             # Try again with trailing slash
    3689             [ tdTestRemoveHostDir(os.path.join(sScratchHst, 'empty')), tdTestResult() ],
    3690             [ tdTestCopyFromDir(oSrc = oEmptyDirGst, sDst = sScratchHst + os.path.sep),
     3786            # Try again with trailing slash, should yield the same result:
     3787            [ tdTestRemoveHostDir(os.path.join(sScratchDstDir1Hst, 'empty')), tdTestResult() ],
     3788            [ tdTestCopyFromDir(oSrc = oEmptyDirGst, sDst = sScratchDstDir1Hst + os.path.sep),
    36913789              tdTestResultSuccess() ],
    3692             [ tdTestCopyFromDir(oSrc = oEmptyDirGst, sDst = sScratchHst + os.path.sep),
     3790            [ tdTestCopyFromDir(oSrc = oEmptyDirGst, sDst = sScratchDstDir1Hst + os.path.sep),
    36933791              tdTestResultFailure() ],
    3694             [ tdTestCopyFromDir(oSrc = oEmptyDirGst, sDst = sScratchHst + os.path.sep,
     3792            [ tdTestCopyFromDir(oSrc = oEmptyDirGst, sDst = sScratchDstDir1Hst + os.path.sep,
    36953793                                fFlags = [ vboxcon.DirectoryCopyFlag_CopyIntoExisting, ]), tdTestResultSuccess() ],
    3696             # Copy with a different destination name:
    3697             [ tdTestCopyFromDir(oSrc = oEmptyDirGst, sDst = os.path.join(sScratchHst, 'empty2')),
     3794            # Copy with a different destination name just for the heck of it:
     3795            [ tdTestCopyFromDir(oSrc = oEmptyDirGst, sDst = os.path.join(sScratchHst, 'empty2'), fIntoDst = True),
    36983796              tdTestResultFailure() ],
    36993797            # Now the same using a directory with files in it:
    3700             [ tdTestCopyFromDir(oSrc = oNonEmptyDirGst, sDst = sScratchHst), tdTestResultSuccess() ],
    3701             [ tdTestCopyFromDir(oSrc = oNonEmptyDirGst, sDst = sScratchHst), tdTestResultFailure() ],
    3702             [ tdTestCopyFromDir(oSrc = oNonEmptyDirGst, sDst = sScratchHst,
     3798            [ tdTestCopyFromDir(oSrc = oNonEmptyDirGst, sDst = sScratchDstDir2Hst), tdTestResultSuccess() ],
     3799            [ tdTestCopyFromDir(oSrc = oNonEmptyDirGst, sDst = sScratchDstDir2Hst), tdTestResultFailure() ],
     3800            [ tdTestCopyFromDir(oSrc = oNonEmptyDirGst, sDst = sScratchDstDir2Hst,
    37033801                                fFlags = [ vboxcon.DirectoryCopyFlag_CopyIntoExisting, ]), tdTestResultSuccess() ],
    37043802            # Copy the entire test tree:
    3705             [ tdTestCopyFromDir(sSrc = self.oTestFiles.oTreeDir.sPath, sDst = sScratchHst), tdTestResultSuccess() ],
     3803            [ tdTestCopyFromDir(sSrc = self.oTestFiles.oTreeDir.sPath, sDst = sScratchDstDir3Hst), tdTestResultSuccess() ],
    37063804        ]);
    37073805
     
    37333831
    37343832                if isinstance(oCurTest, tdTestCopyFromFile):
    3735                     fRc2 = self.gctrlCopyFileFrom(oCurGuestSession, oCurTest.sSrc, oCurTest.sDst, oCurTest.fFlags, oCurRes.fRc);
     3833                    fRc2 = self.gctrlCopyFileFrom(oCurGuestSession, oCurTest, oCurRes.fRc);
    37363834                else:
    3737                     fRc2 = self.gctrlCopyDirFrom(oCurGuestSession, oCurTest.sSrc, oCurTest.sDst, oCurTest.fFlags, oCurRes.fRc);
     3835                    fRc2 = self.gctrlCopyDirFrom(oCurGuestSession, oCurTest, oCurRes.fRc);
    37383836
    37393837                if fRc2 != oCurRes.fRc:
    37403838                    fRc = reporter.error('Test #%d failed: Got %s, expected %s' % (i, fRc2, oCurRes.fRc));
    37413839                    fRc2 = False;
    3742 
    3743                 if fRc2 is True:
    3744                     pass; ## @todo verify the result.
    37453840
    37463841                fRc = oCurTest.closeSession(fIsError = True) and fRc;
Note: See TracChangeset for help on using the changeset viewer.

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