Index: /trunk/src/VBox/Main/include/USBGetDevices.h
===================================================================
--- /trunk/src/VBox/Main/include/USBGetDevices.h	(revision 37617)
+++ /trunk/src/VBox/Main/include/USBGetDevices.h	(revision 37618)
@@ -85,4 +85,24 @@
 
 #ifdef UNIT_TEST
+void TestUSBSetupInit(const char *pcszUsbfsRoot, bool fUsbfsAccessible,
+                      const char *pcszDevicesRoot, bool fDevicesAccessible,
+                      int rcMethodInitResult);
+void TestUSBSetEnv(const char *pcszEnvUsb, const char *pcszEnvUsbRoot);
+#endif
+
+/**
+ * Selects the access method that will be used to access USB devices based on
+ * what is available on the host and what if anything the user has specified
+ * in the environment.
+ * @returns iprt status value
+ * @param  pfUsingUsbfsDevices  on success this will be set to true if 
+ *                              the prefered access method is USBFS-like and to
+ *                              false if it is sysfs/device node-like
+ * @param  ppcszDevicesRoot     on success the root of the tree of USBFS-like
+ *                              device nodes will be stored here
+ */
+extern int USBProxyLinuxChooseMethod(bool *pfUsingUsbfsDevices,
+                                     const char **ppcszDevicesRoot);
+#ifdef UNIT_TEST
 /**
  * Specify the list of devices that will appear to be available through
Index: /trunk/src/VBox/Main/include/USBProxyService.h
===================================================================
--- /trunk/src/VBox/Main/include/USBProxyService.h	(revision 37617)
+++ /trunk/src/VBox/Main/include/USBProxyService.h	(revision 37618)
@@ -197,11 +197,4 @@
 #  endif
 
-#ifdef UNIT_TEST
-void TestUSBSetupInit(const char *pcszUsbfsRoot, bool fUsbfsAccessible,
-                      const char *pcszDevicesRoot, bool fDevicesAccessible,
-                      int rcMethodInitResult);
-void TestUSBSetEnv(const char *pcszEnvUsb, const char *pcszEnvUsbRoot);
-#endif
-
 /**
  * The Linux hosted USB Proxy Service.
@@ -217,10 +210,4 @@
     virtual int captureDevice(HostUSBDevice *aDevice);
     virtual int releaseDevice(HostUSBDevice *aDevice);
-
-#  ifdef UNIT_TEST
-    /* Test getters for querying internal state.  This will go away. */
-    bool testGetUsingUsbfs(void) { return mUsingUsbfsDevices; }
-    const char *testGetDevicesRoot(void) { return mDevicesRoot.c_str(); }
-#  endif
 
 protected:
Index: /trunk/src/VBox/Main/src-server/linux/USBGetDevices.cpp
===================================================================
--- /trunk/src/VBox/Main/src-server/linux/USBGetDevices.cpp	(revision 37617)
+++ /trunk/src/VBox/Main/src-server/linux/USBGetDevices.cpp	(revision 37618)
@@ -23,4 +23,5 @@
 #include "USBGetDevices.h"
 
+#include <VBox/err.h>
 #include <VBox/usb.h>
 #include <VBox/usblib.h>
@@ -29,5 +30,7 @@
 #include <iprt/cdefs.h>
 #include <iprt/ctype.h>
-#include <iprt/err.h>
+#include <iprt/dir.h>
+#include <iprt/env.h>
+#include <iprt/file.h>
 #include <iprt/fs.h>
 #include <iprt/log.h>
@@ -1503,4 +1506,154 @@
 #endif
 
+#ifdef UNIT_TEST
+#  ifdef UNIT_TEST
+    /** The path we pretend the usbfs root is located at, or NULL. */
+    const char *s_pcszTestUsbfsRoot;
+    /** Should usbfs be accessible to the current user? */
+    bool s_fTestUsbfsAccessible;
+    /** The path we pretend the device node tree root is located at, or NULL. */
+    const char *s_pcszTestDevicesRoot;
+    /** Should the device node tree be accessible to the current user? */
+    bool s_fTestDevicesAccessible;
+    /** The result of the usbfs/inotify-specific init */
+    int s_rcTestMethodInitResult;
+    /** The value of the VBOX_USB environment variable. */
+    const char *s_pcszTestEnvUsb;
+    /** The value of the VBOX_USB_ROOT environment variable. */
+    const char *s_pcszTestEnvUsbRoot;
+#  endif
+
+/** Select which access methods will be available to the @a init method
+ * during unit testing, and (hack!) what return code it will see from
+ * the access method-specific initialisation. */
+void TestUSBSetupInit(const char *pcszUsbfsRoot, bool fUsbfsAccessible,
+                      const char *pcszDevicesRoot, bool fDevicesAccessible,
+                      int rcMethodInitResult)
+{
+    s_pcszTestUsbfsRoot = pcszUsbfsRoot;
+    s_fTestUsbfsAccessible = fUsbfsAccessible;
+    s_pcszTestDevicesRoot = pcszDevicesRoot;
+    s_fTestDevicesAccessible = fDevicesAccessible;
+    s_rcTestMethodInitResult = rcMethodInitResult;
+}
+
+/** Specify the environment that the @a init method will see during unit
+ * testing. */
+void TestUSBSetEnv(const char *pcszEnvUsb, const char *pcszEnvUsbRoot)
+{
+    s_pcszTestEnvUsb = pcszEnvUsb;
+    s_pcszTestEnvUsbRoot = pcszEnvUsbRoot;
+}
+
+/* For testing we redefine anything that accesses the outside world to
+ * return test values. */
+# define RTEnvGet(a) \
+    (  !RTStrCmp(a, "VBOX_USB") ? s_pcszTestEnvUsb \
+     : !RTStrCmp(a, "VBOX_USB_ROOT") ? s_pcszTestEnvUsbRoot \
+     : NULL)
+# define USBProxyLinuxCheckDeviceRoot(pcszPath, fUseNodes) \
+    (   ((fUseNodes) && s_fTestDevicesAccessible \
+                     && !RTStrCmp(pcszPath, s_pcszTestDevicesRoot)) \
+     || (!(fUseNodes) && s_fTestUsbfsAccessible \
+                      && !RTStrCmp(pcszPath, s_pcszTestUsbfsRoot)))
+# define RTDirExists(pcszDir) \
+    (   (pcszDir) \
+     && (   !RTStrCmp(pcszDir, s_pcszTestDevicesRoot) \
+         || !RTStrCmp(pcszDir, s_pcszTestUsbfsRoot)))
+# define RTFileExists(pcszFile) \
+    (   (pcszFile) \
+     && s_pcszTestUsbfsRoot \
+     && !RTStrNCmp(pcszFile, s_pcszTestUsbfsRoot, strlen(s_pcszTestUsbfsRoot)) \
+     && !RTStrCmp(pcszFile + strlen(s_pcszTestUsbfsRoot), "/devices"))
+#endif
+
+/**
+ * Selects the access method that will be used to access USB devices based on
+ * what is available on the host and what if anything the user has specified
+ * in the environment.
+ * @returns iprt status value
+ * @param  pfUsingUsbfsDevices  on success this will be set to true if 
+ *                              the prefered access method is USBFS-like and to
+ *                              false if it is sysfs/device node-like
+ * @param  ppcszDevicesRoot     on success the root of the tree of USBFS-like
+ *                              device nodes will be stored here
+ */
+int USBProxyLinuxChooseMethod(bool *pfUsingUsbfsDevices,
+                              const char **ppcszDevicesRoot)
+{
+    /*
+     * We have two methods available for getting host USB device data - using
+     * USBFS and using sysfs.  The default choice is sysfs; if that is not
+     * available we fall back to USBFS.
+     * In the event of both failing, an appropriate error will be returned.
+     * The user may also specify a method and root using the VBOX_USB and
+     * VBOX_USB_ROOT environment variables.  In this case we don't check
+     * the root they provide for validity.
+     */
+    bool fUsbfsChosen = false, fSysfsChosen = false;
+    const char *pcszUsbFromEnv = RTEnvGet("VBOX_USB");
+    const char *pcszUsbRoot = NULL;
+    if (pcszUsbFromEnv)
+    {
+        bool fValidVBoxUSB = true;
+
+        pcszUsbRoot = RTEnvGet("VBOX_USB_ROOT");
+        if (!RTStrICmp(pcszUsbFromEnv, "USBFS"))
+        {
+            LogRel(("Default USB access method set to \"usbfs\" from environment\n"));
+            fUsbfsChosen = true;
+        }
+        else if (!RTStrICmp(pcszUsbFromEnv, "SYSFS"))
+        {
+            LogRel(("Default USB method set to \"sysfs\" from environment\n"));
+            fSysfsChosen = true;
+        }
+        else
+        {
+            LogRel(("Invalid VBOX_USB environment variable setting \"%s\"\n",
+                    pcszUsbFromEnv));
+            fValidVBoxUSB = false;
+            pcszUsbFromEnv = NULL;
+        }
+        if (!fValidVBoxUSB && pcszUsbRoot)
+            pcszUsbRoot = NULL;
+    }
+    if (!pcszUsbRoot)
+    {
+        if (   !fUsbfsChosen
+            && USBProxyLinuxCheckDeviceRoot("/dev/vboxusb", true))
+        {
+            fSysfsChosen = true;
+            pcszUsbRoot = "/dev/vboxusb";
+        }
+        else if (   !fSysfsChosen
+                 && USBProxyLinuxCheckDeviceRoot("/proc/bus/usb", false))
+        {
+            fUsbfsChosen = true;
+            pcszUsbRoot = "/proc/bus/usb";
+        }
+    }
+    else if (!USBProxyLinuxCheckDeviceRoot(pcszUsbRoot, fSysfsChosen))
+        pcszUsbRoot = NULL;
+    if (pcszUsbRoot)
+    {
+        *pfUsingUsbfsDevices = fUsbfsChosen;
+        *ppcszDevicesRoot = pcszUsbRoot;
+        return VINF_SUCCESS;
+    }
+    /* else */
+    return   pcszUsbFromEnv ? VERR_NOT_FOUND
+           : RTDirExists("/dev/vboxusb") ? VERR_VUSB_USB_DEVICE_PERMISSION
+           : RTFileExists("/proc/bus/usb/devices") ? VERR_VUSB_USBFS_PERMISSION
+           : VERR_NOT_FOUND;
+}
+
+#ifdef UNIT_TEST
+# undef RTEnvGet
+# undef USBProxyLinuxCheckDeviceRoot
+# undef RTDirExists
+# undef RTFileExists
+#endif
+
 bool USBProxyLinuxCheckDeviceRoot(const char *pcszRoot, bool fIsDeviceNodes)
 {
Index: /trunk/src/VBox/Main/src-server/linux/USBProxyServiceLinux.cpp
===================================================================
--- /trunk/src/VBox/Main/src-server/linux/USBProxyServiceLinux.cpp	(revision 37617)
+++ /trunk/src/VBox/Main/src-server/linux/USBProxyServiceLinux.cpp	(revision 37618)
@@ -68,147 +68,4 @@
 }
 
-#ifdef UNIT_TEST
-#  ifdef UNIT_TEST
-    /** The path we pretend the usbfs root is located at, or NULL. */
-    const char *s_pcszTestUsbfsRoot;
-    /** Should usbfs be accessible to the current user? */
-    bool s_fTestUsbfsAccessible;
-    /** The path we pretend the device node tree root is located at, or NULL. */
-    const char *s_pcszTestDevicesRoot;
-    /** Should the device node tree be accessible to the current user? */
-    bool s_fTestDevicesAccessible;
-    /** The result of the usbfs/inotify-specific init */
-    int s_rcTestMethodInitResult;
-    /** The value of the VBOX_USB environment variable. */
-    const char *s_pcszTestEnvUsb;
-    /** The value of the VBOX_USB_ROOT environment variable. */
-    const char *s_pcszTestEnvUsbRoot;
-#  endif
-
-/** Select which access methods will be available to the @a init method
- * during unit testing, and (hack!) what return code it will see from
- * the access method-specific initialisation. */
-void TestUSBSetupInit(const char *pcszUsbfsRoot, bool fUsbfsAccessible,
-                      const char *pcszDevicesRoot, bool fDevicesAccessible,
-                      int rcMethodInitResult)
-{
-    s_pcszTestUsbfsRoot = pcszUsbfsRoot;
-    s_fTestUsbfsAccessible = fUsbfsAccessible;
-    s_pcszTestDevicesRoot = pcszDevicesRoot;
-    s_fTestDevicesAccessible = fDevicesAccessible;
-    s_rcTestMethodInitResult = rcMethodInitResult;
-}
-
-/** Specify the environment that the @a init method will see during unit
- * testing. */
-void TestUSBSetEnv(const char *pcszEnvUsb, const char *pcszEnvUsbRoot)
-{
-    s_pcszTestEnvUsb = pcszEnvUsb;
-    s_pcszTestEnvUsbRoot = pcszEnvUsbRoot;
-}
-
-/* For testing we redefine anything that accesses the outside world to
- * return test values. */
-# define RTEnvGet(a) \
-    (  !RTStrCmp(a, "VBOX_USB") ? s_pcszTestEnvUsb \
-     : !RTStrCmp(a, "VBOX_USB_ROOT") ? s_pcszTestEnvUsbRoot \
-     : NULL)
-# define USBProxyLinuxCheckDeviceRoot(pcszPath, fUseNodes) \
-    (   ((fUseNodes) && s_fTestDevicesAccessible \
-                     && !RTStrCmp(pcszPath, s_pcszTestDevicesRoot)) \
-     || (!(fUseNodes) && s_fTestUsbfsAccessible \
-                      && !RTStrCmp(pcszPath, s_pcszTestUsbfsRoot)))
-# define RTDirExists(pcszDir) \
-    (   (pcszDir) \
-     && (   !RTStrCmp(pcszDir, s_pcszTestDevicesRoot) \
-         || !RTStrCmp(pcszDir, s_pcszTestUsbfsRoot)))
-# define RTFileExists(pcszFile) \
-    (   (pcszFile) \
-     && s_pcszTestUsbfsRoot \
-     && !RTStrNCmp(pcszFile, s_pcszTestUsbfsRoot, strlen(s_pcszTestUsbfsRoot)) \
-     && !RTStrCmp(pcszFile + strlen(s_pcszTestUsbfsRoot), "/devices"))
-#endif
-
-/**
- * Selects the access method that will be used to access USB devices based on
- * what is available on the host and what if anything the user has specified
- * in the environment.
- * @returns iprt status value
- * @param  pfUsingUsbfsDevices  on success this will be set to true if 
- *                              the prefered access method is USBFS-like and to
- *                              false if it is sysfs/device node-like
- * @param  ppcszDevicesRoot     on success the root of the tree of USBFS-like
- *                              device nodes will be stored here
- */
-int USBProxyLinuxChooseMethod(bool *pfUsingUsbfsDevices,
-                              const char **ppcszDevicesRoot)
-{
-    /*
-     * We have two methods available for getting host USB device data - using
-     * USBFS and using sysfs.  The default choice is sysfs; if that is not
-     * available we fall back to USBFS.
-     * In the event of both failing, an appropriate error will be returned.
-     * The user may also specify a method and root using the VBOX_USB and
-     * VBOX_USB_ROOT environment variables.  In this case we don't check
-     * the root they provide for validity.
-     */
-    bool fUsbfsChosen = false, fSysfsChosen = false;
-    const char *pcszUsbFromEnv = RTEnvGet("VBOX_USB");
-    const char *pcszUsbRoot = NULL;
-    if (pcszUsbFromEnv)
-    {
-        bool fValidVBoxUSB = true;
-
-        pcszUsbRoot = RTEnvGet("VBOX_USB_ROOT");
-        if (!RTStrICmp(pcszUsbFromEnv, "USBFS"))
-        {
-            LogRel(("Default USB access method set to \"usbfs\" from environment\n"));
-            fUsbfsChosen = true;
-        }
-        else if (!RTStrICmp(pcszUsbFromEnv, "SYSFS"))
-        {
-            LogRel(("Default USB method set to \"sysfs\" from environment\n"));
-            fSysfsChosen = true;
-        }
-        else
-        {
-            LogRel(("Invalid VBOX_USB environment variable setting \"%s\"\n",
-                    pcszUsbFromEnv));
-            fValidVBoxUSB = false;
-            pcszUsbFromEnv = NULL;
-        }
-        if (!fValidVBoxUSB && pcszUsbRoot)
-            pcszUsbRoot = NULL;
-    }
-    if (!pcszUsbRoot)
-    {
-        if (   !fUsbfsChosen
-            && USBProxyLinuxCheckDeviceRoot("/dev/vboxusb", true))
-        {
-            fSysfsChosen = true;
-            pcszUsbRoot = "/dev/vboxusb";
-        }
-        else if (   !fSysfsChosen
-                 && USBProxyLinuxCheckDeviceRoot("/proc/bus/usb", false))
-        {
-            fUsbfsChosen = true;
-            pcszUsbRoot = "/proc/bus/usb";
-        }
-    }
-    else if (!USBProxyLinuxCheckDeviceRoot(pcszUsbRoot, fSysfsChosen))
-        pcszUsbRoot = NULL;
-    if (pcszUsbRoot)
-    {
-        *pfUsingUsbfsDevices = fUsbfsChosen;
-        *ppcszDevicesRoot = pcszUsbRoot;
-        return VINF_SUCCESS;
-    }
-    /* else */
-    return   pcszUsbFromEnv ? VERR_NOT_FOUND
-           : RTDirExists("/dev/vboxusb") ? VERR_VUSB_USB_DEVICE_PERMISSION
-           : RTFileExists("/proc/bus/usb/devices") ? VERR_VUSB_USBFS_PERMISSION
-           : VERR_NOT_FOUND;
-}
-
 /**
  * Initializes the object (called right after construction).
@@ -223,9 +80,5 @@
     {
         mDevicesRoot = pcszDevicesRoot;
-#ifndef UNIT_TEST /* Hack for now */
         rc = mUsingUsbfsDevices ? initUsbfs() : initSysfs();
-#else
-        rc = s_rcTestMethodInitResult;
-#endif
         /* For the day when we have VBoxSVC release logging... */
         LogRel((RT_SUCCESS(rc) ? "Successfully initialised host USB using %s\n"
@@ -236,11 +89,4 @@
     return S_OK;
 }
-
-#ifdef UNIT_TEST
-# undef RTEnvGet
-# undef USBProxyLinuxCheckDeviceRoot
-# undef RTDirExists
-# undef RTFileExists
-#endif
 
 /**
Index: /trunk/src/VBox/Main/testcase/tstUSBProxyLinux.cpp
===================================================================
--- /trunk/src/VBox/Main/testcase/tstUSBProxyLinux.cpp	(revision 37617)
+++ /trunk/src/VBox/Main/testcase/tstUSBProxyLinux.cpp	(revision 37618)
@@ -89,6 +89,4 @@
     /* "sysfs" and valid root in the environment */
     { "sysfs", "/dev/bus/usb", "/dev/bus/usb", true, NULL, false, VINF_SUCCESS, "/dev/bus/usb", false, VINF_SUCCESS },
-    /* "sysfs" and valid root in the environment, method-specific init failed */
-    { "sysfs", "/dev/bus/usb",  "/dev/bus/usb", true, NULL, false, VERR_NO_MEMORY, "/dev/bus/usb", false, VERR_NO_MEMORY },
     /* "sysfs" and bad root in the environment */
     { "sysfs", "/dev/bus/usb", "/dev/vboxusb", false, "/proc/usb/bus", false, VINF_SUCCESS, "", true, VERR_NOT_FOUND },
@@ -97,6 +95,4 @@
     /* "usbfs" and valid root in the environment */
     { "usbfs", "/dev/bus/usb", NULL, false, "/dev/bus/usb", true, VINF_SUCCESS, "/dev/bus/usb", true, VINF_SUCCESS },
-    /* "usbfs" and valid root in the environment, method-specific init failed */
-    { "usbfs", "/dev/bus/usb", NULL, false, "/dev/bus/usb", true, VERR_NO_MEMORY, "/dev/bus/usb", true, VERR_NO_MEMORY },
     /* "usbfs" and bad root in the environment */
     { "usbfs", "/dev/bus/usb", "/dev/vboxusb", false, "/proc/usb/bus", false, VINF_SUCCESS, "", true, VERR_NOT_FOUND },
@@ -119,34 +115,27 @@
     /* No environment, sysfs available but without access permissions. */
     { NULL, NULL, "/dev/vboxusb", false, NULL, false, VERR_NO_MEMORY, "", true, VERR_VUSB_USB_DEVICE_PERMISSION },
-    /* No environment, sysfs available with access permissions, method-specific init failed. */
-    { NULL, NULL, "/dev/vboxusb", true, NULL, false, VERR_NO_MEMORY, "/dev/vboxusb", false, VERR_NO_MEMORY },
     /* No environment, usbfs available but without access permissions. */
     { NULL, NULL, NULL, false, "/proc/bus/usb", false, VERR_NO_MEMORY, "", true, VERR_VUSB_USBFS_PERMISSION },
-    /* No environment, usbfs available with access permissions, method-specific
-     * init failed. */
-    { NULL, NULL, NULL, false, "/proc/bus/usb", true, VERR_NO_MEMORY, "/proc/bus/usb", true, VERR_NO_MEMORY }
 };
 
 static void testInit(RTTEST hTest)
 {
-    RTTestSub(hTest, "Testing USBProxyServiceLinux initialisation");
+    RTTestSub(hTest, "Testing USBProxyLinuxChooseMethod");
     for (unsigned i = 0; i < RT_ELEMENTS(s_testEnvironment); ++i)
     {
-        USBProxyServiceLinux test(NULL);
+        bool fUsingUsbfs = true;
+        const char *pcszDevicesRoot = "";
+
         TestUSBSetEnv(s_testEnvironment[i].pcszEnvUsb,
-                        s_testEnvironment[i].pcszEnvUsbRoot);
+                      s_testEnvironment[i].pcszEnvUsbRoot);
         TestUSBSetupInit(s_testEnvironment[i].pcszUsbfsRoot,
-                           s_testEnvironment[i].fUsbfsAccessible,
-                           s_testEnvironment[i].pcszDevicesRoot,
-                           s_testEnvironment[i].fDevicesAccessible,
-                           s_testEnvironment[i].rcMethodInit);
-        HRESULT hrc = test.init();
-        RTTESTI_CHECK_MSG(hrc == S_OK,
-                           ("init() returned 0x%x (test index %i)!\n", hrc, i));
-        int rc = test.getLastError();
+                         s_testEnvironment[i].fUsbfsAccessible,
+                         s_testEnvironment[i].pcszDevicesRoot,
+                         s_testEnvironment[i].fDevicesAccessible,
+                         s_testEnvironment[i].rcMethodInit);
+        int rc = USBProxyLinuxChooseMethod(&fUsingUsbfs, &pcszDevicesRoot);
         RTTESTI_CHECK_MSG(rc == s_testEnvironment[i].rcExpected,
-                          ("getLastError() returned %Rrc (test index %i) instead of %Rrc!\n",
+                          ("rc=%Rrc (test index %i) instead of %Rrc!\n",
                            rc, i, s_testEnvironment[i].rcExpected));
-        const char *pcszDevicesRoot = test.testGetDevicesRoot();
         RTTESTI_CHECK_MSG(!RTStrCmp(pcszDevicesRoot,
                                s_testEnvironment[i].pcszDevicesRootExpected),
@@ -154,5 +143,4 @@
                            pcszDevicesRoot, i,
                            s_testEnvironment[i].pcszDevicesRootExpected));
-        bool fUsingUsbfs = test.testGetUsingUsbfs();
         RTTESTI_CHECK_MSG(   fUsingUsbfs
                           == s_testEnvironment[i].fUsingUsbfsExpected,
