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