parse.c 5.11 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#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);

17 18
	cl_git_pass(git_object__from_raw((git_object **) &tag, data, datalen, GIT_OBJECT_TAG));
	cl_assert_equal_i(tag->type, GIT_OBJECT_TAG);
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

	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);
57
	cl_git_fail(git_object__from_raw(&object, data, datalen, GIT_OBJECT_TAG));
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
}

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);
}
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216

void test_object_tag_parse__no_oob_read_when_searching_message(void)
{
	const char *tag =
		"object a8d447f68076d1520f69649bb52629941be7031f\n"
		"type tag\n"
		"tag \n"
		"tagger <>\n"
		" \n\n"
		"Message";
	/*
	 * The OOB read previously resulted in an OOM error. We
	 * thus want to make sure that the resulting error is the
	 * expected one.
	 */
	assert_tag_fails(tag, strlen(tag) - strlen("\n\nMessage"));
217
	cl_assert(strstr(git_error_last()->message, "tag contains no message"));
218
}