1 #include "clar_libgit2.h"
2 #include "buffer.h"
3 
expect_decode_pass(const char * expected,const char * encoded)4 static void expect_decode_pass(const char *expected, const char *encoded)
5 {
6 	git_buf in = GIT_BUF_INIT, out = GIT_BUF_INIT;
7 
8 	/*
9 	 * ensure that we only read the given length of the input buffer
10 	 * by putting garbage at the end.  this will ensure that we do
11 	 * not, eg, rely on nul-termination or walk off the end of the buf.
12 	 */
13 	cl_git_pass(git_buf_puts(&in, encoded));
14 	cl_git_pass(git_buf_PUTS(&in, "TRAILER"));
15 
16 	cl_git_pass(git_buf_decode_percent(&out, in.ptr, strlen(encoded)));
17 
18 	cl_assert_equal_s(expected, git_buf_cstr(&out));
19 	cl_assert_equal_i(strlen(expected), git_buf_len(&out));
20 
21 	git_buf_dispose(&in);
22 	git_buf_dispose(&out);
23 }
24 
test_buf_percent__decode_succeeds(void)25 void test_buf_percent__decode_succeeds(void)
26 {
27 	expect_decode_pass("", "");
28 	expect_decode_pass(" ", "%20");
29 	expect_decode_pass("a", "a");
30 	expect_decode_pass(" a", "%20a");
31 	expect_decode_pass("a ", "a%20");
32 	expect_decode_pass("github.com", "github.com");
33 	expect_decode_pass("github.com", "githu%62.com");
34 	expect_decode_pass("github.com", "github%2ecom");
35 	expect_decode_pass("foo bar baz", "foo%20bar%20baz");
36 	expect_decode_pass("foo bar baz", "foo%20bar%20baz");
37 	expect_decode_pass("foo bar ", "foo%20bar%20");
38 }
39 
test_buf_percent__ignores_invalid(void)40 void test_buf_percent__ignores_invalid(void)
41 {
42 	expect_decode_pass("githu%%.com", "githu%%.com");
43 	expect_decode_pass("github.co%2", "github.co%2");
44 	expect_decode_pass("github%2.com", "github%2.com");
45 	expect_decode_pass("githu%2z.com", "githu%2z.com");
46 	expect_decode_pass("github.co%9z", "github.co%9z");
47 	expect_decode_pass("github.co%2", "github.co%2");
48 	expect_decode_pass("github.co%", "github.co%");
49 }
50