summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichele Calgaro <michele.calgaro@yahoo.it>2021-11-20 17:25:17 +0900
committerMichele Calgaro <michele.calgaro@yahoo.it>2021-11-20 17:25:17 +0900
commit652c2aea23bd9dea082f5de2c0babe1728ac608f (patch)
tree3e649e665e3b073d2017f727fa0515f27474abd2
parent1200d940add87b767246e45080e25ed207eee014 (diff)
downloadpolkit-tqt-652c2aea23bd9dea082f5de2c0babe1728ac608f.tar.gz
polkit-tqt-652c2aea23bd9dea082f5de2c0babe1728ac608f.zip
Added test framework.
Signed-off-by: Michele Calgaro <michele.calgaro@yahoo.it>
-rw-r--r--CMakeLists.txt2
-rwxr-xr-xdebian/rules3
-rw-r--r--tests/CMakeLists.txt70
-rwxr-xr-xtests/run_test.sh48
-rw-r--r--tests/test_check_authorization.cpp61
-rw-r--r--tests/test_details.cpp57
-rw-r--r--tests/test_identity.cpp53
-rw-r--r--tests/test_subject.cpp32
8 files changed, 326 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index ba276505b..fc3526b0f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -27,6 +27,7 @@ include( FindPkgConfig )
include( CheckCXXSourceCompiles )
include( CheckSymbolExists )
include( TDEMacros )
+enable_testing( )
##### optional stuff
@@ -82,6 +83,7 @@ add_definitions(
add_subdirectory(core)
add_subdirectory(gui)
add_subdirectory(agent)
+add_subdirectory(tests)
##### install files #### ########################
diff --git a/debian/rules b/debian/rules
index bf14bf133..e06abc709 100755
--- a/debian/rules
+++ b/debian/rules
@@ -11,3 +11,6 @@ DEB_CMAKE_EXTRA_FLAGS := \
-DCMAKE_VERBOSE_MAKEFILE="ON" \
-DCMAKE_BUILD_TYPE=RelWithDebInfo \
-DWITH_ALL_OPTIONS="ON"
+
+DEB_MAKE_CHECK_TARGET = check
+
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
new file mode 100644
index 000000000..cc2083861
--- /dev/null
+++ b/tests/CMakeLists.txt
@@ -0,0 +1,70 @@
+#################################################
+#
+# (C) 2019 Michele Calgaro
+# michele (DOT) calgaro (AT) yahoo (DOT) it
+#
+# Improvements and feedbacks are welcome
+#
+# This file is released under GPL >= 3
+#
+#################################################
+
+include_directories(
+ ${TQT_INCLUDE_DIRS}
+ ${TDE_INCLUDE_DIR}
+ ${CMAKE_SOURCE_DIR}
+ ${CMAKE_BINARY_DIR}
+ ${CMAKE_CURRENT_SOURCE_DIR}
+ ${CMAKE_CURRENT_BINARY_DIR}
+ ${CMAKE_SOURCE_DIR}/core
+ ${CMAKE_SOURCE_DIR}/gui
+ ${CMAKE_SOURCE_DIR}/agent
+)
+
+set( link-polkit-tqt-test
+ ${TQT_LIBRARIES}
+ polkit-tqt-core-shared
+ polkit-tqt-gui-shared
+ polkit-tqt-agent-shared
+)
+
+
+### executable files
+
+# These tests are executed automatically during build
+set( _test_auto_executables
+ test_details
+ test_identity
+ test_subject
+)
+
+# These executables are built but no automatic test is performed.
+# To run these tests manually, a full polkit environment is needed
+# and either the test is run as root or the file
+# ${CMAKE_SOURCE_DIR}/examples/org.tqt.policykit.examples.policy
+# needs to be copied to the /usr/share/polkit-1/actions folder first.
+# If run as root, sudo is also required because the inner part of the
+# tests need to be run as a non root user. A "testuser" user is created
+# and removed during the test. To run a test, use the following commands:
+# cd ${CMAKE_CURRENT_BINARY_DIR}
+# ${CMAKE_CURRENT_SOURCE_DIR}/run_test.sh <path/to/test_executable_file>
+set( _test_manual_executables
+ test_check_authorization
+)
+
+foreach( _test_name ${_test_auto_executables} )
+ tde_add_check_executable( ${_test_name}
+ SOURCES ${_test_name}.cpp AUTOMOC
+ LINK ${link-polkit-tqt-test}
+ )
+
+ add_test( ${_test_name} ${_test_name} )
+endforeach( )
+
+foreach( _test_name ${_test_manual_executables} )
+ tde_add_check_executable( ${_test_name}
+ SOURCES ${_test_name}.cpp AUTOMOC
+ LINK ${link-polkit-tqt-test}
+ )
+endforeach( )
+
diff --git a/tests/run_test.sh b/tests/run_test.sh
new file mode 100755
index 000000000..15e15682b
--- /dev/null
+++ b/tests/run_test.sh
@@ -0,0 +1,48 @@
+#!/bin/sh
+
+SCRIPT_DIR=$(dirname $(readlink -f "$0"))
+
+# check user
+USERNAME=`whoami`
+I_AM_ROOT=n
+if [ "${USERNAME}" = "root" ]; then
+ I_AM_ROOT=y
+fi
+
+# check if polkit files need to be copied
+DEST_FILE="/usr/share/polkit-1/actions/org.tqt.policykit.examples.policy"
+FILE_EXISTS='n'
+if [ -f "${DEST_FILE}" ]; then
+ FILE_EXISTS='y'
+fi
+
+if [ "${I_AM_ROOT}" = "y" ]; then
+ if [ "${FILE_EXISTS}" = "n" ]; then
+ cp "${SCRIPT_DIR}/../examples/org.tqt.policykit.examples.policy" "${DEST_FILE}"
+ fi
+ useradd testuser
+ sudo -u testuser $1
+ retval=$?
+ userdel testuser
+ if [ "${FILE_EXISTS}" = "n" ]; then
+ rm "${DEST_FILE}"
+ fi
+else
+ if [ "${FILE_EXISTS}" = "y" ]; then
+ # Required file already exist, can do the test
+ $1
+ retval=$?
+ else
+ echo "This test requires file ${DEST_FILE} to exists. Skipping test."
+ retval=0
+ fi
+fi
+
+if [ ${retval} = 0 ]; then
+ echo "Test passed."
+else
+ echo "Test failed."
+fi
+
+exit $retval
+
diff --git a/tests/test_check_authorization.cpp b/tests/test_check_authorization.cpp
new file mode 100644
index 000000000..4fb6674c1
--- /dev/null
+++ b/tests/test_check_authorization.cpp
@@ -0,0 +1,61 @@
+
+#include <unistd.h>
+#include <tqstring.h>
+#include "core/polkit-tqt-authority.h"
+
+#define TEST_PASSED 0
+#define TEST_FAILED 1
+
+using namespace PolkitTQt;
+
+int main(void)
+{
+ // This needs the file org.tqt.policykit.examples.policy from examples to be installed
+ UnixProcessSubject process(getpid());
+ Authority::Result result;
+ // Check if this method returns good authorization results
+ Authority *authority = Authority::instance();
+ result = authority->checkAuthorizationSync("org.tqt.policykit.examples.kick", process, Authority::None);
+ if (result != Authority::No)
+ {
+ return TEST_FAILED;
+ }
+ if (authority->hasError())
+ {
+ return TEST_FAILED;
+ }
+ result = authority->checkAuthorizationSync("org.tqt.policykit.examples.cry", process, Authority::None);
+ if (result != Authority::Yes)
+ {
+ return TEST_FAILED;
+ }
+ if (authority->hasError())
+ {
+ return TEST_FAILED;
+ }
+ result = authority->checkAuthorizationSync("org.tqt.policykit.examples.bleed", process, Authority::None);
+ if (result != Authority::Challenge)
+ {
+ return TEST_FAILED;
+ }
+ if (authority->hasError())
+ {
+ return TEST_FAILED;
+ }
+
+ // Check if it can cancel user authentication dialog
+ authority->checkAuthorization("org.tqt.policykit.examples.bleed", process, Authority::AllowUserInteraction);
+ // Show it for second
+ sleep(1);
+ // And now kill it
+ authority->checkAuthorizationCancel();
+ if (authority->hasError())
+ {
+ return TEST_FAILED;
+ }
+ // But how to test if it was successful?
+ tqWarning("You should see an authentication dialog for a short period.");
+
+ return TEST_PASSED;
+}
+
diff --git a/tests/test_details.cpp b/tests/test_details.cpp
new file mode 100644
index 000000000..e74a9adfe
--- /dev/null
+++ b/tests/test_details.cpp
@@ -0,0 +1,57 @@
+
+#include <stdio.h>
+#include <tqstring.h>
+#include <tqstringlist.h>
+#include "core/polkit-tqt-details.h"
+
+#define TEST_PASSED 0
+#define TEST_FAILED 1
+
+using namespace PolkitTQt;
+
+int main(void)
+{
+ Details details;
+ details.insert("1", "aaa");
+ details.insert("2", "bbb");
+ details.insert("3", "ccc");
+ details.insert("4", "ddd");
+
+ if (details.lookup("1") != TQString("aaa"))
+ {
+ return TEST_FAILED;
+ }
+ if (details.lookup("2") != TQString("bbb"))
+ {
+ return TEST_FAILED;
+ }
+ if (details.lookup("3") != TQString("ccc"))
+ {
+ return TEST_FAILED;
+ }
+ if (details.lookup("4") != TQString("ddd"))
+ {
+ return TEST_FAILED;
+ }
+
+ TQStringList list = details.keys();
+ if (!list.contains("1"))
+ {
+ return TEST_FAILED;
+ }
+ if (!list.contains("2"))
+ {
+ return TEST_FAILED;
+ }
+ if (!list.contains("3"))
+ {
+ return TEST_FAILED;
+ }
+ if (!list.contains("4"))
+ {
+ return TEST_FAILED;
+ }
+
+ return TEST_PASSED;
+}
+
diff --git a/tests/test_identity.cpp b/tests/test_identity.cpp
new file mode 100644
index 000000000..340a8a4fd
--- /dev/null
+++ b/tests/test_identity.cpp
@@ -0,0 +1,53 @@
+
+#include <pwd.h>
+#include <tqstring.h>
+#include "core/polkit-tqt-identity.h"
+
+#define TEST_PASSED 0
+#define TEST_FAILED 1
+
+using namespace PolkitTQt;
+
+int main(void)
+{
+ int test_result = TEST_PASSED;
+
+ // Get real name and id of current user and group
+ struct passwd *userinfo = getpwuid(getuid());
+ TQString userName = userinfo->pw_name;
+ unsigned int userId = userinfo->pw_uid;
+ unsigned int groupId = userinfo->pw_gid;
+
+ // Create generic Identity from UnixUser via string representation
+ UnixUserIdentity user(userName);
+ if (!user.identity())
+ {
+ return TEST_FAILED;
+ }
+ Identity id = Identity::fromString(user.toString());
+ if (static_cast<UnixUserIdentity*>(&id)->uid() != userId)
+ {
+ return TEST_FAILED;
+ }
+
+ UnixGroupIdentity group(groupId);
+ if (!group.identity())
+ {
+ return TEST_FAILED;
+ }
+ id = Identity::fromString(group.toString());
+ if (static_cast<UnixGroupIdentity*>(&id)->gid() != groupId)
+ {
+ return TEST_FAILED;
+ }
+
+ // Test setting gid to another value
+ group.setGid(9999U);
+ id = Identity::fromString(group.toString());
+ if (static_cast<UnixGroupIdentity*>(&id)->gid() != 9999U)
+ {
+ return TEST_FAILED;
+ }
+
+ return TEST_PASSED;
+}
diff --git a/tests/test_subject.cpp b/tests/test_subject.cpp
new file mode 100644
index 000000000..b07317ec5
--- /dev/null
+++ b/tests/test_subject.cpp
@@ -0,0 +1,32 @@
+
+#include <unistd.h>
+#include <tqstring.h>
+#include "core/polkit-tqt-subject.h"
+
+#define TEST_PASSED 0
+#define TEST_FAILED 1
+
+using namespace PolkitTQt;
+
+int main(void)
+{
+ int test_result = TEST_PASSED;
+
+ // Create unix process for current process
+ TQ_LONG pid = getpid();
+ UnixProcessSubject *process = new UnixProcessSubject(pid);
+ if (process->pid() != pid)
+ {
+ test_result = TEST_FAILED;
+ }
+
+ // Serialize and deserialize subject
+ Subject subject = Subject::fromString(process->toString());
+ if (((UnixProcessSubject*)&subject)->pid() != pid)
+ {
+ test_result = TEST_FAILED;
+ }
+
+ delete process;
+ return test_result;
+}