From 077a3aa8723304203b39d27b1be682e1ab739065 Mon Sep 17 00:00:00 2001
From: Tristan Matthews <tristan.matthews@savoirfairelinux.com>
Date: Wed, 6 Mar 2013 18:36:40 -0500
Subject: [PATCH] * #20736: hooks: added test, fixed code

UrlHooks with no arguments were silently failing
---
 daemon/src/hooks/Makefile.am |  2 --
 daemon/src/hooks/urlhook.cpp |  7 ++---
 daemon/test/Makefile.am      |  4 ++-
 daemon/test/hooktest.cpp     | 42 +++++++++++++++++++++++++++++
 daemon/test/hooktest.h       | 52 ++++++++++++++++++++++++++++++++++++
 5 files changed, 99 insertions(+), 8 deletions(-)
 create mode 100644 daemon/test/hooktest.cpp
 create mode 100644 daemon/test/hooktest.h

diff --git a/daemon/src/hooks/Makefile.am b/daemon/src/hooks/Makefile.am
index 149f232406..33a0882f81 100644
--- a/daemon/src/hooks/Makefile.am
+++ b/daemon/src/hooks/Makefile.am
@@ -1,5 +1,3 @@
-SUBDIRS =
-
 noinst_LTLIBRARIES = libhooks.la
 
 libhooks_la_SOURCES = \
diff --git a/daemon/src/hooks/urlhook.cpp b/daemon/src/hooks/urlhook.cpp
index ce7f96d2b2..e942343917 100644
--- a/daemon/src/hooks/urlhook.cpp
+++ b/daemon/src/hooks/urlhook.cpp
@@ -33,12 +33,9 @@
 
 int UrlHook::runAction(const std::string &command, const std::string &args)
 {
-    if (args.empty())
-        return 0;
-
     //FIXME : use fork and execve, so no need to escape shell arguments
-    std::string cmd = command + "\"" + args + "\" &";
+    const std::string cmd = command + (args.empty() ? "" : " ") +
+                            "\"" + args + "\" &";
     return system(cmd.c_str());
 }
 
-
diff --git a/daemon/test/Makefile.am b/daemon/test/Makefile.am
index 343e3c1518..3c9fc09df6 100644
--- a/daemon/test/Makefile.am
+++ b/daemon/test/Makefile.am
@@ -33,7 +33,9 @@ test_SOURCES = constants.h \
 			   mainbuffertest.h \
 			   mainbuffertest.cpp \
 			   resamplertest.h \
-			   resamplertest.cpp
+			   resamplertest.cpp \
+			   hooktest.h \
+			   hooktest.cpp
 
 if BUILD_SDES
 test_SOURCES+=sdesnegotiatortest.h \
diff --git a/daemon/test/hooktest.cpp b/daemon/test/hooktest.cpp
new file mode 100644
index 0000000000..067cb1c759
--- /dev/null
+++ b/daemon/test/hooktest.cpp
@@ -0,0 +1,42 @@
+/*
+ *  Copyright (C) 2004-2012 Savoir-Faire Linux Inc.
+ *  Author: Julien Bonjean <julien.bonjean@savoirfairelinux.com>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
+ *
+ *  Additional permission under GNU GPL version 3 section 7:
+ *
+ *  If you modify this program, or any covered work, by linking or
+ *  combining it with the OpenSSL project's OpenSSL library (or a
+ *  modified version of that library), containing parts covered by the
+ *  terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
+ *  grants you additional permission to convey the resulting work.
+ *  Corresponding Source for a non-source form of such a combination
+ *  shall include the source code for the parts of OpenSSL used as well
+ *  as that of the covered work.
+ */
+
+#include "hooktest.h"
+#include "hooks/urlhook.h"
+
+void HookTest::RunHookWithNoArgs()
+{
+    CPPUNIT_ASSERT(!UrlHook::runAction("ls", ""));
+}
+
+void HookTest::RunHookWithArgs()
+{
+    CPPUNIT_ASSERT(!UrlHook::runAction("ls", "-l"));
+}
diff --git a/daemon/test/hooktest.h b/daemon/test/hooktest.h
new file mode 100644
index 0000000000..aaf54c27ad
--- /dev/null
+++ b/daemon/test/hooktest.h
@@ -0,0 +1,52 @@
+/*
+ *  Copyright (C) 2013 Savoir-Faire Linux Inc.
+ *  Author: Tristan Matthews <tristan.matthews@savoirfairelinux.com>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
+ *
+ *  Additional permission under GNU GPL version 3 section 7:
+ *
+ *  If you modify this program, or any covered work, by linking or
+ *  combining it with the OpenSSL project's OpenSSL library (or a
+ *  modified version of that library), containing parts covered by the
+ *  terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
+ *  grants you additional permission to convey the resulting work.
+ *  Corresponding Source for a non-source form of such a combination
+ *  shall include the source code for the parts of OpenSSL used as well
+ *  as that of the covered work.
+ */
+
+#ifndef HOOKTEST_H_
+#define HOOKTEST_H_
+
+#include <cppunit/TestFixture.h>
+#include <cppunit/extensions/HelperMacros.h>
+
+class HookTest : public CppUnit::TestFixture {
+
+        CPPUNIT_TEST_SUITE(HookTest);
+        CPPUNIT_TEST(RunHookWithNoArgs);
+        CPPUNIT_TEST(RunHookWithArgs);
+        CPPUNIT_TEST_SUITE_END();
+
+    public:
+        void RunHookWithNoArgs();
+        void RunHookWithArgs();
+};
+
+CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(HookTest, "HookTest");
+CPPUNIT_TEST_SUITE_REGISTRATION(HookTest);
+
+#endif /* HOOKTEST_H_ */
-- 
GitLab