|
| 1 | +/* SPDX-License-Identifier: LGPL-3.0-or-later */ |
| 2 | +/* Copyright (C) 2024 Intel Corporation |
| 3 | + * Paweł Marczewski <[email protected]> |
| 4 | + * Michael Steiner <[email protected]> |
| 5 | + */ |
| 6 | + |
| 7 | +/* |
| 8 | + * Tests for rollback protection of protected (encrypted) files |
| 9 | + */ |
| 10 | + |
| 11 | +#include <assert.h> |
| 12 | +#include <err.h> |
| 13 | +#include <errno.h> |
| 14 | +#include <stdio.h> |
| 15 | +#include <stdlib.h> |
| 16 | +#include <string.h> |
| 17 | + |
| 18 | +#include "common.h" |
| 19 | +#include "rw_file.h" |
| 20 | + |
| 21 | +static const char message1[] = "first message\n"; |
| 22 | +static const size_t message1_len = sizeof(message1) - 1; |
| 23 | + |
| 24 | +static const char message2[] = "second message\n"; |
| 25 | +static const size_t message2_len = sizeof(message2) - 1; |
| 26 | + |
| 27 | +static_assert(sizeof(message1) != sizeof(message2), "the messages should have different lengths"); |
| 28 | + |
| 29 | +/* TODO: eventually remove below copy/paste/extract heap |
| 30 | +static int create_file(const char* path, const char* str, size_t len) { |
| 31 | + int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0600); |
| 32 | + if (fd < 0) |
| 33 | + err(1, "open %s", path); |
| 34 | +
|
| 35 | + ssize_t n = posix_fd_write(fd, str, len); |
| 36 | + if (n < 0) |
| 37 | + errx(1, "posix_fd_write %s", path); |
| 38 | + if ((size_t)n != len) |
| 39 | + errx(1, "written less bytes than expected into %s", path); |
| 40 | +
|
| 41 | + if (rename(path, path) != 0) |
| 42 | + err(1, "rename"); |
| 43 | +
|
| 44 | + if (unlink(path) != 0) |
| 45 | + err(1, "unlink %s", path); |
| 46 | +
|
| 47 | + if (close(fd) != 0) |
| 48 | + err(1, "close %s", path); |
| 49 | +
|
| 50 | +} |
| 51 | +*/ |
| 52 | + |
| 53 | +/* dummy functions which are gdb break-point targets */ |
| 54 | +#pragma GCC push_options |
| 55 | +#pragma GCC optimize("O0") |
| 56 | +static void adversary_save_file(const char* path) {} |
| 57 | +static void adversary_reset_file(const char* path) {} |
| 58 | +static void adversary_delete_file(const char* path) {} |
| 59 | +#pragma GCC pop_options |
| 60 | + |
| 61 | +static void test_test(const char* path1, const char* path2) { |
| 62 | + adversary_save_file(path1); |
| 63 | + adversary_reset_file(path1); |
| 64 | + adversary_delete_file(path1); |
| 65 | + adversary_delete_file(path2); |
| 66 | +} |
| 67 | + |
| 68 | +int main(int argc, char* argv[]) { |
| 69 | + setbuf(stdout, NULL); |
| 70 | + setbuf(stderr, NULL); |
| 71 | + |
| 72 | + if (argc != 3) |
| 73 | + errx(1, "Usage: %s <file1> <file2>", argv[0]); |
| 74 | + |
| 75 | + const char* path1 = argv[1]; |
| 76 | + const char* path2 = argv[2]; |
| 77 | + |
| 78 | + test_test(path1, path2); |
| 79 | + printf("TEST OK\n"); |
| 80 | + return 0; |
| 81 | +} |
0 commit comments