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