From 73a3c5a3065062645577437c4c09a22a1f98ed93 Mon Sep 17 00:00:00 2001
From: Tristan Matthews <tristan.matthews@savoirfairelinux.com>
Date: Wed, 29 Jun 2011 17:51:17 -0400
Subject: [PATCH] * #6300: tests do not require an installed sflphone

Instead, they will use environment variables.
---
 hudson-sflphone-script.sh                     |  2 +-
 .../src/audio/codecs/audiocodecfactory.cpp    |  4 +++
 sflphone-common/src/plug-in/pluginmanager.cpp | 18 ++++++++--
 sflphone-common/test/Makefile.am              |  3 ++
 sflphone-common/test/pluginmanagertest.cpp    | 33 +++++++++----------
 sflphone-common/test/pluginmanagertest.h      |  2 ++
 6 files changed, 41 insertions(+), 21 deletions(-)

diff --git a/hudson-sflphone-script.sh b/hudson-sflphone-script.sh
index 3cd75ab500..a0b1ab68d1 100755
--- a/hudson-sflphone-script.sh
+++ b/hudson-sflphone-script.sh
@@ -29,7 +29,7 @@ pushd sflphone-common/test
 rm -rf $XML_RESULTS
 make check
 # if at least one test failed, exit
-./test --xml || exit 1
+CODECS_PATH="../src/audio/codecs" FAKE_PLUGIN_DIR="../src/plug-in/test/" FAKE_PLUGIN_NAME="../src/plug-in/test/libplugintest.so" ./test --xml || exit 1
 popd
 
 # Compile the client
diff --git a/sflphone-common/src/audio/codecs/audiocodecfactory.cpp b/sflphone-common/src/audio/codecs/audiocodecfactory.cpp
index e751d1cab5..2dc459c41f 100644
--- a/sflphone-common/src/audio/codecs/audiocodecfactory.cpp
+++ b/sflphone-common/src/audio/codecs/audiocodecfactory.cpp
@@ -192,9 +192,13 @@ std::vector<sfl::Codec*> AudioCodecFactory::scanCodecDirectory (void)
 
     std::string libDir = std::string (CODECS_DIR).append ("/");
     std::string homeDir = std::string (HOMEDIR)  + DIR_SEPARATOR_STR + "." + PROGDIR + "/";
+    // look for a CODECS_PATH environment variable...used in tests
+    const char *envDir = getenv("CODECS_PATH");
     std::vector<std::string> dirToScan;
     dirToScan.push_back (homeDir);
     dirToScan.push_back (libDir);
+    if (envDir)
+        dirToScan.push_back(std::string(envDir) + DIR_SEPARATOR_STR);
 
     for (i = 0 ; (unsigned int) i < dirToScan.size() ; i++) {
         std::string dirStr = dirToScan[i];
diff --git a/sflphone-common/src/plug-in/pluginmanager.cpp b/sflphone-common/src/plug-in/pluginmanager.cpp
index 92027908a9..4789973859 100644
--- a/sflphone-common/src/plug-in/pluginmanager.cpp
+++ b/sflphone-common/src/plug-in/pluginmanager.cpp
@@ -56,6 +56,15 @@ PluginManager::~PluginManager()
     _instance = 0;
 }
 
+namespace {
+bool hasSharedExtension(const std::string &fn)
+{
+    size_t dot_position = fn.rfind(".");
+    return (dot_position != std::string::npos and
+            fn.substr(dot_position) == ".so");
+}
+}
+
 int
 PluginManager::loadPlugins (const std::string &path)
 {
@@ -69,7 +78,10 @@ PluginManager::loadPlugins (const std::string &path)
     const std::string cDir = ".";
 
     /* The directory in which plugins are dropped. Default: /usr/lib/sflphone/plugins/ */
-    (path == "") ? pluginDir = std::string (PLUGINS_DIR).append ("/") :pluginDir = path;
+    if (path.empty())
+        pluginDir = std::string (PLUGINS_DIR).append("/");
+    else
+        pluginDir = path;
     _debug ("Loading plugins from %s...", pluginDir.c_str());
 
     dir = opendir (pluginDir.c_str());
@@ -80,9 +92,9 @@ PluginManager::loadPlugins (const std::string &path)
         while ( (dirStruct=readdir (dir))) {
             /* Get the name of the current item in the directory */
             current = dirStruct->d_name;
-            /* Test if the current item is not the parent or the current directory */
+            /* Test if the current item is not the parent or the current directory and that it ends with .so*/
 
-            if (current != pDir && current != cDir) {
+            if (current != pDir && current != cDir and hasSharedExtension(current)) {
 
                 /* Load the dynamic library */
                 library = loadDynamicLibrary (pluginDir + current);
diff --git a/sflphone-common/test/Makefile.am b/sflphone-common/test/Makefile.am
index 42741e2b36..e62c988530 100644
--- a/sflphone-common/test/Makefile.am
+++ b/sflphone-common/test/Makefile.am
@@ -1,5 +1,8 @@
 include ../globals.mak
 
+TESTS_ENVIRONMENT = CODECS_PATH="$(top_builddir)/src/audio/codecs" \
+					FAKE_PLUGIN_DIR="$(top_builddir)/src/plug-in/test/" \
+					FAKE_PLUGIN_NAME="$(top_builddir)/src/plug-in/test/libplugintest.so"
 check_PROGRAMS = test
 
 test_CXXFLAGS = $(CPPUNIT_CFLAGS)
diff --git a/sflphone-common/test/pluginmanagertest.cpp b/sflphone-common/test/pluginmanagertest.cpp
index 9182dab99a..cffbf4c3bb 100644
--- a/sflphone-common/test/pluginmanagertest.cpp
+++ b/sflphone-common/test/pluginmanagertest.cpp
@@ -37,13 +37,13 @@
 using std::cout;
 using std::endl;
 
-#define PLUGIN_TEST_DIR  "/usr/lib/sflphone/plugins/"
-#define PLUGIN_TEST_DESC  "mytest"
-#define PLUGIN_TEST_NAME  "/usr/lib/sflphone/plugins/libplugintest.so"
+#define FAKE_PLUGIN_DESC  "mytest"
 
 
 void PluginManagerTest::setUp()
 {
+    FAKE_PLUGIN_DIR = std::string(getenv("FAKE_PLUGIN_DIR"));
+    FAKE_PLUGIN_NAME =  std::string(getenv("FAKE_PLUGIN_NAME"));
     // Instanciate the plugin manager singleton
     _pm = PluginManager::instance();
     library = 0;
@@ -54,14 +54,14 @@ void PluginManagerTest::testLoadDynamicLibrary()
 {
     _debug ("-------------------- PluginManagerTest::testLoadDynamicLibrary --------------------\n");
 
-    CPPUNIT_ASSERT (_pm->loadDynamicLibrary (PLUGIN_TEST_NAME) != NULL);
+    CPPUNIT_ASSERT (_pm->loadDynamicLibrary (FAKE_PLUGIN_NAME) != NULL);
 }
 
 void PluginManagerTest::testUnloadDynamicLibrary()
 {
     _debug ("-------------------- PluginManagerTest::testUnloadDynamicLibrary --------------------\n");
 
-    library = _pm->loadDynamicLibrary (PLUGIN_TEST_NAME);
+    library = _pm->loadDynamicLibrary (FAKE_PLUGIN_NAME);
     CPPUNIT_ASSERT (library != NULL);
     CPPUNIT_ASSERT (_pm->unloadDynamicLibrary (library) == 0);
 }
@@ -70,7 +70,7 @@ void PluginManagerTest::testInstanciatePlugin()
 {
     _debug ("-------------------- PluginManagerTest::testInstanciatePlugin --------------------\n");
 
-    library = _pm->loadDynamicLibrary (PLUGIN_TEST_NAME);
+    library = _pm->loadDynamicLibrary (FAKE_PLUGIN_NAME);
     CPPUNIT_ASSERT (library != NULL);
     CPPUNIT_ASSERT (_pm->instanciatePlugin (library, &plugin) == 0);
     CPPUNIT_ASSERT (plugin!=NULL);
@@ -80,32 +80,31 @@ void PluginManagerTest::testInitPlugin()
 {
     _debug ("-------------------- PluginManagerTest::testInitPlugin --------------------\n");
 
-    library = _pm->loadDynamicLibrary (PLUGIN_TEST_NAME);
+    library = _pm->loadDynamicLibrary (FAKE_PLUGIN_NAME);
     CPPUNIT_ASSERT (library != NULL);
     CPPUNIT_ASSERT (_pm->instanciatePlugin (library, &plugin) == 0);
     CPPUNIT_ASSERT (plugin!=NULL);
-    CPPUNIT_ASSERT (plugin->getPluginName() == PLUGIN_TEST_DESC);
+    CPPUNIT_ASSERT (plugin->getPluginName() == FAKE_PLUGIN_DESC);
 }
 
 void PluginManagerTest::testRegisterPlugin()
 {
     _debug ("-------------------- PluginManagerTest::testRegisterPlugin --------------------\n");
 
-    library = _pm->loadDynamicLibrary (PLUGIN_TEST_NAME);
+    library = _pm->loadDynamicLibrary (FAKE_PLUGIN_NAME);
     CPPUNIT_ASSERT (library != NULL);
     CPPUNIT_ASSERT (_pm->instanciatePlugin (library, &plugin) == 0);
-    CPPUNIT_ASSERT (_pm->isPluginLoaded (PLUGIN_TEST_DESC) == false);
+    CPPUNIT_ASSERT (_pm->isPluginLoaded (FAKE_PLUGIN_DESC) == false);
     CPPUNIT_ASSERT (_pm->registerPlugin (plugin, library) == 0);
-    CPPUNIT_ASSERT (_pm->isPluginLoaded (PLUGIN_TEST_DESC) == true);
+    CPPUNIT_ASSERT (_pm->isPluginLoaded (FAKE_PLUGIN_DESC) == true);
 }
 
 void PluginManagerTest::testLoadPlugins ()
 {
     _debug ("-------------------- PluginManagerTest::testLoadPlugins --------------------\n");
     try {
-    
-        CPPUNIT_ASSERT (_pm->loadPlugins (PLUGIN_TEST_DIR) == 0);
-        CPPUNIT_ASSERT (_pm->isPluginLoaded (PLUGIN_TEST_DESC) == true);
+        CPPUNIT_ASSERT (_pm->loadPlugins (FAKE_PLUGIN_DIR) == 0);
+        CPPUNIT_ASSERT (_pm->isPluginLoaded (FAKE_PLUGIN_DESC) == true);
     }
    catch (LibraryManagerException &e){
 	
@@ -118,10 +117,10 @@ void PluginManagerTest::testUnloadPlugins ()
 
     try {
 
-        CPPUNIT_ASSERT (_pm->loadPlugins (PLUGIN_TEST_DIR) == 0);
-        CPPUNIT_ASSERT (_pm->isPluginLoaded (PLUGIN_TEST_DESC) == true);
+        CPPUNIT_ASSERT (_pm->loadPlugins (FAKE_PLUGIN_DIR) == 0);
+        CPPUNIT_ASSERT (_pm->isPluginLoaded (FAKE_PLUGIN_DESC) == true);
         CPPUNIT_ASSERT (_pm->unloadPlugins () == 0);
-        CPPUNIT_ASSERT (_pm->isPluginLoaded (PLUGIN_TEST_DESC) == false);
+        CPPUNIT_ASSERT (_pm->isPluginLoaded (FAKE_PLUGIN_DESC) == false);
     }
     catch (LibraryManagerException &e) {
 
diff --git a/sflphone-common/test/pluginmanagertest.h b/sflphone-common/test/pluginmanagertest.h
index 35da2468f7..2a2e78bf78 100644
--- a/sflphone-common/test/pluginmanagertest.h
+++ b/sflphone-common/test/pluginmanagertest.h
@@ -94,6 +94,8 @@ class PluginManagerTest : public CppUnit::TestCase {
         void testUnloadPlugins ();
 
     private:
+        std::string FAKE_PLUGIN_DIR;
+        std::string FAKE_PLUGIN_NAME;
         PluginManager *_pm;
         LibraryManager *library;
         Plugin *plugin;
-- 
GitLab