VirtualBox

Ignore:
Timestamp:
May 6, 2011 9:53:21 PM (14 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
71604
Message:

Main/linux/USB: unit tests for the code changed in r71554 and r71557

Location:
trunk/src/VBox/Main/src-server/linux
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/src-server/linux/USBGetDevices.cpp

    r36958 r36993  
    13901390}
    13911391
     1392#ifdef UNIT_TEST
     1393/* Set up mock functions for USBProxyLinuxCheckDeviceRoot - here dlsym and close
     1394 * for the inotify presence check. */
     1395static int testInotifyInitGood(void) { return 0; }
     1396static int testInotifyInitBad(void) { return -1; }
     1397static bool s_fHaveInotifyLibC = true;
     1398static bool s_fHaveInotifyKernel = true;
     1399
     1400static void *testDLSym(void *handle, const char *symbol)
     1401{
     1402    Assert(handle == RTLD_DEFAULT);
     1403    Assert(!RTStrCmp(symbol, "inotify_init"));
     1404    if (!s_fHaveInotifyLibC)
     1405        return NULL;
     1406    if (s_fHaveInotifyKernel)
     1407        return (void *)testInotifyInitGood;
     1408    return (void *)testInotifyInitBad;
     1409}
     1410
     1411void TestUSBSetInotifyAvailable(bool fHaveInotifyLibC, bool fHaveInotifyKernel)
     1412{
     1413    s_fHaveInotifyLibC = fHaveInotifyLibC;
     1414    s_fHaveInotifyKernel = fHaveInotifyKernel;
     1415}
     1416# define dlsym testDLSym
     1417# define close(a) do {} while(0)
     1418#endif
     1419
    13921420/** Is inotify available and working on this system?  This is a requirement
    13931421 * for using USB with sysfs */
     
    14061434}
    14071435
     1436#ifdef UNIT_TEST
     1437# undef dlsym
     1438# undef close
     1439#endif
     1440
     1441#ifdef UNIT_TEST
     1442/** Unit test list of usbfs addresses of connected devices. */
     1443static const char **s_pacszUsbfsDeviceAddresses = NULL;
     1444
     1445static PUSBDEVICE testGetUsbfsDevices(const char *pcszUsbfsRoot, bool testfs)
     1446{
     1447    const char **pcsz;
     1448    PUSBDEVICE pList = NULL, pTail = NULL;
     1449    for (pcsz = s_pacszUsbfsDeviceAddresses; pcsz && *pcsz; ++pcsz)
     1450    {
     1451        PUSBDEVICE pNext = (PUSBDEVICE)RTMemAllocZ(sizeof(USBDEVICE));
     1452        if (pNext)
     1453            pNext->pszAddress = RTStrDup(*pcsz);
     1454        if (!pNext || !pNext->pszAddress)
     1455        {
     1456            deviceListFree(&pList);
     1457            return NULL;
     1458        }
     1459        if (pTail)
     1460            pTail->pNext = pNext;
     1461        else
     1462            pList = pNext;
     1463        pTail = pNext;
     1464    }
     1465    return pList;
     1466}
     1467# define getDevicesFromUsbfs testGetUsbfsDevices
     1468
     1469void TestUSBSetAvailableUsbfsDevices(const char **pacszDeviceAddresses)
     1470{
     1471    s_pacszUsbfsDeviceAddresses = pacszDeviceAddresses;
     1472}
     1473
     1474/** Unit test list of files reported as accessible by access(3).  We only do
     1475 * accessible or not accessible. */
     1476static const char **s_pacszAccessibleFiles = NULL;
     1477
     1478static int testAccess(const char *pcszPath, int mode)
     1479{
     1480    const char **pcsz;
     1481    for (pcsz = s_pacszAccessibleFiles; pcsz && *pcsz; ++pcsz)
     1482        if (!RTStrCmp(pcszPath, *pcsz))
     1483            return 0;
     1484    return -1;
     1485}
     1486# define access testAccess
     1487
     1488void TestUSBSetAccessibleFiles(const char **pacszAccessibleFiles)
     1489{
     1490    s_pacszAccessibleFiles = pacszAccessibleFiles;
     1491}
     1492#endif
     1493
    14081494bool USBProxyLinuxCheckDeviceRoot(const char *pcszRoot, bool fIsDeviceNodes)
    14091495{
     
    14311517}
    14321518
     1519#ifdef UNIT_TEST
     1520# undef getDevicesFromUsbfs
     1521# undef access
     1522#endif
    14331523
    14341524PUSBDEVICE USBProxyLinuxGetDevices(const char *pcszDevicesRoot,
  • trunk/src/VBox/Main/src-server/linux/USBProxyServiceLinux.cpp

    r36964 r36993  
    6363      mWakeupPipeW(NIL_RTFILE), mUsingUsbfsDevices(true /* see init */),
    6464      mUdevPolls(0), mpWaiter(NULL)
     65#ifdef UNIT_TEST
     66      , mpcszTestUsbfsRoot(NULL), mfTestUsbfsAccessible(false),
     67      mpcszTestDevicesRoot(NULL), mfTestDevicesAccessible(false),
     68      mrcTestMethodInitResult(VINF_SUCCESS), mpcszTestEnvUsb(NULL),
     69      mpcszTestEnvUsbRoot(NULL)
     70#endif
    6571{
    6672    LogFlowThisFunc(("aHost=%p\n", aHost));
    6773}
    6874
     75#ifdef UNIT_TEST
     76/* For testing we redefine anything that accesses the outside world to
     77 * return test values. */
     78# define RTEnvGet(a) \
     79    (  !RTStrCmp(a, "VBOX_USB") ? mpcszTestEnvUsb \
     80     : !RTStrCmp(a, "VBOX_USB_ROOT") ? mpcszTestEnvUsbRoot \
     81     : NULL)
     82# define USBProxyLinuxCheckDeviceRoot(pcszPath, fUseNodes) \
     83    (   ((fUseNodes) && mfTestDevicesAccessible \
     84                     && !RTStrCmp(pcszPath, mpcszTestDevicesRoot)) \
     85     || (!(fUseNodes) && mfTestUsbfsAccessible \
     86                      && !RTStrCmp(pcszPath, mpcszTestUsbfsRoot)))
     87# define RTDirExists(pcszDir) \
     88    (   (pcszDir) \
     89     && (   !RTStrCmp(pcszDir, mpcszTestDevicesRoot) \
     90         || !RTStrCmp(pcszDir, mpcszTestUsbfsRoot)))
     91# define RTFileExists(pcszFile) \
     92    (   (pcszFile) \
     93     && mpcszTestUsbfsRoot \
     94     && !RTStrNCmp(pcszFile, mpcszTestUsbfsRoot, strlen(mpcszTestUsbfsRoot)) \
     95     && !RTStrCmp(pcszFile + strlen(mpcszTestUsbfsRoot), "/devices"))
     96#endif
    6997
    7098/**
     
    131159        mUsingUsbfsDevices = !fUseSysfs;
    132160        mDevicesRoot = pcszUsbRoot;
     161#ifndef UNIT_TEST /* Hack for now */
    133162        int rc = mUsingUsbfsDevices ? initUsbfs() : initSysfs();
     163#else
     164        int rc = mrcTestMethodInitResult;
     165#endif
    134166        /* For the day when we have VBoxSVC release logging... */
    135167        LogRel((RT_SUCCESS(rc) ? "Successfully initialised host USB using %s\n"
     
    145177}
    146178
     179#ifdef UNIT_TEST
     180# undef RTEnvGet
     181# undef USBProxyLinuxCheckDeviceRoot
     182# undef RTDirExists
     183# undef RTFileExists
     184#endif
    147185
    148186/**
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