blob: d1c645f4c9940e1e3efe66d2a32e5239649d9ebf [file] [log] [blame]
Bram Moolenaar680e0152016-09-25 20:54:11 +02001" Tests for encryption.
Bram Moolenaar1eceada2016-09-26 20:14:56 +02002
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02003source check.vim
4CheckFeature cryptv
Bram Moolenaar680e0152016-09-25 20:54:11 +02005
Bram Moolenaar90e66ec2022-11-10 00:25:05 +00006" Use the xxd command from:
7" 1: $XXDPROG if set and it is executable
8" 2: the ../xxd directory if the executable is found there
9if !empty($XXDPROG) && executable($XXDPROG)
James McCoya5d4f3b2021-10-11 16:27:03 +010010 let s:xxd_cmd = $XXDPROG
Bram Moolenaar90e66ec2022-11-10 00:25:05 +000011elseif executable('..\xxd\xxd.exe')
12 " we're on MS-Windows
13 let s:xxd_cmd = '..\xxd\xxd.exe'
14elseif executable('../xxd/xxd')
15 " we're on something like Unix
16 let s:xxd_cmd = '../xxd/xxd'
17else
18 " looks like xxd wasn't build (yet)
19 let s:xxd_cmd = ''
James McCoya5d4f3b2021-10-11 16:27:03 +010020endif
21
Bram Moolenaar680e0152016-09-25 20:54:11 +020022func Common_head_only(text)
23 " This was crashing Vim
24 split Xtest.txt
25 call setline(1, a:text)
26 wq
27 call feedkeys(":split Xtest.txt\<CR>foobar\<CR>", "tx")
28 call delete('Xtest.txt')
29 call assert_match('VimCrypt', getline(1))
30 bwipe!
31endfunc
32
33func Test_head_only_2()
34 call Common_head_only('VimCrypt~02!abc')
35endfunc
36
37func Test_head_only_3()
38 call Common_head_only('VimCrypt~03!abc')
39endfunc
Bram Moolenaar17777852016-09-27 21:30:22 +020040
Christian Brabandtf573c6e2021-06-20 14:02:16 +020041func Test_head_only_4()
42 CheckFeature sodium
43 call Common_head_only('VimCrypt~04!abc')
44endfunc
45
Bram Moolenaar17777852016-09-27 21:30:22 +020046func Crypt_uncrypt(method)
47 exe "set cryptmethod=" . a:method
48 " If the blowfish test fails 'cryptmethod' will be 'zip' now.
49 call assert_equal(a:method, &cryptmethod)
50
51 split Xtest.txt
52 let text = ['01234567890123456789012345678901234567',
53 \ 'line 2 foo bar blah',
54 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx']
55 call setline(1, text)
56 call feedkeys(":X\<CR>foobar\<CR>foobar\<CR>", 'xt')
Bram Moolenaar987411d2019-01-18 22:48:34 +010057 call assert_equal('*****', &key)
Bram Moolenaar17777852016-09-27 21:30:22 +020058 w!
59 bwipe!
60 call feedkeys(":split Xtest.txt\<CR>foobar\<CR>", 'xt')
61 call assert_equal(text, getline(1, 3))
62 set key= cryptmethod&
63 bwipe!
64 call delete('Xtest.txt')
65endfunc
66
67func Test_crypt_zip()
68 call Crypt_uncrypt('zip')
69endfunc
70
71func Test_crypt_blowfish()
72 call Crypt_uncrypt('blowfish')
73endfunc
74
75func Test_crypt_blowfish2()
76 call Crypt_uncrypt('blowfish2')
77endfunc
78
Christian Brabandtf573c6e2021-06-20 14:02:16 +020079func Test_crypt_sodium()
80 CheckFeature sodium
81 call Crypt_uncrypt('xchacha20')
82endfunc
83
Bram Moolenaar17777852016-09-27 21:30:22 +020084func Uncrypt_stable(method, crypted_text, key, uncrypted_text)
85 split Xtest.txt
86 set bin noeol key= fenc=latin1
87 exe "set cryptmethod=" . a:method
88 call setline(1, a:crypted_text)
89 w!
90 bwipe!
91 set nobin
92 call feedkeys(":split Xtest.txt\<CR>" . a:key . "\<CR>", 'xt')
93 call assert_equal(a:uncrypted_text, getline(1, len(a:uncrypted_text)))
94 bwipe!
95 call delete('Xtest.txt')
96 set key=
97endfunc
98
Christian Brabandtf573c6e2021-06-20 14:02:16 +020099func Uncrypt_stable_xxd(method, hex, key, uncrypted_text)
James McCoya5d4f3b2021-10-11 16:27:03 +0100100 if empty(s:xxd_cmd)
101 throw 'Skipped: xxd program missing'
102 endif
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200103 " use xxd to write the binary content
James McCoya5d4f3b2021-10-11 16:27:03 +0100104 call system(s:xxd_cmd .. ' -r >Xtest.txt', a:hex)
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200105 call feedkeys(":split Xtest.txt\<CR>" . a:key . "\<CR>", 'xt')
106 call assert_equal(a:uncrypted_text, getline(1, len(a:uncrypted_text)))
107 bwipe!
108 call delete('Xtest.txt')
109 set key=
110endfunc
111
Bram Moolenaar17777852016-09-27 21:30:22 +0200112func Test_uncrypt_zip()
113 call Uncrypt_stable('zip', "VimCrypt~01!\u0006\u001clV'\u00de}Mg\u00a0\u00ea\u00a3V\u00a9\u00e7\u0007E#3\u008e2U\u00e9\u0097", "foofoo", ["1234567890", "aábbccddeëff"])
114endfunc
115
116func Test_uncrypt_blowfish()
117 call Uncrypt_stable('blowfish', "VimCrypt~02!k)\u00be\u0017\u0097#\u0016\u00ddS\u009c\u00f5=\u00ba\u00e0\u00c8#\u00a5M\u00b4\u0086J\u00c3A\u00cd\u00a5M\u00b4\u0086!\u0080\u0015\u009b\u00f5\u000f\u00e1\u00d2\u0019\u0082\u0016\u0098\u00f7\u000d\u00da", "barbar", ["asdfasdfasdf", "0001112223333"])
118endfunc
119
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200120func Test_uncrypt_blowfish2a()
Bram Moolenaar17777852016-09-27 21:30:22 +0200121 call Uncrypt_stable('blowfish', "VimCrypt~03!\u001e\u00d1N\u00e3;\u00d3\u00c0\u00a0^C)\u0004\u00f7\u007f.\u00b6\u00abF\u000eS\u0019\u00e0\u008b6\u00d2[T\u00cb\u00a7\u0085\u00d8\u00be9\u000b\u00812\u000bQ\u00b3\u00cc@\u0097\u000f\u00df\u009a\u00adIv\u00aa.\u00d8\u00c9\u00ee\u009e`\u00bd$\u00af%\u00d0", "barburp", ["abcdefghijklmnopqrstuvwxyz", "!@#$%^&*()_+=-`~"])
122endfunc
Bram Moolenaar987411d2019-01-18 22:48:34 +0100123
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200124func Test_uncrypt_blowfish2()
125 call Uncrypt_stable('blowfish2', "VimCrypt~03!\u001e\u00d1N\u00e3;\u00d3\u00c0\u00a0^C)\u0004\u00f7\u007f.\u00b6\u00abF\u000eS\u0019\u00e0\u008b6\u00d2[T\u00cb\u00a7\u0085\u00d8\u00be9\u000b\u00812\u000bQ\u00b3\u00cc@\u0097\u000f\u00df\u009a\u00adIv\u00aa.\u00d8\u00c9\u00ee\u009e`\u00bd$\u00af%\u00d0", "barburp", ["abcdefghijklmnopqrstuvwxyz", "!@#$%^&*()_+=-`~"])
126endfunc
127
128func Test_uncrypt_xchacha20()
129 CheckFeature sodium
Bram Moolenaar90e66ec2022-11-10 00:25:05 +0000130 let hex = ['00000000: 5669 6d43 7279 7074 7e30 3421 6b7d e607 vimCrypt~04!k}..',
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200131 \ '00000010: 4ea4 e99f 923e f67f 7b59 a80d 3bca 2f06 N....>..{Y..;./.',
132 \ '00000020: fa11 b951 8d09 0dc9 470f e7cf 8b90 4310 ...Q....G.....C.',
133 \ '00000030: 653b b83b e493 378b 0390 0e38 f912 626b e;.;..7....8..bk',
134 \ '00000040: a02e 4697 0254 2625 2d8e 3a0b 784b e89c ..F..T&%-.:.xK..',
135 \ '00000050: 0c67 a975 3c17 9319 8ffd 1463 7783 a1f3 .g.u<......cw...',
136 \ '00000060: d917 dcb3 8b3e ecd7 c7d4 086b 6059 7ead .....>.....k`Y~.',
137 \ '00000070: 9b07 f96b 5c1b 4d08 cd91 f208 5221 7484 ...k\.M.....R!t.',
138 \ '00000080: 72be 0136 84a1 d3 r..6...']
139 " the file should be in latin1 encoding, this makes sure that readfile()
140 " retries several times converting the multi-byte characters
141 call Uncrypt_stable_xxd('xchacha20', hex, "sodium_crypt", ["abcdefghijklmnopqrstuvwxyzäöü", "ZZZ_äüöÄÜÖ_!@#$%^&*()_+=-`~"])
142endfunc
143
144func Test_uncrypt_xchacha20_invalid()
145 CheckFeature sodium
Bram Moolenaardb864722021-07-08 11:37:50 +0200146
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200147 " load an invalid encrypted file and verify it can be decrypted with an
148 " error message
149 try
150 call feedkeys(":split samples/crypt_sodium_invalid.txt\<CR>sodium\<CR>", 'xt')
151 call assert_false(1, 'should not happen')
152 catch
153 call assert_exception('pre-mature')
154 endtry
Christian Brabandt8a4c8122021-07-25 14:36:05 +0200155 call assert_match("Note: Encryption of swapfile not supported, disabling swap file", execute(':5messages'))
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200156
157 call assert_equal(0, &swapfile)
158 call assert_equal("xchacha20", &cryptmethod)
159 call assert_equal('311111111111111111111111', getline('$'))
160 bw!
161endfunc
162
163func Test_uncrypt_xchacha20_2()
164 CheckFeature sodium
Bram Moolenaardb864722021-07-08 11:37:50 +0200165
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200166 sp Xcrypt_sodium.txt
167 " Create a larger file, so that Vim will write in several blocks
168 call setline(1, range(1,4000))
169 call assert_equal(1, &swapfile)
170 set cryptmethod=xchacha20
171 call feedkeys(":X\<CR>sodium\<CR>sodium\<CR>", 'xt')
172 " swapfile disabled
173 call assert_equal(0, &swapfile)
Christian Brabandt8a4c8122021-07-25 14:36:05 +0200174 call assert_match("Note: Encryption of swapfile not supported, disabling swap file", execute(':messages'))
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200175 w!
176 " encrypted using xchacha20
177 call assert_match("\[xchacha20\]", execute(':messages'))
178 bw!
179 call feedkeys(":sp Xcrypt_sodium.txt\<CR>sodium\<CR>", 'xt')
180 " successfully decrypted
181 call assert_equal(range(1, 4000)->map( {_, v -> string(v)}), getline(1,'$'))
182 set key=
Bram Moolenaardb864722021-07-08 11:37:50 +0200183 w! ++ff=unix
Dominique Pelle81b573d2022-03-22 21:14:55 +0000184 " encryption removed (on MS-Windows the .* matches [unix])
Christian Brabandt16e26a32021-07-13 19:09:12 +0200185 call assert_match('"Xcrypt_sodium.txt".*4000L, 18893B written', execute(':message'))
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200186 bw!
187 call delete('Xcrypt_sodium.txt')
188 set cryptmethod&vim
189endfunc
190
191func Test_uncrypt_xchacha20_3_persistent_undo()
192 CheckFeature sodium
193 CheckFeature persistent_undo
Bram Moolenaardb864722021-07-08 11:37:50 +0200194
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200195 sp Xcrypt_sodium_undo.txt
196 set cryptmethod=xchacha20 undofile
197 call feedkeys(":X\<CR>sodium\<CR>sodium\<CR>", 'xt')
Christian Brabandt8a4c8122021-07-25 14:36:05 +0200198 call assert_equal(1, &undofile)
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200199 let ufile=undofile(@%)
200 call append(0, ['monday', 'tuesday', 'wednesday', 'thursday', 'friday'])
201 call cursor(1, 1)
202
203 set undolevels=100
204 normal dd
205 set undolevels=100
206 normal dd
207 set undolevels=100
208 normal dd
209 set undolevels=100
210 w!
Christian Brabandt8a4c8122021-07-25 14:36:05 +0200211 call assert_equal(0, &undofile)
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200212 bw!
213 call feedkeys(":sp Xcrypt_sodium_undo.txt\<CR>sodium\<CR>", 'xt')
214 " should fail
215 norm! u
216 call assert_match('Already at oldest change', execute(':1mess'))
Bram Moolenaardb864722021-07-08 11:37:50 +0200217 call assert_fails('verbose rundo ' .. fnameescape(ufile), 'E822')
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200218 bw!
219 set undolevels& cryptmethod& undofile&
220 call delete('Xcrypt_sodium_undo.txt')
221endfunc
222
223func Test_encrypt_xchacha20_missing()
224 if has("sodium")
225 return
226 endif
227 sp Xcrypt_sodium_undo.txt
228 call assert_fails(':set cryptmethod=xchacha20', 'E474')
229 bw!
230 set cm&
231endfunc
232
Bram Moolenaar987411d2019-01-18 22:48:34 +0100233func Test_uncrypt_unknown_method()
234 split Xuncrypt_unknown.txt
235 set bin noeol key= fenc=latin1
236 call setline(1, "VimCrypt~93!\u001e\u00d1")
237 w!
238 bwipe!
239 set nobin
240 call assert_fails(":split Xuncrypt_unknown.txt", 'E821:')
241
242 bwipe!
243 call delete('Xuncrypt_unknown.txt')
244 set key=
245endfunc
246
247func Test_crypt_key_mismatch()
248 set cryptmethod=blowfish
249
250 split Xtest.txt
251 call setline(1, 'nothing')
252 call feedkeys(":X\<CR>foobar\<CR>nothing\<CR>", 'xt')
253 call assert_match("Keys don't match!", execute(':2messages'))
254 call assert_equal('', &key)
255 call feedkeys("\<CR>\<CR>", 'xt')
256
257 set cryptmethod&
258 bwipe!
259endfunc
260
Bram Moolenaar76cb6832020-05-15 22:30:38 +0200261func Test_crypt_set_key_changes_buffer()
262
263 new Xtest1.txt
264 call setline(1, 'nothing')
265 set cryptmethod=blowfish2
266 call feedkeys(":X\<CR>foobar\<CR>foobar\<CR>", 'xt')
267 call assert_fails(":q", "E37:")
268 w
269 set key=anotherkey
270 call assert_fails(":bw")
271 w
272 call feedkeys(":X\<CR>foobar\<CR>foobar\<CR>", 'xt')
273 call assert_fails(":bw")
274 w
275 let winnr = winnr()
276 wincmd p
277 call setwinvar(winnr, '&key', 'yetanotherkey')
278 wincmd p
279 call assert_fails(":bw")
280 w
281
282 set cryptmethod&
283 set key=
284 bwipe!
285 call delete('Xtest1.txt')
286endfunc
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200287
288" vim: shiftwidth=2 sts=2 expandtab