summaryrefslogtreecommitdiff
path: root/src/stest_performance.cpp
blob: 15b81c6f7bda92ae9758faf6070a9e1ede6217f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include "internal/include/fb_generated.h"
#include "internal/naive_policy_checker.hpp"
#include "internal/policy.hpp"
#include "internal/serializer.hpp"
#include "internal/storage_backend_serialized.hpp"
#include "internal/storage_backend_serialized.hpp"
#include "internal/tslog.hpp"
#include "libdbuspolicy1-private.h"
#include <getopt.h>
#include <iostream>
#include <map>
#include <string>
#include <sys/types.h>
#include <unistd.h>

using namespace ldp_serialized;
using namespace ldp_serializer;
using namespace ldp_xml_parser;

std::map<Decision, const char*> DECISIONS {
	{ Decision::ANY,   "ANY"   },
	{ Decision::ALLOW, "ALLOW" },
	{ Decision::DENY,  "DENY"  },
	{ Decision::CHECK, "CHECK" }
};

struct Test {
	Decision expected_result;
	uid_t user;
	gid_t group;
	const char *label;
	const char *destination;
	const char *path;
	const char *interface;
	const char *member;
	ldp_xml_parser::MessageType type;
};

const int ROOT = 0;

#define TC(expected_result, names) \
	{(expected_result), ROOT, ROOT, "User::Shell", (names), "/", "a.b", "d", ldp_xml_parser::MessageType::METHOD_CALL}

/**
 * This test set tests ability to parse xml db
 * and check sending privilege in use cases
 * checking send_destination_prefix
 */
struct Test tests[]={
	/* straight-forward tests - base allow */
	TC(Decision::ALLOW, "org.tizen.test.dest_prefix.ap"),
	TC(Decision::ALLOW, "org.tizen.test.dest_prefix.ap.f.f.f.f.f"),
	TC(Decision::DENY,  "org.tizen.test.dest_prefix.apf"),
	TC(Decision::DENY,  "org.tizen.test.dest_prefix.apf.f.f.f.f"),
	/* multiple names owned */
	TC(Decision::ALLOW, "org.tizen.test.dest_prefix.ao org.tizen.test.dest_prefix.ap.f"),
	TC(Decision::ALLOW, "org.tizen.test.dest_prefix.ap.f org.tizen.test.dest_prefix.ao"),
	TC(Decision::DENY,  "org.tizen.test.dest_prefix.do org.tizen.test.dest_prefix.ap.f"),
	TC(Decision::DENY,  "org.tizen.test.dest_prefix.ap.f org.tizen.test.dest_prefix.do"),
	/* target holes in default allow */
	TC(Decision::DENY,  "org.tizen.test.dest_prefix.ap.1.d"),
	TC(Decision::DENY,  "org.tizen.test.dest_prefix.ap.1.dp"),
	TC(Decision::DENY,  "org.tizen.test.dest_prefix.ap.1.dp.f.f.f.f"),
	TC(Decision::DENY,  "org.tizen.test.dest_prefix.ap.1.dp.f.f.f.f org.tizen.test.dest_prefix.ao"),
	TC(Decision::DENY,  "org.tizen.test.dest_prefix.ap.1.dp.f.f.f.f org.tizen.test.dest_prefix.ap"),
	TC(Decision::DENY,  "org.tizen.test.dest_prefix.ao org.tizen.test.dest_prefix.ap.1.dp.f.f.f.f"),
	TC(Decision::DENY,  "org.tizen.test.dest_prefix.ap org.tizen.test.dest_prefix.ap.1.dp.f.f.f.f"),
	/* target holes in holes in default allow */
	TC(Decision::ALLOW, "org.tizen.test.dest_prefix.ap.1.d.ap"),
	TC(Decision::ALLOW, "org.tizen.test.dest_prefix.ap.1.d.ap.f.f.f.f"),
	TC(Decision::ALLOW, "org.tizen.test.dest_prefix.ap.1.dp.ap"),
	TC(Decision::ALLOW, "org.tizen.test.dest_prefix.ap.1.dp.ap.f.f.f.f"),
	TC(Decision::ALLOW, "org.tizen.test.dest_prefix.ap.1.dp.a"),
	/* check redefinitions in default allow */
	TC(Decision::DENY,  "org.tizen.test.dest_prefix.ap.2.apxdp"),
	TC(Decision::DENY,  "org.tizen.test.dest_prefix.ap.2.apxdp.f.f.f.f"),
	TC(Decision::DENY,  "org.tizen.test.dest_prefix.ap.2.apxdp.dp"),
	TC(Decision::DENY,  "org.tizen.test.dest_prefix.ap.2.apxdp.dp.f.f.f.f"),
	TC(Decision::DENY,  "org.tizen.test.dest_prefix.ap.2.apxdp.dp.ap"),
	TC(Decision::DENY,  "org.tizen.test.dest_prefix.ap.2.apxdp.dp.ap.f.f.f.f"),
	TC(Decision::DENY,  "org.tizen.test.dest_prefix.ap.2.apxdp.dp.ap.d"),
	TC(Decision::ALLOW, "org.tizen.test.dest_prefix.ap.2.apxdp.dp.a"),
	TC(Decision::ALLOW, "org.tizen.test.dest_prefix.ap.2.apxdp.dp.ap.f.a"),
	TC(Decision::ALLOW, "org.tizen.test.dest_prefix.ap.2.apxdp.f.f.f.ap"),
	TC(Decision::ALLOW, "org.tizen.test.dest_prefix.ap.2.apxdp.f.f.f.ap.f.f.f"),
	/* totally cancelling previous definitions in default allow */
	TC(Decision::ALLOW, "org.tizen.test.dest_prefix.ap.3.dpxap"),
	TC(Decision::ALLOW, "org.tizen.test.dest_prefix.ap.3.dpxap.f.f.f.f"),
	TC(Decision::ALLOW, "org.tizen.test.dest_prefix.ap.3.dpxap.ap"),
	TC(Decision::ALLOW, "org.tizen.test.dest_prefix.ap.3.dpxap.ap.f.f.f"),
	TC(Decision::ALLOW, "org.tizen.test.dest_prefix.ap.3.dpxap.ap.dp"),
	TC(Decision::ALLOW, "org.tizen.test.dest_prefix.ap.3.dpxap.ap.dp.f.f.f.f"),
	TC(Decision::ALLOW, "org.tizen.test.dest_prefix.ap.3.dpxap.ap.dp.ap"),
	TC(Decision::ALLOW, "org.tizen.test.dest_prefix.ap.3.dpxap.ap.dp.ap.f.f.f.f"),
	TC(Decision::ALLOW, "org.tizen.test.dest_prefix.ap.3.dpxap.ap.dp.a"),
	/* straight-forward tests - base deny */
	TC(Decision::DENY,  "org.tizen.test.dest_prefix.dp"),
	TC(Decision::DENY,  "org.tizen.test.dest_prefix.dp.f.f.f.f.f"),
	TC(Decision::DENY,  "org.tizen.test.dest_prefix.dpf"),
	TC(Decision::DENY,  "org.tizen.test.dest_prefix.dpf.f.f.f.f"),
	/* multiple names owned */
	TC(Decision::DENY,  "org.tizen.test.dest_prefix.do org.tizen.test.dest_prefix.dp.f"),
	TC(Decision::DENY,  "org.tizen.test.dest_prefix.dp.f org.tizen.test.dest_prefix.do"),
	TC(Decision::DENY,  "org.tizen.test.dest_prefix.ao org.tizen.test.dest_prefix.dp.f"),
	TC(Decision::DENY,  "org.tizen.test.dest_prefix.dp.f org.tizen.test.dest_prefix.ao"),
	/* target holes in default deny */
	TC(Decision::ALLOW, "org.tizen.test.dest_prefix.dp.1.a"),
	TC(Decision::ALLOW, "org.tizen.test.dest_prefix.dp.1.ap"),
	TC(Decision::ALLOW, "org.tizen.test.dest_prefix.dp.1.ap.f.f.f.f"),
	TC(Decision::ALLOW, "org.tizen.test.dest_prefix.dp.1.ap.f.f.f.f org.tizen.test.dest_prefix.do"),
	TC(Decision::ALLOW, "org.tizen.test.dest_prefix.dp.1.ap.f.f.f.f org.tizen.test.dest_prefix.dp"),
	TC(Decision::ALLOW, "org.tizen.test.dest_prefix.do org.tizen.test.dest_prefix.dp.1.ap.f.f.f.f"),
	TC(Decision::ALLOW, "org.tizen.test.dest_prefix.dp org.tizen.test.dest_prefix.dp.1.ap.f.f.f.f"),
	/* target holes in holes in default demy */
	TC(Decision::DENY,  "org.tizen.test.dest_prefix.dp.1.a.dp"),
	TC(Decision::DENY,  "org.tizen.test.dest_prefix.dp.1.a.dp.f.f.f.f"),
	TC(Decision::DENY,  "org.tizen.test.dest_prefix.dp.1.ap.dp"),
	TC(Decision::DENY,  "org.tizen.test.dest_prefix.dp.1.ap.dp.f.f.f.f"),
	TC(Decision::DENY,  "org.tizen.test.dest_prefix.dp.1.ap.d"),
	/* check redefinitions in default deny */
	TC(Decision::ALLOW, "org.tizen.test.dest_prefix.dp.2.dpxap"),
	TC(Decision::ALLOW, "org.tizen.test.dest_prefix.dp.2.dpxap.f.f.f.f"),
	TC(Decision::ALLOW, "org.tizen.test.dest_prefix.dp.2.dpxap.ap"),
	TC(Decision::ALLOW, "org.tizen.test.dest_prefix.dp.2.dpxap.ap.f.f.f.f"),
	TC(Decision::ALLOW, "org.tizen.test.dest_prefix.dp.2.dpxap.ap.dp"),
	TC(Decision::ALLOW, "org.tizen.test.dest_prefix.dp.2.dpxap.ap.dp.f.f.f.f"),
	TC(Decision::ALLOW, "org.tizen.test.dest_prefix.dp.2.dpxap.ap.dp.a"),
	TC(Decision::DENY,  "org.tizen.test.dest_prefix.dp.2.dpxap.ap.d"),
	TC(Decision::DENY,  "org.tizen.test.dest_prefix.dp.2.dpxap.ap.dp.f.d"),
	TC(Decision::DENY,  "org.tizen.test.dest_prefix.dp.2.dpxap.f.f.f.dp"),
	TC(Decision::DENY,  "org.tizen.test.dest_prefix.dp.2.dpxap.f.f.f.dp.f.f.f"),
	/* totally cancelling previous definitions in default deny */
	TC(Decision::DENY,  "org.tizen.test.dest_prefix.dp.3.apxdp"),
	TC(Decision::DENY,  "org.tizen.test.dest_prefix.dp.3.apxdp.f.f.f.f"),
	TC(Decision::DENY,  "org.tizen.test.dest_prefix.dp.3.apxdp.dp"),
	TC(Decision::DENY,  "org.tizen.test.dest_prefix.dp.3.apxdp.dp.f.f.f"),
	TC(Decision::DENY,  "org.tizen.test.dest_prefix.dp.3.apxdp.dp.ap"),
	TC(Decision::DENY,  "org.tizen.test.dest_prefix.dp.3.apxdp.dp.ap.f.f.f.f"),
	TC(Decision::DENY,  "org.tizen.test.dest_prefix.dp.3.apxdp.dp.ap.dp"),
	TC(Decision::DENY,  "org.tizen.test.dest_prefix.dp.3.apxdp.dp.ap.dp.f.f.f.f"),
	TC(Decision::DENY,  "org.tizen.test.dest_prefix.dp.3.apxdp.dp.ap.d"),
	/* checking order in multiple names case */
	TC(Decision::DENY,  "org.tizen.test.dest_prefix.ao org.tizen.test.dest_prefix.do"),
	TC(Decision::ALLOW, "org.tizen.test.dest_prefix.ao.ao org.tizen.test.dest_prefix.do"),
	TC(Decision::DENY,  "org.tizen.test.dest_prefix.do org.tizen.test.dest_prefix.ao"),
	TC(Decision::ALLOW, "org.tizen.test.dest_prefix.do org.tizen.test.dest_prefix.ao.ao"),
};

struct Test test_send_to_nonexistent[]={
	{Decision::DENY, ROOT, ROOT, "User::Shell", "no.such.destination", "/no/such/path",
	"no.such.interface", "no_such_method", ldp_xml_parser::MessageType::METHOD_CALL}
};

void test_print(const struct Test* t, Decision result) {
	printf("uid = %lu, gid = %lu, label = %s, destination = %s, expected = %s, result = %s",
		   (unsigned long)t->user, (unsigned long)t->group, t->label, t->destination, DECISIONS[t->expected_result], DECISIONS[result]);
}

template <typename DB, typename Array>
void send_prefix_test(const DB &db, const Array &tests)
{
	for (const auto &test : tests) {
		KdbusBusNames names;
		MatchItemSend m_item(test.interface, test.member, test.path, test.type,
				names.addSpaceSeparatedNames(test.destination));

		auto ret = db.getDecisionItemContextMandatory(m_item);

		if (ret.getDecision() == Decision::ANY)
			ret = db.getDecisionItemUser(test.user, m_item);

		if (ret.getDecision() == Decision::ANY)
			ret = db.getDecisionItemGroup(test.group, m_item);

		if (ret.getDecision() == Decision::ANY)
			ret = db.getDecisionItemContextDefault(m_item);
	}
}

void run_x_times(std::function<void(void)> func, size_t times) {
	clock_t begin = clock();
	for (size_t i = 0; i < times; i++)
		func();
	clock_t end = clock();

	std::cout << "run: " << static_cast<double>(end - begin)/CLOCKS_PER_SEC << std::endl;
}

void run_fb(const char *conf_file, bool verify, size_t count, bool worst) {
	Serializer serializer;
	size_t size;
	auto buff = serializer.serialize(conf_file, size);

	if (verify) {
		auto verifier = flatbuffers::Verifier(buff, size);
		if (!FB::VerifyFileBuffer(verifier)) {
			std::cout << "verification of serialized data: failed" << std::endl;
			return;
		}
	}

	StorageBackendSerialized storage;
	storage.initFromData(buff);

	printf("FLATBUFFERS:\n");
	if (!worst)
		run_x_times([&storage](){ send_prefix_test(storage, tests); }, count);
	else
		run_x_times([&storage](){ send_prefix_test(storage, test_send_to_nonexistent); }, count);
}

void run_tests(const char *conf_file, bool verify, size_t c, bool worst) {
	run_fb(conf_file, verify, c, worst);
}

void print_help(const char *name) {
	std::cout << std::endl;
	std::cout << "usage: " << name << " [-v] {--system|--session|-c <config_xml>} <count>" << std::endl;
	std::cout << std::endl;
	std::cout << "       -v - Verify" << std::endl;
	std::cout << std::endl;
}

static const struct option options[] {
	{"system", no_argument, 0, 0},
	{"session", no_argument, 0, 0},
	{nullptr, 0, nullptr, 0}
};

int main(int argc, char *argv[])
{
	int c;
	std::string input_filename = system_bus_conf_file_primary();
	size_t count = 100;
	bool verify = false;
	bool worst = false;

	while (1) {
		int option_index;
		c = getopt_long(argc, argv, "vwc:", options, &option_index);
		if (c == -1)
			break;
		switch(c) {
		case 0:
			if (option_index == 1)
				input_filename = session_bus_conf_file_primary();
			break;
		case 'c':
			  input_filename = optarg;
			  break;
		case 'v':
			  verify = true;
			  break;
		case 'w':
			  worst = true;
			  break;
		}
	}

	if (optind < argc) {
		count = std::stoi(argv[optind]);
	} else {
		print_help(argv[0]);
		return 1;
	}

	tslog::init();
	run_tests(input_filename.c_str(), verify, count, worst);

	return 0;
}