VirtualBox

Ignore:
Timestamp:
Jan 26, 2017 4:59:23 PM (8 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
113086
Message:

ValidationKit/tdUsb1: Add option to capture USB traffic and upload it for failed tests

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/ValidationKit/tests/usb/tdUsb1.py

    r60898 r65461  
    108108        self.sGadgetHostnameDef    = 'usbtest.de.oracle.com';
    109109        self.uGadgetPortDef        = None;
     110        self.sUsbCapturePathDef    = self.sScratchPath;
     111        self.sUsbCapturePath       = self.sUsbCapturePathDef;
     112        self.fUsbCapture           = False;
    110113
    111114    #
     
    140143        reporter.log('  --default-gadget-port <port>');
    141144        reporter.log('      Default: %s' % (6042));
     145        reporter.log('  --usb-capture-path <path>');
     146        reporter.log('      Default: %s' % (self.sUsbCapturePathDef));
     147        reporter.log('  --usb-capture');
     148        reporter.log('      Whether to capture the USB traffic for each test');
    142149        return rc;
    143150
     
    222229                raise base.InvalidOption('The "--default-gadget-port" value "%s" is zero or negative.' \
    223230                    % (self.uGadgetPortDef,));
     231        elif asArgs[iArg] == '--usb-capture-path':
     232            if iArg >= len(asArgs): raise base.InvalidOption('The "--usb-capture-path" takes a path argument');
     233            self.sUsbCapturePath = asArgs[iArg];
     234        elif asArgs[iArg] == '--usb-capture':
     235            self.fUsbCapture = True;
    224236        else:
    225237            return vbox.TestDriver.parseOption(self, asArgs, iArg);
     
    306318        return (self.sGadgetHostnameDef, self.uGadgetPortDef);
    307319
     320    def getCaptureFilePath(self, sUsbCtrl, sSpeed):
     321        """
     322        Returns capture filename from the given data.
     323        """
     324
     325        return '%s/%s-%s.pcap' % (self.sUsbCapturePath, sUsbCtrl, sSpeed);
     326
     327    def attachUsbDeviceToVm(self, oSession, sVendorId, sProductId, iBusId,
     328                            sCaptureFile = None):
     329        """
     330        Attaches the given USB device to the VM either via a filter
     331        or directly if capturing the USB traffic is enabled.
     332
     333        Returns True on success, False on failure.
     334        """
     335        fRc = False;
     336        if sCaptureFile is None:
     337            fRc = oSession.addUsbDeviceFilter('Compliance device', sVendorId = sVendorId, sProductId = sProductId, \
     338                                              sPort = format(iBusId, 'x'));
     339        else:
     340            # Search for the correct device in the USB device list waiting for some time
     341            # to let it appear.
     342            iVendorId = int(sVendorId, 16);
     343            iProductId = int(sProductId, 16);
     344
     345            # Try a few times to give VBoxSVC a chance to detect the new device.
     346            for _ in xrange(5):
     347                fFound = False;
     348                aoUsbDevs = self.oVBoxMgr.getArray(self.oVBox.host, 'USBDevices');
     349                for oUsbDev in aoUsbDevs:
     350                    if     oUsbDev.vendorId == iVendorId \
     351                       and oUsbDev.productId == iProductId \
     352                       and oUsbDev.port == iBusId:
     353                        fFound = True;
     354                        fRc = oSession.attachUsbDevice(oUsbDev.id, sCaptureFile);
     355                        break;
     356
     357                if fFound:
     358                    break;
     359
     360                # Wait a moment until the next try.
     361                self.sleep(1);
     362
     363        if fRc:
     364            # Wait a moment to let the USB device appear
     365            self.sleep(3);
     366
     367        return fRc;
     368
    308369    #
    309370    # Test execution helpers.
    310371    #
    311     def testUsbCompliance(self, oSession, oTxsSession, sUsbCtrl, sSpeed):
     372    def testUsbCompliance(self, oSession, oTxsSession, sUsbCtrl, sSpeed, sCaptureFile = None):
    312373        """
    313374        Test VirtualBoxs USB stack in a VM.
     
    331392            if fRc is True:
    332393                iBusId, _ = oUsbGadget.getGadgetBusAndDevId();
    333                 fRc = oSession.addUsbDeviceFilter('Compliance device', sVendorId = '0525', sProductId = 'a4a0', \
    334                                                   sPort = format(iBusId, 'x'));
     394                fRc = self.attachUsbDeviceToVm(oSession, '0525', 'a4a0', iBusId, sCaptureFile);
    335395                if fRc is True:
    336 
    337                     # Wait a moment to let the USB device appear
    338                     self.sleep(3);
    339 
    340396                    tupCmdLine = ('UsbTest', );
    341397                    # Exclude a few tests which hang and cause a timeout, need investigation.
     
    362418        return fRc;
    363419
    364     def testUsbReattach(self, oSession, oTxsSession, sUsbCtrl, sSpeed): # pylint: disable=W0613
     420    def testUsbReattach(self, oSession, oTxsSession, sUsbCtrl, sSpeed, sCaptureFile = None): # pylint: disable=W0613
    365421        """
    366422        Tests that rapid connect/disconnect cycles work.
     
    383439            if fRc is True:
    384440                iBusId, _ = oUsbGadget.getGadgetBusAndDevId();
    385                 fRc = oSession.addUsbDeviceFilter('Compliance device', sVendorId = '0525', sProductId = 'a4a0', \
    386                                                   sPort = str(iBusId));
     441                fRc = self.attachUsbDeviceToVm(oSession, '0525', 'a4a0', iBusId, sCaptureFile);
    387442                if fRc is True:
    388443
     
    456511                self.sleep(5);
    457512
     513                sCaptureFile = None;
     514                if self.fUsbCapture:
     515                    sCaptureFile = self.getCaptureFilePath(sUsbCtrl, sSpeed);
     516
    458517                if sUsbTest == 'Compliance':
    459                     fRc = self.testUsbCompliance(oSession, oTxsSession, sUsbCtrl, sSpeed);
     518                    fRc = self.testUsbCompliance(oSession, oTxsSession, sUsbCtrl, sSpeed, sCaptureFile);
    460519                elif sUsbTest == 'Reattach':
    461                     fRc = self.testUsbReattach(oSession, oTxsSession, sUsbCtrl, sSpeed);
     520                    fRc = self.testUsbReattach(oSession, oTxsSession, sUsbCtrl, sSpeed, sCaptureFile);
    462521
    463522                # cleanup.
    464523                self.removeTask(oTxsSession);
    465524                self.terminateVmBySession(oSession)
     525
     526                # Add the traffic dump if it exists and the test failed
     527                if reporter.testErrorCount() > 0 \
     528                   and sCaptureFile is not None \
     529                   and os.path.exists(sCaptureFile):
     530                    reporter.addLogFile(sCaptureFile, 'misc/other', 'USB traffic dump');
    466531            else:
    467532                fRc = False;
     
    472537        Runs one VM thru the various configurations.
    473538        """
     539        fRc = False;
    474540        reporter.testStart(sVmName);
    475541        for sUsbCtrl in self.asUsbCtrls:
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