Bram Moolenaar | 680e015 | 2016-09-25 20:54:11 +0200 | [diff] [blame] | 1 | " Tests for encryption. |
Bram Moolenaar | 1eceada | 2016-09-26 20:14:56 +0200 | [diff] [blame] | 2 | |
Bram Moolenaar | b46fecd | 2019-06-15 17:58:09 +0200 | [diff] [blame] | 3 | source check.vim |
| 4 | CheckFeature cryptv |
Bram Moolenaar | 680e015 | 2016-09-25 20:54:11 +0200 | [diff] [blame] | 5 | |
| 6 | func 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! |
| 15 | endfunc |
| 16 | |
| 17 | func Test_head_only_2() |
| 18 | call Common_head_only('VimCrypt~02!abc') |
| 19 | endfunc |
| 20 | |
| 21 | func Test_head_only_3() |
| 22 | call Common_head_only('VimCrypt~03!abc') |
| 23 | endfunc |
Bram Moolenaar | 1777785 | 2016-09-27 21:30:22 +0200 | [diff] [blame] | 24 | |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 25 | func Test_head_only_4() |
| 26 | CheckFeature sodium |
| 27 | call Common_head_only('VimCrypt~04!abc') |
| 28 | endfunc |
| 29 | |
Bram Moolenaar | 1777785 | 2016-09-27 21:30:22 +0200 | [diff] [blame] | 30 | func 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 Moolenaar | 987411d | 2019-01-18 22:48:34 +0100 | [diff] [blame] | 41 | call assert_equal('*****', &key) |
Bram Moolenaar | 1777785 | 2016-09-27 21:30:22 +0200 | [diff] [blame] | 42 | 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') |
| 49 | endfunc |
| 50 | |
| 51 | func Test_crypt_zip() |
| 52 | call Crypt_uncrypt('zip') |
| 53 | endfunc |
| 54 | |
| 55 | func Test_crypt_blowfish() |
| 56 | call Crypt_uncrypt('blowfish') |
| 57 | endfunc |
| 58 | |
| 59 | func Test_crypt_blowfish2() |
| 60 | call Crypt_uncrypt('blowfish2') |
| 61 | endfunc |
| 62 | |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 63 | func Test_crypt_sodium() |
| 64 | CheckFeature sodium |
| 65 | call Crypt_uncrypt('xchacha20') |
| 66 | endfunc |
| 67 | |
Bram Moolenaar | 1777785 | 2016-09-27 21:30:22 +0200 | [diff] [blame] | 68 | func 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= |
| 81 | endfunc |
| 82 | |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 83 | func 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= |
| 91 | endfunc |
| 92 | |
Bram Moolenaar | 1777785 | 2016-09-27 21:30:22 +0200 | [diff] [blame] | 93 | func 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"]) |
| 95 | endfunc |
| 96 | |
| 97 | func 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"]) |
| 99 | endfunc |
| 100 | |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 101 | func Test_uncrypt_blowfish2a() |
Bram Moolenaar | 1777785 | 2016-09-27 21:30:22 +0200 | [diff] [blame] | 102 | 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", "!@#$%^&*()_+=-`~"]) |
| 103 | endfunc |
Bram Moolenaar | 987411d | 2019-01-18 22:48:34 +0100 | [diff] [blame] | 104 | |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 105 | func 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", "!@#$%^&*()_+=-`~"]) |
| 107 | endfunc |
| 108 | |
| 109 | func 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_äüöÄÜÖ_!@#$%^&*()_+=-`~"]) |
| 123 | endfunc |
| 124 | |
| 125 | func Test_uncrypt_xchacha20_invalid() |
| 126 | CheckFeature sodium |
Bram Moolenaar | db86472 | 2021-07-08 11:37:50 +0200 | [diff] [blame] | 127 | |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 128 | " 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 Brabandt | 8a4c812 | 2021-07-25 14:36:05 +0200 | [diff] [blame] | 136 | call assert_match("Note: Encryption of swapfile not supported, disabling swap file", execute(':5messages')) |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 137 | |
| 138 | call assert_equal(0, &swapfile) |
| 139 | call assert_equal("xchacha20", &cryptmethod) |
| 140 | call assert_equal('311111111111111111111111', getline('$')) |
| 141 | bw! |
| 142 | endfunc |
| 143 | |
| 144 | func Test_uncrypt_xchacha20_2() |
| 145 | CheckFeature sodium |
Bram Moolenaar | db86472 | 2021-07-08 11:37:50 +0200 | [diff] [blame] | 146 | |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 147 | 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 Brabandt | 8a4c812 | 2021-07-25 14:36:05 +0200 | [diff] [blame] | 155 | call assert_match("Note: Encryption of swapfile not supported, disabling swap file", execute(':messages')) |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 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 Moolenaar | db86472 | 2021-07-08 11:37:50 +0200 | [diff] [blame] | 164 | w! ++ff=unix |
Christian Brabandt | 16e26a3 | 2021-07-13 19:09:12 +0200 | [diff] [blame] | 165 | " enryption removed (on MS-Windows the .* matches [unix]) |
| 166 | call assert_match('"Xcrypt_sodium.txt".*4000L, 18893B written', execute(':message')) |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 167 | bw! |
| 168 | call delete('Xcrypt_sodium.txt') |
| 169 | set cryptmethod&vim |
| 170 | endfunc |
| 171 | |
| 172 | func Test_uncrypt_xchacha20_3_persistent_undo() |
| 173 | CheckFeature sodium |
| 174 | CheckFeature persistent_undo |
Bram Moolenaar | db86472 | 2021-07-08 11:37:50 +0200 | [diff] [blame] | 175 | |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 176 | sp Xcrypt_sodium_undo.txt |
| 177 | set cryptmethod=xchacha20 undofile |
| 178 | call feedkeys(":X\<CR>sodium\<CR>sodium\<CR>", 'xt') |
Christian Brabandt | 8a4c812 | 2021-07-25 14:36:05 +0200 | [diff] [blame] | 179 | call assert_equal(1, &undofile) |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 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! |
Christian Brabandt | 8a4c812 | 2021-07-25 14:36:05 +0200 | [diff] [blame] | 192 | call assert_equal(0, &undofile) |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 193 | 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 Moolenaar | db86472 | 2021-07-08 11:37:50 +0200 | [diff] [blame] | 198 | call assert_fails('verbose rundo ' .. fnameescape(ufile), 'E822') |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 199 | bw! |
| 200 | set undolevels& cryptmethod& undofile& |
| 201 | call delete('Xcrypt_sodium_undo.txt') |
| 202 | endfunc |
| 203 | |
| 204 | func 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& |
| 212 | endfunc |
| 213 | |
Bram Moolenaar | 987411d | 2019-01-18 22:48:34 +0100 | [diff] [blame] | 214 | func 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= |
| 226 | endfunc |
| 227 | |
| 228 | func 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! |
| 240 | endfunc |
| 241 | |
Bram Moolenaar | 76cb683 | 2020-05-15 22:30:38 +0200 | [diff] [blame] | 242 | func 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') |
| 267 | endfunc |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 268 | |
| 269 | " vim: shiftwidth=2 sts=2 expandtab |