94833c940ff36d30fd7395642b025abb9401866a
[platform/core/test/security-tests.git] / tests / security-server-tests / security_server_clean_env.cpp
1 /*
2  * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
3  */
4 /*
5  * @file    security_server_tests_clean_env.cpp
6  * @author  Zbigniew Jasinski (z.jasinski@samsung.com)
7  * @version 1.0
8  * @brief   Functions to prepare clean env for tests.
9  *
10  */
11
12 #include <ftw.h>
13 #include <unistd.h>
14
15 #include <service_manager.h>
16
17 int restart_security_server() {
18     ServiceManager serviceManager("security-server.service");
19     serviceManager.restartService();
20
21     return 0;
22 }
23
24 static int nftw_rmdir_contents(const char *fpath, const struct stat * /*sb*/,
25                                int tflag, struct FTW *ftwbuf)
26 {
27     if (tflag == FTW_F)
28         unlink(fpath);
29     else if (tflag == FTW_DP && ftwbuf->level != 0)
30         rmdir(fpath);
31
32     return 0;
33 }
34
35 /**
36  * This function should be called at the begining of every SS test, so all the tests
37  * are independent of each other.
38  */
39 int reset_security_server()
40 {
41     const char* path = "/opt/data/security-server/";
42     const int max_descriptors = 10; //max number of open file descriptors by nftw function
43
44     // Clear /opt/data/security-server/ directory
45     if (access(path, F_OK) == 0) {
46         if (nftw(path, &nftw_rmdir_contents, max_descriptors, FTW_DEPTH) == -1) {
47             return 1;
48         }
49         sync();
50     }
51
52     restart_security_server();
53     return 0;
54 }