static void test_exec_workingdirectory(Manager *m) { assert_se(mkdir_p("/tmp/test-exec_workingdirectory", 0755) >= 0); test(m, "exec-workingdirectory.service", 0, CLD_EXITED); rm_rf_dangerous("/tmp/test-exec_workingdirectory", false, true, false); }
static void test_conf_files_list(bool use_root) { char tmp_dir[] = "/tmp/test-conf-files-XXXXXX"; _cleanup_strv_free_ char **found_files = NULL; const char *root_dir, *search_1, *search_2, *expect_a, *expect_b; setup_test_dir(tmp_dir, "/dir1/a.conf", "/dir2/a.conf", "/dir2/b.conf", NULL); if (use_root) { root_dir = tmp_dir; search_1 = "/dir1"; search_2 = "/dir2"; } else { root_dir = NULL; search_1 = strappenda(tmp_dir, "/dir1"); search_2 = strappenda(tmp_dir, "/dir2"); } expect_a = strappenda(tmp_dir, "/dir1/a.conf"); expect_b = strappenda(tmp_dir, "/dir2/b.conf"); assert_se(conf_files_list(&found_files, ".conf", root_dir, search_1, search_2, NULL) == 0); strv_print(found_files); assert_se(found_files); assert_se(streq_ptr(found_files[0], expect_a)); assert_se(streq_ptr(found_files[1], expect_b)); assert_se(found_files[2] == NULL); assert_se(rm_rf_dangerous(tmp_dir, false, true, false) == 0); }
TarImport* tar_import_unref(TarImport *i) { if (!i) return NULL; if (i->tar_pid > 1) { (void) kill_and_sigcont(i->tar_pid, SIGKILL); (void) wait_for_terminate(i->tar_pid, NULL); } import_job_unref(i->tar_job); import_job_unref(i->checksum_job); import_job_unref(i->signature_job); curl_glue_unref(i->glue); sd_event_unref(i->event); if (i->temp_path) { (void) btrfs_subvol_remove(i->temp_path); (void) rm_rf_dangerous(i->temp_path, false, true, false); free(i->temp_path); } free(i->final_path); free(i->image_root); free(i->local); free(i); return NULL; }
static void test_copy_tree(void) { char original_dir[] = "/tmp/test-copy_tree/"; char copy_dir[] = "/tmp/test-copy_tree-copy/"; char **files = STRV_MAKE("file", "dir1/file", "dir1/dir2/file", "dir1/dir2/dir3/dir4/dir5/file"); char **links = STRV_MAKE("link", "file", "link2", "dir1/file"); char **p, **link; rm_rf_dangerous(copy_dir, false, true, false); rm_rf_dangerous(original_dir, false, true, false); STRV_FOREACH(p, files) { char *f = strappenda(original_dir, *p); assert_se(mkdir_parents(f, 0755) >= 0); assert_se(write_string_file(f, "file") == 0); }
static void test_skip(void (*setup)(void)) { char t[] = "/tmp/journal-skip-XXXXXX"; sd_journal *j; int r; assert_se(mkdtemp(t)); assert_se(chdir(t) >= 0); setup(); /* Seek to head, iterate down. */ assert_ret(sd_journal_open_directory(&j, t, 0)); assert_ret(sd_journal_seek_head(j)); assert_ret(sd_journal_next(j)); test_check_numbers_down(j, 4); sd_journal_close(j); /* Seek to tail, iterate up. */ assert_ret(sd_journal_open_directory(&j, t, 0)); assert_ret(sd_journal_seek_tail(j)); assert_ret(sd_journal_previous(j)); test_check_numbers_up(j, 4); sd_journal_close(j); /* Seek to tail, skip to head, iterate down. */ assert_ret(sd_journal_open_directory(&j, t, 0)); assert_ret(sd_journal_seek_tail(j)); assert_ret(r = sd_journal_previous_skip(j, 4)); assert_se(r == 4); test_check_numbers_down(j, 4); sd_journal_close(j); /* Seek to head, skip to tail, iterate up. */ assert_ret(sd_journal_open_directory(&j, t, 0)); assert_ret(sd_journal_seek_head(j)); assert_ret(r = sd_journal_next_skip(j, 4)); assert_se(r == 4); test_check_numbers_up(j, 4); sd_journal_close(j); log_info("Done..."); if (arg_keep) log_info("Not removing %s", t); else { journal_directory_vacuum(".", 3000000, 0, 0, NULL); assert_se(rm_rf_dangerous(t, false, true, false) >= 0); } puts("------------------------------------------------------------"); }
static int remove_item_instance(Item *i, const char *instance) { int r; assert(i); switch (i->type) { case CREATE_FILE: case TRUNCATE_FILE: case CREATE_DIRECTORY: case CREATE_FIFO: case CREATE_SYMLINK: case CREATE_BLOCK_DEVICE: case CREATE_CHAR_DEVICE: case IGNORE_PATH: case IGNORE_DIRECTORY_PATH: case RELABEL_PATH: case RECURSIVE_RELABEL_PATH: case WRITE_FILE: break; case REMOVE_PATH: if (remove(instance) < 0 && errno != ENOENT) { log_error("remove(%s): %m", instance); return -errno; } break; case TRUNCATE_DIRECTORY: case RECURSIVE_REMOVE_PATH: /* FIXME: we probably should use dir_cleanup() here * instead of rm_rf() so that 'x' is honoured. */ r = rm_rf_dangerous(instance, false, i->type == RECURSIVE_REMOVE_PATH, false); if (r < 0 && r != -ENOENT) { log_error("rm_rf(%s): %s", instance, strerror(-r)); return r; } break; } return 0; }
static int setup_test(Manager **m) { char **tests_path = STRV_MAKE("exists", "existsglobFOOBAR", "changed", "modified", "unit", "directorynotempty", "makedirectory"); char **test_path; Manager *tmp; int r; assert_se(m); r = manager_new(SYSTEMD_USER, true, &tmp); if (IN_SET(r, -EPERM, -EACCES, -EADDRINUSE, -EHOSTDOWN, -ENOENT)) { printf("Skipping test: manager_new: %s", strerror(-r)); return -EXIT_TEST_SKIP; } assert_se(r >= 0); assert_se(manager_startup(tmp, NULL, NULL) >= 0); STRV_FOREACH(test_path, tests_path) { rm_rf_dangerous(strappenda("/tmp/test-path_", *test_path), false, true, false); }
int main(int argc, char *argv[]) { JournalFile *one, *two, *three; char t[] = "/tmp/journal-stream-XXXXXX"; unsigned i; _cleanup_journal_close_ sd_journal *j = NULL; char *z; const void *data; size_t l; log_set_max_level(LOG_DEBUG); assert_se(mkdtemp(t)); assert_se(chdir(t) >= 0); assert_se(journal_file_open("one.journal", O_RDWR|O_CREAT, 0666, true, NULL, NULL, NULL, &one) == 0); assert_se(journal_file_open("two.journal", O_RDWR|O_CREAT, 0666, true, NULL, NULL, NULL, &two) == 0); assert_se(journal_file_open("three.journal", O_RDWR|O_CREAT, 0666, true, NULL, NULL, NULL, &three) == 0); for (i = 0; i < N_ENTRIES; i++) { char *p, *q; dual_timestamp ts; struct iovec iovec[2]; dual_timestamp_get(&ts); assert_se(asprintf(&p, "NUMBER=%u", i) >= 0); iovec[0].iov_base = p; iovec[0].iov_len = strlen(p); assert_se(asprintf(&q, "MAGIC=%s", i % 5 == 0 ? "quux" : "waldo") >= 0); iovec[1].iov_base = q; iovec[1].iov_len = strlen(q); if (i % 10 == 0) assert_se(journal_file_append_entry(three, &ts, iovec, 2, NULL, NULL, NULL) == 0); else { if (i % 3 == 0) assert_se(journal_file_append_entry(two, &ts, iovec, 2, NULL, NULL, NULL) == 0); assert_se(journal_file_append_entry(one, &ts, iovec, 2, NULL, NULL, NULL) == 0); } free(p); free(q); } journal_file_close(one); journal_file_close(two); journal_file_close(three); assert_se(sd_journal_open_directory(&j, t, 0) >= 0); assert_se(sd_journal_add_match(j, "MAGIC=quux", 0) >= 0); SD_JOURNAL_FOREACH_BACKWARDS(j) { _cleanup_free_ char *c = NULL; assert_se(sd_journal_get_data(j, "NUMBER", &data, &l) >= 0); printf("\t%.*s\n", (int) l, (const char*) data); assert_se(sd_journal_get_cursor(j, &c) >= 0); assert_se(sd_journal_test_cursor(j, c) > 0); } SD_JOURNAL_FOREACH(j) { _cleanup_free_ char *c = NULL; assert_se(sd_journal_get_data(j, "NUMBER", &data, &l) >= 0); printf("\t%.*s\n", (int) l, (const char*) data); assert_se(sd_journal_get_cursor(j, &c) >= 0); assert_se(sd_journal_test_cursor(j, c) > 0); } sd_journal_flush_matches(j); verify_contents(j, 1); printf("NEXT TEST\n"); assert_se(sd_journal_add_match(j, "MAGIC=quux", 0) >= 0); assert_se(z = journal_make_match_string(j)); printf("resulting match expression is: %s\n", z); free(z); verify_contents(j, 5); printf("NEXT TEST\n"); sd_journal_flush_matches(j); assert_se(sd_journal_add_match(j, "MAGIC=waldo", 0) >= 0); assert_se(sd_journal_add_match(j, "NUMBER=10", 0) >= 0); assert_se(sd_journal_add_match(j, "NUMBER=11", 0) >= 0); assert_se(sd_journal_add_match(j, "NUMBER=12", 0) >= 0); assert_se(z = journal_make_match_string(j)); printf("resulting match expression is: %s\n", z); free(z); verify_contents(j, 0); assert_se(sd_journal_query_unique(j, "NUMBER") >= 0); SD_JOURNAL_FOREACH_UNIQUE(j, data, l) printf("%.*s\n", (int) l, (const char*) data); assert_se(rm_rf_dangerous(t, false, true, false) >= 0); return 0; }
int main(int argc, char *argv[]) { char t[] = "/tmp/journal-XXXXXX"; unsigned n; JournalFile *f; const char *verification_key = argv[1]; usec_t from = 0, to = 0, total = 0; char a[FORMAT_TIMESTAMP_MAX]; char b[FORMAT_TIMESTAMP_MAX]; char c[FORMAT_TIMESPAN_MAX]; struct stat st; uint64_t p; log_set_max_level(LOG_DEBUG); assert_se(mkdtemp(t)); assert_se(chdir(t) >= 0); log_info("Generating..."); assert_se(journal_file_open("test.journal", O_RDWR|O_CREAT, 0666, true, !!verification_key, NULL, NULL, NULL, &f) == 0); for (n = 0; n < N_ENTRIES; n++) { struct iovec iovec; struct dual_timestamp ts; char *test; dual_timestamp_get(&ts); assert_se(asprintf(&test, "RANDOM=%lu", random() % RANDOM_RANGE)); iovec.iov_base = (void*) test; iovec.iov_len = strlen(test); assert_se(journal_file_append_entry(f, &ts, &iovec, 1, NULL, NULL, NULL) == 0); free(test); } journal_file_close(f); log_info("Verifying..."); assert_se(journal_file_open("test.journal", O_RDONLY, 0666, true, !!verification_key, NULL, NULL, NULL, &f) == 0); /* journal_file_print_header(f); */ journal_file_dump(f); assert_se(journal_file_verify(f, verification_key, &from, &to, &total, true) >= 0); if (verification_key && JOURNAL_HEADER_SEALED(f->header)) { log_info("=> Validated from %s to %s, %s missing", format_timestamp(a, sizeof(a), from), format_timestamp(b, sizeof(b), to), format_timespan(c, sizeof(c), total > to ? total - to : 0, 0)); } journal_file_close(f); if (verification_key) { log_info("Toggling bits..."); assert_se(stat("test.journal", &st) >= 0); for (p = 38448*8+0; p < ((uint64_t) st.st_size * 8); p ++) { bit_toggle("test.journal", p); log_info("[ %llu+%llu]", (unsigned long long) p / 8, (unsigned long long) p % 8); if (raw_verify("test.journal", verification_key) >= 0) log_notice(ANSI_HIGHLIGHT_RED_ON ">>>> %llu (bit %llu) can be toggled without detection." ANSI_HIGHLIGHT_OFF, (unsigned long long) p / 8, (unsigned long long) p % 8); bit_toggle("test.journal", p); } } log_info("Exiting..."); assert_se(rm_rf_dangerous(t, false, true, false) >= 0); return 0; }
static void test_sequence_numbers(void) { char t[] = "/tmp/journal-seq-XXXXXX"; JournalFile *one, *two; uint64_t seqnum = 0; sd_id128_t seqnum_id; assert_se(mkdtemp(t)); assert_se(chdir(t) >= 0); assert_se(journal_file_open("one.journal", O_RDWR|O_CREAT, 0644, true, false, NULL, NULL, NULL, &one) == 0); append_number(one, 1, &seqnum); printf("seqnum=%"PRIu64"\n", seqnum); assert(seqnum == 1); append_number(one, 2, &seqnum); printf("seqnum=%"PRIu64"\n", seqnum); assert(seqnum == 2); assert(one->header->state == STATE_ONLINE); assert(!sd_id128_equal(one->header->file_id, one->header->machine_id)); assert(!sd_id128_equal(one->header->file_id, one->header->boot_id)); assert(sd_id128_equal(one->header->file_id, one->header->seqnum_id)); memcpy(&seqnum_id, &one->header->seqnum_id, sizeof(sd_id128_t)); assert_se(journal_file_open("two.journal", O_RDWR|O_CREAT, 0644, true, false, NULL, NULL, one, &two) == 0); assert(two->header->state == STATE_ONLINE); assert(!sd_id128_equal(two->header->file_id, one->header->file_id)); assert(sd_id128_equal(one->header->machine_id, one->header->machine_id)); assert(sd_id128_equal(one->header->boot_id, one->header->boot_id)); assert(sd_id128_equal(one->header->seqnum_id, one->header->seqnum_id)); append_number(two, 3, &seqnum); printf("seqnum=%"PRIu64"\n", seqnum); assert(seqnum == 3); append_number(two, 4, &seqnum); printf("seqnum=%"PRIu64"\n", seqnum); assert(seqnum == 4); test_close(two); append_number(one, 5, &seqnum); printf("seqnum=%"PRIu64"\n", seqnum); assert(seqnum == 5); append_number(one, 6, &seqnum); printf("seqnum=%"PRIu64"\n", seqnum); assert(seqnum == 6); test_close(one); /* restart server */ seqnum = 0; assert_se(journal_file_open("two.journal", O_RDWR, 0, true, false, NULL, NULL, NULL, &two) == 0); assert(sd_id128_equal(two->header->seqnum_id, seqnum_id)); append_number(two, 7, &seqnum); printf("seqnum=%"PRIu64"\n", seqnum); assert(seqnum == 5); /* So..., here we have the same seqnum in two files with the * same seqnum_id. */ test_close(two); log_info("Done..."); if (arg_keep) log_info("Not removing %s", t); else { journal_directory_vacuum(".", 3000000, 0, 0, NULL); assert_se(rm_rf_dangerous(t, false, true, false) >= 0); } }