blob: 4a96c3070201998d20bceaddc4ffe9b9aab24739 [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 +02003CheckFeature cryptv
Bram Moolenaar680e0152016-09-25 20:54:11 +02004
Bram Moolenaar90e66ec2022-11-10 00:25:05 +00005" Use the xxd command from:
6" 1: $XXDPROG if set and it is executable
7" 2: the ../xxd directory if the executable is found there
8if !empty($XXDPROG) && executable($XXDPROG)
James McCoya5d4f3b2021-10-11 16:27:03 +01009 let s:xxd_cmd = $XXDPROG
Bram Moolenaar90e66ec2022-11-10 00:25:05 +000010elseif executable('..\xxd\xxd.exe')
11 " we're on MS-Windows
12 let s:xxd_cmd = '..\xxd\xxd.exe'
13elseif executable('../xxd/xxd')
14 " we're on something like Unix
15 let s:xxd_cmd = '../xxd/xxd'
16else
17 " looks like xxd wasn't build (yet)
18 let s:xxd_cmd = ''
James McCoya5d4f3b2021-10-11 16:27:03 +010019endif
20
Bram Moolenaar680e0152016-09-25 20:54:11 +020021func Common_head_only(text)
22 " This was crashing Vim
Bram Moolenaar16abd992023-07-08 00:54:06 +010023 split Xtest_head.txt
Bram Moolenaar680e0152016-09-25 20:54:11 +020024 call setline(1, a:text)
25 wq
Bram Moolenaar16abd992023-07-08 00:54:06 +010026 call feedkeys(":split Xtest_head.txt\<CR>foobar\<CR>", "tx")
27 call delete('Xtest_head.txt')
Bram Moolenaar680e0152016-09-25 20:54:11 +020028 call assert_match('VimCrypt', getline(1))
29 bwipe!
30endfunc
31
32func Test_head_only_2()
33 call Common_head_only('VimCrypt~02!abc')
34endfunc
35
36func Test_head_only_3()
37 call Common_head_only('VimCrypt~03!abc')
38endfunc
Bram Moolenaar17777852016-09-27 21:30:22 +020039
Christian Brabandtf573c6e2021-06-20 14:02:16 +020040func Test_head_only_4()
41 CheckFeature sodium
42 call Common_head_only('VimCrypt~04!abc')
43endfunc
44
Bram Moolenaar17777852016-09-27 21:30:22 +020045func Crypt_uncrypt(method)
46 exe "set cryptmethod=" . a:method
47 " If the blowfish test fails 'cryptmethod' will be 'zip' now.
48 call assert_equal(a:method, &cryptmethod)
49
Bram Moolenaar16abd992023-07-08 00:54:06 +010050 split Xtest_uncrypt.txt
ichizok35a2ec12023-04-25 15:27:27 +010051 let text =<< trim END
52 01234567890123456789012345678901234567,
53 line 2 foo bar blah,
54 line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
55 END
Bram Moolenaar17777852016-09-27 21:30:22 +020056 call setline(1, text)
57 call feedkeys(":X\<CR>foobar\<CR>foobar\<CR>", 'xt')
Bram Moolenaar987411d2019-01-18 22:48:34 +010058 call assert_equal('*****', &key)
Bram Moolenaar17777852016-09-27 21:30:22 +020059 w!
60 bwipe!
Bram Moolenaar16abd992023-07-08 00:54:06 +010061 call feedkeys(":split Xtest_uncrypt.txt\<CR>foobar\<CR>", 'xt')
Bram Moolenaar17777852016-09-27 21:30:22 +020062 call assert_equal(text, getline(1, 3))
63 set key= cryptmethod&
64 bwipe!
Bram Moolenaar16abd992023-07-08 00:54:06 +010065 call delete('Xtest_uncrypt.txt')
Bram Moolenaar17777852016-09-27 21:30:22 +020066endfunc
67
68func Test_crypt_zip()
69 call Crypt_uncrypt('zip')
70endfunc
71
72func Test_crypt_blowfish()
73 call Crypt_uncrypt('blowfish')
74endfunc
75
76func Test_crypt_blowfish2()
77 call Crypt_uncrypt('blowfish2')
78endfunc
79
Christian Brabandtf573c6e2021-06-20 14:02:16 +020080func Test_crypt_sodium()
81 CheckFeature sodium
82 call Crypt_uncrypt('xchacha20')
83endfunc
84
Christian Brabandtaae58342023-04-23 17:50:22 +010085func Test_crypt_sodium_v2()
86 CheckFeature sodium
87 call Crypt_uncrypt('xchacha20v2')
88endfunc
89
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +010090func Test_crypt_sodium_v2_startup()
91 CheckFeature sodium
92 CheckRunVimInTerminal
93
94 let buf = RunVimInTerminal('--cmd "set cm=xchacha20v2" -x Xfoo', #{wait_for_ruler: 0, rows: 6})
95 call g:TermWait(buf, g:RunningWithValgrind() ? 1000 : 50)
96 call term_sendkeys(buf, "foo\<CR>foo\<CR>")
97 call term_sendkeys(buf, "ifoo\<Esc>")
98 call term_sendkeys(buf, "ZZ")
99 call TermWait(buf)
100
101 " Wait for Vim to write the file and exit. Then wipe out the terminal buffer.
102 call WaitForAssert({-> assert_equal("finished", term_getstatus(buf))})
103 exe buf .. 'bwipe!'
104 call assert_true(filereadable('Xfoo'))
105
Christian Brabandt19e6c4f2023-06-27 18:57:10 +0100106 let buf = RunVimInTerminal('--cmd "set ch=3 cm=xchacha20v2 key=foo" Xfoo', #{wait_for_ruler: 0, rows: 10})
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100107 call g:TermWait(buf, g:RunningWithValgrind() ? 1000 : 50)
108 call StopVimInTerminal(buf)
109
110 call delete('Xfoo')
111endfunc
112
Bram Moolenaar17777852016-09-27 21:30:22 +0200113func Uncrypt_stable(method, crypted_text, key, uncrypted_text)
Bram Moolenaar16abd992023-07-08 00:54:06 +0100114 split Xtest_stable.txt
Bram Moolenaar17777852016-09-27 21:30:22 +0200115 set bin noeol key= fenc=latin1
116 exe "set cryptmethod=" . a:method
117 call setline(1, a:crypted_text)
118 w!
119 bwipe!
120 set nobin
Bram Moolenaar16abd992023-07-08 00:54:06 +0100121 call feedkeys(":split Xtest_stable.txt\<CR>" . a:key . "\<CR>", 'xt')
Bram Moolenaar17777852016-09-27 21:30:22 +0200122 call assert_equal(a:uncrypted_text, getline(1, len(a:uncrypted_text)))
123 bwipe!
Bram Moolenaar16abd992023-07-08 00:54:06 +0100124 call delete('Xtest_stable.txt')
Bram Moolenaar17777852016-09-27 21:30:22 +0200125 set key=
126endfunc
127
Christian Brabandtaae58342023-04-23 17:50:22 +0100128func Uncrypt_stable_xxd(method, hex, key, uncrypted_text, verbose)
James McCoya5d4f3b2021-10-11 16:27:03 +0100129 if empty(s:xxd_cmd)
130 throw 'Skipped: xxd program missing'
131 endif
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200132 " use xxd to write the binary content
Bram Moolenaar16abd992023-07-08 00:54:06 +0100133 call system(s:xxd_cmd .. ' -r >Xtest_stable_xxd.txt', a:hex)
Christian Brabandtaae58342023-04-23 17:50:22 +0100134 let cmd = (a:verbose ? ':verbose' : '') ..
Bram Moolenaar16abd992023-07-08 00:54:06 +0100135 \ ":split Xtest_stable_xxd.txt\<CR>" . a:key . "\<CR>"
Christian Brabandtaae58342023-04-23 17:50:22 +0100136 call feedkeys(cmd, 'xt')
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200137 call assert_equal(a:uncrypted_text, getline(1, len(a:uncrypted_text)))
138 bwipe!
Bram Moolenaar16abd992023-07-08 00:54:06 +0100139 call delete('Xtest_stable_xxd.txt')
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200140 set key=
141endfunc
142
Bram Moolenaar17777852016-09-27 21:30:22 +0200143func Test_uncrypt_zip()
144 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"])
145endfunc
146
147func Test_uncrypt_blowfish()
148 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"])
149endfunc
150
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200151func Test_uncrypt_blowfish2a()
Bram Moolenaar17777852016-09-27 21:30:22 +0200152 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", "!@#$%^&*()_+=-`~"])
153endfunc
Bram Moolenaar987411d2019-01-18 22:48:34 +0100154
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200155func Test_uncrypt_blowfish2()
156 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", "!@#$%^&*()_+=-`~"])
157endfunc
158
159func Test_uncrypt_xchacha20()
160 CheckFeature sodium
ichizok35a2ec12023-04-25 15:27:27 +0100161 let hex =<< trim END
162 00000000: 5669 6d43 7279 7074 7e30 3421 6b7d e607 vimCrypt~04!k}..
163 00000010: 4ea4 e99f 923e f67f 7b59 a80d 3bca 2f06 N....>..{Y..;./.
164 00000020: fa11 b951 8d09 0dc9 470f e7cf 8b90 4310 ...Q....G.....C.
165 00000030: 653b b83b e493 378b 0390 0e38 f912 626b e;.;..7....8..bk
166 00000040: a02e 4697 0254 2625 2d8e 3a0b 784b e89c ..F..T&%-.:.xK..
167 00000050: 0c67 a975 3c17 9319 8ffd 1463 7783 a1f3 .g.u<......cw...
168 00000060: d917 dcb3 8b3e ecd7 c7d4 086b 6059 7ead .....>.....k`Y~.
169 00000070: 9b07 f96b 5c1b 4d08 cd91 f208 5221 7484 ...k\.M.....R!t.
170 00000080: 72be 0136 84a1 d3 r..6...
171 END
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200172 " the file should be in latin1 encoding, this makes sure that readfile()
173 " retries several times converting the multi-byte characters
Christian Brabandtaae58342023-04-23 17:50:22 +0100174 call Uncrypt_stable_xxd('xchacha20', hex, "sodium_crypt", ["abcdefghijklmnopqrstuvwxyzäöü", "ZZZ_äüöÄÜÖ_!@#$%^&*()_+=-`~"], 0)
175endfunc
176
177func Test_uncrypt_xchacha20v2_custom()
178 CheckFeature sodium
179 " Test, reading xchacha20v2 with custom encryption parameters
ichizok35a2ec12023-04-25 15:27:27 +0100180 let hex =<< trim END
181 00000000: 5669 6d43 7279 7074 7e30 3521 934b f288 VimCrypt~05!.K..
182 00000010: 10ba 8bc9 25a0 8876 f85c f135 6fb8 518b ....%..v.\.5o.Q.
183 00000020: b133 9af1 0300 0000 0000 0000 0000 0010 .3..............
184 00000030: 0000 0000 0200 0000 b973 5f33 80e9 54fc .........s_3..T.
185 00000040: 138f ba3e 046b 3135 90b7 7783 5eac 7fe3 ...>.k15..w.^...
186 00000050: 0cd2 14df ed75 4b65 8763 8205 035c ec81 .....uKe.c...\..
187 00000060: a4cf 33d2 7507 ec38 ba62 a327 9068 d8ad ..3.u..8.b.'.h..
188 00000070: 2607 3fa6 f95d 7ea8 9799 f997 4820 0c &.?..]~.....H .
189 END
Christian Brabandt335c5842023-08-09 16:32:28 +0200190 try
191 call Uncrypt_stable_xxd('xchacha20v2', hex, "foobar", ["", "foo", "bar", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"], 1)
192 catch /^Vim\%((\a\+)\)\=:E1230:/ " sodium_mlock() not possible, may happen at Github CI
193 throw 'Skipped: sodium_mlock() not possible'
194 endtry
Christian Brabandtaae58342023-04-23 17:50:22 +0100195 call assert_match('xchacha20v2: using custom \w\+ "\d\+" for Key derivation.', execute(':messages'))
196endfunc
197
198func Test_uncrypt_xchacha20v2()
199 CheckFeature sodium
200 " Test, reading xchacha20v2
ichizok35a2ec12023-04-25 15:27:27 +0100201 let hex =<< trim END
202 00000000: 5669 6d43 7279 7074 7e30 3521 9f20 4e14 VimCrypt~05!. N.
203 00000010: c7da c1bd 7dea 8fbc db6c 38e6 7a77 6fef ....}....l8.zwo.
204 00000020: 82dd 964b 0300 0000 0000 0000 0000 0010 ...K............
205 00000030: 0000 0000 0200 0000 a97c 2f00 0b9d 19eb .........|/.....
206 00000040: 1d92 1ea5 3f22 c179 4b3e 870a eb19 6380 ....?".yK>....c.
207 00000050: 63f8 222d b5d1 3c73 7be5 d580 47ea 44cc c."-..<s{...G.D.
208 00000060: 6c25 8078 3fd5 d836 c700 0122 bb30 7a59 l%.x?..6...".0zY
209 00000070: b184 2ae8 e7db 113a f732 938f 7a34 1333 ..*....:.2..z4.3
210 00000080: dc89 1491 51a0 67b9 0f3a b56c 1f9d 53b0 ....Q.g..:.l..S.
211 00000090: 2416 205a 8c4c 5fde 4dac 2611 8a48 24f0 $. Z.L_.M.&..H$.
212 000000a0: ba00 92c1 60 ....`
213 END
Christian Brabandt335c5842023-08-09 16:32:28 +0200214 try
215 call Uncrypt_stable_xxd('xchacha20v2', hex, "foo1234", ["abcdefghijklmnopqrstuvwxyzäöü", 'ZZZ_äüöÄÜÖ_!@#$%^&*()_+=-`~"'], 0)
216 catch /^Vim\%((\a\+)\)\=:E1230:/ " sodium_mlock() not possible, may happen at Github CI
217 throw 'Skipped: sodium_mlock() not possible'
218 endtry
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200219endfunc
220
221func Test_uncrypt_xchacha20_invalid()
222 CheckFeature sodium
Bram Moolenaardb864722021-07-08 11:37:50 +0200223
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200224 " load an invalid encrypted file and verify it can be decrypted with an
225 " error message
226 try
227 call feedkeys(":split samples/crypt_sodium_invalid.txt\<CR>sodium\<CR>", 'xt')
228 call assert_false(1, 'should not happen')
Christian Brabandt335c5842023-08-09 16:32:28 +0200229 catch /^Vim\%((\a\+)\)\=:E1230:/ " sodium_mlock() not possible, may happen at Github CI
230 throw 'Skipped: sodium_mlock() not possible'
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200231 catch
232 call assert_exception('pre-mature')
233 endtry
Christian Brabandt8a4c8122021-07-25 14:36:05 +0200234 call assert_match("Note: Encryption of swapfile not supported, disabling swap file", execute(':5messages'))
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200235
236 call assert_equal(0, &swapfile)
237 call assert_equal("xchacha20", &cryptmethod)
238 call assert_equal('311111111111111111111111', getline('$'))
239 bw!
240endfunc
241
242func Test_uncrypt_xchacha20_2()
243 CheckFeature sodium
Bram Moolenaardb864722021-07-08 11:37:50 +0200244
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200245 sp Xcrypt_sodium.txt
246 " Create a larger file, so that Vim will write in several blocks
Christian Brabandtaae58342023-04-23 17:50:22 +0100247 call setline(1, range(1, 4000))
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200248 call assert_equal(1, &swapfile)
249 set cryptmethod=xchacha20
250 call feedkeys(":X\<CR>sodium\<CR>sodium\<CR>", 'xt')
251 " swapfile disabled
252 call assert_equal(0, &swapfile)
Christian Brabandt8a4c8122021-07-25 14:36:05 +0200253 call assert_match("Note: Encryption of swapfile not supported, disabling swap file", execute(':messages'))
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200254 w!
255 " encrypted using xchacha20
Isao Satoe6fca0e2023-09-24 23:27:25 +0200256 call assert_match('\[xchacha20\]', execute(':messages'))
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200257 bw!
258 call feedkeys(":sp Xcrypt_sodium.txt\<CR>sodium\<CR>", 'xt')
259 " successfully decrypted
260 call assert_equal(range(1, 4000)->map( {_, v -> string(v)}), getline(1,'$'))
261 set key=
Bram Moolenaardb864722021-07-08 11:37:50 +0200262 w! ++ff=unix
Dominique Pelle81b573d2022-03-22 21:14:55 +0000263 " encryption removed (on MS-Windows the .* matches [unix])
Christian Brabandt16e26a32021-07-13 19:09:12 +0200264 call assert_match('"Xcrypt_sodium.txt".*4000L, 18893B written', execute(':message'))
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200265 bw!
266 call delete('Xcrypt_sodium.txt')
267 set cryptmethod&vim
Christian Brabandtaae58342023-04-23 17:50:22 +0100268
269endfunc
270
271func Test_uncrypt_xchacha20v2_2()
272 CheckFeature sodium
273
274 sp Xcrypt_sodium_v2.txt
275 " Create a larger file, so that Vim will write in several blocks
276 call setline(1, range(1, 4000))
277 call assert_equal(1, &swapfile)
278 set cryptmethod=xchacha20v2
279 call feedkeys(":X\<CR>sodium\<CR>sodium\<CR>", 'xt')
280 " swapfile disabled
281 call assert_equal(0, &swapfile)
282 call assert_match("Note: Encryption of swapfile not supported, disabling swap file", execute(':messages'))
Christian Brabandt335c5842023-08-09 16:32:28 +0200283 try
284 w!
285 catch /^Vim\%((\a\+)\)\=:E1230:/ " sodium_mlock() not possible, may happen at Github CI
286 throw 'Skipped: sodium_mlock() not possible'
287 endtry
Christian Brabandtaae58342023-04-23 17:50:22 +0100288 " encrypted using xchacha20
Isao Satoe6fca0e2023-09-24 23:27:25 +0200289 call assert_match('\[xchacha20v2\]', execute(':messages'))
Christian Brabandtaae58342023-04-23 17:50:22 +0100290 bw!
Christian Brabandt93ebb5e2023-08-28 21:17:36 +0200291 try
292 call feedkeys(":verbose :sp Xcrypt_sodium_v2.txt\<CR>sodium\<CR>", 'xt')
293 catch /^Vim\%((\a\+)\)\=:E1230:/ " sodium_mlock() not possible, may happen at Github CI
294 throw 'Skipped: sodium_mlock() not possible'
295 endtry
Christian Brabandtaae58342023-04-23 17:50:22 +0100296 " successfully decrypted
297 call assert_equal(range(1, 4000)->map( {_, v -> string(v)}), getline(1,'$'))
298 call assert_match('xchacha20v2: using default \w\+ "\d\+" for Key derivation.', execute(':messages'))
299 set key=
300 w! ++ff=unix
301 " encryption removed (on MS-Windows the .* matches [unix])
302 call assert_match('"Xcrypt_sodium_v2.txt".*4000L, 18893B written', execute(':message'))
303 bw!
304 call delete('Xcrypt_sodium_v2.txt')
305 set cryptmethod&vim
306
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200307endfunc
308
309func Test_uncrypt_xchacha20_3_persistent_undo()
310 CheckFeature sodium
311 CheckFeature persistent_undo
Bram Moolenaardb864722021-07-08 11:37:50 +0200312
Christian Brabandtaae58342023-04-23 17:50:22 +0100313 for meth in ['xchacha20', 'xchacha20v2']
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200314
Christian Brabandtaae58342023-04-23 17:50:22 +0100315 sp Xcrypt_sodium_undo.txt
316 exe "set cryptmethod=" .. meth .. " undofile"
317 call feedkeys(":X\<CR>sodium\<CR>sodium\<CR>", 'xt')
318 call assert_equal(1, &undofile)
319 let ufile=undofile(@%)
320 call append(0, ['monday', 'tuesday', 'wednesday', 'thursday', 'friday'])
321 call cursor(1, 1)
322
323 set undolevels=100
324 normal dd
325 set undolevels=100
326 normal dd
327 set undolevels=100
328 normal dd
329 set undolevels=100
Christian Brabandt88786532023-08-12 09:41:23 +0200330 try
331 w!
332 catch /^Vim\%((\a\+)\)\=:E1230:/ " sodium_mlock() not possible, may happen at Github CI
333 throw 'Skipped: sodium_mlock() not possible'
334 endtry
Christian Brabandtaae58342023-04-23 17:50:22 +0100335 call assert_equal(0, &undofile)
336 bw!
Christian Brabandt25dec332023-08-19 22:15:44 +0200337 try
338 call feedkeys(":sp Xcrypt_sodium_undo.txt\<CR>sodium\<CR>", 'xt')
339 catch /^Vim\%((\a\+)\)\=:E1230:/ " sodium_mlock() not possible, may happen at Github CI
340 throw 'Skipped: sodium_mlock() not possible'
341 endtry
Christian Brabandtaae58342023-04-23 17:50:22 +0100342 " should fail
343 norm! u
344 call assert_match('Already at oldest change', execute(':1mess'))
zeertzjq67fe77d2025-04-20 10:21:18 +0200345 call assert_fails('verbose rundo ' .. fnameescape(ufile), 'E822:')
Christian Brabandtaae58342023-04-23 17:50:22 +0100346 bw!
347 set undolevels& cryptmethod& undofile&
348 call delete('Xcrypt_sodium_undo.txt')
349
350 endfor
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200351endfunc
352
353func Test_encrypt_xchacha20_missing()
354 if has("sodium")
355 return
356 endif
357 sp Xcrypt_sodium_undo.txt
zeertzjq67fe77d2025-04-20 10:21:18 +0200358 call assert_fails(':set cryptmethod=xchacha20', 'E474:')
359 call assert_fails(':set cryptmethod=xchacha20v2', 'E474:')
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200360 bw!
361 set cm&
362endfunc
363
Bram Moolenaar987411d2019-01-18 22:48:34 +0100364func Test_uncrypt_unknown_method()
365 split Xuncrypt_unknown.txt
366 set bin noeol key= fenc=latin1
367 call setline(1, "VimCrypt~93!\u001e\u00d1")
368 w!
369 bwipe!
370 set nobin
371 call assert_fails(":split Xuncrypt_unknown.txt", 'E821:')
372
373 bwipe!
374 call delete('Xuncrypt_unknown.txt')
375 set key=
376endfunc
377
378func Test_crypt_key_mismatch()
379 set cryptmethod=blowfish
380
Bram Moolenaar16abd992023-07-08 00:54:06 +0100381 split Xtest_mismatch.txt
Bram Moolenaar987411d2019-01-18 22:48:34 +0100382 call setline(1, 'nothing')
383 call feedkeys(":X\<CR>foobar\<CR>nothing\<CR>", 'xt')
384 call assert_match("Keys don't match!", execute(':2messages'))
385 call assert_equal('', &key)
386 call feedkeys("\<CR>\<CR>", 'xt')
387
388 set cryptmethod&
389 bwipe!
390endfunc
391
Bram Moolenaar76cb6832020-05-15 22:30:38 +0200392func Test_crypt_set_key_changes_buffer()
393
394 new Xtest1.txt
395 call setline(1, 'nothing')
396 set cryptmethod=blowfish2
397 call feedkeys(":X\<CR>foobar\<CR>foobar\<CR>", 'xt')
398 call assert_fails(":q", "E37:")
399 w
400 set key=anotherkey
401 call assert_fails(":bw")
402 w
403 call feedkeys(":X\<CR>foobar\<CR>foobar\<CR>", 'xt')
404 call assert_fails(":bw")
405 w
406 let winnr = winnr()
407 wincmd p
408 call setwinvar(winnr, '&key', 'yetanotherkey')
409 wincmd p
410 call assert_fails(":bw")
411 w
412
413 set cryptmethod&
414 set key=
415 bwipe!
416 call delete('Xtest1.txt')
417endfunc
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200418
Christian Brabandt19e6c4f2023-06-27 18:57:10 +0100419func Test_crypt_set_key_segfault()
420 CheckFeature sodium
421
422 defer delete('Xtest2.txt')
423 new Xtest2.txt
424 call setline(1, 'nothing')
425 set cryptmethod=xchacha20
426 set key=foobar
427 w
428 new Xtest3
429 put ='other content'
430 setl modified
431 sil! preserve
432 bwipe!
433
434 set cryptmethod&
435 set key=
436 bwipe!
437endfunc
438
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +0200439func Test_crypt_set_key_disallow_append_subtract()
440 new Xtest4
441
442 set key=foobar
443 call assert_true(&modified)
444 setl nomodified
445
446 call assert_fails('set key-=foo', 'E474:')
447 call assert_fails('set key-=bar', 'E474:')
448 call assert_fails('set key-=foobar', 'E474:')
449 call assert_fails('set key-=test1', 'E474:')
450
451 call assert_false(&modified)
452 call assert_equal('*****', &key)
453
454 call assert_fails('set key+=test2', 'E474:')
455 call assert_fails('set key^=test3', 'E474:')
456
457 call assert_false(&modified)
458 set key=
459 bwipe!
460endfunc
461
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200462" vim: shiftwidth=2 sts=2 expandtab