blob: a25a8802a6c525481be6c528655ecabe14849bd2 [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
Christian Brabandtaae58342023-04-23 17:50:22 +010084func Test_crypt_sodium_v2()
85 CheckFeature sodium
86 call Crypt_uncrypt('xchacha20v2')
87endfunc
88
Bram Moolenaar17777852016-09-27 21:30:22 +020089func Uncrypt_stable(method, crypted_text, key, uncrypted_text)
90 split Xtest.txt
91 set bin noeol key= fenc=latin1
92 exe "set cryptmethod=" . a:method
93 call setline(1, a:crypted_text)
94 w!
95 bwipe!
96 set nobin
97 call feedkeys(":split Xtest.txt\<CR>" . a:key . "\<CR>", 'xt')
98 call assert_equal(a:uncrypted_text, getline(1, len(a:uncrypted_text)))
99 bwipe!
100 call delete('Xtest.txt')
101 set key=
102endfunc
103
Christian Brabandtaae58342023-04-23 17:50:22 +0100104func Uncrypt_stable_xxd(method, hex, key, uncrypted_text, verbose)
James McCoya5d4f3b2021-10-11 16:27:03 +0100105 if empty(s:xxd_cmd)
106 throw 'Skipped: xxd program missing'
107 endif
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200108 " use xxd to write the binary content
James McCoya5d4f3b2021-10-11 16:27:03 +0100109 call system(s:xxd_cmd .. ' -r >Xtest.txt', a:hex)
Christian Brabandtaae58342023-04-23 17:50:22 +0100110 let cmd = (a:verbose ? ':verbose' : '') ..
111 \ ":split Xtest.txt\<CR>" . a:key . "\<CR>"
112 call feedkeys(cmd, 'xt')
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200113 call assert_equal(a:uncrypted_text, getline(1, len(a:uncrypted_text)))
114 bwipe!
115 call delete('Xtest.txt')
116 set key=
117endfunc
118
Bram Moolenaar17777852016-09-27 21:30:22 +0200119func Test_uncrypt_zip()
120 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"])
121endfunc
122
123func Test_uncrypt_blowfish()
124 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"])
125endfunc
126
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200127func Test_uncrypt_blowfish2a()
Bram Moolenaar17777852016-09-27 21:30:22 +0200128 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", "!@#$%^&*()_+=-`~"])
129endfunc
Bram Moolenaar987411d2019-01-18 22:48:34 +0100130
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200131func Test_uncrypt_blowfish2()
132 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", "!@#$%^&*()_+=-`~"])
133endfunc
134
135func Test_uncrypt_xchacha20()
136 CheckFeature sodium
Bram Moolenaar90e66ec2022-11-10 00:25:05 +0000137 let hex = ['00000000: 5669 6d43 7279 7074 7e30 3421 6b7d e607 vimCrypt~04!k}..',
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200138 \ '00000010: 4ea4 e99f 923e f67f 7b59 a80d 3bca 2f06 N....>..{Y..;./.',
139 \ '00000020: fa11 b951 8d09 0dc9 470f e7cf 8b90 4310 ...Q....G.....C.',
140 \ '00000030: 653b b83b e493 378b 0390 0e38 f912 626b e;.;..7....8..bk',
141 \ '00000040: a02e 4697 0254 2625 2d8e 3a0b 784b e89c ..F..T&%-.:.xK..',
142 \ '00000050: 0c67 a975 3c17 9319 8ffd 1463 7783 a1f3 .g.u<......cw...',
143 \ '00000060: d917 dcb3 8b3e ecd7 c7d4 086b 6059 7ead .....>.....k`Y~.',
144 \ '00000070: 9b07 f96b 5c1b 4d08 cd91 f208 5221 7484 ...k\.M.....R!t.',
145 \ '00000080: 72be 0136 84a1 d3 r..6...']
146 " the file should be in latin1 encoding, this makes sure that readfile()
147 " retries several times converting the multi-byte characters
Christian Brabandtaae58342023-04-23 17:50:22 +0100148 call Uncrypt_stable_xxd('xchacha20', hex, "sodium_crypt", ["abcdefghijklmnopqrstuvwxyzäöü", "ZZZ_äüöÄÜÖ_!@#$%^&*()_+=-`~"], 0)
149endfunc
150
151func Test_uncrypt_xchacha20v2_custom()
152 CheckFeature sodium
153 " Test, reading xchacha20v2 with custom encryption parameters
154 let hex = ['00000000: 5669 6d43 7279 7074 7e30 3521 934b f288 VimCrypt~05!.K..',
155 \ '00000010: 10ba 8bc9 25a0 8876 f85c f135 6fb8 518b ....%..v.\.5o.Q.',
156 \ '00000020: b133 9af1 0300 0000 0000 0000 0000 0010 .3..............',
157 \ '00000030: 0000 0000 0200 0000 b973 5f33 80e9 54fc .........s_3..T.',
158 \ '00000040: 138f ba3e 046b 3135 90b7 7783 5eac 7fe3 ...>.k15..w.^...',
159 \ '00000050: 0cd2 14df ed75 4b65 8763 8205 035c ec81 .....uKe.c...\..',
160 \ "00000060: a4cf 33d2 7507 ec38 ba62 a327 9068 d8ad ..3.u..8.b.'.h..",
161 \ '00000070: 2607 3fa6 f95d 7ea8 9799 f997 4820 0c &.?..]~.....H .']
162 call Uncrypt_stable_xxd('xchacha20v2', hex, "foobar", ["", "foo", "bar", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"], 1)
163 call assert_match('xchacha20v2: using custom \w\+ "\d\+" for Key derivation.', execute(':messages'))
164endfunc
165
166func Test_uncrypt_xchacha20v2()
167 CheckFeature sodium
168 " Test, reading xchacha20v2
169 let hex = [
170 \ '00000000: 5669 6d43 7279 7074 7e30 3521 9f20 4e14 VimCrypt~05!. N.',
171 \ '00000010: c7da c1bd 7dea 8fbc db6c 38e6 7a77 6fef ....}....l8.zwo.',
172 \ '00000020: 82dd 964b 0300 0000 0000 0000 0000 0010 ...K............',
173 \ '00000030: 0000 0000 0200 0000 a97c 2f00 0b9d 19eb .........|/.....',
174 \ '00000040: 1d92 1ea5 3f22 c179 4b3e 870a eb19 6380 ....?".yK>....c.',
175 \ '00000050: 63f8 222d b5d1 3c73 7be5 d580 47ea 44cc c."-..<s{...G.D.',
176 \ '00000060: 6c25 8078 3fd5 d836 c700 0122 bb30 7a59 l%.x?..6...".0zY',
177 \ '00000070: b184 2ae8 e7db 113a f732 938f 7a34 1333 ..*....:.2..z4.3',
178 \ '00000080: dc89 1491 51a0 67b9 0f3a b56c 1f9d 53b0 ....Q.g..:.l..S.',
179 \ '00000090: 2416 205a 8c4c 5fde 4dac 2611 8a48 24f0 $. Z.L_.M.&..H$.',
180 \ '000000a0: ba00 92c1 60 ....`']
181 call Uncrypt_stable_xxd('xchacha20v2', hex, "foo1234", ["abcdefghijklmnopqrstuvwxyzäöü", 'ZZZ_äüöÄÜÖ_!@#$%^&*()_+=-`~"'], 0)
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200182endfunc
183
184func Test_uncrypt_xchacha20_invalid()
185 CheckFeature sodium
Bram Moolenaardb864722021-07-08 11:37:50 +0200186
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200187 " load an invalid encrypted file and verify it can be decrypted with an
188 " error message
189 try
190 call feedkeys(":split samples/crypt_sodium_invalid.txt\<CR>sodium\<CR>", 'xt')
191 call assert_false(1, 'should not happen')
192 catch
193 call assert_exception('pre-mature')
194 endtry
Christian Brabandt8a4c8122021-07-25 14:36:05 +0200195 call assert_match("Note: Encryption of swapfile not supported, disabling swap file", execute(':5messages'))
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200196
197 call assert_equal(0, &swapfile)
198 call assert_equal("xchacha20", &cryptmethod)
199 call assert_equal('311111111111111111111111', getline('$'))
200 bw!
201endfunc
202
203func Test_uncrypt_xchacha20_2()
204 CheckFeature sodium
Bram Moolenaardb864722021-07-08 11:37:50 +0200205
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200206 sp Xcrypt_sodium.txt
207 " Create a larger file, so that Vim will write in several blocks
Christian Brabandtaae58342023-04-23 17:50:22 +0100208 call setline(1, range(1, 4000))
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200209 call assert_equal(1, &swapfile)
210 set cryptmethod=xchacha20
211 call feedkeys(":X\<CR>sodium\<CR>sodium\<CR>", 'xt')
212 " swapfile disabled
213 call assert_equal(0, &swapfile)
Christian Brabandt8a4c8122021-07-25 14:36:05 +0200214 call assert_match("Note: Encryption of swapfile not supported, disabling swap file", execute(':messages'))
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200215 w!
216 " encrypted using xchacha20
217 call assert_match("\[xchacha20\]", execute(':messages'))
218 bw!
219 call feedkeys(":sp Xcrypt_sodium.txt\<CR>sodium\<CR>", 'xt')
220 " successfully decrypted
221 call assert_equal(range(1, 4000)->map( {_, v -> string(v)}), getline(1,'$'))
222 set key=
Bram Moolenaardb864722021-07-08 11:37:50 +0200223 w! ++ff=unix
Dominique Pelle81b573d2022-03-22 21:14:55 +0000224 " encryption removed (on MS-Windows the .* matches [unix])
Christian Brabandt16e26a32021-07-13 19:09:12 +0200225 call assert_match('"Xcrypt_sodium.txt".*4000L, 18893B written', execute(':message'))
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200226 bw!
227 call delete('Xcrypt_sodium.txt')
228 set cryptmethod&vim
Christian Brabandtaae58342023-04-23 17:50:22 +0100229
230endfunc
231
232func Test_uncrypt_xchacha20v2_2()
233 CheckFeature sodium
234
235 sp Xcrypt_sodium_v2.txt
236 " Create a larger file, so that Vim will write in several blocks
237 call setline(1, range(1, 4000))
238 call assert_equal(1, &swapfile)
239 set cryptmethod=xchacha20v2
240 call feedkeys(":X\<CR>sodium\<CR>sodium\<CR>", 'xt')
241 " swapfile disabled
242 call assert_equal(0, &swapfile)
243 call assert_match("Note: Encryption of swapfile not supported, disabling swap file", execute(':messages'))
244 w!
245 " encrypted using xchacha20
246 call assert_match("\[xchachav2\]", execute(':messages'))
247 bw!
248 call feedkeys(":verbose :sp Xcrypt_sodium_v2.txt\<CR>sodium\<CR>", 'xt')
249 " successfully decrypted
250 call assert_equal(range(1, 4000)->map( {_, v -> string(v)}), getline(1,'$'))
251 call assert_match('xchacha20v2: using default \w\+ "\d\+" for Key derivation.', execute(':messages'))
252 set key=
253 w! ++ff=unix
254 " encryption removed (on MS-Windows the .* matches [unix])
255 call assert_match('"Xcrypt_sodium_v2.txt".*4000L, 18893B written', execute(':message'))
256 bw!
257 call delete('Xcrypt_sodium_v2.txt')
258 set cryptmethod&vim
259
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200260endfunc
261
262func Test_uncrypt_xchacha20_3_persistent_undo()
263 CheckFeature sodium
264 CheckFeature persistent_undo
Bram Moolenaardb864722021-07-08 11:37:50 +0200265
Christian Brabandtaae58342023-04-23 17:50:22 +0100266 for meth in ['xchacha20', 'xchacha20v2']
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200267
Christian Brabandtaae58342023-04-23 17:50:22 +0100268 sp Xcrypt_sodium_undo.txt
269 exe "set cryptmethod=" .. meth .. " undofile"
270 call feedkeys(":X\<CR>sodium\<CR>sodium\<CR>", 'xt')
271 call assert_equal(1, &undofile)
272 let ufile=undofile(@%)
273 call append(0, ['monday', 'tuesday', 'wednesday', 'thursday', 'friday'])
274 call cursor(1, 1)
275
276 set undolevels=100
277 normal dd
278 set undolevels=100
279 normal dd
280 set undolevels=100
281 normal dd
282 set undolevels=100
283 w!
284 call assert_equal(0, &undofile)
285 bw!
286 call feedkeys(":sp Xcrypt_sodium_undo.txt\<CR>sodium\<CR>", 'xt')
287 " should fail
288 norm! u
289 call assert_match('Already at oldest change', execute(':1mess'))
290 call assert_fails('verbose rundo ' .. fnameescape(ufile), 'E822')
291 bw!
292 set undolevels& cryptmethod& undofile&
293 call delete('Xcrypt_sodium_undo.txt')
294
295 endfor
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200296endfunc
297
298func Test_encrypt_xchacha20_missing()
299 if has("sodium")
300 return
301 endif
302 sp Xcrypt_sodium_undo.txt
303 call assert_fails(':set cryptmethod=xchacha20', 'E474')
Christian Brabandtaae58342023-04-23 17:50:22 +0100304 call assert_fails(':set cryptmethod=xchacha20v2', 'E474')
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200305 bw!
306 set cm&
307endfunc
308
Bram Moolenaar987411d2019-01-18 22:48:34 +0100309func Test_uncrypt_unknown_method()
310 split Xuncrypt_unknown.txt
311 set bin noeol key= fenc=latin1
312 call setline(1, "VimCrypt~93!\u001e\u00d1")
313 w!
314 bwipe!
315 set nobin
316 call assert_fails(":split Xuncrypt_unknown.txt", 'E821:')
317
318 bwipe!
319 call delete('Xuncrypt_unknown.txt')
320 set key=
321endfunc
322
323func Test_crypt_key_mismatch()
324 set cryptmethod=blowfish
325
326 split Xtest.txt
327 call setline(1, 'nothing')
328 call feedkeys(":X\<CR>foobar\<CR>nothing\<CR>", 'xt')
329 call assert_match("Keys don't match!", execute(':2messages'))
330 call assert_equal('', &key)
331 call feedkeys("\<CR>\<CR>", 'xt')
332
333 set cryptmethod&
334 bwipe!
335endfunc
336
Bram Moolenaar76cb6832020-05-15 22:30:38 +0200337func Test_crypt_set_key_changes_buffer()
338
339 new Xtest1.txt
340 call setline(1, 'nothing')
341 set cryptmethod=blowfish2
342 call feedkeys(":X\<CR>foobar\<CR>foobar\<CR>", 'xt')
343 call assert_fails(":q", "E37:")
344 w
345 set key=anotherkey
346 call assert_fails(":bw")
347 w
348 call feedkeys(":X\<CR>foobar\<CR>foobar\<CR>", 'xt')
349 call assert_fails(":bw")
350 w
351 let winnr = winnr()
352 wincmd p
353 call setwinvar(winnr, '&key', 'yetanotherkey')
354 wincmd p
355 call assert_fails(":bw")
356 w
357
358 set cryptmethod&
359 set key=
360 bwipe!
361 call delete('Xtest1.txt')
362endfunc
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200363
364" vim: shiftwidth=2 sts=2 expandtab