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 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
|
/*
* crun - OCI runtime written in C
*
* Copyright (C) 2017, 2018, 2019 Giuseppe Scrivano <giuseppe@scrivano.org>
* crun 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.
*
* crun 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 crun. If not, see <http://www.gnu.org/licenses/>.
*/
#include <libcrun/error.h>
#include <libcrun/utils.h>
#include <libcrun/cgroup.h>
#include <libcrun/cgroup-systemd.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
typedef int (*test) ();
extern int cpuset_string_to_bitmask (const char *str, char **out, size_t *out_size, libcrun_error_t *err);
static int
test_socket_pair ()
{
libcrun_error_t err = NULL;
int fds[2];
__attribute__ ((unused)) cleanup_close int fd0 = -1;
__attribute__ ((unused)) cleanup_close int fd1 = -1;
char buffer[256];
int ret = create_socket_pair (fds, &err);
if (ret < 0)
return -1;
fd0 = fds[0];
fd1 = fds[1];
if (err != NULL)
return -1;
ret = write (fds[0], "HELLO", 6);
if (ret != 6)
return -1;
ret = read (fds[1], buffer, sizeof (buffer));
if (ret != 6)
return -1;
if (strcmp (buffer, "HELLO") != 0)
return -1;
ret = write (fds[1], "WORLD", 6);
if (ret != 6)
return -1;
ret = read (fds[0], buffer, sizeof (buffer));
if (ret != 6)
return -1;
if (strcmp (buffer, "WORLD") != 0)
return -1;
return 0;
}
static int
test_send_receive_fd ()
{
libcrun_error_t err = NULL;
int fds[2], pipes[2];
cleanup_close int fd0 = -1;
cleanup_close int fd1 = -1;
pid_t pid;
int ret = create_socket_pair (fds, &err);
if (ret < 0)
return -1;
fd0 = fds[0];
fd1 = fds[1];
if (err != NULL)
return -1;
pid = fork ();
if (pid < 0)
return -1;
if (pid)
{
cleanup_close int pipefd0 = -1;
cleanup_close int pipefd1 = -1;
char buffer[256];
const char *test_string = "TEST STRING";
if (pipe (pipes) < 0)
return -1;
pipefd0 = pipes[0];
pipefd1 = pipes[1];
close (fd0);
fd0 = -1;
if (send_fd_to_socket (fd1, pipefd0, &err) < 0)
return -1;
if (write (pipefd1, test_string, strlen (test_string) + 1) < 0)
return -1;
ret = read (fd1, buffer, sizeof (buffer));
if (ret < 0)
return -1;
if (ret != (int) strlen (test_string) + 1)
return -1;
return strcmp (buffer, test_string);
}
else
{
char buffer[256];
int ret, fd = receive_fd_from_socket (fd0, &err);
if (fd < 0)
_exit (1);
close (fd1);
fd1 = -1;
ret = read (fd, buffer, sizeof (buffer));
if (ret <= 0)
return -1;
if (write (fd0, buffer, ret) < 0)
return -1;
_exit (0);
}
return 0;
}
static int
test_run_process ()
{
libcrun_error_t err = NULL;
{
char *args[] = { "/bin/true", NULL };
if (run_process (args, &err) != 0)
return -1;
}
{
char *args[] = { "/bin/false", NULL };
if (run_process (args, &err) <= 0)
return -1;
if (err != NULL)
return -1;
}
{
char *args[] = { "/does/not/exist", NULL };
int r = run_process (args, &err);
if (r <= 0)
return -1;
if (err != NULL)
return -1;
}
return 0;
}
static int
test_dir_p ()
{
libcrun_error_t err;
if (crun_dir_p ("/usr", false, &err) <= 0)
return -1;
if (crun_dir_p ("/dev/zero", false, &err) != 0)
return -1;
if (crun_dir_p ("/hopefully/does/not/really/exist", false, &err) >= 0)
return -1;
return 0;
}
static int
test_write_read_file ()
{
libcrun_error_t err = NULL;
cleanup_free char *name = NULL;
size_t len;
size_t i;
int ret, failed = 0;
size_t max = 1 << 10;
cleanup_free char *written = xmalloc (max);
xasprintf (&name, "tests/write-file-%i", getpid ());
for (i = 0; i < max; i++)
written[i] = i;
for (i = 1; i <= max; i *= 2)
{
cleanup_free char *read_buf = NULL;
ret = write_file (name, written, i, &err);
if (ret < 0)
{
failed = 1;
break;
}
ret = read_all_file (name, &read_buf, &len, &err);
if (ret < 0)
{
failed = 1;
break;
}
if (len != i)
{
failed = 1;
break;
}
if (memcmp (read_buf, written, len) != 0)
{
failed = 1;
break;
}
}
unlink (name);
return failed ? -1 : 0;
}
static int
test_crun_path_exists ()
{
libcrun_error_t err = NULL;
if (crun_path_exists ("/dev/null", &err) <= 0)
return -1;
if (crun_path_exists ("/usr/foo/bin/ls", &err) != 0)
return -1;
if (crun_path_exists ("/proc/sysrq-trigger", &err) != 1)
return -1;
return 0;
}
static int
test_path_is_slash_dev ()
{
if (! path_is_slash_dev ("/dev"))
return -1;
if (! path_is_slash_dev ("/dev//"))
return -1;
if (! path_is_slash_dev ("///dev///"))
return -1;
if (! path_is_slash_dev ("/dev/"))
return -1;
if (! path_is_slash_dev ("dev"))
return -1;
if (! path_is_slash_dev ("dev////"))
return -1;
if (path_is_slash_dev ("dev////foo"))
return -1;
if (path_is_slash_dev ("/dev/foo"))
return -1;
if (path_is_slash_dev ("/dev/foo/"))
return -1;
if (path_is_slash_dev ("/dev/foo/bar"))
return -1;
if (path_is_slash_dev ("///dev/foo//bar"))
return -1;
if (path_is_slash_dev ("///dev/foo/////"))
return -1;
return 0;
}
static int
test_append_paths ()
{
#define PROLOGUE() \
cleanup_free char *out = NULL; \
libcrun_error_t err = NULL; \
int ret;
#define EXPECT_STRING(exp) \
{ \
if (ret < 0 || out == NULL) \
{ \
crun_error_release (&err); \
return ret; \
} \
if (strcmp (out, exp)) \
return -1; \
}
{
PROLOGUE ();
ret = append_paths (&out, &err, "/sys/fs/cgroup/", "memory", "some/path", NULL);
EXPECT_STRING ("/sys/fs/cgroup/memory/some/path");
}
{
PROLOGUE ();
ret = append_paths (&out, &err, "/sys/fs/cgroup", "memory", "some/path", NULL);
EXPECT_STRING ("/sys/fs/cgroup/memory/some/path");
}
{
PROLOGUE ();
ret = append_paths (&out, &err, "/sys/fs/cgroup////////", "memory////////", "some/path//////", NULL);
EXPECT_STRING ("/sys/fs/cgroup/memory/some/path");
}
{
PROLOGUE ();
ret = append_paths (&out, &err, "/sys/fs/cgroup////////", "memory////////", "///////some/path//////", NULL);
EXPECT_STRING ("/sys/fs/cgroup/memory/some/path");
}
{
PROLOGUE ();
ret = append_paths (&out, &err, "", "//", "", "", "", NULL);
EXPECT_STRING ("/");
}
{
PROLOGUE ();
ret = append_paths (&out, &err, "///", "/", "", "///", "a", NULL);
EXPECT_STRING ("/a");
}
{
PROLOGUE ();
ret = append_paths (&out, &err, "////", "/////", "///", "", "", NULL);
EXPECT_STRING ("/");
}
{
PROLOGUE ();
ret = append_paths (&out, &err, "", "", "", "", "", NULL);
EXPECT_STRING ("");
}
{
PROLOGUE ();
ret = append_paths (&out, &err, "", "///sys/fs/cgroup////////", "", "some/path", NULL);
EXPECT_STRING ("/sys/fs/cgroup/some/path");
}
{
PROLOGUE ();
ret = append_paths (&out, &err, "a", "b", "c", "d", "a", "b", "c", "d", "a", "b", "c", "d",
"a", "b", "c", "d", "a", "b", "c", "d", "a", "b", "c", "d",
"a", "b", "c", "d", "a", "b", "c", "d", "a", "b", "c", "d", NULL);
if (ret == 0)
return -1;
crun_error_release (&err);
}
return 0;
}
#ifdef HAVE_SYSTEMD
static int
test_parse_sd_array ()
{
char *out, *next;
libcrun_error_t err = NULL;
{
cleanup_free char *s = xstrdup (" 'firewalld.service' ");
if (parse_sd_array (s, &out, &next, &err) < 0)
{
crun_error_release (&err);
return -1;
}
if (strcmp (out, "firewalld.service"))
return -1;
if (next != NULL)
return -1;
}
{
cleanup_free char *s = xstrdup ("'fir\\ewalld.ser\\vi\\ce']");
if (parse_sd_array (s, &out, &next, &err) < 0)
{
crun_error_release (&err);
return -1;
}
if (strcmp (out, "firewalld.service"))
return -1;
if (next != NULL)
return -1;
}
{
cleanup_free char *s = xstrdup ("'fi\\rew\\alld.s\\erv\\ice', 'foo.service']");
if (parse_sd_array (s, &out, &next, &err) < 0)
{
crun_error_release (&err);
return -1;
}
if (strcmp (out, "firewalld.service"))
return -1;
if (strcmp (next, " 'foo.service']"))
return -1;
}
return 0;
}
static int
test_get_scope_path ()
{
# define CHECK(x, y) \
{ \
cleanup_free char *r = x; \
if (strcmp (r, y)) \
{ \
fprintf (stderr, "expected %s, got %s\n", y, r); \
return -1; \
} \
}
CHECK (get_cgroup_scope_path ("foo.scope", "foo.scope"), "foo.scope");
CHECK (get_cgroup_scope_path ("/foo/bar/user.slice/foo.scope/a/b/c", "foo.scope"), "/foo/bar/user.slice/foo.scope");
CHECK (get_cgroup_scope_path ("/foo/bar-foo.scope/user.slice/foo.scope/a/b/c", "foo.scope"), "/foo/bar-foo.scope/user.slice/foo.scope");
CHECK (get_cgroup_scope_path ("/foo/foo.scope-bar/user.slice/foo.scope/a/b/c", "foo.scope"), "/foo/foo.scope-bar/user.slice/foo.scope");
CHECK (get_cgroup_scope_path ("////foo.scope", "foo.scope"), "////foo.scope");
# undef CHECK
return 0;
}
static int
test_cpuset_string_to_bitmask ()
{
libcrun_error_t err = NULL;
cleanup_free char *mask = NULL;
size_t mask_size;
if (cpuset_string_to_bitmask ("0", &mask, &mask_size, &err) < 0)
return -1;
if (mask_size != 1 || mask[0] != 0x01)
return -1;
free (mask);
mask = NULL;
if (cpuset_string_to_bitmask ("0-1", &mask, &mask_size, &err) < 0)
return -1;
if (mask_size != 1 || mask[0] != 0x03)
return -1;
free (mask);
mask = NULL;
if (cpuset_string_to_bitmask ("0,2", &mask, &mask_size, &err) < 0)
return -1;
if (mask_size != 1 || mask[0] != 0x05)
return -1;
free (mask);
mask = NULL;
if (cpuset_string_to_bitmask ("0-2", &mask, &mask_size, &err) < 0)
return -1;
if (mask_size != 1 || mask[0] != 0x07)
return -1;
free (mask);
mask = NULL;
if (cpuset_string_to_bitmask ("0,8", &mask, &mask_size, &err) < 0)
return -1;
if (mask_size != 2 || mask[0] != 0x01 || mask[1] != 0x01)
return -1;
free (mask);
mask = NULL;
if (cpuset_string_to_bitmask ("a", &mask, &mask_size, &err) == 0)
return -1;
if (cpuset_string_to_bitmask ("-1", &mask, &mask_size, &err) == 0)
return -1;
if (cpuset_string_to_bitmask ("0-", &mask, &mask_size, &err) == 0)
return -1;
if (cpuset_string_to_bitmask ("0-2,4-6", &mask, &mask_size, &err) < 0)
return -1;
if (mask_size != 1 || mask[0] != 0x77)
return -1;
free (mask);
mask = NULL;
return 0;
}
#endif
static void
run_and_print_test_result (const char *name, int id, test t)
{
int ret = t ();
if (ret == 0)
printf ("ok %d - %s\n", id, name);
else if (ret == 77)
printf ("ok %d - %s #SKIP\n", id, name);
else
printf ("not ok %d - %s\n", id, name);
}
#define RUN_TEST(T) \
do \
{ \
run_and_print_test_result (#T, id++, T); \
} while (0)
int
main ()
{
int id = 1;
#ifdef HAVE_SYSTEMD
printf ("1..11\n");
#else
printf ("1..8\n");
#endif
RUN_TEST (test_crun_path_exists);
RUN_TEST (test_write_read_file);
RUN_TEST (test_run_process);
RUN_TEST (test_dir_p);
RUN_TEST (test_socket_pair);
RUN_TEST (test_send_receive_fd);
RUN_TEST (test_append_paths);
RUN_TEST (test_path_is_slash_dev);
#ifdef HAVE_SYSTEMD
RUN_TEST (test_parse_sd_array);
RUN_TEST (test_get_scope_path);
RUN_TEST (test_cpuset_string_to_bitmask);
#endif
return 0;
}
|