From 49ed794c5a7f7670793ba8a84ef56d5a2db1c6dd Mon Sep 17 00:00:00 2001 From: Bartlomiej Grzelewski Date: Mon, 10 Jun 2013 17:27:49 +0200 Subject: [PATCH] Add tests program for security-server2 This commit contains binary security-client that may be used to test security-server2. Minor changes: * Remove duplicated optimalization option from build commands. * Remove --std=c++0x option from gcc command. * Set up DEBUG profile as default. [Issue#] N/A [Feature] Add security-client binary. [Cause] DPL framework is too limited to test security-server2. [Solution] N/A [Verification] Run "security-client echo" and "security-client rule oneoneone twotwotwo". In second case you shoud see new rule in system. Change-Id: I4c8316a8e9ce4e2a80d636a3e9235ccd50171e79 --- CMakeLists.txt | 15 +- packaging/security-tests.spec | 4 +- tests/CMakeLists.txt | 1 + tests/security-server-client/CMakeLists.txt | 24 +++ tests/security-server-client/client.c | 234 ++++++++++++++++++++++++++++ 5 files changed, 270 insertions(+), 8 deletions(-) create mode 100644 tests/security-server-client/CMakeLists.txt create mode 100644 tests/security-server-client/client.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 314b148..ce54dbe 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,12 +33,14 @@ INCLUDE(FindPkgConfig) ############################# compiler flags ################################## -SET(CMAKE_C_FLAGS_PROFILING "-O0 -g -pg") -SET(CMAKE_CXX_FLAGS_PROFILING "-O0 -std=c++0x -g -pg") -SET(CMAKE_C_FLAGS_DEBUG "-O0 -g") -SET(CMAKE_CXX_FLAGS_DEBUG "-O0 -std=c++0x -g") -SET(CMAKE_C_FLAGS_RELEASE "-O2 -g") -SET(CMAKE_CXX_FLAGS_RELEASE "-O2 -std=c++0x -g") +SET(CMAKE_C_FLAGS "-g") +SET(CMAKE_CXX_FLAGS "-g -std=c++0x") +SET(CMAKE_C_FLAGS_PROFILING "-O0 -pg") +SET(CMAKE_CXX_FLAGS_PROFILING "-O0 -pg") +SET(CMAKE_C_FLAGS_DEBUG "-O0 -ggdb") +SET(CMAKE_CXX_FLAGS_DEBUG "-O0 -ggdb") +SET(CMAKE_C_FLAGS_RELEASE "-O2") +SET(CMAKE_CXX_FLAGS_RELEASE "-O2") #SET(SMACK_ENABLE ON) @@ -66,7 +68,6 @@ ADD_DEFINITIONS("-Wall") # Generate all warnings ADD_DEFINITIONS("-Wextra") # Generate even more extra warnings ADD_DEFINITIONS("-Wno-variadic-macros") # Inhibit variadic macros warnings (needed for ORM) ADD_DEFINITIONS("-Wno-deprecated") # No warnings about deprecated features -ADD_DEFINITIONS("-std=c++0x") # No warnings about deprecated features STRING(REGEX MATCH "([^.]*)" API_VERSION "${VERSION}") ADD_DEFINITIONS("-DAPI_VERSION=\"$(API_VERSION)\"") diff --git a/packaging/security-tests.spec b/packaging/security-tests.spec index 8350fa6..cc6e9f0 100644 --- a/packaging/security-tests.spec +++ b/packaging/security-tests.spec @@ -27,7 +27,8 @@ export LDFLAGS+="-Wl,--rpath=%{_prefix}/lib" cmake . -DCMAKE_INSTALL_PREFIX=%{_prefix} \ -DDPL_LOG="ON" \ - -DVERSION=%{version} + -DVERSION=%{version} \ + -DCMAKE_BUILD_TYPE=DEBUG make %{?jobs:-j%jobs} %install @@ -69,6 +70,7 @@ osp-installer --uninstall=V5LKqDFBXm /usr/bin/security-server-tests-pid-reuser /usr/bin/security-server-tests-server /usr/bin/security-server-tests-password +/usr/bin/security-client /etc/smack/test_smack_rules_full /etc/smack/test_smack_rules2 /etc/smack/test_smack_rules3 diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 9618761..a4bba5f 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -51,3 +51,4 @@ INSTALL(FILES ADD_SUBDIRECTORY(libprivilege-control-tests) ADD_SUBDIRECTORY(libsmack-tests) ADD_SUBDIRECTORY(security-server-tests) +ADD_SUBDIRECTORY(security-server-client) diff --git a/tests/security-server-client/CMakeLists.txt b/tests/security-server-client/CMakeLists.txt new file mode 100644 index 0000000..d81bc90 --- /dev/null +++ b/tests/security-server-client/CMakeLists.txt @@ -0,0 +1,24 @@ +SET(TARGET_CLIENT2 security-client) + +PKG_CHECK_MODULES(CLIENT2_DEP + libsmack + security-server + REQUIRED + ) + +INCLUDE_DIRECTORIES( + ${CLIENT2_DEP_INCLUDE_DIRS} + ) + +SET(CLIENT2_SOURCES + ${PROJECT_SOURCE_DIR}/tests/security-server-client/client.c + ) + +ADD_EXECUTABLE(${TARGET_CLIENT2} ${CLIENT2_SOURCES}) + +TARGET_LINK_LIBRARIES(${TARGET_CLIENT2} + ${CLIENT2_DEP_LIBRARIES} + ) + +INSTALL(TARGETS ${TARGET_CLIENT2} DESTINATION bin) + diff --git a/tests/security-server-client/client.c b/tests/security-server-client/client.c new file mode 100644 index 0000000..63163f6 --- /dev/null +++ b/tests/security-server-client/client.c @@ -0,0 +1,234 @@ +/* + * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Bumjin Im + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + * + * @file client.c + * @author Bartlomiej Grzelewski (b.grzelewski@samsung.com) + * @version 1.0 + * @brief This file is implementation of security-server client. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#define SOCK_PATH "/tmp/security-server-api-echo" +#define SOCK_DATA_SHARE "/tmp/security-server-api-data-share" +#define MSG "test" + +int send_to_server(const char *interface, const char *snd, size_t snd_size, char *rvc, size_t rvc_size) { + struct sockaddr_un sockaddr = {.sun_family = AF_UNIX}; + int sock = socket(AF_UNIX, SOCK_STREAM, 0); + int ret; + + strcpy(sockaddr.sun_path, interface); + + ret = connect(sock, (struct sockaddr *) &sockaddr, sizeof(struct sockaddr_un)); + if (ret < 0) err(-1, "connect"); + + ret = write(sock, snd, snd_size); + if (ret < 0) err(-1, "write"); + + printf("%d: start read time", getpid()); + + rvc[0] = 0; + ret = read(sock, rvc, rvc_size); + printf("%d: read returned: %d buffer: %s\n", getpid(), ret, rvc); + close(sock); + + return 0; +} + +int echo() { + struct sockaddr_un sockaddr = {.sun_family = AF_UNIX, .sun_path = SOCK_PATH}; + int sock = socket(AF_UNIX, SOCK_STREAM, 0); + int ret; + + ret = connect(sock, (struct sockaddr *) &sockaddr, sizeof(struct sockaddr_un)); + if (ret < 0) err(-1, "connect"); + + ret = write(sock, MSG, sizeof(MSG)); + if (ret < 0) err(-1, "write"); + + char buffer[100]; + ret = read(sock, buffer, 100); + printf("%d: read %s\n", getpid(), buffer); + close(sock); + + return 0; +} + +int timeout() { + struct sockaddr_un sockaddr = {.sun_family = AF_UNIX, .sun_path = SOCK_PATH}; + int sock = socket(AF_UNIX, SOCK_STREAM, 0); + int ret; + char buffer[100] = {0}; + + ret = connect(sock, (struct sockaddr *) &sockaddr, sizeof(struct sockaddr_un)); + if (ret < 0) err(-1, "connect"); + + time_t start; + printf("%d: start read time: %lld\n", getpid(), (long long)(start=time(NULL))); + ret = read(sock, buffer, 100); + printf("%d: read returned: %d timeout: %lld buffer: %s\n", getpid(), ret, (long long)(time(NULL)-start), buffer); + close(sock); + + return 0; +} + +int rule(const char *subject, const char *object) { + int ret = 0; + struct smack_accesses *accesses = NULL; + + if (0 != smack_accesses_new(&accesses)) { + printf("%d: Error in smack_accesses_new\n", getpid()); + goto error; + } + + smack_accesses_add(accesses, object, "security-server::api-data-share", "w"); + smack_accesses_apply(accesses); + smack_set_label_for_self(object); + + if (SECURITY_SERVER_API_SUCCESS != security_server_app_give_access(subject, 15)) { + printf("%d: Error in app_give_access\n", getpid()); + goto error; + } + + goto end; +error: + ret = -1; +end: + smack_accesses_free(accesses); + return ret; +} + +int fakerule(const char * subject, const char * object) { + (void)subject; + struct smack_accesses *accesses = NULL; + + if (0 != smack_accesses_new(&accesses)) { + printf("%d: Error in smack_accesses_new\n", getpid()); + return -1; + } + + smack_accesses_add(accesses, object, "security-server::api-data-share", "w"); + smack_accesses_apply(accesses); + smack_set_label_for_self(object); + + size_t len = 10; + char buffer[400]; + memcpy(buffer, &len, sizeof(size_t)); + char buffer2[400]; + + send_to_server(SOCK_DATA_SHARE, buffer, 8, buffer2, 400); + + memcpy(&buffer[4], &len, sizeof(size_t)); + + send_to_server(SOCK_DATA_SHARE, buffer, 400, buffer2, 400); + + memcpy(&buffer[8], &len, sizeof(size_t)); + + send_to_server(SOCK_DATA_SHARE, buffer, 400, buffer2, 400); + + return 0; +} + +int runtest(const char *name, const char *par1, const char *par2, const char *par3) { + (void)par3; + if (0 == strcmp(name, "timeout")) { + return timeout(); + } else if (0 == strcmp(name, "rule")) { + return rule(par1, par2); + } else if (0 == strcmp(name, "echo")) { + return echo(); + } + printf("%d: Unknown command: %s\n", getpid(), name); + return -1; +} + +void fclosep(FILE **f) { + if (*f) fclose(*f); +} + +int filedata(const char *filename) { + FILE *in __attribute__ ((cleanup(fclosep))) = NULL; + int size; + char table[4][100]; + + if (NULL == (in = fopen(filename, "r"))) { + printf("Error opening file %s\n", filename); + return -1; + } + + if (EOF == fscanf(in, "%d", &size)) { + printf("Error reading configuration file.\n"); + return -1; + } + + while(size--) { + int i, len; + if (EOF == fscanf(in , "%d", &len)) { + printf("Error reading configuration file.\n"); + return -1; + } + for (i = 0; i < len; ++i) { + if (EOF == fscanf(in, "%s", table[i])) { + printf("Error reading configuration file.\n"); + return -1; + } + } + if(0 == fork()) { + printf("%d: Process started. Command: %s\n", getpid(), table[0]); + int ret = runtest(table[0], table[1], table[2], table[3]); + printf("%d: Process ended.\n", getpid()); + return ret; + } + } + return 0; +} + +int main(int argc, const char **argv) +{ + if (argc < 2) { + printf("wrong usage. available commands: echo, rule, timeout, file, fakerule"); + return 0; + } + + if (0 == strcmp(argv[1], "echo")) { + return echo(); + } else if (0 == strcmp(argv[1], "rule")) { + return rule(argv[2], argv[3]); + } else if (0 == strcmp(argv[1], "fakerule")) { + return fakerule(argv[2], argv[3]); + } else if (0 == strcmp(argv[1], "timeout")) { + return timeout(); + } else if (0 == strcmp(argv[1], "file")) { + return filedata(argv[2]); + } + + return 0; +} + -- 2.7.4