Add unit tests for Commit parsing A few initial tests for commit parsing: "parse_buffer_test" tests git_commit__parse_buffer() with several malformed commit messages and a few corner cases which should pass. "parse_oid_test" tests git_commit__parse_oid() with several malformed commit lines containing broken SHA1 OIDs. Signed-off-by: Vicent Marti <tanoku@gmail.com> Signed-off-by: Andreas Ericsson <ae@op5.se>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
diff --git a/src/commit.c b/src/commit.c
index bbb0d9b..1b30c5d 100644
--- a/src/commit.c
+++ b/src/commit.c
@@ -40,6 +40,9 @@ git_commit *git_commit_lookup(git_revpool *pool, const git_oid *id)
git_obj commit_obj;
git_commit *commit = NULL;
+ if (pool == NULL || pool->db == NULL)
+ return NULL;
+
/*
* TODO: check if the commit is already cached in the
* revpool instead of loading it from the odb
diff --git a/tests/NAMING b/tests/NAMING
index 73bb602..eba9147 100644
--- a/tests/NAMING
+++ b/tests/NAMING
@@ -20,3 +20,5 @@ Categories
02__: Basic object read access.
03__: Basic object writing.
+
+04__: Parsing and loading commit data
diff --git a/tests/t0401-parse.c b/tests/t0401-parse.c
new file mode 100644
index 0000000..5b209c9
--- /dev/null
+++ b/tests/t0401-parse.c
@@ -0,0 +1,163 @@
+#include "test_lib.h"
+#include "test_helpers.h"
+#include "commit.h"
+#include <git/odb.h>
+#include <git/commit.h>
+
+static char *test_commits_broken[] = {
+
+ // empty commit
+"",
+
+ // random garbage
+"asd97sa9du902e9a0jdsuusad09as9du098709aweu8987sd\n",
+
+ // broken endlines 1
+"tree f6c0dad3c7b3481caa9d73db21f91964894a945b\r\n\
+parent 05452d6349abcd67aa396dfb28660d765d8b2a36\r\n\
+author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\r\n\
+committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\r\n\
+\r\n\
+a test commit with broken endlines\r\n",
+
+ // broken endlines 2
+"tree f6c0dad3c7b3481caa9d73db21f91964894a945b\
+parent 05452d6349abcd67aa396dfb28660d765d8b2a36\
+author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\
+committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\
+\
+another test commit with broken endlines",
+
+ // starting endlines
+"\ntree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
+parent 05452d6349abcd67aa396dfb28660d765d8b2a36\n\
+author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
+committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
+\n\
+a test commit with a starting endline\n",
+
+ // corrupted commit 1
+"tree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
+parent 05452d6349abcd67aa396df",
+
+ // corrupted commit 2
+"tree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
+parent ",
+
+ // corrupted commit 3
+"tree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
+parent ",
+
+ // corrupted commit 4
+"tree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
+par",
+
+ // FIXME: duplicated parents?
+ // It this supposed to pass?
+/*
+"tree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
+parent 05452d6349abcd67aa396dfb28660d765d8b2a36\n\
+parent 05452d6349abcd67aa396dfb28660d765d8b2a36\n\
+author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
+committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
+\n\
+duplicated parent",
+*/
+};
+
+
+static char *test_commits_working[] = {
+ // simple commit with no message
+"tree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
+author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
+committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
+\n",
+
+ // simple commit, no parent
+"tree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
+author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
+committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
+\n\
+a simple commit which works\n",
+
+ // simple commit, 1 parents
+"tree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
+parent 05452d6349abcd67aa396dfb28660d765d8b2a36\n\
+author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
+committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
+\n\
+a simple commit which works\n",
+};
+
+BEGIN_TEST(parse_oid_test)
+
+ git_oid oid;
+
+#define TEST_OID_PASS(string, header){ \
+ char *ptr = string;\
+ char *ptr_original = ptr;\
+ size_t len = strlen(ptr);\
+ must_pass(git_commit__parse_oid(&oid, &ptr, ptr + len, header));\
+ must_be_true(ptr == ptr_original + len);\
+}
+
+#define TEST_OID_FAIL(string, header){ \
+ char *ptr = string;\
+ size_t len = strlen(ptr);\
+ must_fail(git_commit__parse_oid(&oid, &ptr, ptr + len, header));\
+}
+
+ TEST_OID_PASS("parent 05452d6349abcd67aa396dfb28660d765d8b2a36\n", "parent ");
+ TEST_OID_PASS("tree 05452d6349abcd67aa396dfb28660d765d8b2a36\n", "tree ");
+ TEST_OID_PASS("random_heading 05452d6349abcd67aa396dfb28660d765d8b2a36\n", "random_heading ");
+ TEST_OID_PASS("stuck_heading05452d6349abcd67aa396dfb28660d765d8b2a36\n", "stuck_heading");
+ TEST_OID_PASS("tree 5F4BEFFC0759261D015AA63A3A85613FF2F235DE\n", "tree ");
+ TEST_OID_PASS("tree 1A669B8AB81B5EB7D9DB69562D34952A38A9B504\n", "tree ");
+ TEST_OID_PASS("tree 5B20DCC6110FCC75D31C6CEDEBD7F43ECA65B503\n", "tree ");
+ TEST_OID_PASS("tree 173E7BF00EA5C33447E99E6C1255954A13026BE4\n", "tree ");
+
+ TEST_OID_FAIL("parent 05452d6349abcd67aa396dfb28660d765d8b2a36", "parent ");
+ TEST_OID_FAIL("05452d6349abcd67aa396dfb28660d765d8b2a36\n", "tree ");
+ TEST_OID_FAIL("parent05452d6349abcd67aa396dfb28660d765d8b2a6a\n", "parent ");
+ TEST_OID_FAIL("parent 05452d6349abcd67aa396dfb280d765d8b2a6\n", "parent ");
+ TEST_OID_FAIL("tree 05452d6349abcd67aa396dfb28660d765d8b2a36\n", "tree ");
+ TEST_OID_FAIL("parent 0545xd6349abcd67aa396dfb28660d765d8b2a36\n", "parent ");
+ TEST_OID_FAIL("parent 0545xd6349abcd67aa396dfb28660d765d8b2a36FF\n", "parent ");
+ TEST_OID_FAIL("", "tree ");
+ TEST_OID_FAIL("", "");
+
+#undef TEST_OID_PASS
+#undef TEST_OID_FAIL
+
+END_TEST
+
+BEGIN_TEST(parse_buffer_test)
+ const int broken_commit_count = sizeof(test_commits_broken) / sizeof(*test_commits_broken);
+ const int working_commit_count = sizeof(test_commits_working) / sizeof(*test_commits_working);
+ int i;
+
+ for (i = 0; i < broken_commit_count; ++i) {
+ git_commit commit;
+ commit.parsed = 0;
+ commit.pool = NULL;
+
+ must_fail(git_commit__parse_buffer(
+ &commit,
+ test_commits_broken[i],
+ strlen(test_commits_broken[i]))
+ );
+ }
+
+ for (i = 0; i < working_commit_count; ++i) {
+ git_commit commit;
+ commit.parsed = 0;
+ commit.pool = NULL;
+
+ must_pass(git_commit__parse_buffer(
+ &commit,
+ test_commits_working[i],
+ strlen(test_commits_working[i]))
+ );
+ }
+
+END_TEST