]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/journal/test-journal-stream.c
util-lib: split string parsing related calls from util.[ch] into parse-util.[ch]
[thirdparty/systemd.git] / src / journal / test-journal-stream.c
index 627faa7eb56d0e756ee3d29e7ac01b0d41ce3389..b8482743a2c5841796d84d645cbbe06d63475609 100644 (file)
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
-#include <unistd.h>
 #include <fcntl.h>
+#include <unistd.h>
 
-#include <systemd/sd-journal.h>
+#include "sd-journal.h"
 
 #include "journal-file.h"
 #include "journal-internal.h"
-#include "util.h"
 #include "log.h"
+#include "macro.h"
+#include "parse-util.h"
+#include "rm-rf.h"
+#include "util.h"
 
 #define N_ENTRIES 200
 
 static void verify_contents(sd_journal *j, unsigned skip) {
         unsigned i;
 
-        assert(j);
+        assert_se(j);
 
         i = 0;
         SD_JOURNAL_FOREACH(j) {
                 const void *d;
-                char *k;
+                char *k, *c;
                 size_t l;
-                unsigned u;
+                unsigned u = 0;
 
                 assert_se(sd_journal_get_cursor(j, &k) >= 0);
                 printf("cursor: %s\n", k);
@@ -61,6 +64,10 @@ static void verify_contents(sd_journal *j, unsigned skip) {
                 }
 
                 free(k);
+
+                assert_se(sd_journal_get_cursor(j, &c) >= 0);
+                assert_se(sd_journal_test_cursor(j, c) > 0);
+                free(c);
         }
 
         if (skip > 0)
@@ -71,17 +78,24 @@ int main(int argc, char *argv[]) {
         JournalFile *one, *two, *three;
         char t[] = "/tmp/journal-stream-XXXXXX";
         unsigned i;
-        sd_journal *j;
+        _cleanup_journal_close_ sd_journal *j = NULL;
         char *z;
+        const void *data;
+        size_t l;
+        dual_timestamp previous_ts = DUAL_TIMESTAMP_NULL;
+
+        /* journal_file_open requires a valid machine id */
+        if (access("/etc/machine-id", F_OK) != 0)
+                return EXIT_TEST_SKIP;
 
         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, NULL, NULL, &one) == 0);
-        assert_se(journal_file_open("two.journal", O_RDWR|O_CREAT, 0666, NULL, NULL, &two) == 0);
-        assert_se(journal_file_open("three.journal", O_RDWR|O_CREAT, 0666, NULL, NULL, &three) == 0);
+        assert_se(journal_file_open("one.journal", O_RDWR|O_CREAT, 0666, true, false, NULL, NULL, NULL, &one) == 0);
+        assert_se(journal_file_open("two.journal", O_RDWR|O_CREAT, 0666, true, false, NULL, NULL, NULL, &two) == 0);
+        assert_se(journal_file_open("three.journal", O_RDWR|O_CREAT, 0666, true, false, NULL, NULL, NULL, &three) == 0);
 
         for (i = 0; i < N_ENTRIES; i++) {
                 char *p, *q;
@@ -90,6 +104,14 @@ int main(int argc, char *argv[]) {
 
                 dual_timestamp_get(&ts);
 
+                if (ts.monotonic <= previous_ts.monotonic)
+                        ts.monotonic = previous_ts.monotonic + 1;
+
+                if (ts.realtime <= previous_ts.realtime)
+                        ts.realtime = previous_ts.realtime + 1;
+
+                previous_ts = ts;
+
                 assert_se(asprintf(&p, "NUMBER=%u", i) >= 0);
                 iovec[0].iov_base = p;
                 iovec[0].iov_len = strlen(p);
@@ -120,19 +142,23 @@ int main(int argc, char *argv[]) {
 
         assert_se(sd_journal_add_match(j, "MAGIC=quux", 0) >= 0);
         SD_JOURNAL_FOREACH_BACKWARDS(j) {
-                const void *d;
-                size_t l;
+                _cleanup_free_ char *c;
 
-                assert_se(sd_journal_get_data(j, "NUMBER", &d, &l) >= 0);
-                printf("\t%.*s\n", (int) l, (const char*) d);
+                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) {
-                const void *d;
-                size_t l;
+                _cleanup_free_ char *c;
 
-                assert_se(sd_journal_get_data(j, "NUMBER", &d, &l) >= 0);
-                printf("\t%.*s\n", (int) l, (const char*) d);
+                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);
@@ -161,9 +187,11 @@ int main(int argc, char *argv[]) {
 
         verify_contents(j, 0);
 
-        sd_journal_close(j);
+        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(t, false, true, false) >= 0);
+        assert_se(rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
 
         return 0;
 }