xref: /vim-8.2.3635/src/testdir/test_crypt.vim (revision a5d4f3b0)
1" Tests for encryption.
2
3source check.vim
4CheckFeature cryptv
5
6let 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
13func 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
31
32func Test_head_only_4()
33  CheckFeature sodium
34  call Common_head_only('VimCrypt~04!abc')
35endfunc
36
37func 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')
48  call assert_equal('*****', &key)
49  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
70func Test_crypt_sodium()
71  CheckFeature sodium
72  call Crypt_uncrypt('xchacha20')
73endfunc
74
75func 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
90func Uncrypt_stable_xxd(method, hex, key, uncrypted_text)
91  if empty(s:xxd_cmd)
92    throw 'Skipped: xxd program missing'
93  endif
94  " use xxd to write the binary content
95  call system(s:xxd_cmd .. ' -r >Xtest.txt', a:hex)
96  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
103func 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
111func Test_uncrypt_blowfish2a()
112  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
114
115func 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
137
138  " 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
146  call assert_match("Note: Encryption of swapfile not supported, disabling swap file", execute(':5messages'))
147
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
156
157  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)
165  call assert_match("Note: Encryption of swapfile not supported, disabling swap file", execute(':messages'))
166  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=
174  w! ++ff=unix
175  " enryption removed (on MS-Windows the .* matches [unix])
176  call assert_match('"Xcrypt_sodium.txt".*4000L, 18893B written', execute(':message'))
177  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
185
186  sp Xcrypt_sodium_undo.txt
187  set cryptmethod=xchacha20 undofile
188  call feedkeys(":X\<CR>sodium\<CR>sodium\<CR>", 'xt')
189  call assert_equal(1, &undofile)
190  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!
202  call assert_equal(0, &undofile)
203  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'))
208  call assert_fails('verbose rundo ' .. fnameescape(ufile), 'E822')
209  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
224func 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
252func 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
278
279" vim: shiftwidth=2 sts=2 expandtab
280