security-server tests on DPL framework
[platform/core/test/security-tests.git] / tests / security-server-tests / security_server_tests_pid_reuser.cpp
1 /*
2  * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
3  */
4 /*
5  * @file    security_server_tests_pid_reuser.cpp
6  * @author  Bumjin Im (bj.im@samsung.com)
7  * @author  Mariusz Domanski (m.domanski@samsung.com)
8  * @version 1.0
9  * @brief   Test cases for security server
10  */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <errno.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <unistd.h>
19 #include "security-server.h"
20 #include <dpl/test/test_runner.h>
21 #include <dlog.h>
22 #include "test.h"
23
24 unsigned int target_pid, target_uid;
25 unsigned char *prev_cookie, *new_cookie;
26 int cookie_size;
27
28 /* deprecated info for old c-style binary
29  * still useful for understanding the test itself
30  *
31  * Usage:
32  * cmd uid pid hexa_decimal_cookie
33  * pid: PID want to be reused
34  * hexa_decimal_cookie: Cookie value which is issued to the previous process
35  *                      with the [pid] for comparison\nThe cookie must be hexa
36  *                      decimal, with lower case and without whitespace and
37  *                      new line characters
38  * This test program must be executed as root process
39  */
40
41 void convert_prev_cookie(const char *cmdline, const char *prev, unsigned char *now)
42 {
43     int i, cnt;
44     char tmphexnum[3] = {0};
45     cnt = security_server_get_cookie_size();
46     cnt = cnt * 2;
47     if(strlen(prev) != cnt)
48     {
49         printf("%s\n", "Cookie length is wrong");
50         exit(1);
51     }
52
53     for(i=0, cnt=0 ; i<strlen(prev) ; i=i+2)
54     {
55         strncpy(tmphexnum, prev+i, 2);
56         tmphexnum[2] = 0;
57         errno = 0;
58         now[cnt] = strtoul(tmphexnum, 0, 16);
59         if(errno != 0)
60         {
61             printf("%s\n", "cannot convert hex cookie to binary");
62             exit(1);
63         }
64         cnt++;
65     }
66 }
67
68 void check_status()
69 {
70     struct stat statbuf;
71     int ret;
72     ret = stat("/opt/home/root/pid_cycle", &statbuf);
73     if(ret != 0)
74     {
75         printf("Interrupt encountered. exiting...\n");
76         exit(0);
77     }
78
79 }
80
81 void cycle_pid(int pid)
82 {
83     int cur_pid = getpid();
84     int dotval;
85
86     while(cur_pid != pid)
87     {
88         if(fork() != 0)
89         {
90             dotval = cur_pid % 1000;
91             if(dotval == 0)
92                 LOGD(".");
93             exit(0);
94         }
95         cur_pid = getpid();
96         check_status();
97     }
98 }
99
100 RUNNER_TEST_GROUP_INIT(SECURITY_SERVER_TESTS_PID);
101
102 RUNNER_TEST(tc_security_server_request_cookie)
103 {
104     RUNNER_IGNORED_MSG("needs FIX, check FIXME comment");
105     LOGD("Cycling PID to %d\n", target_pid);
106     cycle_pid(target_pid);
107     unlink("/opt/home/root/pid_cycle");
108     RUNNER_ASSERT(security_server_request_cookie((char *)new_cookie, 20) == SECURITY_SERVER_API_SUCCESS);
109     LOGD("\nTarget PID: %d, Target UID: %d\n", target_pid, target_uid);
110     LOGD("Previous cookie:\n");
111     printhex(prev_cookie, 20);
112     LOGD("Newly issued cookie:\n");
113     printhex(new_cookie, 20);
114     RUNNER_ASSERT_MSG(memcmp(prev_cookie, new_cookie, cookie_size) != 0, "cookies are the same");
115 }
116
117 int main(int argc, char *argv[])
118 {
119     /* FIXME: proper parameters needed (these below are fake)
120      *        or better idea for this test case...*/
121     char *argvv[] = {argv[0], "12345", "5000" , "abcdefabcdefabcdefabcdefabcdefabcdefabcd"};
122     int argcc = 4;
123
124     target_uid = getuid();
125     if(target_uid != 0)
126     {
127         printf("Error: %s must be executed by root\n", argv[0]);
128         exit(1);
129     }
130
131     cookie_size = security_server_get_cookie_size();
132     unsigned char prev_cookie_tmp[cookie_size], new_cookie_tmp[cookie_size];
133     prev_cookie = prev_cookie_tmp;
134     new_cookie = new_cookie_tmp;
135
136     errno = 0;
137     target_uid = strtoul(argvv[1], 0, 10);
138     if(errno != 0)
139     {
140         printf("%s\n", "cannot convert string uid to integer");
141         exit(1);
142     }
143     LOGD("Target UID is %d. change user...\n", target_uid);
144     setuid(target_uid);
145
146     errno = 0;
147     target_pid = strtoul(argvv[2], 0, 10);
148     if(errno != 0)
149     {
150         printf("%s\n", "cannot convert string pid to integer");
151         exit(1);
152     }
153     convert_prev_cookie(argvv[0], argvv[3], prev_cookie);
154     check_status();
155
156     int status =
157         DPL::Test::TestRunnerSingleton::Instance().ExecTestRunner(argc, argv);
158
159     return status;
160 }