robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 9a29622b3b00ddb601

master
3.7 KiB118 linesraw
1" Tests for the C omni-completion plugin (runtime/autoload/ccomplete.vim).
2
3func s:WriteTags(lines)
4  " Mark unsorted so lookup is a linear scan regardless of entry order.
5  let tagsfile = tempname()
6  call writefile(["!_TAG_FILE_SORTED\t0\t/0/"] + a:lines, tagsfile)
7  return tagsfile
8endfunc
9
10" A crafted typeref field is interpolated into the :vimgrep pattern in
11" StructMembers().  Without escaping, "/" closes the pattern and "|" starts a
12" new Ex command, so the field runs as an Ex command during completion.
13func Test_ccomplete_no_exec_via_typeref()
14  unlet! g:ccomplete_injected
15  let tagsfile = s:WriteTags([
16        \ "myvar\tmain.c\t/^x$/;\"\tv\ttyperef:x/|let g:ccomplete_injected = 1|\"",
17        \ ])
18
19  let save_tags = &tags
20  let &tags = tagsfile
21
22  new
23  call ccomplete#Complete(1, '')
24  call ccomplete#Complete(0, 'myvar.x')
25
26  call assert_false(exists('g:ccomplete_injected'),
27        \ 'typeref field was executed as an Ex command during omni-completion')
28
29  bwipe!
30  let &tags = save_tags
31  unlet! g:ccomplete_injected
32endfunc
33
34" Escaping "/" and "\" is not enough: with no "/" in the payload, an unclosed
35" "[" makes vimgrep's pattern skipping fail, and the command parser then treats
36" the first "|" as a command separator.  The typeref must be matched literally.
37func Test_ccomplete_no_exec_via_typeref_bracket()
38  CheckUnix
39  let sentinel = tempname()
40  call delete(sentinel)
41  let tagsfile = s:WriteTags([
42        \ "myvar\tmain.c\t/^x$/;\"\tv\ttyperef:struct:[|call system('touch " .. sentinel .. "')|####",
43        \ ])
44
45  let save_tags = &tags
46  let &tags = tagsfile
47
48  new
49  call ccomplete#Complete(1, '')
50  call ccomplete#Complete(0, 'myvar.x')
51
52  call assert_false(filereadable(sentinel),
53        \ 'typeref field was executed as an Ex command during omni-completion')
54
55  bwipe!
56  let &tags = save_tags
57  call delete(sentinel)
58endfunc
59
60" A legitimate typeref must still drive struct-member completion: escaping the
61" field value must not break the normal path.
62func Test_ccomplete_typeref_completion_still_works()
63  let tagsfile = s:WriteTags([
64        \ "myvar\tmain.c\t/^x$/;\"\tv\ttyperef:struct:mystruct",
65        \ "alpha\tmain.c\t/^x$/;\"\tm\tstruct:mystruct",
66        \ "beta\tmain.c\t/^x$/;\"\tm\tstruct:mystruct",
67        \ ])
68
69  let save_tags = &tags
70  let &tags = tagsfile
71
72  new
73  call ccomplete#Complete(1, '')
74  let items = ccomplete#Complete(0, 'myvar.')
75
76  call assert_equal(type([]), type(items),
77        \ 'ccomplete#Complete did not return a list')
78  let names = map(copy(items), 'v:val.word')
79  call assert_true(index(names, 'alpha') >= 0,
80        \ 'struct member "alpha" missing from completion: ' . string(names))
81  call assert_true(index(names, 'beta') >= 0,
82        \ 'struct member "beta" missing from completion: ' . string(names))
83
84  bwipe!
85  let &tags = save_tags
86endfunc
87
88" The tags file names from tagfiles() are executed
89" via a single vimgrep command and trailing | does
90" not cause code execution
91func Test_ccomplete_no_exec_via_tagfile_name()
92  CheckUnix
93  let dir = "Xcc|&titlestring\t=\t'INJECTED'|ls\t"
94  call mkdir(dir, 'pR')
95  let tagsfile = dir .. '/tags'
96  call writefile(["!_TAG_FILE_SORTED\t0\t/0/",
97        \ "myvar\tmain.c\t/^x$/;\"\tv\ttyperef:struct:mystruct",
98        \ "alpha\tmain.c\t/^x$/;\"\tm\tstruct:mystruct",
99        \ ], tagsfile)
100
101  let save_tags = &tags
102  let save_title = &titlestring
103  let &tags = fnamemodify(tagsfile, ':p')
104  set titlestring=orig
105
106  new
107  call ccomplete#Complete(1, '')
108  call ccomplete#Complete(0, 'myvar.x')
109
110  call assert_equal('orig', &titlestring,
111        \ 'tags file name was executed as an Ex command during omni-completion')
112
113  bwipe!
114  let &tags = save_tags
115  let &titlestring = save_title
116endfunc
117
118" vim: shiftwidth=2 sts=2 expandtab