diff --git a/libc3/facts.c b/libc3/facts.c
index fcc5d5f..35a98b9 100644
--- a/libc3/facts.c
+++ b/libc3/facts.c
@@ -219,14 +219,14 @@ sw facts_load (s_facts *facts, s_buf *buf)
if ((r = buf_read_1(buf,
"%{module: C3.Facts.Dump,\n"
" version: 0x0000000000000001,\n"
- " count: 0x")) < 0)
- return r;
+ " count: 0x")) <= 0)
+ goto ko_header;
result += r;
- if ((r = buf_parse_u64_hex(buf, &count)) < 0)
- return r;
+ if ((r = buf_parse_u64_hex(buf, &count)) <= 0)
+ goto ko_header;
result += r;
- if ((r = buf_read_1(buf, "}\n")) < 0)
- return r;
+ if ((r = buf_read_1(buf, "}\n")) <= 0)
+ goto ko_header;
result += r;
hash_init(&hash);
for (i = 0; i < count; i++) {
@@ -255,6 +255,9 @@ sw facts_load (s_facts *facts, s_buf *buf)
if (hash_u64_buf != hash_u64)
goto ko_hash;
return result;
+ ko_header:
+ warnx("facts_load: invalid or missing header");
+ return -1;
ko_fact:
if (r)
warnx("facts_load: invalid fact line %llu", i + 3);
@@ -334,15 +337,21 @@ s_facts * facts_new ()
sw facts_open_buf (s_facts *facts, s_buf *buf)
{
sw r;
+ sw result = 0;
if ((r = buf_read_1(buf,
"%{module: C3.Facts.Save,\n"
- " version: 0x0000000000000001}")) < 0)
- return r;
- if ((r = facts_load(facts, buf)) < 0)
+ " version: 0x0000000000000001}\n")) <= 0) {
+ warnx("facts_open_buf: invalid or missing header");
+ return -1;
+ }
+ result += r;
+ if ((r = facts_load(facts, buf)) <= 0)
return r;
+ result += r;
if ((r = facts_open_log(facts, buf)) < 0)
return r;
- return 0;
+ result += r;
+ return result;
}
sw facts_open_file (s_facts *facts, const s8 *path)
@@ -431,7 +440,9 @@ sw facts_open_log (s_facts *facts, s_buf *buf)
result += r;
facts_remove_fact(facts, &fact);
ok:
- buf_read_1(buf, "\n");
+ if ((r = buf_read_1(buf, "\n")) <= 0)
+ break;
+ result += r;
}
return result;
}
diff --git a/test/facts_test.c b/test/facts_test.c
index bd0b3e5..f8c4c0b 100644
--- a/test/facts_test.c
+++ b/test/facts_test.c
@@ -29,6 +29,7 @@ void facts_test_load ();
void facts_test_log_add ();
void facts_test_log_remove ();
void facts_test_new_delete ();
+void facts_test_open_file ();
void facts_test_remove ();
void facts_test_save ();
@@ -44,6 +45,7 @@ void facts_test ()
facts_test_dump_file();
facts_test_load();
facts_test_save();
+ facts_test_open_file();
}
void facts_test_add ()
@@ -138,7 +140,7 @@ void facts_test_dump_file ()
}
facts_dump_file(&facts, "facts_test_dump_file.facts");
test_file_compare("facts_test_dump_file.facts",
- "facts_test_dump_file.facts.expected");
+ "facts_test_dump_file.expected.facts");
if (g_test_last_ok)
unlink("facts_test_dump_file.facts");
i = 0;
@@ -252,7 +254,7 @@ void facts_test_load ()
s_facts facts;
facts_init(&facts);
TEST_EQ(facts_load_file(&facts,
- "facts_test_dump_file.facts.expected"),
+ "facts_test_load_file.facts"),
742);
TEST_EQ(facts_count(&facts), 23);
while (p[i]) {
@@ -309,7 +311,7 @@ void facts_test_log_add ()
facts_clean(&facts);
fclose(fp);
test_file_compare("facts_test_log_add.facts",
- "facts_test_log_add.facts.expected");
+ "facts_test_log_add.expected.facts");
if (g_test_last_ok)
unlink("facts_test_log_add.facts");
}
@@ -366,7 +368,7 @@ void facts_test_log_remove ()
facts_clean(&facts);
fclose(fp);
test_file_compare("facts_test_log_remove.facts",
- "facts_test_log_remove.facts.expected");
+ "facts_test_log_remove.expected.facts");
if (g_test_last_ok)
unlink("facts_test_log_remove.facts");
}
@@ -393,6 +395,145 @@ void facts_test_new_delete ()
}
}
+void facts_test_open_file ()
+{
+ uw i = 0;
+ s8 *p[24] = {
+ "\"a\"",
+ ":a",
+ "A",
+ "a",
+ "[]",
+ "[[], []]",
+ "{:a, :b}",
+ "{{:a, :b}, {:c, :d}}",
+ "{a, b}",
+ "{{a, b}, {c, d}}",
+ "0",
+ "1",
+ "10",
+ "0x100",
+ "0x10000",
+ "0x100000000",
+ "0x10000000000000000",
+ "-1",
+ "-10",
+ "-0x100",
+ "-0x10000",
+ "-0x100000000",
+ "-0x10000000000000000",
+ NULL
+ };
+ s8 *q[24] = {
+ "\"b\"",
+ ":b",
+ "B",
+ "b",
+ "[[]]",
+ "[[[]], []]",
+ "{:b, :b}",
+ "{{:b, :b}, {:c, :d}}",
+ "{b, b}",
+ "{{b, b}, {c, d}}",
+ "2",
+ "3",
+ "11",
+ "0x101",
+ "0x10001",
+ "0x100000001",
+ "0x10000000000000001",
+ "-2",
+ "-11",
+ "-0x101",
+ "-0x10001",
+ "-0x100000001",
+ "-0x10000000000000001",
+ NULL
+ };
+ s_fact fact;
+ s_facts facts;
+ system("cp facts_test_open_file.1.in.facts facts_test_open_file.1.facts");
+ facts_init(&facts);
+ TEST_EQ(facts_open_file(&facts,
+ "facts_test_open_file.1.facts"),
+ 798);
+ TEST_EQ(facts_count(&facts), 23);
+ i = 0;
+ while (p[i]) {
+ fact_test_init_1(&fact, p[i]);
+ TEST_ASSERT(facts_find_fact(&facts, &fact));
+ TEST_ASSERT(facts_remove_fact(&facts, &fact));
+ fact_test_clean_1(&fact);
+ i++;
+ }
+ i = 0;
+ while (q[i]) {
+ fact_test_init_1(&fact, q[i]);
+ TEST_ASSERT(! facts_find_fact(&facts, &fact));
+ TEST_ASSERT(facts_add_fact(&facts, &fact));
+ fact_test_clean_1(&fact);
+ i++;
+ }
+ facts_close(&facts);
+ facts_clean(&facts);
+ test_file_compare("facts_test_open_file.1.facts",
+ "facts_test_open_file.1.expected.facts");
+ if (g_test_last_ok)
+ unlink("facts_test_open_file.1.facts");
+ facts_init(&facts);
+ system("cp facts_test_open_file.2.in.facts facts_test_open_file.2.facts");
+ TEST_EQ(facts_open_file(&facts,
+ "facts_test_open_file.2.facts"),
+ 1531);
+ TEST_EQ(facts_count(&facts), 46);
+ i = 0;
+ while (p[i]) {
+ fact_test_init_1(&fact, p[i]);
+ TEST_ASSERT(facts_find_fact(&facts, &fact));
+ fact_test_clean_1(&fact);
+ i++;
+ }
+ i = 0;
+ while (q[i]) {
+ fact_test_init_1(&fact, q[i]);
+ TEST_ASSERT(facts_find_fact(&facts, &fact));
+ fact_test_clean_1(&fact);
+ i++;
+ }
+ facts_clean(&facts);
+ test_file_compare("facts_test_open_file.2.facts",
+ "facts_test_open_file.2.expected.facts");
+ if (g_test_last_ok)
+ unlink("facts_test_open_file.2.facts");
+ facts_init(&facts);
+ system("cp facts_test_open_file.3.in.facts facts_test_open_file.3.facts");
+ TEST_EQ(facts_open_file(&facts,
+ "facts_test_open_file.3.facts"),
+ 1588);
+ TEST_EQ(facts_count(&facts), 0);
+ i = 0;
+ while (p[i]) {
+ fact_test_init_1(&fact, p[i]);
+ TEST_ASSERT(! facts_find_fact(&facts, &fact));
+ TEST_ASSERT(facts_add_fact(&facts, &fact));
+ fact_test_clean_1(&fact);
+ i++;
+ }
+ i = 0;
+ while (q[i]) {
+ fact_test_init_1(&fact, q[i]);
+ TEST_ASSERT(! facts_find_fact(&facts, &fact));
+ TEST_ASSERT(facts_add_fact(&facts, &fact));
+ fact_test_clean_1(&fact);
+ i++;
+ }
+ facts_clean(&facts);
+ test_file_compare("facts_test_open_file.3.facts",
+ "facts_test_open_file.3.expected.facts");
+ if (g_test_last_ok)
+ unlink("facts_test_open_file.3.facts");
+}
+
void facts_test_remove ()
{
uw i = 0;
@@ -481,7 +622,7 @@ void facts_test_save ()
}
facts_save_file(&facts, "facts_test_save.facts");
test_file_compare("facts_test_save.facts",
- "facts_test_save.facts.expected");
+ "facts_test_save.expected.facts");
if (g_test_last_ok)
unlink("facts_test_save.facts");
i = 0;
diff --git a/test/facts_test_dump_file.expected.facts b/test/facts_test_dump_file.expected.facts
new file mode 100644
index 0000000..52b1c78
--- /dev/null
+++ b/test/facts_test_dump_file.expected.facts
@@ -0,0 +1,27 @@
+%{module: C3.Facts.Dump,
+ version: 0x0000000000000001,
+ count: 0x0000000000000017}
+{a, a, a}
+{-18446744073709551616, -18446744073709551616, -18446744073709551616}
+{18446744073709551616, 18446744073709551616, 18446744073709551616}
+{-4294967296, -4294967296, -4294967296}
+{-65536, -65536, -65536}
+{-256, -256, -256}
+{-10, -10, -10}
+{-1, -1, -1}
+{0, 0, 0}
+{1, 1, 1}
+{10, 10, 10}
+{256, 256, 256}
+{65536, 65536, 65536}
+{4294967296, 4294967296, 4294967296}
+{[], [], []}
+{[[], []], [[], []], [[], []]}
+{"a", "a", "a"}
+{A, A, A}
+{:a, :a, :a}
+{{a, b}, {a, b}, {a, b}}
+{{:a, :b}, {:a, :b}, {:a, :b}}
+{{{a, b}, {c, d}}, {{a, b}, {c, d}}, {{a, b}, {c, d}}}
+{{{:a, :b}, {:c, :d}}, {{:a, :b}, {:c, :d}}, {{:a, :b}, {:c, :d}}}
+%{hash: 0x12B617E8ED748848}
diff --git a/test/facts_test_dump_file.facts.expected b/test/facts_test_dump_file.facts.expected
deleted file mode 100644
index 52b1c78..0000000
--- a/test/facts_test_dump_file.facts.expected
+++ /dev/null
@@ -1,27 +0,0 @@
-%{module: C3.Facts.Dump,
- version: 0x0000000000000001,
- count: 0x0000000000000017}
-{a, a, a}
-{-18446744073709551616, -18446744073709551616, -18446744073709551616}
-{18446744073709551616, 18446744073709551616, 18446744073709551616}
-{-4294967296, -4294967296, -4294967296}
-{-65536, -65536, -65536}
-{-256, -256, -256}
-{-10, -10, -10}
-{-1, -1, -1}
-{0, 0, 0}
-{1, 1, 1}
-{10, 10, 10}
-{256, 256, 256}
-{65536, 65536, 65536}
-{4294967296, 4294967296, 4294967296}
-{[], [], []}
-{[[], []], [[], []], [[], []]}
-{"a", "a", "a"}
-{A, A, A}
-{:a, :a, :a}
-{{a, b}, {a, b}, {a, b}}
-{{:a, :b}, {:a, :b}, {:a, :b}}
-{{{a, b}, {c, d}}, {{a, b}, {c, d}}, {{a, b}, {c, d}}}
-{{{:a, :b}, {:c, :d}}, {{:a, :b}, {:c, :d}}, {{:a, :b}, {:c, :d}}}
-%{hash: 0x12B617E8ED748848}
diff --git a/test/facts_test_log_add.expected.facts b/test/facts_test_log_add.expected.facts
new file mode 100644
index 0000000..5286f7a
--- /dev/null
+++ b/test/facts_test_log_add.expected.facts
@@ -0,0 +1,23 @@
+add {"a", "a", "a"}
+add {:a, :a, :a}
+add {A, A, A}
+add {a, a, a}
+add {[], [], []}
+add {[[], []], [[], []], [[], []]}
+add {{:a, :b}, {:a, :b}, {:a, :b}}
+add {{{:a, :b}, {:c, :d}}, {{:a, :b}, {:c, :d}}, {{:a, :b}, {:c, :d}}}
+add {{a, b}, {a, b}, {a, b}}
+add {{{a, b}, {c, d}}, {{a, b}, {c, d}}, {{a, b}, {c, d}}}
+add {0, 0, 0}
+add {1, 1, 1}
+add {10, 10, 10}
+add {256, 256, 256}
+add {65536, 65536, 65536}
+add {4294967296, 4294967296, 4294967296}
+add {18446744073709551616, 18446744073709551616, 18446744073709551616}
+add {-1, -1, -1}
+add {-10, -10, -10}
+add {-256, -256, -256}
+add {-65536, -65536, -65536}
+add {-4294967296, -4294967296, -4294967296}
+add {-18446744073709551616, -18446744073709551616, -18446744073709551616}
diff --git a/test/facts_test_log_add.facts.expected b/test/facts_test_log_add.facts.expected
deleted file mode 100644
index 5286f7a..0000000
--- a/test/facts_test_log_add.facts.expected
+++ /dev/null
@@ -1,23 +0,0 @@
-add {"a", "a", "a"}
-add {:a, :a, :a}
-add {A, A, A}
-add {a, a, a}
-add {[], [], []}
-add {[[], []], [[], []], [[], []]}
-add {{:a, :b}, {:a, :b}, {:a, :b}}
-add {{{:a, :b}, {:c, :d}}, {{:a, :b}, {:c, :d}}, {{:a, :b}, {:c, :d}}}
-add {{a, b}, {a, b}, {a, b}}
-add {{{a, b}, {c, d}}, {{a, b}, {c, d}}, {{a, b}, {c, d}}}
-add {0, 0, 0}
-add {1, 1, 1}
-add {10, 10, 10}
-add {256, 256, 256}
-add {65536, 65536, 65536}
-add {4294967296, 4294967296, 4294967296}
-add {18446744073709551616, 18446744073709551616, 18446744073709551616}
-add {-1, -1, -1}
-add {-10, -10, -10}
-add {-256, -256, -256}
-add {-65536, -65536, -65536}
-add {-4294967296, -4294967296, -4294967296}
-add {-18446744073709551616, -18446744073709551616, -18446744073709551616}
diff --git a/test/facts_test_log_remove.expected.facts b/test/facts_test_log_remove.expected.facts
new file mode 100644
index 0000000..f96c838
--- /dev/null
+++ b/test/facts_test_log_remove.expected.facts
@@ -0,0 +1,46 @@
+add {"a", "a", "a"}
+add {:a, :a, :a}
+add {A, A, A}
+add {a, a, a}
+add {[], [], []}
+add {[[], []], [[], []], [[], []]}
+add {{:a, :b}, {:a, :b}, {:a, :b}}
+add {{{:a, :b}, {:c, :d}}, {{:a, :b}, {:c, :d}}, {{:a, :b}, {:c, :d}}}
+add {{a, b}, {a, b}, {a, b}}
+add {{{a, b}, {c, d}}, {{a, b}, {c, d}}, {{a, b}, {c, d}}}
+add {0, 0, 0}
+add {1, 1, 1}
+add {10, 10, 10}
+add {256, 256, 256}
+add {65536, 65536, 65536}
+add {4294967296, 4294967296, 4294967296}
+add {18446744073709551616, 18446744073709551616, 18446744073709551616}
+add {-1, -1, -1}
+add {-10, -10, -10}
+add {-256, -256, -256}
+add {-65536, -65536, -65536}
+add {-4294967296, -4294967296, -4294967296}
+add {-18446744073709551616, -18446744073709551616, -18446744073709551616}
+remove {-18446744073709551616, -18446744073709551616, -18446744073709551616}
+remove {-4294967296, -4294967296, -4294967296}
+remove {-65536, -65536, -65536}
+remove {-256, -256, -256}
+remove {-10, -10, -10}
+remove {-1, -1, -1}
+remove {18446744073709551616, 18446744073709551616, 18446744073709551616}
+remove {4294967296, 4294967296, 4294967296}
+remove {65536, 65536, 65536}
+remove {256, 256, 256}
+remove {10, 10, 10}
+remove {1, 1, 1}
+remove {0, 0, 0}
+remove {{{a, b}, {c, d}}, {{a, b}, {c, d}}, {{a, b}, {c, d}}}
+remove {{a, b}, {a, b}, {a, b}}
+remove {{{:a, :b}, {:c, :d}}, {{:a, :b}, {:c, :d}}, {{:a, :b}, {:c, :d}}}
+remove {{:a, :b}, {:a, :b}, {:a, :b}}
+remove {[[], []], [[], []], [[], []]}
+remove {[], [], []}
+remove {a, a, a}
+remove {A, A, A}
+remove {:a, :a, :a}
+remove {"a", "a", "a"}
diff --git a/test/facts_test_log_remove.facts.expected b/test/facts_test_log_remove.facts.expected
deleted file mode 100644
index f96c838..0000000
--- a/test/facts_test_log_remove.facts.expected
+++ /dev/null
@@ -1,46 +0,0 @@
-add {"a", "a", "a"}
-add {:a, :a, :a}
-add {A, A, A}
-add {a, a, a}
-add {[], [], []}
-add {[[], []], [[], []], [[], []]}
-add {{:a, :b}, {:a, :b}, {:a, :b}}
-add {{{:a, :b}, {:c, :d}}, {{:a, :b}, {:c, :d}}, {{:a, :b}, {:c, :d}}}
-add {{a, b}, {a, b}, {a, b}}
-add {{{a, b}, {c, d}}, {{a, b}, {c, d}}, {{a, b}, {c, d}}}
-add {0, 0, 0}
-add {1, 1, 1}
-add {10, 10, 10}
-add {256, 256, 256}
-add {65536, 65536, 65536}
-add {4294967296, 4294967296, 4294967296}
-add {18446744073709551616, 18446744073709551616, 18446744073709551616}
-add {-1, -1, -1}
-add {-10, -10, -10}
-add {-256, -256, -256}
-add {-65536, -65536, -65536}
-add {-4294967296, -4294967296, -4294967296}
-add {-18446744073709551616, -18446744073709551616, -18446744073709551616}
-remove {-18446744073709551616, -18446744073709551616, -18446744073709551616}
-remove {-4294967296, -4294967296, -4294967296}
-remove {-65536, -65536, -65536}
-remove {-256, -256, -256}
-remove {-10, -10, -10}
-remove {-1, -1, -1}
-remove {18446744073709551616, 18446744073709551616, 18446744073709551616}
-remove {4294967296, 4294967296, 4294967296}
-remove {65536, 65536, 65536}
-remove {256, 256, 256}
-remove {10, 10, 10}
-remove {1, 1, 1}
-remove {0, 0, 0}
-remove {{{a, b}, {c, d}}, {{a, b}, {c, d}}, {{a, b}, {c, d}}}
-remove {{a, b}, {a, b}, {a, b}}
-remove {{{:a, :b}, {:c, :d}}, {{:a, :b}, {:c, :d}}, {{:a, :b}, {:c, :d}}}
-remove {{:a, :b}, {:a, :b}, {:a, :b}}
-remove {[[], []], [[], []], [[], []]}
-remove {[], [], []}
-remove {a, a, a}
-remove {A, A, A}
-remove {:a, :a, :a}
-remove {"a", "a", "a"}
diff --git a/test/facts_test_save.expected.facts b/test/facts_test_save.expected.facts
new file mode 100644
index 0000000..442f3c6
--- /dev/null
+++ b/test/facts_test_save.expected.facts
@@ -0,0 +1,29 @@
+%{module: C3.Facts.Save,
+ version: 0x0000000000000001}
+%{module: C3.Facts.Dump,
+ version: 0x0000000000000001,
+ count: 0x0000000000000017}
+{a, a, a}
+{-18446744073709551616, -18446744073709551616, -18446744073709551616}
+{18446744073709551616, 18446744073709551616, 18446744073709551616}
+{-4294967296, -4294967296, -4294967296}
+{-65536, -65536, -65536}
+{-256, -256, -256}
+{-10, -10, -10}
+{-1, -1, -1}
+{0, 0, 0}
+{1, 1, 1}
+{10, 10, 10}
+{256, 256, 256}
+{65536, 65536, 65536}
+{4294967296, 4294967296, 4294967296}
+{[], [], []}
+{[[], []], [[], []], [[], []]}
+{"a", "a", "a"}
+{A, A, A}
+{:a, :a, :a}
+{{a, b}, {a, b}, {a, b}}
+{{:a, :b}, {:a, :b}, {:a, :b}}
+{{{a, b}, {c, d}}, {{a, b}, {c, d}}, {{a, b}, {c, d}}}
+{{{:a, :b}, {:c, :d}}, {{:a, :b}, {:c, :d}}, {{:a, :b}, {:c, :d}}}
+%{hash: 0x12B617E8ED748848}
diff --git a/test/facts_test_save.facts.expected b/test/facts_test_save.facts.expected
deleted file mode 100644
index 442f3c6..0000000
--- a/test/facts_test_save.facts.expected
+++ /dev/null
@@ -1,29 +0,0 @@
-%{module: C3.Facts.Save,
- version: 0x0000000000000001}
-%{module: C3.Facts.Dump,
- version: 0x0000000000000001,
- count: 0x0000000000000017}
-{a, a, a}
-{-18446744073709551616, -18446744073709551616, -18446744073709551616}
-{18446744073709551616, 18446744073709551616, 18446744073709551616}
-{-4294967296, -4294967296, -4294967296}
-{-65536, -65536, -65536}
-{-256, -256, -256}
-{-10, -10, -10}
-{-1, -1, -1}
-{0, 0, 0}
-{1, 1, 1}
-{10, 10, 10}
-{256, 256, 256}
-{65536, 65536, 65536}
-{4294967296, 4294967296, 4294967296}
-{[], [], []}
-{[[], []], [[], []], [[], []]}
-{"a", "a", "a"}
-{A, A, A}
-{:a, :a, :a}
-{{a, b}, {a, b}, {a, b}}
-{{:a, :b}, {:a, :b}, {:a, :b}}
-{{{a, b}, {c, d}}, {{a, b}, {c, d}}, {{a, b}, {c, d}}}
-{{{:a, :b}, {:c, :d}}, {{:a, :b}, {:c, :d}}, {{:a, :b}, {:c, :d}}}
-%{hash: 0x12B617E8ED748848}