aboutsummaryrefslogtreecommitdiffstats
path: root/samsung-ipc/tests/main.c
blob: b89daa200ed67e3e20376bc821841290de33e441 (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
/*
 * This file is part of libsamsung-ipc.
 *
 * Copyright (C) 2016 Paul Kocialkowsk <contact@paulk.fr>
 * Copyright (C) 2021 Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
 *
 * libsamsung-ipc is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * libsamsung-ipc 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with libsamsung-ipc.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>

#include <samsung-ipc.h>

/* TODO: Fixme */
#include <ipc.h>
int test_open_android_modem_partition(struct ipc_client *client);

struct test {
	char *name;
	int (*func)(struct ipc_client *client);
};

static struct test tests[] = {
	{
		"open_android_modem_partition",
		test_open_android_modem_partition
	},
};

static void usage(char *progname)
{
	printf("Usage: %s list-tests\n", progname);
	printf("Usage: %s test <test>\n", progname);
}

static void list_tests(char *progname)
{
	int i;

	printf("Available tests:\n");

	for (i=0; i < sizeof(tests) / sizeof (struct test); i++) {
		printf("  %s\n", tests[i].name);
	}
}

static struct test *get_test(char *name)
{
	int i;

	for (i=0; i < sizeof(tests) / sizeof (struct test); i++) {
		if (!strcmp(tests[i].name, name))
			return &(tests[i]);
	}

	return NULL;
}

static void log_callback(void *data, const char *message)
{
	/* TODO: add better logging mechanism in libsamsung-ipc with
	 * tags and log levels which would enable to filter the
	 * messages from different provenance (this file, the code
	 * under test, other parts of libsamsung-ipc, etc).
	 */
#if 0
	char *buffer;
	size_t length;
	int i;

	if (message == NULL)
		return;

	buffer = strdup(message);
	length = strlen(message);

	for (i = length; i > 0; i--) {
		if (buffer[i] == '\n')
			buffer[i] = '\0';
		else if (buffer[i] != '\0')
			break;
	}

	printf("[ipc] %s\n", buffer);

	free(buffer);
#endif
}

int main(int argc, char *argv[])
{
	struct ipc_client *client = NULL;
	char *given_test_name;
	struct test *test;
	int rc = 0;

	if (argc == 2 && !strcmp("list-tests", argv[1])) {
		list_tests(argv[0]);
		return 0;
	} else if (argc == 3 && !strcmp("test", argv[1])) {
		given_test_name = argv[2];
	} else {
		usage(argv[0]);
		return EX_USAGE;
	}

	test = get_test(given_test_name);
	if (test == NULL) {
		printf("Unknown test %s\n", given_test_name);
		return EX_USAGE;
	}

	client = ipc_client_create(IPC_CLIENT_TYPE_DUMMY);
	if (client == NULL) {
		printf("[ !! ] Creating client failed\n");
		goto error;
	}

	rc = ipc_client_log_callback_register(client, log_callback, NULL);
	if (rc < 0) {
		printf("[ !! ] Registering log callback failed: error %d\n", rc);
		goto error;
	}

	rc = test->func(client);
	if (rc == 0) {
		printf("[ OK ] %s succedded\n", test->name);
	} else {
		printf("[ !! ] %s failed\n", test->name);
		goto error;
	}

	return 0;

error:
	if (client != NULL)
		ipc_client_destroy(client);

	return EX_SOFTWARE;
}