aboutsummaryrefslogtreecommitdiffstats
path: root/samsung-ipc/tests/partitions/android.c
blob: 7b067e9fd6b0558cbdcd41ac04d210f3049b78e7 (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
/*
 * This file is part of libsamsung-ipc.
 *
 * 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 <fcntl.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <sys/stat.h>
#include <sys/types.h>

#include <samsung-ipc.h>

#include <partitions/android/android.h>
#include "android.h"

static char const * const dummy_modem_image_paths[] = {
	/* We can't use mktemp here since everything is const
	 * echo libsamsung-ipc | sha1sum | cut -c-20
	 * gives 55f4731d2e11e85bd889
	 */
	"/tmp/libsamsung-ipc.55f4731d2e11e85bd889/modem.img",
	NULL
};

int delete_dummy_modem_image(struct ipc_client *client,
			     __attribute__((unused)) const char * const path)
{
	int rc;
	char *endp;
	char *dir;

	rc = unlink(path);
	if (rc == -1) {
		rc = errno;
		if (rc != ENOENT) {
			ipc_client_log(client,
				       "%s: unlink %s failed with error %d: %s",
				       __func__, path, rc, strerror(rc));
			errno = rc;
			return -1;
		}
	}

	endp = strrchr(path, '/');

	dir = malloc(endp - path + 1);
	if (dir == NULL) {
		rc = errno;
		ipc_client_log(client,
			       "%s: calloc failed with error %d: %s",
			       __func__, rc, strerror(rc));
		errno = rc;
		return -1;

	}

	memcpy(dir, path, endp - path);
	dir[endp - path] = '\0';

	rc = rmdir(dir);
	if (rc == -1) {
		rc = errno;
		if (rc != ENOENT) {
			ipc_client_log(client,
				       "%s: rmdir %s failed with error %d: %s",
				       __func__, dir, rc, strerror(rc));

			free(dir);
			errno = rc;
			return -1;
		}
	}

	free(dir);
	return 0;
}


int create_dummy_modem_image(struct ipc_client *client,
			     __attribute__((unused)) const char * const path)
{
	int fd;
	int rc;

	rc = mkdir("/tmp/", 0755);
	if (rc == -1) {
		rc = errno;
		if (rc != EEXIST) {
			ipc_client_log(client,
				       "%s: mkdir %s failed with error %d: %s",
				       __func__, "/tmp/", rc, strerror(rc));
			return -1;
		}
	}

	rc = mkdir("/tmp/libsamsung-ipc.55f4731d2e11e85bd889/", 0755);
	if (rc == -1) {
		rc = errno;
		if (rc != EEXIST) {
			ipc_client_log(client,
				       "%s: mkdir %s failed with error %d: %s",
				       __func__,
				       "/tmp/libsamsung-ipc.55f4731d2e11e85bd889/",
				       rc, strerror(rc));
			return -1;
		}
	}

	fd = open("/tmp/libsamsung-ipc.55f4731d2e11e85bd889/modem.img",
		  O_CREAT, 0755);
	if (fd == -1) {
		rc = errno;
		ipc_client_log(client,
			       "%s: open %s failed with error %d: %s",
			       __func__,
			       "/tmp/libsamsung-ipc.55f4731d2e11e85bd889/modem.img",
			       rc, strerror(rc));
		goto error;
	}

	rc = close(fd);
	if (rc == -1) {
		rc = errno;
		ipc_client_log(client,
			       "%s: close %s failed with error %d: %s",
			       __func__,
			       "/tmp/libsamsung-ipc.55f4731d2e11e85bd889/modem.img",
			       rc, strerror(rc));
		goto error;
	}

	return 0;

error:
	rc = delete_dummy_modem_image(
		client, "/tmp/libsamsung-ipc.55f4731d2e11e85bd889/modem.img");
	if (rc == -1)
		ipc_client_log(client,
			       "%s: delete_dummy_modem_image %s failed with error -1",
			       __func__);
	return -1;
}

int test_open_android_modem_partition(struct ipc_client *client)
{
	int i;
	int rc;

	for (i = 0; dummy_modem_image_paths[i] != NULL; i++) {
		rc = create_dummy_modem_image(client,
					      dummy_modem_image_paths[i]);
		if (rc == -1) {
			ipc_client_log(
				client,
				"%s: create_dummy_modem_image(client, %s)"
				" failed\n",
				__func__, dummy_modem_image_paths[i]);
			return -1;
		}
	}

	rc = open_android_modem_partition(client, dummy_modem_image_paths);
	if (rc == -1) {
		rc = errno;
		ipc_client_log(client, "%s: open_android_modem_partition"
			       " failed with errror %d: %s\n",
			       __func__, rc, strerror(rc));
		errno = rc;
		return -1;
	}

	rc = close(rc);
	if (rc == -1) {
		rc = errno;
		ipc_client_log(client,
			       "%s: close() failed with errror %d: %s\n",
			       __func__, rc, strerror(rc));
		return -1;
	}

	rc = delete_dummy_modem_image(
		client, "/tmp/libsamsung-ipc.55f4731d2e11e85bd889/modem.img");
	if (rc == -1) {
		rc = errno;
		ipc_client_log(client,
			       "%s: delete_dummy_modem_image() failed with errror %d: %s\n",
			       __func__, rc, strerror(rc));
		return -1;
	}

	return 0;
}