]> git.proxmox.com Git - libgit2.git/blob - tests/buf/quote.c
git_buf_quote/unquote: handle > \177
[libgit2.git] / tests / buf / quote.c
1 #include "clar_libgit2.h"
2 #include "buffer.h"
3
4 static void expect_quote_pass(const char *expected, const char *str)
5 {
6 git_buf buf = GIT_BUF_INIT;
7
8 cl_git_pass(git_buf_puts(&buf, str));
9 cl_git_pass(git_buf_quote(&buf));
10
11 cl_assert_equal_s(expected, git_buf_cstr(&buf));
12 cl_assert_equal_i(strlen(expected), git_buf_len(&buf));
13
14 git_buf_free(&buf);
15 }
16
17 void test_buf_quote__quote_succeeds(void)
18 {
19 expect_quote_pass("", "");
20 expect_quote_pass("foo", "foo");
21 expect_quote_pass("foo/bar/baz.c", "foo/bar/baz.c");
22 expect_quote_pass("foo bar", "foo bar");
23 expect_quote_pass("\"\\\"leading quote\"", "\"leading quote");
24 expect_quote_pass("\"slash\\\\y\"", "slash\\y");
25 expect_quote_pass("\"foo\\r\\nbar\"", "foo\r\nbar");
26 expect_quote_pass("\"foo\\177bar\"", "foo\177bar");
27 expect_quote_pass("\"foo\\001bar\"", "foo\001bar");
28 expect_quote_pass("\"foo\\377bar\"", "foo\377bar");
29 }
30
31 static void expect_unquote_pass(const char *expected, const char *quoted)
32 {
33 git_buf buf = GIT_BUF_INIT;
34
35 cl_git_pass(git_buf_puts(&buf, quoted));
36 cl_git_pass(git_buf_unquote(&buf));
37
38 cl_assert_equal_s(expected, git_buf_cstr(&buf));
39 cl_assert_equal_i(strlen(expected), git_buf_len(&buf));
40
41 git_buf_free(&buf);
42 }
43
44 static void expect_unquote_fail(const char *quoted)
45 {
46 git_buf buf = GIT_BUF_INIT;
47
48 cl_git_pass(git_buf_puts(&buf, quoted));
49 cl_git_fail(git_buf_unquote(&buf));
50
51 git_buf_free(&buf);
52 }
53
54 void test_buf_quote__unquote_succeeds(void)
55 {
56 expect_unquote_pass("", "\"\"");
57 expect_unquote_pass(" ", "\" \"");
58 expect_unquote_pass("foo", "\"foo\"");
59 expect_unquote_pass("foo bar", "\"foo bar\"");
60 expect_unquote_pass("foo\"bar", "\"foo\\\"bar\"");
61 expect_unquote_pass("foo\\bar", "\"foo\\\\bar\"");
62 expect_unquote_pass("foo\tbar", "\"foo\\tbar\"");
63 expect_unquote_pass("\vfoo\tbar\n", "\"\\vfoo\\tbar\\n\"");
64 expect_unquote_pass("foo\nbar", "\"foo\\012bar\"");
65 expect_unquote_pass("foo\r\nbar", "\"foo\\015\\012bar\"");
66 expect_unquote_pass("foo\r\nbar", "\"\\146\\157\\157\\015\\012\\142\\141\\162\"");
67 expect_unquote_pass("newline: \n", "\"newline: \\012\"");
68 expect_unquote_pass("0xff: \377", "\"0xff: \\377\"");
69 }
70
71 void test_buf_quote__unquote_fails(void)
72 {
73 expect_unquote_fail("no quotes at all");
74 expect_unquote_fail("\"no trailing quote");
75 expect_unquote_fail("no leading quote\"");
76 expect_unquote_fail("\"invalid \\z escape char\"");
77 expect_unquote_fail("\"\\q invalid escape char\"");
78 expect_unquote_fail("\"invalid escape char \\p\"");
79 expect_unquote_fail("\"invalid \\1 escape char \"");
80 expect_unquote_fail("\"invalid \\14 escape char \"");
81 expect_unquote_fail("\"invalid \\280 escape char\"");
82 expect_unquote_fail("\"invalid \\378 escape char\"");
83 expect_unquote_fail("\"invalid \\380 escape char\"");
84 expect_unquote_fail("\"invalid \\411 escape char\"");
85 expect_unquote_fail("\"truncated escape char \\\"");
86 expect_unquote_fail("\"truncated escape char \\0\"");
87 expect_unquote_fail("\"truncated escape char \\01\"");
88 }