tests: add tests that exercise tag parsing While the tests in object::tag::read exercises reading and parsing valid tags from the ODB, they barely try to verify that the parser fails in a sane way when parsing invalid tags. Create a new test suite object::tag::parse that directly exercise the parser by using `git_object__from_raw` and add various tests for valid and invalid tags.
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 194 195 196 197 198 199 200 201 202 203 204 205 206
diff --git a/tests/object/tag/parse.c b/tests/object/tag/parse.c
new file mode 100644
index 0000000..7e58ed1
--- /dev/null
+++ b/tests/object/tag/parse.c
@@ -0,0 +1,200 @@
+#include "clar_libgit2.h"
+#include "object.h"
+#include "signature.h"
+#include "tag.h"
+
+static void assert_tag_parses(const char *data, size_t datalen,
+ const char *expected_oid,
+ const char *expected_name,
+ const char *expected_tagger,
+ const char *expected_message)
+{
+ git_tag *tag;
+
+ if (!datalen)
+ datalen = strlen(data);
+
+ cl_git_pass(git_object__from_raw((git_object **) &tag, data, datalen, GIT_OBJ_TAG));
+ cl_assert_equal_i(tag->type, GIT_OBJ_TAG);
+
+ if (expected_oid) {
+ git_oid oid;
+ cl_git_pass(git_oid_fromstr(&oid, expected_oid));
+ cl_assert_equal_oid(&oid, &tag->target);
+ }
+
+ if (expected_name)
+ cl_assert_equal_s(expected_name, tag->tag_name);
+ else
+ cl_assert_equal_s(tag->message, NULL);
+
+ if (expected_tagger) {
+ git_signature *tagger;
+ cl_git_pass(git_signature_from_buffer(&tagger, expected_tagger));
+ cl_assert_equal_s(tagger->name, tag->tagger->name);
+ cl_assert_equal_s(tagger->email, tag->tagger->email);
+ cl_assert_equal_i(tagger->when.time, tag->tagger->when.time);
+ cl_assert_equal_i(tagger->when.offset, tag->tagger->when.offset);
+ cl_assert_equal_i(tagger->when.sign, tag->tagger->when.sign);
+ git_signature_free(tagger);
+ } else {
+ cl_assert_equal_s(tag->tagger, NULL);
+ }
+
+ if (expected_message)
+ cl_assert_equal_s(expected_message, tag->message);
+ else
+ cl_assert_equal_s(tag->message, NULL);
+
+ git_object__free(&tag->object);
+}
+
+static void assert_tag_fails(const char *data, size_t datalen)
+{
+ git_object *object;
+ if (!datalen)
+ datalen = strlen(data);
+ cl_git_fail(git_object__from_raw(&object, data, datalen, GIT_OBJ_TAG));
+}
+
+void test_object_tag_parse__valid_tag_parses(void)
+{
+ const char *tag =
+ "object a8d447f68076d1520f69649bb52629941be7031f\n"
+ "type tag\n"
+ "tag tagname\n"
+ "tagger Taggy Mr. Taggart <taggy@taggart.com>\n"
+ "\n"
+ "Message";
+ assert_tag_parses(tag, 0,
+ "a8d447f68076d1520f69649bb52629941be7031f",
+ "tagname",
+ "Taggy Mr. Taggart <taggy@taggart.com>",
+ "Message");
+}
+
+void test_object_tag_parse__missing_tagger_parses(void)
+{
+ const char *tag =
+ "object a8d447f68076d1520f69649bb52629941be7031f\n"
+ "type tag\n"
+ "tag tagname\n"
+ "\n"
+ "Message";
+ assert_tag_parses(tag, 0,
+ "a8d447f68076d1520f69649bb52629941be7031f",
+ "tagname",
+ NULL,
+ "Message");
+}
+
+void test_object_tag_parse__missing_message_parses(void)
+{
+ const char *tag =
+ "object a8d447f68076d1520f69649bb52629941be7031f\n"
+ "type tag\n"
+ "tag tagname\n"
+ "tagger Taggy Mr. Taggart <taggy@taggart.com>\n";
+ assert_tag_parses(tag, 0,
+ "a8d447f68076d1520f69649bb52629941be7031f",
+ "tagname",
+ "Taggy Mr. Taggart <taggy@taggart.com>",
+ NULL);
+}
+
+void test_object_tag_parse__unknown_field_parses(void)
+{
+ const char *tag =
+ "object a8d447f68076d1520f69649bb52629941be7031f\n"
+ "type tag\n"
+ "tag tagname\n"
+ "tagger Taggy Mr. Taggart <taggy@taggart.com>\n"
+ "foo bar\n"
+ "frubble frabble\n"
+ "\n"
+ "Message";
+ assert_tag_parses(tag, 0,
+ "a8d447f68076d1520f69649bb52629941be7031f",
+ "tagname",
+ "Taggy Mr. Taggart <taggy@taggart.com>",
+ "Message");
+}
+
+void test_object_tag_parse__missing_object_fails(void)
+{
+ const char *tag =
+ "type tag\n"
+ "tag tagname\n"
+ "tagger Taggy Mr. Taggart <taggy@taggart.com>\n"
+ "\n"
+ "Message";
+ assert_tag_fails(tag, 0);
+}
+
+void test_object_tag_parse__malformatted_object_fails(void)
+{
+ const char *tag =
+ "object a8d447f68076d15xxxxxxxxxxxxxxxx41be7031f\n"
+ "type tag\n"
+ "tag tagname\n"
+ "tagger Taggy Mr. Taggart <taggy@taggart.com>\n"
+ "\n"
+ "Message";
+ assert_tag_fails(tag, 0);
+}
+
+void test_object_tag_parse__missing_type_fails(void)
+{
+ const char *tag =
+ "object a8d447f68076d1520f69649bb52629941be7031f\n"
+ "tag tagname\n"
+ "tagger Taggy Mr. Taggart <taggy@taggart.com>\n"
+ "\n"
+ "Message";
+ assert_tag_fails(tag, 0);
+}
+
+void test_object_tag_parse__invalid_type_fails(void)
+{
+ const char *tag =
+ "object a8d447f68076d1520f69649bb52629941be7031f\n"
+ "type garbage\n"
+ "tag tagname\n"
+ "tagger Taggy Mr. Taggart <taggy@taggart.com>\n"
+ "\n"
+ "Message";
+ assert_tag_fails(tag, 0);
+}
+
+void test_object_tag_parse__missing_tagname_fails(void)
+{
+ const char *tag =
+ "object a8d447f68076d1520f69649bb52629941be7031f\n"
+ "type tag\n"
+ "tagger Taggy Mr. Taggart <taggy@taggart.com>\n"
+ "\n"
+ "Message";
+ assert_tag_fails(tag, 0);
+}
+
+void test_object_tag_parse__misformatted_tagger_fails(void)
+{
+ const char *tag =
+ "object a8d447f68076d1520f69649bb52629941be7031f\n"
+ "type tag\n"
+ "tag Tag\n"
+ "tagger taggy@taggart.com>\n"
+ "\n"
+ "Message";
+ assert_tag_fails(tag, 0);
+}
+
+void test_object_tag_parse__missing_message_fails(void)
+{
+ const char *tag =
+ "object a8d447f68076d1520f69649bb52629941be7031f\n"
+ "type tag\n"
+ "tag Tag\n"
+ "tagger taggy@taggart.com>\n";
+ assert_tag_fails(tag, 0);
+}