blob: f3750b3da494cc367e3f7f3202ed1b59fafaa5eb [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
James McCoya5d4f3b2021-10-11 16:27:03 +01006let s:xxd_cmd = ''
7if empty($XXDPROG) && executable('..\xxd\xxd.exe')
8 let s:xxd_cmd = '..\xxd\xxd.exe'
9elseif !empty($XXDPROG) && executable($XXDPROG)
10 let s:xxd_cmd = $XXDPROG
11endif
12
Bram Moolenaar680e0152016-09-25 20:54:11 +020013func Common_head_only(text)
14 " This was crashing Vim
15 split Xtest.txt
16 call setline(1, a:text)
17 wq
18 call feedkeys(":split Xtest.txt\<CR>foobar\<CR>", "tx")
19 call delete('Xtest.txt')
20 call assert_match('VimCrypt', getline(1))
21 bwipe!
22endfunc
23
24func Test_head_only_2()
25 call Common_head_only('VimCrypt~02!abc')
26endfunc
27
28func Test_head_only_3()
29 call Common_head_only('VimCrypt~03!abc')
30endfunc
Bram Moolenaar17777852016-09-27 21:30:22 +020031
Christian Brabandtf573c6e2021-06-20 14:02:16 +020032func Test_head_only_4()
33 CheckFeature sodium
34 call Common_head_only('VimCrypt~04!abc')
35endfunc
36
Bram Moolenaar17777852016-09-27 21:30:22 +020037func Crypt_uncrypt(method)
38 exe "set cryptmethod=" . a:method
39 " If the blowfish test fails 'cryptmethod' will be 'zip' now.
40 call assert_equal(a:method, &cryptmethod)
41
42 split Xtest.txt
43 let text = ['01234567890123456789012345678901234567',
44 \ 'line 2 foo bar blah',
45 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx']
46 call setline(1, text)
47 call feedkeys(":X\<CR>foobar\<CR>foobar\<CR>", 'xt')
Bram Moolenaar987411d2019-01-18 22:48:34 +010048 call assert_equal('*****', &key)
Bram Moolenaar17777852016-09-27 21:30:22 +020049 w!
50 bwipe!
51 call feedkeys(":split Xtest.txt\<CR>foobar\<CR>", 'xt')
52 call assert_equal(text, getline(1, 3))
53 set key= cryptmethod&
54 bwipe!
55 call delete('Xtest.txt')
56endfunc
57
58func Test_crypt_zip()
59 call Crypt_uncrypt('zip')
60endfunc
61
62func Test_crypt_blowfish()
63 call Crypt_uncrypt('blowfish')
64endfunc
65
66func Test_crypt_blowfish2()
67 call Crypt_uncrypt('blowfish2')
68endfunc
69
Christian Brabandtf573c6e2021-06-20 14:02:16 +020070func Test_crypt_sodium()
71 CheckFeature sodium
72 call Crypt_uncrypt('xchacha20')
73endfunc
74
Bram Moolenaar17777852016-09-27 21:30:22 +020075func Uncrypt_stable(method, crypted_text, key, uncrypted_text)
76 split Xtest.txt
77 set bin noeol key= fenc=latin1
78 exe "set cryptmethod=" . a:method
79 call setline(1, a:crypted_text)
80 w!
81 bwipe!
82 set nobin
83 call feedkeys(":split Xtest.txt\<CR>" . a:key . "\<CR>", 'xt')
84 call assert_equal(a:uncrypted_text, getline(1, len(a:uncrypted_text)))
85 bwipe!
86 call delete('Xtest.txt')
87 set key=
88endfunc
89
Christian Brabandtf573c6e2021-06-20 14:02:16 +020090func Uncrypt_stable_xxd(method, hex, key, uncrypted_text)
James McCoya5d4f3b2021-10-11 16:27:03 +010091 if empty(s:xxd_cmd)
92 throw 'Skipped: xxd program missing'
93 endif
Christian Brabandtf573c6e2021-06-20 14:02:16 +020094 " use xxd to write the binary content
James McCoya5d4f3b2021-10-11 16:27:03 +010095 call system(s:xxd_cmd .. ' -r >Xtest.txt', a:hex)
Christian Brabandtf573c6e2021-06-20 14:02:16 +020096 call feedkeys(":split Xtest.txt\<CR>" . a:key . "\<CR>", 'xt')
97 call assert_equal(a:uncrypted_text, getline(1, len(a:uncrypted_text)))
98 bwipe!
99 call delete('Xtest.txt')
100 set key=
101endfunc
102
Bram Moolenaar17777852016-09-27 21:30:22 +0200103func Test_uncrypt_zip()
104 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"])
105endfunc
106
107func Test_uncrypt_blowfish()
108 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"])
109endfunc
110
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200111func Test_uncrypt_blowfish2a()
Bram Moolenaar17777852016-09-27 21:30:22 +0200112 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", "!@#$%^&*()_+=-`~"])
113endfunc
Bram Moolenaar987411d2019-01-18 22:48:34 +0100114
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200115func Test_uncrypt_blowfish2()
116 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", "!@#$%^&*()_+=-`~"])
117endfunc
118
119func Test_uncrypt_xchacha20()
120 CheckFeature sodium
121 let hex=['00000000: 5669 6d43 7279 7074 7e30 3421 6b7d e607 vimCrypt~04!k}..',
122 \ '00000010: 4ea4 e99f 923e f67f 7b59 a80d 3bca 2f06 N....>..{Y..;./.',
123 \ '00000020: fa11 b951 8d09 0dc9 470f e7cf 8b90 4310 ...Q....G.....C.',
124 \ '00000030: 653b b83b e493 378b 0390 0e38 f912 626b e;.;..7....8..bk',
125 \ '00000040: a02e 4697 0254 2625 2d8e 3a0b 784b e89c ..F..T&%-.:.xK..',
126 \ '00000050: 0c67 a975 3c17 9319 8ffd 1463 7783 a1f3 .g.u<......cw...',
127 \ '00000060: d917 dcb3 8b3e ecd7 c7d4 086b 6059 7ead .....>.....k`Y~.',
128 \ '00000070: 9b07 f96b 5c1b 4d08 cd91 f208 5221 7484 ...k\.M.....R!t.',
129 \ '00000080: 72be 0136 84a1 d3 r..6...']
130 " the file should be in latin1 encoding, this makes sure that readfile()
131 " retries several times converting the multi-byte characters
132 call Uncrypt_stable_xxd('xchacha20', hex, "sodium_crypt", ["abcdefghijklmnopqrstuvwxyzäöü", "ZZZ_äüöÄÜÖ_!@#$%^&*()_+=-`~"])
133endfunc
134
135func Test_uncrypt_xchacha20_invalid()
136 CheckFeature sodium
Bram Moolenaardb864722021-07-08 11:37:50 +0200137
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200138 " load an invalid encrypted file and verify it can be decrypted with an
139 " error message
140 try
141 call feedkeys(":split samples/crypt_sodium_invalid.txt\<CR>sodium\<CR>", 'xt')
142 call assert_false(1, 'should not happen')
143 catch
144 call assert_exception('pre-mature')
145 endtry
Christian Brabandt8a4c8122021-07-25 14:36:05 +0200146 call assert_match("Note: Encryption of swapfile not supported, disabling swap file", execute(':5messages'))
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200147
148 call assert_equal(0, &swapfile)
149 call assert_equal("xchacha20", &cryptmethod)
150 call assert_equal('311111111111111111111111', getline('$'))
151 bw!
152endfunc
153
154func Test_uncrypt_xchacha20_2()
155 CheckFeature sodium
Bram Moolenaardb864722021-07-08 11:37:50 +0200156
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200157 sp Xcrypt_sodium.txt
158 " Create a larger file, so that Vim will write in several blocks
159 call setline(1, range(1,4000))
160 call assert_equal(1, &swapfile)
161 set cryptmethod=xchacha20
162 call feedkeys(":X\<CR>sodium\<CR>sodium\<CR>", 'xt')
163 " swapfile disabled
164 call assert_equal(0, &swapfile)
Christian Brabandt8a4c8122021-07-25 14:36:05 +0200165 call assert_match("Note: Encryption of swapfile not supported, disabling swap file", execute(':messages'))
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200166 w!
167 " encrypted using xchacha20
168 call assert_match("\[xchacha20\]", execute(':messages'))
169 bw!
170 call feedkeys(":sp Xcrypt_sodium.txt\<CR>sodium\<CR>", 'xt')
171 " successfully decrypted
172 call assert_equal(range(1, 4000)->map( {_, v -> string(v)}), getline(1,'$'))
173 set key=
Bram Moolenaardb864722021-07-08 11:37:50 +0200174 w! ++ff=unix
Dominique Pelle81b573d2022-03-22 21:14:55 +0000175 " encryption removed (on MS-Windows the .* matches [unix])
Christian Brabandt16e26a32021-07-13 19:09:12 +0200176 call assert_match('"Xcrypt_sodium.txt".*4000L, 18893B written', execute(':message'))
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200177 bw!
178 call delete('Xcrypt_sodium.txt')
179 set cryptmethod&vim
180endfunc
181
182func Test_uncrypt_xchacha20_3_persistent_undo()
183 CheckFeature sodium
184 CheckFeature persistent_undo
Bram Moolenaardb864722021-07-08 11:37:50 +0200185
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200186 sp Xcrypt_sodium_undo.txt
187 set cryptmethod=xchacha20 undofile
188 call feedkeys(":X\<CR>sodium\<CR>sodium\<CR>", 'xt')
Christian Brabandt8a4c8122021-07-25 14:36:05 +0200189 call assert_equal(1, &undofile)
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200190 let ufile=undofile(@%)
191 call append(0, ['monday', 'tuesday', 'wednesday', 'thursday', 'friday'])
192 call cursor(1, 1)
193
194 set undolevels=100
195 normal dd
196 set undolevels=100
197 normal dd
198 set undolevels=100
199 normal dd
200 set undolevels=100
201 w!
Christian Brabandt8a4c8122021-07-25 14:36:05 +0200202 call assert_equal(0, &undofile)
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200203 bw!
204 call feedkeys(":sp Xcrypt_sodium_undo.txt\<CR>sodium\<CR>", 'xt')
205 " should fail
206 norm! u
207 call assert_match('Already at oldest change', execute(':1mess'))
Bram Moolenaardb864722021-07-08 11:37:50 +0200208 call assert_fails('verbose rundo ' .. fnameescape(ufile), 'E822')
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200209 bw!
210 set undolevels& cryptmethod& undofile&
211 call delete('Xcrypt_sodium_undo.txt')
212endfunc
213
214func Test_encrypt_xchacha20_missing()
215 if has("sodium")
216 return
217 endif
218 sp Xcrypt_sodium_undo.txt
219 call assert_fails(':set cryptmethod=xchacha20', 'E474')
220 bw!
221 set cm&
222endfunc
223
Bram Moolenaar987411d2019-01-18 22:48:34 +0100224func Test_uncrypt_unknown_method()
225 split Xuncrypt_unknown.txt
226 set bin noeol key= fenc=latin1
227 call setline(1, "VimCrypt~93!\u001e\u00d1")
228 w!
229 bwipe!
230 set nobin
231 call assert_fails(":split Xuncrypt_unknown.txt", 'E821:')
232
233 bwipe!
234 call delete('Xuncrypt_unknown.txt')
235 set key=
236endfunc
237
238func Test_crypt_key_mismatch()
239 set cryptmethod=blowfish
240
241 split Xtest.txt
242 call setline(1, 'nothing')
243 call feedkeys(":X\<CR>foobar\<CR>nothing\<CR>", 'xt')
244 call assert_match("Keys don't match!", execute(':2messages'))
245 call assert_equal('', &key)
246 call feedkeys("\<CR>\<CR>", 'xt')
247
248 set cryptmethod&
249 bwipe!
250endfunc
251
Bram Moolenaar76cb6832020-05-15 22:30:38 +0200252func Test_crypt_set_key_changes_buffer()
253
254 new Xtest1.txt
255 call setline(1, 'nothing')
256 set cryptmethod=blowfish2
257 call feedkeys(":X\<CR>foobar\<CR>foobar\<CR>", 'xt')
258 call assert_fails(":q", "E37:")
259 w
260 set key=anotherkey
261 call assert_fails(":bw")
262 w
263 call feedkeys(":X\<CR>foobar\<CR>foobar\<CR>", 'xt')
264 call assert_fails(":bw")
265 w
266 let winnr = winnr()
267 wincmd p
268 call setwinvar(winnr, '&key', 'yetanotherkey')
269 wincmd p
270 call assert_fails(":bw")
271 w
272
273 set cryptmethod&
274 set key=
275 bwipe!
276 call delete('Xtest1.txt')
277endfunc
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200278
279" vim: shiftwidth=2 sts=2 expandtab