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("------------------------------------------------------------");
}
Esempio n. 2
0
static void test_empty(void) {
        JournalFile *f1, *f2, *f3, *f4;
        char t[] = "/tmp/journal-XXXXXX";

        test_setup_logging(LOG_DEBUG);

        assert_se(mkdtemp(t));
        assert_se(chdir(t) >= 0);

        assert_se(journal_file_open(-1, "test.journal", O_RDWR|O_CREAT, 0666, false, (uint64_t) -1, false, NULL, NULL, NULL, NULL, &f1) == 0);

        assert_se(journal_file_open(-1, "test-compress.journal", O_RDWR|O_CREAT, 0666, true, (uint64_t) -1, false, NULL, NULL, NULL, NULL, &f2) == 0);

        assert_se(journal_file_open(-1, "test-seal.journal", O_RDWR|O_CREAT, 0666, false, (uint64_t) -1, true, NULL, NULL, NULL, NULL, &f3) == 0);

        assert_se(journal_file_open(-1, "test-seal-compress.journal", O_RDWR|O_CREAT, 0666, true, (uint64_t) -1, true, NULL, NULL, NULL, NULL, &f4) == 0);

        journal_file_print_header(f1);
        puts("");
        journal_file_print_header(f2);
        puts("");
        journal_file_print_header(f3);
        puts("");
        journal_file_print_header(f4);
        puts("");

        log_info("Done...");

        if (arg_keep)
                log_info("Not removing %s", t);
        else {
                journal_directory_vacuum(".", 3000000, 0, 0, NULL, true);

                assert_se(rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
        }

        (void) journal_file_close(f1);
        (void) journal_file_close(f2);
        (void) journal_file_close(f3);
        (void) journal_file_close(f4);
}
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);
    }
}
Esempio n. 4
0
static bool check_compressed(uint64_t compress_threshold, uint64_t data_size) {
        dual_timestamp ts;
        JournalFile *f;
        struct iovec iovec;
        Object *o;
        uint64_t p;
        char t[] = "/tmp/journal-XXXXXX";
        char data[2048] = {0};
        bool is_compressed;
        int r;

        assert_se(data_size <= sizeof(data));

        test_setup_logging(LOG_DEBUG);

        assert_se(mkdtemp(t));
        assert_se(chdir(t) >= 0);

        assert_se(journal_file_open(-1, "test.journal", O_RDWR|O_CREAT, 0666, true, compress_threshold, true, NULL, NULL, NULL, NULL, &f) == 0);

        dual_timestamp_get(&ts);

        iovec.iov_base = (void*) data;
        iovec.iov_len = data_size;
        assert_se(journal_file_append_entry(f, &ts, NULL, &iovec, 1, NULL, NULL, NULL) == 0);

#if HAVE_GCRYPT
        journal_file_append_tag(f);
#endif
        journal_file_dump(f);

        /* We have to partially reimplement some of the dump logic, because the normal next_entry does the
         * decompression for us. */
        p = le64toh(f->header->header_size);
        for (;;) {
                r = journal_file_move_to_object(f, OBJECT_UNUSED, p, &o);
                assert_se(r == 0);
                if (o->object.type == OBJECT_DATA)
                        break;

                assert_se(p < le64toh(f->header->tail_object_offset));
                p = p + ALIGN64(le64toh(o->object.size));
        }

        is_compressed = (o->object.flags & OBJECT_COMPRESSION_MASK) != 0;

        (void) journal_file_close(f);

        log_info("Done...");

        if (arg_keep)
                log_info("Not removing %s", t);
        else {
                journal_directory_vacuum(".", 3000000, 0, 0, NULL, true);

                assert_se(rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
        }

        puts("------------------------------------------------------------");

        return is_compressed;
}
Esempio n. 5
0
static void test_non_empty(void) {
        dual_timestamp ts;
        JournalFile *f;
        struct iovec iovec;
        static const char test[] = "TEST1=1", test2[] = "TEST2=2";
        Object *o;
        uint64_t p;
        sd_id128_t fake_boot_id;
        char t[] = "/tmp/journal-XXXXXX";

        test_setup_logging(LOG_DEBUG);

        assert_se(mkdtemp(t));
        assert_se(chdir(t) >= 0);

        assert_se(journal_file_open(-1, "test.journal", O_RDWR|O_CREAT, 0666, true, (uint64_t) -1, true, NULL, NULL, NULL, NULL, &f) == 0);

        assert_se(dual_timestamp_get(&ts));
        assert_se(sd_id128_randomize(&fake_boot_id) == 0);

        iovec.iov_base = (void*) test;
        iovec.iov_len = strlen(test);
        assert_se(journal_file_append_entry(f, &ts, NULL, &iovec, 1, NULL, NULL, NULL) == 0);

        iovec.iov_base = (void*) test2;
        iovec.iov_len = strlen(test2);
        assert_se(journal_file_append_entry(f, &ts, NULL, &iovec, 1, NULL, NULL, NULL) == 0);

        iovec.iov_base = (void*) test;
        iovec.iov_len = strlen(test);
        assert_se(journal_file_append_entry(f, &ts, &fake_boot_id, &iovec, 1, NULL, NULL, NULL) == 0);

#if HAVE_GCRYPT
        journal_file_append_tag(f);
#endif
        journal_file_dump(f);

        assert_se(journal_file_next_entry(f, 0, DIRECTION_DOWN, &o, &p) == 1);
        assert_se(le64toh(o->entry.seqnum) == 1);

        assert_se(journal_file_next_entry(f, p, DIRECTION_DOWN, &o, &p) == 1);
        assert_se(le64toh(o->entry.seqnum) == 2);

        assert_se(journal_file_next_entry(f, p, DIRECTION_DOWN, &o, &p) == 1);
        assert_se(le64toh(o->entry.seqnum) == 3);
        assert_se(sd_id128_equal(o->entry.boot_id, fake_boot_id));

        assert_se(journal_file_next_entry(f, p, DIRECTION_DOWN, &o, &p) == 0);

        assert_se(journal_file_next_entry(f, 0, DIRECTION_DOWN, &o, &p) == 1);
        assert_se(le64toh(o->entry.seqnum) == 1);

        assert_se(journal_file_find_data_object(f, test, strlen(test), NULL, &p) == 1);
        assert_se(journal_file_next_entry_for_data(f, NULL, 0, p, DIRECTION_DOWN, &o, NULL) == 1);
        assert_se(le64toh(o->entry.seqnum) == 1);

        assert_se(journal_file_next_entry_for_data(f, NULL, 0, p, DIRECTION_UP, &o, NULL) == 1);
        assert_se(le64toh(o->entry.seqnum) == 3);

        assert_se(journal_file_find_data_object(f, test2, strlen(test2), NULL, &p) == 1);
        assert_se(journal_file_next_entry_for_data(f, NULL, 0, p, DIRECTION_UP, &o, NULL) == 1);
        assert_se(le64toh(o->entry.seqnum) == 2);

        assert_se(journal_file_next_entry_for_data(f, NULL, 0, p, DIRECTION_DOWN, &o, NULL) == 1);
        assert_se(le64toh(o->entry.seqnum) == 2);

        assert_se(journal_file_find_data_object(f, "quux", 4, NULL, &p) == 0);

        assert_se(journal_file_move_to_entry_by_seqnum(f, 1, DIRECTION_DOWN, &o, NULL) == 1);
        assert_se(le64toh(o->entry.seqnum) == 1);

        assert_se(journal_file_move_to_entry_by_seqnum(f, 3, DIRECTION_DOWN, &o, NULL) == 1);
        assert_se(le64toh(o->entry.seqnum) == 3);

        assert_se(journal_file_move_to_entry_by_seqnum(f, 2, DIRECTION_DOWN, &o, NULL) == 1);
        assert_se(le64toh(o->entry.seqnum) == 2);

        assert_se(journal_file_move_to_entry_by_seqnum(f, 10, DIRECTION_DOWN, &o, NULL) == 0);

        journal_file_rotate(&f, true, (uint64_t) -1, true, NULL);
        journal_file_rotate(&f, true, (uint64_t) -1, true, NULL);

        (void) journal_file_close(f);

        log_info("Done...");

        if (arg_keep)
                log_info("Not removing %s", t);
        else {
                journal_directory_vacuum(".", 3000000, 0, 0, NULL, true);

                assert_se(rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
        }

        puts("------------------------------------------------------------");
}