Added test framework.

Signed-off-by: Michele Calgaro <michele.calgaro@yahoo.it>
pull/1/head
Michele Calgaro 2 years ago
parent 1200d940ad
commit 652c2aea23
Signed by: MicheleC
GPG Key ID: 2A75B7CA8ADED5CF

@ -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 #### ########################

3
debian/rules vendored

@ -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

@ -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( )

@ -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

@ -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;
}

@ -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;
}

@ -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;
}

@ -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;
}
Loading…
Cancel
Save