Fix tests for new api get_cookie_gid/get_cookie_uid.
[platform/core/test/security-tests.git] / tests / security-server-tests / security_server_tests_label.cpp
1 /*
2 * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
3 */
4 /*
5 * @file    security_server_tests_label.cpp
6 * @author  Pawel Polawski (p.polawski@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 <sys/types.h>
15 #include <unistd.h>
16 #include <sys/smack.h>
17
18 #include <memory>
19 #include <functional>
20
21 #include <security-server.h>
22 #include <dpl/test/test_runner.h>
23 #include <dlog.h>
24 #include "tests_common.h"
25 #include "test.h"
26
27 RUNNER_TEST_GROUP_INIT(SECURITY_SERVER_TESTS_LABEL);
28
29 RUNNER_TEST_SMACK(tc_security_server_get_smacklabel_cookie) {
30     int res;
31
32     pid_t mypid;
33
34     char *label_smack = NULL;
35     char *label_ss = NULL;
36     char *cookie = NULL;
37
38     int COOKIESIZE = security_server_get_cookie_size();
39     RUNNER_ASSERT_MSG(20 == COOKIESIZE, "Wrong cookie size from security-server");
40     SLOGD("%s %d", "Cookie size:", COOKIESIZE);
41
42     cookie = (char*) calloc(COOKIESIZE, 1);
43     RUNNER_ASSERT_MSG(NULL != cookie, "Memory allocation error");
44
45     mypid = getpid();
46     SLOGD("%s %d", "This binary PID is:", mypid);
47
48     res = smack_new_label_from_self(&label_smack);
49     if (res < 0) {
50         if (NULL != label_smack)
51             free(label_smack);
52         if (NULL != label_ss)
53             free(label_ss);
54         if (NULL != cookie)
55             free(cookie);
56         RUNNER_ASSERT_MSG(false, "Error in getting self SMACK label");
57     }
58
59     res = security_server_request_cookie(cookie, COOKIESIZE);
60     if (SECURITY_SERVER_API_SUCCESS != res) {
61         if (NULL != label_smack)
62             free(label_smack);
63         if (NULL != label_ss)
64             free(label_ss);
65         if (NULL != cookie)
66             free(cookie);
67         RUNNER_ASSERT_MSG(false, "Error in requesting cookie from security-server");
68     }
69
70     SLOGD("%s", "Received cookie:");
71     printhex((unsigned char*) cookie, COOKIESIZE);
72
73     label_ss = security_server_get_smacklabel_cookie(cookie);
74     if (NULL == label_ss) {
75         if (NULL != label_smack)
76             free(label_smack);
77         if (NULL != label_ss)
78             free(label_ss);
79         if (NULL != cookie)
80             free(cookie);
81         RUNNER_ASSERT_MSG(false, "Error in getting label by cookie");
82     }
83
84     SLOGD("%s %s", "Label from SMACK:", label_smack);
85     SLOGD("%s %s", "Label from SS:", label_ss);
86
87     if (sizeof(label_smack) != sizeof(label_ss)) {
88         if (NULL != label_smack)
89             free(label_smack);
90         if (NULL != label_ss)
91             free(label_ss);
92         if (NULL != cookie)
93             free(cookie);
94         RUNNER_ASSERT_MSG(false, "No match in SMACK labels sizes");
95     }
96
97     res = strcmp(label_smack, label_ss);
98     if (0 != res) {
99         if (NULL != label_smack)
100             free(label_smack);
101         if (NULL != label_ss)
102             free(label_ss);
103         if (NULL != cookie)
104             free(cookie);
105         RUNNER_ASSERT_MSG(false, "No match in SMACK labels");
106     }
107
108     //TODO: here could be label change using SMACK API and checking if it
109     //is changed using security-server API function based on the same cookie
110 }
111
112 /**
113  * NOSMACK version of tc_security_server_get_smacklabel_cookie test.
114  *
115  * Most of this test goes exactly as the original one. The only difference are the labels:
116  * - We assume that libsmack tests passed and smack_new_label_from_self will return -1 and NULL
117  *   label - there is no need to re-check it.
118  * - Label acquired from security_server_get_smacklabel_cookie should be an empty string.
119  */
120 RUNNER_TEST_NOSMACK(tc_security_server_get_smacklabel_cookie_nosmack) {
121     int res;
122
123     pid_t mypid;
124
125     char* label_ss = NULL;
126     char* cookie = NULL;
127
128     int COOKIESIZE = security_server_get_cookie_size();
129     RUNNER_ASSERT_MSG(20 == COOKIESIZE,
130             "Wrong cookie size from security-server. Size: " << COOKIESIZE);
131
132     cookie = (char*) calloc(COOKIESIZE, sizeof(char));
133     RUNNER_ASSERT_MSG(NULL != cookie, "Memory allocation error");
134
135     //wrap cookie in smart ptr for garbage collection
136     std::unique_ptr<char, std::function<void(char*)> > cookie_ptr(cookie, free);
137
138     mypid = getpid();
139
140     //Request cookie from SS
141     res = security_server_request_cookie(cookie, COOKIESIZE);
142     RUNNER_ASSERT_MSG(res == SECURITY_SERVER_API_SUCCESS,
143             "Error in requesting cookie from security-server. Result: " << res);
144
145     printhex((unsigned char*) cookie, COOKIESIZE);
146
147     //Get label; since we only use label_ss to check if it indeed is an empty string there is no
148     //need to wrap it into unique_ptr (we have only one assert before the end of the test)
149     label_ss = security_server_get_smacklabel_cookie(cookie);
150     RUNNER_ASSERT_MSG(label_ss != NULL, "Error in getting label by cookie");
151
152     //Check if label_ss is correct, that is only one NULL character.
153     if (label_ss[0] != '\0') {
154         free(label_ss);
155         RUNNER_ASSERT_MSG(false, "label_ss was not an empty string.");
156     }
157
158     free(label_ss);
159 }
160
161 int main(int argc, char *argv[])
162 {
163     int status =
164         DPL::Test::TestRunnerSingleton::Instance().ExecTestRunner(argc, argv);
165
166     return status;
167 }