tests: verify parsing logic for smart packets The commits following this commit are about to introduce quite a lot of refactoring and tightening of the smart packet parser. Unfortunately, we do not yet have any tests despite our online tests that verify that our parser does not regress upon changes. This is doubly unfortunate as our online tests aren't executed by default. Add new tests that exercise the smart parsing logic directly by executing `git_pkt_parse_line`.
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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
diff --git a/tests/transports/smart/packet.c b/tests/transports/smart/packet.c
new file mode 100644
index 0000000..2b9b38f
--- /dev/null
+++ b/tests/transports/smart/packet.c
@@ -0,0 +1,348 @@
+#include "clar_libgit2.h"
+#include "transports/smart.h"
+
+enum expected_status {
+ PARSE_SUCCESS,
+ PARSE_FAILURE
+};
+
+static void assert_flush_parses(const char *line)
+{
+ size_t linelen = strlen(line) + 1;
+ const char *endptr;
+ git_pkt *pkt;
+
+ cl_git_pass(git_pkt_parse_line((git_pkt **) &pkt, line, &endptr, linelen));
+ cl_assert_equal_i(pkt->type, GIT_PKT_FLUSH);
+ cl_assert_equal_strn(endptr, line + 4, linelen - 4);
+
+ git_pkt_free((git_pkt *) pkt);
+}
+
+static void assert_data_pkt_parses(const char *line, const char *expected_data, size_t expected_len)
+{
+ size_t linelen = strlen(line) + 1;
+ const char *endptr;
+ git_pkt_data *pkt;
+
+ cl_git_pass(git_pkt_parse_line((git_pkt **) &pkt, line, &endptr, linelen));
+ cl_assert_equal_i(pkt->type, GIT_PKT_DATA);
+ cl_assert_equal_i(pkt->len, expected_len);
+ cl_assert_equal_strn(pkt->data, expected_data, expected_len);
+
+ git_pkt_free((git_pkt *) pkt);
+}
+
+static void assert_sideband_progress_parses(const char *line, const char *expected_data, size_t expected_len)
+{
+ size_t linelen = strlen(line) + 1;
+ const char *endptr;
+ git_pkt_progress *pkt;
+
+ cl_git_pass(git_pkt_parse_line((git_pkt **) &pkt, line, &endptr, linelen));
+ cl_assert_equal_i(pkt->type, GIT_PKT_PROGRESS);
+ cl_assert_equal_i(pkt->len, expected_len);
+ cl_assert_equal_strn(pkt->data, expected_data, expected_len);
+
+ git_pkt_free((git_pkt *) pkt);
+}
+
+static void assert_error_parses(const char *line, const char *expected_error, size_t expected_len)
+{
+ size_t linelen = strlen(line) + 1;
+ const char *endptr;
+ git_pkt_err *pkt;
+
+ cl_git_pass(git_pkt_parse_line((git_pkt **) &pkt, line, &endptr, linelen));
+ cl_assert_equal_i(pkt->type, GIT_PKT_ERR);
+ cl_assert_equal_i(pkt->len, expected_len);
+ cl_assert_equal_strn(pkt->error, expected_error, expected_len);
+
+ git_pkt_free((git_pkt *) pkt);
+}
+
+static void assert_ack_parses(const char *line, const char *expected_oid, enum git_ack_status expected_status)
+{
+ size_t linelen = strlen(line) + 1;
+ const char *endptr;
+ git_pkt_ack *pkt;
+ git_oid oid;
+
+ cl_git_pass(git_oid_fromstr(&oid, expected_oid));
+
+ cl_git_pass(git_pkt_parse_line((git_pkt **) &pkt, line, &endptr, linelen));
+ cl_assert_equal_i(pkt->type, GIT_PKT_ACK);
+ cl_assert_equal_oid(&pkt->oid, &oid);
+ cl_assert_equal_i(pkt->status, expected_status);
+
+ git_pkt_free((git_pkt *) pkt);
+}
+
+static void assert_nak_parses(const char *line)
+{
+ size_t linelen = strlen(line) + 1;
+ const char *endptr;
+ git_pkt *pkt;
+
+ cl_git_pass(git_pkt_parse_line((git_pkt **) &pkt, line, &endptr, linelen));
+ cl_assert_equal_i(pkt->type, GIT_PKT_NAK);
+ cl_assert_equal_strn(endptr, line + 7, linelen - 7);
+
+ git_pkt_free((git_pkt *) pkt);
+}
+
+static void assert_comment_parses(const char *line, const char *expected_comment)
+{
+ size_t linelen = strlen(line) + 1;
+ const char *endptr;
+ git_pkt_comment *pkt;
+
+ cl_git_pass(git_pkt_parse_line((git_pkt **) &pkt, line, &endptr, linelen));
+ cl_assert_equal_i(pkt->type, GIT_PKT_COMMENT);
+ cl_assert_equal_strn(pkt->comment, expected_comment, strlen(expected_comment));
+
+ git_pkt_free((git_pkt *) pkt);
+}
+
+static void assert_ok_parses(const char *line, const char *expected_ref)
+{
+ size_t linelen = strlen(line) + 1;
+ const char *endptr;
+ git_pkt_ok *pkt;
+
+ cl_git_pass(git_pkt_parse_line((git_pkt **) &pkt, line, &endptr, linelen));
+ cl_assert_equal_i(pkt->type, GIT_PKT_OK);
+ cl_assert_equal_strn(pkt->ref, expected_ref, strlen(expected_ref));
+
+ git_pkt_free((git_pkt *) pkt);
+}
+
+static void assert_unpack_parses(const char *line, bool ok)
+{
+ size_t linelen = strlen(line) + 1;
+ const char *endptr;
+ git_pkt_unpack *pkt;
+
+ cl_git_pass(git_pkt_parse_line((git_pkt **) &pkt, line, &endptr, linelen));
+ cl_assert_equal_i(pkt->type, GIT_PKT_UNPACK);
+ cl_assert_equal_i(pkt->unpack_ok, ok);
+
+ git_pkt_free((git_pkt *) pkt);
+}
+
+static void assert_ng_parses(const char *line, const char *expected_ref, const char *expected_msg)
+{
+ size_t linelen = strlen(line) + 1;
+ const char *endptr;
+ git_pkt_ng *pkt;
+
+ cl_git_pass(git_pkt_parse_line((git_pkt **) &pkt, line, &endptr, linelen));
+ cl_assert_equal_i(pkt->type, GIT_PKT_NG);
+ cl_assert_equal_strn(pkt->ref, expected_ref, strlen(expected_ref));
+ cl_assert_equal_strn(pkt->msg, expected_msg, strlen(expected_msg));
+
+ git_pkt_free((git_pkt *) pkt);
+}
+
+#define assert_ref_parses(line, expected_oid, expected_ref, expected_capabilities) \
+ assert_ref_parses_(line, sizeof(line), expected_oid, expected_ref, expected_capabilities)
+
+static void assert_ref_parses_(const char *line, size_t linelen, const char *expected_oid,
+ const char *expected_ref, const char *expected_capabilities)
+{
+ const char *endptr;
+ git_pkt_ref *pkt;
+ git_oid oid;
+
+ cl_git_pass(git_oid_fromstr(&oid, expected_oid));
+
+ cl_git_pass(git_pkt_parse_line((git_pkt **) &pkt, line, &endptr, linelen));
+ cl_assert_equal_i(pkt->type, GIT_PKT_REF);
+ cl_assert_equal_oid(&pkt->head.oid, &oid);
+ cl_assert_equal_strn(pkt->head.name, expected_ref, strlen(expected_ref));
+ if (expected_capabilities)
+ cl_assert_equal_strn(pkt->capabilities, expected_capabilities, strlen(expected_capabilities));
+ else
+ cl_assert_equal_p(NULL, pkt->capabilities);
+
+ git_pkt_free((git_pkt *) pkt);
+}
+
+static void assert_pkt_fails(const char *line)
+{
+ const char *endptr;
+ git_pkt *pkt;
+ cl_git_fail(git_pkt_parse_line(&pkt, line, &endptr, strlen(line) + 1));
+}
+
+void test_transports_smart_packet__parsing_garbage_fails(void)
+{
+ assert_pkt_fails("0foobar");
+ assert_pkt_fails("00foobar");
+ assert_pkt_fails("000foobar");
+ assert_pkt_fails("0001");
+ assert_pkt_fails("");
+ assert_pkt_fails("0");
+ assert_pkt_fails("0i00");
+ assert_pkt_fails("f");
+}
+
+void test_transports_smart_packet__flush_parses(void)
+{
+ assert_flush_parses("0000");
+ assert_flush_parses("0000foobar");
+}
+
+void test_transports_smart_packet__data_pkt(void)
+{
+ assert_pkt_fails("000foobar");
+ assert_pkt_fails("0001o");
+ assert_pkt_fails("0001\1");
+ assert_data_pkt_parses("0005\1", "", 0);
+ assert_pkt_fails("0009\1o");
+ assert_data_pkt_parses("0009\1data", "data", 4);
+ assert_data_pkt_parses("000a\1data", "data", 5);
+}
+
+void test_transports_smart_packet__sideband_progress_pkt(void)
+{
+ assert_pkt_fails("0001\2");
+ assert_sideband_progress_parses("0005\2", "", 0);
+ assert_pkt_fails("0009\2o");
+ assert_sideband_progress_parses("0009\2data", "data", 4);
+ assert_sideband_progress_parses("000a\2data", "data", 5);
+}
+
+void test_transports_smart_packet__sideband_err_pkt(void)
+{
+ assert_pkt_fails("0001\3");
+ assert_error_parses("0005\3", "", 0);
+ assert_pkt_fails("0009\3o");
+ assert_error_parses("0009\3data", "data", 4);
+ assert_error_parses("000a\3data", "data", 5);
+}
+
+void test_transports_smart_packet__ack_pkt(void)
+{
+ assert_ack_parses("0030ACK 0000000000000000000000000000000000000000",
+ "0000000000000000000000000000000000000000", 0);
+ assert_ack_parses("0039ACK 0000000000000000000000000000000000000000 continue",
+ "0000000000000000000000000000000000000000",
+ GIT_ACK_CONTINUE);
+ assert_ack_parses("0037ACK 0000000000000000000000000000000000000000 common",
+ "0000000000000000000000000000000000000000",
+ GIT_ACK_COMMON);
+ assert_ack_parses("0037ACK 0000000000000000000000000000000000000000 ready",
+ "0000000000000000000000000000000000000000",
+ GIT_ACK_READY);
+
+ /* TODO: these should fail as they don't have OIDs */
+ assert_ack_parses("0007ACK", "0000000000000000000000000000000000000000", 0);
+ assert_ack_parses("0008ACK ", "0000000000000000000000000000000000000000", 0);
+
+ /* TODO: this one is missing a space and should thus fail */
+ assert_ack_parses("0036ACK00000000000000000x0000000000000000000000 ready",
+ "0000000000000000000000000000000000000000", 0);
+
+ /* TODO: the following ones have invalid OIDs and should thus fail */
+ assert_ack_parses("0037ACK 00000000000000000x0000000000000000000000 ready",
+ "0000000000000000000000000000000000000000", GIT_ACK_READY);
+ assert_ack_parses("0036ACK 000000000000000000000000000000000000000 ready",
+ "0000000000000000000000000000000000000000", 0);
+ assert_ack_parses("0036ACK 00000000000000000x0000000000000000000000ready",
+ "0000000000000000000000000000000000000000", 0);
+
+ /* TODO: this one has an invalid status and should thus fail */
+ assert_ack_parses("0036ACK 0000000000000000000000000000000000000000 read",
+ "0000000000000000000000000000000000000000", 0);
+}
+
+void test_transports_smart_packet__nak_pkt(void)
+{
+ assert_nak_parses("0007NAK");
+ assert_pkt_fails("0007NaK");
+ assert_pkt_fails("0007nak");
+ assert_nak_parses("0007NAKfoobar");
+ assert_pkt_fails("0007nakfoobar");
+ assert_pkt_fails("0007 NAK");
+}
+
+void test_transports_smart_packet__error_pkt(void)
+{
+ assert_pkt_fails("0007ERR");
+ assert_pkt_fails("0008ERRx");
+ assert_error_parses("0008ERR ", "", 0);
+ assert_error_parses("000EERR ERRMSG", "ERRMSG", 6);
+}
+
+void test_transports_smart_packet__comment_pkt(void)
+{
+ assert_comment_parses("0005#", "");
+ assert_comment_parses("000B#foobar", "#fooba");
+ assert_comment_parses("000C#foobar", "#foobar");
+ assert_comment_parses("001A#this is a comment\nfoo", "#this is a comment\nfoo");
+}
+
+void test_transports_smart_packet__ok_pkt(void)
+{
+ /*
+ * TODO: the trailing slash is currently mandatory. Check
+ * if we should really enforce this. As this test is part
+ * of a security release, I feel uneasy to change
+ * behaviour right now and leave it for a later point.
+ */
+ assert_pkt_fails("0007ok\n");
+ assert_ok_parses("0008ok \n", "");
+ assert_ok_parses("0009ok x\n", "x");
+ assert_pkt_fails("0013OK ref/foo/bar\n");
+ assert_ok_parses("0013ok ref/foo/bar\n", "ref/foo/bar");
+ assert_ok_parses("0012ok ref/foo/bar\n", "ref/foo/bar");
+}
+
+void test_transports_smart_packet__ng_pkt(void)
+{
+ /* TODO: same as for ok pkt */
+ assert_pkt_fails("0007ng\n");
+ assert_pkt_fails("0008ng \n");
+ assert_pkt_fails("000Bng ref\n");
+ assert_pkt_fails("000Bng ref\n");
+ /* TODO: is this a valid packet line? Probably not. */
+ assert_ng_parses("000Ang x\n", "", "x");
+ assert_ng_parses("000Fng ref msg\n", "ref", "msg");
+ assert_ng_parses("000Fng ref msg\n", "ref", "msg");
+}
+
+void test_transports_smart_packet__unpack_pkt(void)
+{
+ assert_unpack_parses("000Dunpack ok", 1);
+ assert_unpack_parses("000Dunpack ng error-msg", 0);
+ /* TODO: the following tests should fail */
+ assert_unpack_parses("000Aunpack", 0);
+ assert_unpack_parses("0011unpack foobar", 0);
+ assert_unpack_parses("0010unpack ng ok", 0);
+ assert_unpack_parses("0010unpack okfoo", 1);
+}
+
+void test_transports_smart_packet__ref_pkt(void)
+{
+ assert_pkt_fails("002C0000000000000000000000000000000000000000");
+ assert_pkt_fails("002D0000000000000000000000000000000000000000\n");
+ assert_pkt_fails("00300000000000000000000000000000000000000000HEAD");
+ assert_pkt_fails("004800000000x0000000000000000000000000000000 refs/heads/master\0multi_ack");
+ assert_ref_parses(
+ "003F0000000000000000000000000000000000000000 refs/heads/master\0",
+ "0000000000000000000000000000000000000000", "refs/heads/master", "");
+ assert_ref_parses(
+ "00480000000000000000000000000000000000000000 refs/heads/master\0multi_ack",
+ "0000000000000000000000000000000000000000", "refs/heads/master", "multi_ack");
+ assert_ref_parses(
+ "00460000000000000000000000000000000000000000 refs/heads/master\0one two",
+ "0000000000000000000000000000000000000000", "refs/heads/master", "one two");
+ assert_ref_parses(
+ "00310000000000000000000000000000000000000000 HEAD",
+ "0000000000000000000000000000000000000000", "HEAD", NULL);
+ assert_pkt_fails("0031000000000000000000000000000000000000000 HEAD");
+ assert_ref_parses(
+ "00360000000000000000000000000000000000000000 HEAD HEAD",
+ "0000000000000000000000000000000000000000", "HEAD HEAD", NULL);
+}