Add ocsp test for CAPI.
[platform/core/test/security-tests.git] / tests / ckm / 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 <ckm/ckm-control.h>
16
17 #include <dbus_access.h>
18 #include <clean-env.h>
19
20 int restart_security_server() {
21     DBusAccess dbusAccess("/org/freedesktop/systemd1/unit/security_2dserver_2eservice");
22
23     dbusAccess.restart();
24
25     return 0;
26 }
27
28 static int nftw_rmdir_contents(const char *fpath, const struct stat * /*sb*/,
29                                int tflag, struct FTW *ftwbuf)
30 {
31     if (tflag == FTW_F)
32         unlink(fpath);
33     else if (tflag == FTW_DP && ftwbuf->level != 0)
34         rmdir(fpath);
35
36     return 0;
37 }
38
39 /**
40  * This function should be called at the begining of every SS test, so all the tests
41  * are independent of each other.
42  */
43 int reset_security_server()
44 {
45     const char* path = "/opt/data/security-server/";
46     const int max_descriptors = 10; //max number of open file descriptors by nftw function
47
48     // Clear /opt/data/security-server/ directory
49     if (access(path, F_OK) == 0) {
50         if (nftw(path, &nftw_rmdir_contents, max_descriptors, FTW_DEPTH) == -1) {
51             return 1;
52         }
53         sync();
54     }
55
56     restart_security_server();
57     auto control = CKM::Control::create();
58
59     if (!!control) {
60         control->lockUserKey(5000);
61         control->removeUserData(5000);
62         control->unlockUserKey(5000, "");
63     }
64
65     return 0;
66 }
67