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 | 3a2a60c | 2023-05-27 18:02:55 +0100 | [diff] [blame] | 3 | source shared.vim |
Bram Moolenaar | b46fecd | 2019-06-15 17:58:09 +0200 | [diff] [blame] | 4 | source check.vim |
| 5 | CheckFeature cryptv |
Bram Moolenaar | 680e015 | 2016-09-25 20:54:11 +0200 | [diff] [blame] | 6 | |
Bram Moolenaar | 90e66ec | 2022-11-10 00:25:05 +0000 | [diff] [blame] | 7 | " Use the xxd command from: |
| 8 | " 1: $XXDPROG if set and it is executable |
| 9 | " 2: the ../xxd directory if the executable is found there |
| 10 | if !empty($XXDPROG) && executable($XXDPROG) |
James McCoy | a5d4f3b | 2021-10-11 16:27:03 +0100 | [diff] [blame] | 11 | let s:xxd_cmd = $XXDPROG |
Bram Moolenaar | 90e66ec | 2022-11-10 00:25:05 +0000 | [diff] [blame] | 12 | elseif executable('..\xxd\xxd.exe') |
| 13 | " we're on MS-Windows |
| 14 | let s:xxd_cmd = '..\xxd\xxd.exe' |
| 15 | elseif executable('../xxd/xxd') |
| 16 | " we're on something like Unix |
| 17 | let s:xxd_cmd = '../xxd/xxd' |
| 18 | else |
| 19 | " looks like xxd wasn't build (yet) |
| 20 | let s:xxd_cmd = '' |
James McCoy | a5d4f3b | 2021-10-11 16:27:03 +0100 | [diff] [blame] | 21 | endif |
| 22 | |
Bram Moolenaar | 680e015 | 2016-09-25 20:54:11 +0200 | [diff] [blame] | 23 | func Common_head_only(text) |
| 24 | " This was crashing Vim |
| 25 | split Xtest.txt |
| 26 | call setline(1, a:text) |
| 27 | wq |
| 28 | call feedkeys(":split Xtest.txt\<CR>foobar\<CR>", "tx") |
| 29 | call delete('Xtest.txt') |
| 30 | call assert_match('VimCrypt', getline(1)) |
| 31 | bwipe! |
| 32 | endfunc |
| 33 | |
| 34 | func Test_head_only_2() |
| 35 | call Common_head_only('VimCrypt~02!abc') |
| 36 | endfunc |
| 37 | |
| 38 | func Test_head_only_3() |
| 39 | call Common_head_only('VimCrypt~03!abc') |
| 40 | endfunc |
Bram Moolenaar | 1777785 | 2016-09-27 21:30:22 +0200 | [diff] [blame] | 41 | |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 42 | func Test_head_only_4() |
| 43 | CheckFeature sodium |
| 44 | call Common_head_only('VimCrypt~04!abc') |
| 45 | endfunc |
| 46 | |
Bram Moolenaar | 1777785 | 2016-09-27 21:30:22 +0200 | [diff] [blame] | 47 | func Crypt_uncrypt(method) |
| 48 | exe "set cryptmethod=" . a:method |
| 49 | " If the blowfish test fails 'cryptmethod' will be 'zip' now. |
| 50 | call assert_equal(a:method, &cryptmethod) |
| 51 | |
| 52 | split Xtest.txt |
ichizok | 35a2ec1 | 2023-04-25 15:27:27 +0100 | [diff] [blame] | 53 | let text =<< trim END |
| 54 | 01234567890123456789012345678901234567, |
| 55 | line 2 foo bar blah, |
| 56 | line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
| 57 | END |
Bram Moolenaar | 1777785 | 2016-09-27 21:30:22 +0200 | [diff] [blame] | 58 | call setline(1, text) |
| 59 | call feedkeys(":X\<CR>foobar\<CR>foobar\<CR>", 'xt') |
Bram Moolenaar | 987411d | 2019-01-18 22:48:34 +0100 | [diff] [blame] | 60 | call assert_equal('*****', &key) |
Bram Moolenaar | 1777785 | 2016-09-27 21:30:22 +0200 | [diff] [blame] | 61 | w! |
| 62 | bwipe! |
| 63 | call feedkeys(":split Xtest.txt\<CR>foobar\<CR>", 'xt') |
| 64 | call assert_equal(text, getline(1, 3)) |
| 65 | set key= cryptmethod& |
| 66 | bwipe! |
| 67 | call delete('Xtest.txt') |
| 68 | endfunc |
| 69 | |
| 70 | func Test_crypt_zip() |
| 71 | call Crypt_uncrypt('zip') |
| 72 | endfunc |
| 73 | |
| 74 | func Test_crypt_blowfish() |
| 75 | call Crypt_uncrypt('blowfish') |
| 76 | endfunc |
| 77 | |
| 78 | func Test_crypt_blowfish2() |
| 79 | call Crypt_uncrypt('blowfish2') |
| 80 | endfunc |
| 81 | |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 82 | func Test_crypt_sodium() |
| 83 | CheckFeature sodium |
| 84 | call Crypt_uncrypt('xchacha20') |
| 85 | endfunc |
| 86 | |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 87 | func Test_crypt_sodium_v2() |
| 88 | CheckFeature sodium |
| 89 | call Crypt_uncrypt('xchacha20v2') |
| 90 | endfunc |
| 91 | |
Bram Moolenaar | 3a2a60c | 2023-05-27 18:02:55 +0100 | [diff] [blame] | 92 | func Test_crypt_sodium_v2_startup() |
| 93 | CheckFeature sodium |
| 94 | CheckRunVimInTerminal |
| 95 | |
| 96 | let buf = RunVimInTerminal('--cmd "set cm=xchacha20v2" -x Xfoo', #{wait_for_ruler: 0, rows: 6}) |
| 97 | call g:TermWait(buf, g:RunningWithValgrind() ? 1000 : 50) |
| 98 | call term_sendkeys(buf, "foo\<CR>foo\<CR>") |
| 99 | call term_sendkeys(buf, "ifoo\<Esc>") |
| 100 | call term_sendkeys(buf, "ZZ") |
| 101 | call TermWait(buf) |
| 102 | |
| 103 | " Wait for Vim to write the file and exit. Then wipe out the terminal buffer. |
| 104 | call WaitForAssert({-> assert_equal("finished", term_getstatus(buf))}) |
| 105 | exe buf .. 'bwipe!' |
| 106 | call assert_true(filereadable('Xfoo')) |
| 107 | |
Christian Brabandt | 19e6c4f | 2023-06-27 18:57:10 +0100 | [diff] [blame] | 108 | let buf = RunVimInTerminal('--cmd "set ch=3 cm=xchacha20v2 key=foo" Xfoo', #{wait_for_ruler: 0, rows: 10}) |
Bram Moolenaar | 3a2a60c | 2023-05-27 18:02:55 +0100 | [diff] [blame] | 109 | call g:TermWait(buf, g:RunningWithValgrind() ? 1000 : 50) |
| 110 | call StopVimInTerminal(buf) |
| 111 | |
| 112 | call delete('Xfoo') |
| 113 | endfunc |
| 114 | |
Bram Moolenaar | 1777785 | 2016-09-27 21:30:22 +0200 | [diff] [blame] | 115 | func Uncrypt_stable(method, crypted_text, key, uncrypted_text) |
| 116 | split Xtest.txt |
| 117 | set bin noeol key= fenc=latin1 |
| 118 | exe "set cryptmethod=" . a:method |
| 119 | call setline(1, a:crypted_text) |
| 120 | w! |
| 121 | bwipe! |
| 122 | set nobin |
| 123 | call feedkeys(":split Xtest.txt\<CR>" . a:key . "\<CR>", 'xt') |
| 124 | call assert_equal(a:uncrypted_text, getline(1, len(a:uncrypted_text))) |
| 125 | bwipe! |
| 126 | call delete('Xtest.txt') |
| 127 | set key= |
| 128 | endfunc |
| 129 | |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 130 | func Uncrypt_stable_xxd(method, hex, key, uncrypted_text, verbose) |
James McCoy | a5d4f3b | 2021-10-11 16:27:03 +0100 | [diff] [blame] | 131 | if empty(s:xxd_cmd) |
| 132 | throw 'Skipped: xxd program missing' |
| 133 | endif |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 134 | " use xxd to write the binary content |
James McCoy | a5d4f3b | 2021-10-11 16:27:03 +0100 | [diff] [blame] | 135 | call system(s:xxd_cmd .. ' -r >Xtest.txt', a:hex) |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 136 | let cmd = (a:verbose ? ':verbose' : '') .. |
| 137 | \ ":split Xtest.txt\<CR>" . a:key . "\<CR>" |
| 138 | call feedkeys(cmd, 'xt') |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 139 | call assert_equal(a:uncrypted_text, getline(1, len(a:uncrypted_text))) |
| 140 | bwipe! |
| 141 | call delete('Xtest.txt') |
| 142 | set key= |
| 143 | endfunc |
| 144 | |
Bram Moolenaar | 1777785 | 2016-09-27 21:30:22 +0200 | [diff] [blame] | 145 | func Test_uncrypt_zip() |
| 146 | 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"]) |
| 147 | endfunc |
| 148 | |
| 149 | func Test_uncrypt_blowfish() |
| 150 | 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"]) |
| 151 | endfunc |
| 152 | |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 153 | func Test_uncrypt_blowfish2a() |
Bram Moolenaar | 1777785 | 2016-09-27 21:30:22 +0200 | [diff] [blame] | 154 | 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", "!@#$%^&*()_+=-`~"]) |
| 155 | endfunc |
Bram Moolenaar | 987411d | 2019-01-18 22:48:34 +0100 | [diff] [blame] | 156 | |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 157 | func Test_uncrypt_blowfish2() |
| 158 | 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", "!@#$%^&*()_+=-`~"]) |
| 159 | endfunc |
| 160 | |
| 161 | func Test_uncrypt_xchacha20() |
| 162 | CheckFeature sodium |
ichizok | 35a2ec1 | 2023-04-25 15:27:27 +0100 | [diff] [blame] | 163 | let hex =<< trim END |
| 164 | 00000000: 5669 6d43 7279 7074 7e30 3421 6b7d e607 vimCrypt~04!k}.. |
| 165 | 00000010: 4ea4 e99f 923e f67f 7b59 a80d 3bca 2f06 N....>..{Y..;./. |
| 166 | 00000020: fa11 b951 8d09 0dc9 470f e7cf 8b90 4310 ...Q....G.....C. |
| 167 | 00000030: 653b b83b e493 378b 0390 0e38 f912 626b e;.;..7....8..bk |
| 168 | 00000040: a02e 4697 0254 2625 2d8e 3a0b 784b e89c ..F..T&%-.:.xK.. |
| 169 | 00000050: 0c67 a975 3c17 9319 8ffd 1463 7783 a1f3 .g.u<......cw... |
| 170 | 00000060: d917 dcb3 8b3e ecd7 c7d4 086b 6059 7ead .....>.....k`Y~. |
| 171 | 00000070: 9b07 f96b 5c1b 4d08 cd91 f208 5221 7484 ...k\.M.....R!t. |
| 172 | 00000080: 72be 0136 84a1 d3 r..6... |
| 173 | END |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 174 | " the file should be in latin1 encoding, this makes sure that readfile() |
| 175 | " retries several times converting the multi-byte characters |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 176 | call Uncrypt_stable_xxd('xchacha20', hex, "sodium_crypt", ["abcdefghijklmnopqrstuvwxyzäöü", "ZZZ_äüöÄÜÖ_!@#$%^&*()_+=-`~"], 0) |
| 177 | endfunc |
| 178 | |
| 179 | func Test_uncrypt_xchacha20v2_custom() |
| 180 | CheckFeature sodium |
| 181 | " Test, reading xchacha20v2 with custom encryption parameters |
ichizok | 35a2ec1 | 2023-04-25 15:27:27 +0100 | [diff] [blame] | 182 | let hex =<< trim END |
| 183 | 00000000: 5669 6d43 7279 7074 7e30 3521 934b f288 VimCrypt~05!.K.. |
| 184 | 00000010: 10ba 8bc9 25a0 8876 f85c f135 6fb8 518b ....%..v.\.5o.Q. |
| 185 | 00000020: b133 9af1 0300 0000 0000 0000 0000 0010 .3.............. |
| 186 | 00000030: 0000 0000 0200 0000 b973 5f33 80e9 54fc .........s_3..T. |
| 187 | 00000040: 138f ba3e 046b 3135 90b7 7783 5eac 7fe3 ...>.k15..w.^... |
| 188 | 00000050: 0cd2 14df ed75 4b65 8763 8205 035c ec81 .....uKe.c...\.. |
| 189 | 00000060: a4cf 33d2 7507 ec38 ba62 a327 9068 d8ad ..3.u..8.b.'.h.. |
| 190 | 00000070: 2607 3fa6 f95d 7ea8 9799 f997 4820 0c &.?..]~.....H . |
| 191 | END |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 192 | call Uncrypt_stable_xxd('xchacha20v2', hex, "foobar", ["", "foo", "bar", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"], 1) |
| 193 | call assert_match('xchacha20v2: using custom \w\+ "\d\+" for Key derivation.', execute(':messages')) |
| 194 | endfunc |
| 195 | |
| 196 | func Test_uncrypt_xchacha20v2() |
| 197 | CheckFeature sodium |
| 198 | " Test, reading xchacha20v2 |
ichizok | 35a2ec1 | 2023-04-25 15:27:27 +0100 | [diff] [blame] | 199 | let hex =<< trim END |
| 200 | 00000000: 5669 6d43 7279 7074 7e30 3521 9f20 4e14 VimCrypt~05!. N. |
| 201 | 00000010: c7da c1bd 7dea 8fbc db6c 38e6 7a77 6fef ....}....l8.zwo. |
| 202 | 00000020: 82dd 964b 0300 0000 0000 0000 0000 0010 ...K............ |
| 203 | 00000030: 0000 0000 0200 0000 a97c 2f00 0b9d 19eb .........|/..... |
| 204 | 00000040: 1d92 1ea5 3f22 c179 4b3e 870a eb19 6380 ....?".yK>....c. |
| 205 | 00000050: 63f8 222d b5d1 3c73 7be5 d580 47ea 44cc c."-..<s{...G.D. |
| 206 | 00000060: 6c25 8078 3fd5 d836 c700 0122 bb30 7a59 l%.x?..6...".0zY |
| 207 | 00000070: b184 2ae8 e7db 113a f732 938f 7a34 1333 ..*....:.2..z4.3 |
| 208 | 00000080: dc89 1491 51a0 67b9 0f3a b56c 1f9d 53b0 ....Q.g..:.l..S. |
| 209 | 00000090: 2416 205a 8c4c 5fde 4dac 2611 8a48 24f0 $. Z.L_.M.&..H$. |
| 210 | 000000a0: ba00 92c1 60 ....` |
| 211 | END |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 212 | call Uncrypt_stable_xxd('xchacha20v2', hex, "foo1234", ["abcdefghijklmnopqrstuvwxyzäöü", 'ZZZ_äüöÄÜÖ_!@#$%^&*()_+=-`~"'], 0) |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 213 | endfunc |
| 214 | |
| 215 | func Test_uncrypt_xchacha20_invalid() |
| 216 | CheckFeature sodium |
Bram Moolenaar | db86472 | 2021-07-08 11:37:50 +0200 | [diff] [blame] | 217 | |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 218 | " load an invalid encrypted file and verify it can be decrypted with an |
| 219 | " error message |
| 220 | try |
| 221 | call feedkeys(":split samples/crypt_sodium_invalid.txt\<CR>sodium\<CR>", 'xt') |
| 222 | call assert_false(1, 'should not happen') |
| 223 | catch |
| 224 | call assert_exception('pre-mature') |
| 225 | endtry |
Christian Brabandt | 8a4c812 | 2021-07-25 14:36:05 +0200 | [diff] [blame] | 226 | 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] | 227 | |
| 228 | call assert_equal(0, &swapfile) |
| 229 | call assert_equal("xchacha20", &cryptmethod) |
| 230 | call assert_equal('311111111111111111111111', getline('$')) |
| 231 | bw! |
| 232 | endfunc |
| 233 | |
| 234 | func Test_uncrypt_xchacha20_2() |
| 235 | CheckFeature sodium |
Bram Moolenaar | db86472 | 2021-07-08 11:37:50 +0200 | [diff] [blame] | 236 | |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 237 | sp Xcrypt_sodium.txt |
| 238 | " Create a larger file, so that Vim will write in several blocks |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 239 | call setline(1, range(1, 4000)) |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 240 | call assert_equal(1, &swapfile) |
| 241 | set cryptmethod=xchacha20 |
| 242 | call feedkeys(":X\<CR>sodium\<CR>sodium\<CR>", 'xt') |
| 243 | " swapfile disabled |
| 244 | call assert_equal(0, &swapfile) |
Christian Brabandt | 8a4c812 | 2021-07-25 14:36:05 +0200 | [diff] [blame] | 245 | 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] | 246 | w! |
| 247 | " encrypted using xchacha20 |
| 248 | call assert_match("\[xchacha20\]", execute(':messages')) |
| 249 | bw! |
| 250 | call feedkeys(":sp Xcrypt_sodium.txt\<CR>sodium\<CR>", 'xt') |
| 251 | " successfully decrypted |
| 252 | call assert_equal(range(1, 4000)->map( {_, v -> string(v)}), getline(1,'$')) |
| 253 | set key= |
Bram Moolenaar | db86472 | 2021-07-08 11:37:50 +0200 | [diff] [blame] | 254 | w! ++ff=unix |
Dominique Pelle | 81b573d | 2022-03-22 21:14:55 +0000 | [diff] [blame] | 255 | " encryption removed (on MS-Windows the .* matches [unix]) |
Christian Brabandt | 16e26a3 | 2021-07-13 19:09:12 +0200 | [diff] [blame] | 256 | call assert_match('"Xcrypt_sodium.txt".*4000L, 18893B written', execute(':message')) |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 257 | bw! |
| 258 | call delete('Xcrypt_sodium.txt') |
| 259 | set cryptmethod&vim |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 260 | |
| 261 | endfunc |
| 262 | |
| 263 | func Test_uncrypt_xchacha20v2_2() |
| 264 | CheckFeature sodium |
| 265 | |
| 266 | sp Xcrypt_sodium_v2.txt |
| 267 | " Create a larger file, so that Vim will write in several blocks |
| 268 | call setline(1, range(1, 4000)) |
| 269 | call assert_equal(1, &swapfile) |
| 270 | set cryptmethod=xchacha20v2 |
| 271 | call feedkeys(":X\<CR>sodium\<CR>sodium\<CR>", 'xt') |
| 272 | " swapfile disabled |
| 273 | call assert_equal(0, &swapfile) |
| 274 | call assert_match("Note: Encryption of swapfile not supported, disabling swap file", execute(':messages')) |
| 275 | w! |
| 276 | " encrypted using xchacha20 |
| 277 | call assert_match("\[xchachav2\]", execute(':messages')) |
| 278 | bw! |
| 279 | call feedkeys(":verbose :sp Xcrypt_sodium_v2.txt\<CR>sodium\<CR>", 'xt') |
| 280 | " successfully decrypted |
| 281 | call assert_equal(range(1, 4000)->map( {_, v -> string(v)}), getline(1,'$')) |
| 282 | call assert_match('xchacha20v2: using default \w\+ "\d\+" for Key derivation.', execute(':messages')) |
| 283 | set key= |
| 284 | w! ++ff=unix |
| 285 | " encryption removed (on MS-Windows the .* matches [unix]) |
| 286 | call assert_match('"Xcrypt_sodium_v2.txt".*4000L, 18893B written', execute(':message')) |
| 287 | bw! |
| 288 | call delete('Xcrypt_sodium_v2.txt') |
| 289 | set cryptmethod&vim |
| 290 | |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 291 | endfunc |
| 292 | |
| 293 | func Test_uncrypt_xchacha20_3_persistent_undo() |
| 294 | CheckFeature sodium |
| 295 | CheckFeature persistent_undo |
Bram Moolenaar | db86472 | 2021-07-08 11:37:50 +0200 | [diff] [blame] | 296 | |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 297 | for meth in ['xchacha20', 'xchacha20v2'] |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 298 | |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 299 | sp Xcrypt_sodium_undo.txt |
| 300 | exe "set cryptmethod=" .. meth .. " undofile" |
| 301 | call feedkeys(":X\<CR>sodium\<CR>sodium\<CR>", 'xt') |
| 302 | call assert_equal(1, &undofile) |
| 303 | let ufile=undofile(@%) |
| 304 | call append(0, ['monday', 'tuesday', 'wednesday', 'thursday', 'friday']) |
| 305 | call cursor(1, 1) |
| 306 | |
| 307 | set undolevels=100 |
| 308 | normal dd |
| 309 | set undolevels=100 |
| 310 | normal dd |
| 311 | set undolevels=100 |
| 312 | normal dd |
| 313 | set undolevels=100 |
| 314 | w! |
| 315 | call assert_equal(0, &undofile) |
| 316 | bw! |
| 317 | call feedkeys(":sp Xcrypt_sodium_undo.txt\<CR>sodium\<CR>", 'xt') |
| 318 | " should fail |
| 319 | norm! u |
| 320 | call assert_match('Already at oldest change', execute(':1mess')) |
| 321 | call assert_fails('verbose rundo ' .. fnameescape(ufile), 'E822') |
| 322 | bw! |
| 323 | set undolevels& cryptmethod& undofile& |
| 324 | call delete('Xcrypt_sodium_undo.txt') |
| 325 | |
| 326 | endfor |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 327 | endfunc |
| 328 | |
| 329 | func Test_encrypt_xchacha20_missing() |
| 330 | if has("sodium") |
| 331 | return |
| 332 | endif |
| 333 | sp Xcrypt_sodium_undo.txt |
| 334 | call assert_fails(':set cryptmethod=xchacha20', 'E474') |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 335 | call assert_fails(':set cryptmethod=xchacha20v2', 'E474') |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 336 | bw! |
| 337 | set cm& |
| 338 | endfunc |
| 339 | |
Bram Moolenaar | 987411d | 2019-01-18 22:48:34 +0100 | [diff] [blame] | 340 | func Test_uncrypt_unknown_method() |
| 341 | split Xuncrypt_unknown.txt |
| 342 | set bin noeol key= fenc=latin1 |
| 343 | call setline(1, "VimCrypt~93!\u001e\u00d1") |
| 344 | w! |
| 345 | bwipe! |
| 346 | set nobin |
| 347 | call assert_fails(":split Xuncrypt_unknown.txt", 'E821:') |
| 348 | |
| 349 | bwipe! |
| 350 | call delete('Xuncrypt_unknown.txt') |
| 351 | set key= |
| 352 | endfunc |
| 353 | |
| 354 | func Test_crypt_key_mismatch() |
| 355 | set cryptmethod=blowfish |
| 356 | |
| 357 | split Xtest.txt |
| 358 | call setline(1, 'nothing') |
| 359 | call feedkeys(":X\<CR>foobar\<CR>nothing\<CR>", 'xt') |
| 360 | call assert_match("Keys don't match!", execute(':2messages')) |
| 361 | call assert_equal('', &key) |
| 362 | call feedkeys("\<CR>\<CR>", 'xt') |
| 363 | |
| 364 | set cryptmethod& |
| 365 | bwipe! |
| 366 | endfunc |
| 367 | |
Bram Moolenaar | 76cb683 | 2020-05-15 22:30:38 +0200 | [diff] [blame] | 368 | func Test_crypt_set_key_changes_buffer() |
| 369 | |
| 370 | new Xtest1.txt |
| 371 | call setline(1, 'nothing') |
| 372 | set cryptmethod=blowfish2 |
| 373 | call feedkeys(":X\<CR>foobar\<CR>foobar\<CR>", 'xt') |
| 374 | call assert_fails(":q", "E37:") |
| 375 | w |
| 376 | set key=anotherkey |
| 377 | call assert_fails(":bw") |
| 378 | w |
| 379 | call feedkeys(":X\<CR>foobar\<CR>foobar\<CR>", 'xt') |
| 380 | call assert_fails(":bw") |
| 381 | w |
| 382 | let winnr = winnr() |
| 383 | wincmd p |
| 384 | call setwinvar(winnr, '&key', 'yetanotherkey') |
| 385 | wincmd p |
| 386 | call assert_fails(":bw") |
| 387 | w |
| 388 | |
| 389 | set cryptmethod& |
| 390 | set key= |
| 391 | bwipe! |
| 392 | call delete('Xtest1.txt') |
| 393 | endfunc |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 394 | |
Christian Brabandt | 19e6c4f | 2023-06-27 18:57:10 +0100 | [diff] [blame] | 395 | func Test_crypt_set_key_segfault() |
| 396 | CheckFeature sodium |
| 397 | |
| 398 | defer delete('Xtest2.txt') |
| 399 | new Xtest2.txt |
| 400 | call setline(1, 'nothing') |
| 401 | set cryptmethod=xchacha20 |
| 402 | set key=foobar |
| 403 | w |
| 404 | new Xtest3 |
| 405 | put ='other content' |
| 406 | setl modified |
| 407 | sil! preserve |
| 408 | bwipe! |
| 409 | |
| 410 | set cryptmethod& |
| 411 | set key= |
| 412 | bwipe! |
| 413 | endfunc |
| 414 | |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 415 | " vim: shiftwidth=2 sts=2 expandtab |