blob: 2e34c80a1d877b2bc47682171c98150283692254 [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
Christian Brabandt8a4c8122021-07-25 14:36:05 +0200136 call assert_match("Note: Encryption of swapfile not supported, disabling swap file", execute(':5messages'))
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200137
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)
Christian Brabandt8a4c8122021-07-25 14:36:05 +0200155 call assert_match("Note: Encryption of swapfile not supported, disabling swap file", execute(':messages'))
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200156 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 Brabandt16e26a32021-07-13 19:09:12 +0200165 " enryption removed (on MS-Windows the .* matches [unix])
166 call assert_match('"Xcrypt_sodium.txt".*4000L, 18893B written', execute(':message'))
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200167 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')
Christian Brabandt8a4c8122021-07-25 14:36:05 +0200179 call assert_equal(1, &undofile)
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200180 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!
Christian Brabandt8a4c8122021-07-25 14:36:05 +0200192 call assert_equal(0, &undofile)
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200193 bw!
194 call feedkeys(":sp Xcrypt_sodium_undo.txt\<CR>sodium\<CR>", 'xt')
195 " should fail
196 norm! u
197 call assert_match('Already at oldest change', execute(':1mess'))
Bram Moolenaardb864722021-07-08 11:37:50 +0200198 call assert_fails('verbose rundo ' .. fnameescape(ufile), 'E822')
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200199 bw!
200 set undolevels& cryptmethod& undofile&
201 call delete('Xcrypt_sodium_undo.txt')
202endfunc
203
204func Test_encrypt_xchacha20_missing()
205 if has("sodium")
206 return
207 endif
208 sp Xcrypt_sodium_undo.txt
209 call assert_fails(':set cryptmethod=xchacha20', 'E474')
210 bw!
211 set cm&
212endfunc
213
Bram Moolenaar987411d2019-01-18 22:48:34 +0100214func Test_uncrypt_unknown_method()
215 split Xuncrypt_unknown.txt
216 set bin noeol key= fenc=latin1
217 call setline(1, "VimCrypt~93!\u001e\u00d1")
218 w!
219 bwipe!
220 set nobin
221 call assert_fails(":split Xuncrypt_unknown.txt", 'E821:')
222
223 bwipe!
224 call delete('Xuncrypt_unknown.txt')
225 set key=
226endfunc
227
228func Test_crypt_key_mismatch()
229 set cryptmethod=blowfish
230
231 split Xtest.txt
232 call setline(1, 'nothing')
233 call feedkeys(":X\<CR>foobar\<CR>nothing\<CR>", 'xt')
234 call assert_match("Keys don't match!", execute(':2messages'))
235 call assert_equal('', &key)
236 call feedkeys("\<CR>\<CR>", 'xt')
237
238 set cryptmethod&
239 bwipe!
240endfunc
241
Bram Moolenaar76cb6832020-05-15 22:30:38 +0200242func Test_crypt_set_key_changes_buffer()
243
244 new Xtest1.txt
245 call setline(1, 'nothing')
246 set cryptmethod=blowfish2
247 call feedkeys(":X\<CR>foobar\<CR>foobar\<CR>", 'xt')
248 call assert_fails(":q", "E37:")
249 w
250 set key=anotherkey
251 call assert_fails(":bw")
252 w
253 call feedkeys(":X\<CR>foobar\<CR>foobar\<CR>", 'xt')
254 call assert_fails(":bw")
255 w
256 let winnr = winnr()
257 wincmd p
258 call setwinvar(winnr, '&key', 'yetanotherkey')
259 wincmd p
260 call assert_fails(":bw")
261 w
262
263 set cryptmethod&
264 set key=
265 bwipe!
266 call delete('Xtest1.txt')
267endfunc
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200268
269" vim: shiftwidth=2 sts=2 expandtab