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