aboutsummaryrefslogtreecommitdiff
path: root/tests/test_smaf.c
blob: 202ea639e6cb6d9aa44013362734f621eed06dc4 (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
/*
 * test_smaf.c
 *
 * Copyright (C) Linaro SA 2015
 * Author: Benjamin Gaignard <benjamin.gaignard@linaro.org> for Linaro.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA  02110-1335  USA
 */

#include <../lib/libsmaf.h>
#include <stdio.h>

#define LENGTH 1024*16

static void test_create_named_invalid(void)
{
	int ret;
	int fd;

	ret = smaf_create_buffer(LENGTH, O_CLOEXEC | O_RDWR, "deadbeef", &fd);

	if (!ret) {
		printf("%s smaf_create_buffer() failed %d\n", __func__, ret);
		return;
	}

	printf("%s successed\n", __func__);
}

static void test_create_named(void)
{
	int ret;
	int fd;

	ret = smaf_create_buffer(LENGTH, O_CLOEXEC | O_RDWR, "smaf-cma", &fd);

	if (ret || (fd == -1)) {
		printf("%s smaf_create_buffer() failed %d\n", __func__, ret);
		return;
	}

	printf("%s successed\n", __func__);

	close(fd);
}

static void test_secure(void)
{
	int ret;
	int fd;

	ret = smaf_create_buffer(LENGTH, O_CLOEXEC | O_RDWR, NULL, &fd);

	if (ret || (fd == -1)) {
		printf("%s smaf_create_buffer() failed %d\n", __func__, ret);
		return;
	}

	ret = smaf_set_secure(fd, 1);
	if (ret) {
		printf("%s smaf_set_secure() failed %d\n", __func__, ret);
		goto end;
	}

	ret = smaf_get_secure(fd);
	if (!ret) {
		printf("%s smaf_get_secure() failed %d\n", __func__, ret);
		goto end;
	}

	printf("%s successed\n", __func__);
end:
	close(fd);
}

static void test_create_unnamed(void)
{
	int ret;
	int fd;

	ret = smaf_create_buffer(LENGTH, O_CLOEXEC | O_RDWR, NULL, &fd);

	if (ret || (fd == -1)) {
		printf("%s smaf_create_buffer() failed %d\n", __func__, ret);
		return;
	}

	close(fd);
	printf("%s successed\n", __func__);
}

void main (void)
{
	if (smaf_open()) {
		printf("Can't open /dev/smaf\n");
		return;
	}

	test_create_unnamed();
	test_create_named();
	test_create_named_invalid();
	test_secure();

	smaf_close();
}