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