VirtualBox

Ignore:
Timestamp:
Jun 24, 2019 11:00:02 AM (6 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
131520
Message:

tdAddGuestCtrl.py: Added offset checks to the write testcase. bugref:9151 bugref:9320

File:
1 edited

Legend:

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

    r79292 r79303  
    429429        return True;
    430430
    431     def doStepsOnOpenedFile(self, fExpectSuccess, fpApiVer):
     431    def doStepsOnOpenedFile(self, fExpectSuccess, oSubTst):
    432432        """ Overridden by children to do more testing. """
    433         _ = fExpectSuccess; _ = fpApiVer;
     433        _ = fExpectSuccess; _ = oSubTst;
    434434        return True;
    435435
     
    446446        return True;
    447447
    448     def doSteps(self, fExpectSuccess, fpApiVer):
     448    def doSteps(self, fExpectSuccess, oSubTst):
    449449        """ Do the tests. """
    450450        fRc = self.doOpenStep(fExpectSuccess);
    451451        if fRc is True:
    452             fRc = self.doStepsOnOpenedFile(fExpectSuccess, fpApiVer);
     452            fRc = self.doStepsOnOpenedFile(fExpectSuccess, oSubTst);
    453453        if self.oOpenedFile:
    454454            fRc = self.doCloseStep() and fRc;
     
    468468        return 'cbOpenExpected=%s %s' % (self.cbOpenExpected, tdTestFileOpen.toString(self),);
    469469
    470     def doStepsOnOpenedFile(self, fExpectSuccess, fpApiVer):
     470    def doStepsOnOpenedFile(self, fExpectSuccess, oSubTst):
    471471        #
    472472        # Call parent.
    473473        #
    474         fRc = tdTestFileOpen.doStepsOnOpenedFile(self, fExpectSuccess, fpApiVer);
     474        fRc = tdTestFileOpen.doStepsOnOpenedFile(self, fExpectSuccess, oSubTst);
    475475
    476476        #
    477477        # Check the size.  Requires 6.0 or later (E_NOTIMPL in 5.2).
    478478        #
    479         if fpApiVer >= 6.0:
     479        if oSubTst.oTstDrv.fpApiVer >= 6.0:
    480480            try:
    481481                oFsObjInfo = self.oOpenedFile.queryInfo();
     
    521521        self.atChunks  = atChunks   # type: list(tuple(int,bytearray))
    522522        self.fUseAtApi = fUseAtApi;
     523        self.fAppend   = (   eAccessMode in (vboxcon.FileAccessMode_AppendOnly, vboxcon.FileAccessMode_AppendRead)
     524                          or eAction == vboxcon.FileOpenAction_AppendOrCreate);
    523525        self.abContent = abContent  # type: bytearray
    524526
     
    528530        return '%s [%s] %s' % (sApi, sChunks, tdTestFileOpen.toString(self),);
    529531
    530     def doStepsOnOpenedFile(self, fExpectSuccess, fpApiVer):
     532    def doStepsOnOpenedFile(self, fExpectSuccess, oSubTst):
    531533        #
    532534        # Call parent.
    533535        #
    534         fRc = tdTestFileOpen.doStepsOnOpenedFile(self, fExpectSuccess, fpApiVer);
     536        fRc = tdTestFileOpen.doStepsOnOpenedFile(self, fExpectSuccess, oSubTst);
    535537
    536538        #
    537539        # Do the writing.
    538540        #
    539         for tChunk in self.atChunks:
    540             offFile, abBuf = tChunk;
    541             assert offFile is not None or not self.fUseAtApi;
     541        for offFile, abBuf in self.atChunks:
    542542            if self.fUseAtApi:
     543                #
     544                # writeAt:
     545                #
     546                assert offFile is not None;
    543547                reporter.log2('writeAt(%s, %s bytes)' % (offFile, len(abBuf),));
     548                if self.fAppend:
     549                    if self.abContent is not None: # Try avoid seek as it updates the cached offset in GuestFileImpl.
     550                        offExpectAfter = len(self.abContent);
     551                    else:
     552                        try:
     553                            offSave        = self.oOpenedFile.seek(0, vboxcon.FileSeekOrigin_Current);
     554                            offExpectAfter = self.oOpenedFile.seek(0, vboxcon.FileSeekOrigin_End);
     555                            self.oOpenedFile.seek(offSave, vboxcon.FileSeekOrigin_Begin);
     556                        except:
     557                            return reporter.errorXcpt();
     558                    offExpectAfter += len(abBuf);
     559                else:
     560                    offExpectAfter = offFile + len(abBuf);
     561
    544562                try:
    545563                    cbWritten = self.oOpenedFile.writeAt(offFile, abBuf, 30*1000);
    546564                except:
    547565                    return reporter.errorXcpt('writeAt(%s, %s bytes)' % (offFile, len(abBuf),));
     566
    548567            else:
     568                #
     569                # write:
     570                #
     571                if self.fAppend:
     572                    if self.abContent is not None: # Try avoid seek as it updates the cached offset in GuestFileImpl.
     573                        offExpectAfter = len(self.abContent);
     574                    else:
     575                        try:
     576                            offSave        = self.oOpenedFile.seek(0, vboxcon.FileSeekOrigin_Current);
     577                            offExpectAfter = self.oOpenedFile.seek(0, vboxcon.FileSeekOrigin_End);
     578                            self.oOpenedFile.seek(offSave, vboxcon.FileSeekOrigin_Begin);
     579                        except:
     580                            return reporter.errorXcpt('seek(0,End)');
    549581                if offFile is not None:
    550582                    try:
    551583                        self.oOpenedFile.seek(offFile, vboxcon.FileSeekOrigin_Begin);
    552584                    except:
    553                         return reporter.errorXcpt('seek(%s,Begin)' % (offFile, ));
    554                 elif self.eAction != vboxcon.FileOpenAction_AppendOrCreate:
     585                        return reporter.errorXcpt('seek(%s,Begin)' % (offFile,));
     586                else:
    555587                    try:
    556588                        offFile = self.oOpenedFile.seek(0, vboxcon.FileSeekOrigin_Current);
    557589                    except:
    558590                        return reporter.errorXcpt();
     591                if not self.fAppend:
     592                    offExpectAfter = offFile;
     593                offExpectAfter += len(abBuf);
    559594
    560595                reporter.log2('write(%s bytes @ %s)' % (len(abBuf), offFile,));
     
    564599                    return reporter.errorXcpt('write(%s bytes @ %s)' % (len(abBuf), offFile));
    565600
     601            #
    566602            # Check how much was written, ASSUMING nothing we push thru here is too big:
     603            #
    567604            if cbWritten != len(abBuf):
    568605                fRc = reporter.errorXcpt('Wrote less than expected: %s out of %s, expected all to be written'
    569606                                         % (cbWritten, len(abBuf),));
     607                if not self.fAppend:
     608                    offExpectAfter -= len(abBuf) - cbWritten;
    570609
    571610            #
     
    576615                    abBuf = abBuf[:cbWritten];
    577616
     617                #
    578618                # In append mode, the current file offset shall be disregarded and the
    579619                # write always goes to the end of the file, regardless of writeAt or write.
    580                 # Note that RTFileWriteAt only naturally behaves this way on linux, so
    581                 # VBoxService makes the linux behaviour general across all OSes.
    582                 if self.eAction == vboxcon.FileOpenAction_AppendOrCreate:
    583                     reporter.log('len(self.abContent)=%s + %s' % (len(self.abContent), cbWritten, ));
     620                # Note that RTFileWriteAt only naturally behaves this way on linux and
     621                # (probably) windows, so VBoxService makes that behaviour generic across
     622                # all OSes.
     623                #
     624                if self.fAppend:
     625                    reporter.log2('len(self.abContent)=%s + %s' % (len(self.abContent), cbWritten, ));
    584626                    self.abContent.extend(abBuf);
    585                 elif offFile is not None:
    586                     reporter.log('len(self.abContent)=%s + %s @ %s' % (len(self.abContent), cbWritten, offFile, ));
     627                else:
     628                    if offFile is None:
     629                        offFile = offExpectAfter - cbWritten;
     630                    reporter.log2('len(self.abContent)=%s + %s @ %s' % (len(self.abContent), cbWritten, offFile, ));
    587631                    if offFile > len(self.abContent):
    588632                        self.abContent.extend(bytearray(offFile - len(self.abContent)));
    589633                    self.abContent[offFile:offFile + cbWritten] = abBuf;
    590                 reporter.log('len(self.abContent)=%s' % (len(self.abContent),));
    591 
     634                reporter.log2('len(self.abContent)=%s' % (len(self.abContent),));
     635
     636            #
     637            # Check the resulting file offset with IGuestFile::offset.
     638            #
     639            try:
     640                offApi  = self.oOpenedFile.offset; # Must be gotten first!
     641                offSeek = self.oOpenedFile.seek(0, vboxcon.FileSeekOrigin_Current);
     642            except:
     643                fRc = reporter.errorXcpt();
     644            else:
     645                reporter.log2('offApi=%s offSeek=%s offExpectAfter=%s' % (offApi, offSeek, offExpectAfter,));
     646                if offSeek != offExpectAfter:
     647                    fRc = reporter.error('Seek offset is %s, expected %s after %s bytes write @ %s (offApi=%s)'
     648                                         % (offSeek, offExpectAfter, len(abBuf), offFile, offApi,));
     649                if offApi != offExpectAfter:
     650                    fRc = reporter.error('IGuestFile::offset is %s, expected %s after %s bytes write @ %s (offSeek=%s)'
     651                                         % (offApi, offExpectAfter, len(abBuf), offFile, offSeek,));
     652        # for each chunk - end
    592653        return fRc;
    593654
     
    605666        return 'check content %s (%s) %s' % (len(self.abContent), self.cbContentExpected, tdTestFileOpen.toString(self),);
    606667
    607     def doStepsOnOpenedFile(self, fExpectSuccess, fpApiVer):
     668    def doStepsOnOpenedFile(self, fExpectSuccess, oSubTst):
    608669        #
    609670        # Call parent.
    610671        #
    611         fRc = tdTestFileOpen.doStepsOnOpenedFile(self, fExpectSuccess, fpApiVer);
     672        fRc = tdTestFileOpen.doStepsOnOpenedFile(self, fExpectSuccess, oSubTst);
    612673
    613674        #
     
    36083669                break;
    36093670
    3610             fRc2 = oCurTest.doSteps(oCurRes.fRc, self.oTstDrv.fpApiVer);
     3671            fRc2 = oCurTest.doSteps(oCurRes.fRc, self);
    36113672            if fRc2 != oCurRes.fRc:
    36123673                fRc = reporter.error('Test #%d result mismatch: Got %s, expected %s' % (i, fRc2, oCurRes.fRc,));
     
    40324093                                   atChunks = [(1024, randBytes(63)), (1080, randBytes(968)),]),
    40334094            tdTestFileOpenAndCheckContent(sFile = sFile, abContent = abContent, cbContentExpected = 1080+968), # 2048
    4034             # writeAt beyond the end:
     4095
     4096            # writeAt beyond the end (gap is filled with zeros):
    40354097            tdTestFileOpenAndWrite(sFile = sFile, abContent = abContent, fUseAtApi = True, atChunks = [(3070, randBytes(2)),]),
    40364098            tdTestFileOpenAndCheckContent(sFile = sFile, abContent = abContent, cbContentExpected = 3072),
    4037             # write beyond the end:
     4099            # write beyond the end (gap is filled with zeros):
    40384100            tdTestFileOpenAndWrite(sFile = sFile, abContent = abContent, atChunks = [(4090, randBytes(6)),]),
    40394101            tdTestFileOpenAndCheckContent(sFile = sFile, abContent = abContent, cbContentExpected = 4096),
     
    40494111                break;
    40504112
    4051             fRc2 = oCurTest.doSteps(True, self.oTstDrv.fpApiVer);
     4113            fRc2 = oCurTest.doSteps(True, self);
    40524114            if fRc2 is not True:
    40534115                fRc = reporter.error('Test #%d failed!' % (i,));
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