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 | CheckFeature cryptv |
Bram Moolenaar | 680e015 | 2016-09-25 20:54:11 +0200 | [diff] [blame] | 4 | |
Bram Moolenaar | 90e66ec | 2022-11-10 00:25:05 +0000 | [diff] [blame] | 5 | " 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 |
| 8 | if !empty($XXDPROG) && executable($XXDPROG) |
James McCoy | a5d4f3b | 2021-10-11 16:27:03 +0100 | [diff] [blame] | 9 | let s:xxd_cmd = $XXDPROG |
Bram Moolenaar | 90e66ec | 2022-11-10 00:25:05 +0000 | [diff] [blame] | 10 | elseif executable('..\xxd\xxd.exe') |
| 11 | " we're on MS-Windows |
| 12 | let s:xxd_cmd = '..\xxd\xxd.exe' |
| 13 | elseif executable('../xxd/xxd') |
| 14 | " we're on something like Unix |
| 15 | let s:xxd_cmd = '../xxd/xxd' |
| 16 | else |
| 17 | " looks like xxd wasn't build (yet) |
| 18 | let s:xxd_cmd = '' |
James McCoy | a5d4f3b | 2021-10-11 16:27:03 +0100 | [diff] [blame] | 19 | endif |
| 20 | |
Bram Moolenaar | 680e015 | 2016-09-25 20:54:11 +0200 | [diff] [blame] | 21 | func Common_head_only(text) |
| 22 | " This was crashing Vim |
Bram Moolenaar | 16abd99 | 2023-07-08 00:54:06 +0100 | [diff] [blame] | 23 | split Xtest_head.txt |
Bram Moolenaar | 680e015 | 2016-09-25 20:54:11 +0200 | [diff] [blame] | 24 | call setline(1, a:text) |
| 25 | wq |
Bram Moolenaar | 16abd99 | 2023-07-08 00:54:06 +0100 | [diff] [blame] | 26 | call feedkeys(":split Xtest_head.txt\<CR>foobar\<CR>", "tx") |
| 27 | call delete('Xtest_head.txt') |
Bram Moolenaar | 680e015 | 2016-09-25 20:54:11 +0200 | [diff] [blame] | 28 | call assert_match('VimCrypt', getline(1)) |
| 29 | bwipe! |
| 30 | endfunc |
| 31 | |
| 32 | func Test_head_only_2() |
| 33 | call Common_head_only('VimCrypt~02!abc') |
| 34 | endfunc |
| 35 | |
| 36 | func Test_head_only_3() |
| 37 | call Common_head_only('VimCrypt~03!abc') |
| 38 | endfunc |
Bram Moolenaar | 1777785 | 2016-09-27 21:30:22 +0200 | [diff] [blame] | 39 | |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 40 | func Test_head_only_4() |
| 41 | CheckFeature sodium |
| 42 | call Common_head_only('VimCrypt~04!abc') |
| 43 | endfunc |
| 44 | |
Bram Moolenaar | 1777785 | 2016-09-27 21:30:22 +0200 | [diff] [blame] | 45 | func 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 Moolenaar | 16abd99 | 2023-07-08 00:54:06 +0100 | [diff] [blame] | 50 | split Xtest_uncrypt.txt |
ichizok | 35a2ec1 | 2023-04-25 15:27:27 +0100 | [diff] [blame] | 51 | let text =<< trim END |
| 52 | 01234567890123456789012345678901234567, |
| 53 | line 2 foo bar blah, |
| 54 | line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
| 55 | END |
Bram Moolenaar | 1777785 | 2016-09-27 21:30:22 +0200 | [diff] [blame] | 56 | call setline(1, text) |
| 57 | call feedkeys(":X\<CR>foobar\<CR>foobar\<CR>", 'xt') |
Bram Moolenaar | 987411d | 2019-01-18 22:48:34 +0100 | [diff] [blame] | 58 | call assert_equal('*****', &key) |
Bram Moolenaar | 1777785 | 2016-09-27 21:30:22 +0200 | [diff] [blame] | 59 | w! |
| 60 | bwipe! |
Bram Moolenaar | 16abd99 | 2023-07-08 00:54:06 +0100 | [diff] [blame] | 61 | call feedkeys(":split Xtest_uncrypt.txt\<CR>foobar\<CR>", 'xt') |
Bram Moolenaar | 1777785 | 2016-09-27 21:30:22 +0200 | [diff] [blame] | 62 | call assert_equal(text, getline(1, 3)) |
| 63 | set key= cryptmethod& |
| 64 | bwipe! |
Bram Moolenaar | 16abd99 | 2023-07-08 00:54:06 +0100 | [diff] [blame] | 65 | call delete('Xtest_uncrypt.txt') |
Bram Moolenaar | 1777785 | 2016-09-27 21:30:22 +0200 | [diff] [blame] | 66 | endfunc |
| 67 | |
| 68 | func Test_crypt_zip() |
| 69 | call Crypt_uncrypt('zip') |
| 70 | endfunc |
| 71 | |
| 72 | func Test_crypt_blowfish() |
| 73 | call Crypt_uncrypt('blowfish') |
| 74 | endfunc |
| 75 | |
| 76 | func Test_crypt_blowfish2() |
| 77 | call Crypt_uncrypt('blowfish2') |
| 78 | endfunc |
| 79 | |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 80 | func Test_crypt_sodium() |
| 81 | CheckFeature sodium |
| 82 | call Crypt_uncrypt('xchacha20') |
| 83 | endfunc |
| 84 | |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 85 | func Test_crypt_sodium_v2() |
| 86 | CheckFeature sodium |
| 87 | call Crypt_uncrypt('xchacha20v2') |
| 88 | endfunc |
| 89 | |
Bram Moolenaar | 3a2a60c | 2023-05-27 18:02:55 +0100 | [diff] [blame] | 90 | func 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 Brabandt | 19e6c4f | 2023-06-27 18:57:10 +0100 | [diff] [blame] | 106 | 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] | 107 | call g:TermWait(buf, g:RunningWithValgrind() ? 1000 : 50) |
| 108 | call StopVimInTerminal(buf) |
| 109 | |
| 110 | call delete('Xfoo') |
| 111 | endfunc |
| 112 | |
Bram Moolenaar | 1777785 | 2016-09-27 21:30:22 +0200 | [diff] [blame] | 113 | func Uncrypt_stable(method, crypted_text, key, uncrypted_text) |
Bram Moolenaar | 16abd99 | 2023-07-08 00:54:06 +0100 | [diff] [blame] | 114 | split Xtest_stable.txt |
Bram Moolenaar | 1777785 | 2016-09-27 21:30:22 +0200 | [diff] [blame] | 115 | 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 Moolenaar | 16abd99 | 2023-07-08 00:54:06 +0100 | [diff] [blame] | 121 | call feedkeys(":split Xtest_stable.txt\<CR>" . a:key . "\<CR>", 'xt') |
Bram Moolenaar | 1777785 | 2016-09-27 21:30:22 +0200 | [diff] [blame] | 122 | call assert_equal(a:uncrypted_text, getline(1, len(a:uncrypted_text))) |
| 123 | bwipe! |
Bram Moolenaar | 16abd99 | 2023-07-08 00:54:06 +0100 | [diff] [blame] | 124 | call delete('Xtest_stable.txt') |
Bram Moolenaar | 1777785 | 2016-09-27 21:30:22 +0200 | [diff] [blame] | 125 | set key= |
| 126 | endfunc |
| 127 | |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 128 | func Uncrypt_stable_xxd(method, hex, key, uncrypted_text, verbose) |
James McCoy | a5d4f3b | 2021-10-11 16:27:03 +0100 | [diff] [blame] | 129 | if empty(s:xxd_cmd) |
| 130 | throw 'Skipped: xxd program missing' |
| 131 | endif |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 132 | " use xxd to write the binary content |
Bram Moolenaar | 16abd99 | 2023-07-08 00:54:06 +0100 | [diff] [blame] | 133 | call system(s:xxd_cmd .. ' -r >Xtest_stable_xxd.txt', a:hex) |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 134 | let cmd = (a:verbose ? ':verbose' : '') .. |
Bram Moolenaar | 16abd99 | 2023-07-08 00:54:06 +0100 | [diff] [blame] | 135 | \ ":split Xtest_stable_xxd.txt\<CR>" . a:key . "\<CR>" |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 136 | call feedkeys(cmd, 'xt') |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 137 | call assert_equal(a:uncrypted_text, getline(1, len(a:uncrypted_text))) |
| 138 | bwipe! |
Bram Moolenaar | 16abd99 | 2023-07-08 00:54:06 +0100 | [diff] [blame] | 139 | call delete('Xtest_stable_xxd.txt') |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 140 | set key= |
| 141 | endfunc |
| 142 | |
Bram Moolenaar | 1777785 | 2016-09-27 21:30:22 +0200 | [diff] [blame] | 143 | func 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"]) |
| 145 | endfunc |
| 146 | |
| 147 | func 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"]) |
| 149 | endfunc |
| 150 | |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 151 | func Test_uncrypt_blowfish2a() |
Bram Moolenaar | 1777785 | 2016-09-27 21:30:22 +0200 | [diff] [blame] | 152 | 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", "!@#$%^&*()_+=-`~"]) |
| 153 | endfunc |
Bram Moolenaar | 987411d | 2019-01-18 22:48:34 +0100 | [diff] [blame] | 154 | |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 155 | func 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", "!@#$%^&*()_+=-`~"]) |
| 157 | endfunc |
| 158 | |
| 159 | func Test_uncrypt_xchacha20() |
| 160 | CheckFeature sodium |
ichizok | 35a2ec1 | 2023-04-25 15:27:27 +0100 | [diff] [blame] | 161 | 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 Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 172 | " the file should be in latin1 encoding, this makes sure that readfile() |
| 173 | " retries several times converting the multi-byte characters |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 174 | call Uncrypt_stable_xxd('xchacha20', hex, "sodium_crypt", ["abcdefghijklmnopqrstuvwxyzäöü", "ZZZ_äüöÄÜÖ_!@#$%^&*()_+=-`~"], 0) |
| 175 | endfunc |
| 176 | |
| 177 | func Test_uncrypt_xchacha20v2_custom() |
| 178 | CheckFeature sodium |
| 179 | " Test, reading xchacha20v2 with custom encryption parameters |
ichizok | 35a2ec1 | 2023-04-25 15:27:27 +0100 | [diff] [blame] | 180 | 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 Brabandt | 335c584 | 2023-08-09 16:32:28 +0200 | [diff] [blame] | 190 | 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 Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 195 | call assert_match('xchacha20v2: using custom \w\+ "\d\+" for Key derivation.', execute(':messages')) |
| 196 | endfunc |
| 197 | |
| 198 | func Test_uncrypt_xchacha20v2() |
| 199 | CheckFeature sodium |
| 200 | " Test, reading xchacha20v2 |
ichizok | 35a2ec1 | 2023-04-25 15:27:27 +0100 | [diff] [blame] | 201 | 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 Brabandt | 335c584 | 2023-08-09 16:32:28 +0200 | [diff] [blame] | 214 | 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 Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 219 | endfunc |
| 220 | |
| 221 | func Test_uncrypt_xchacha20_invalid() |
| 222 | CheckFeature sodium |
Bram Moolenaar | db86472 | 2021-07-08 11:37:50 +0200 | [diff] [blame] | 223 | |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 224 | " 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 Brabandt | 335c584 | 2023-08-09 16:32:28 +0200 | [diff] [blame] | 229 | catch /^Vim\%((\a\+)\)\=:E1230:/ " sodium_mlock() not possible, may happen at Github CI |
| 230 | throw 'Skipped: sodium_mlock() not possible' |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 231 | catch |
| 232 | call assert_exception('pre-mature') |
| 233 | endtry |
Christian Brabandt | 8a4c812 | 2021-07-25 14:36:05 +0200 | [diff] [blame] | 234 | 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] | 235 | |
| 236 | call assert_equal(0, &swapfile) |
| 237 | call assert_equal("xchacha20", &cryptmethod) |
| 238 | call assert_equal('311111111111111111111111', getline('$')) |
| 239 | bw! |
| 240 | endfunc |
| 241 | |
| 242 | func Test_uncrypt_xchacha20_2() |
| 243 | CheckFeature sodium |
Bram Moolenaar | db86472 | 2021-07-08 11:37:50 +0200 | [diff] [blame] | 244 | |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 245 | sp Xcrypt_sodium.txt |
| 246 | " Create a larger file, so that Vim will write in several blocks |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 247 | call setline(1, range(1, 4000)) |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 248 | 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 Brabandt | 8a4c812 | 2021-07-25 14:36:05 +0200 | [diff] [blame] | 253 | 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] | 254 | w! |
| 255 | " encrypted using xchacha20 |
Isao Sato | e6fca0e | 2023-09-24 23:27:25 +0200 | [diff] [blame] | 256 | call assert_match('\[xchacha20\]', execute(':messages')) |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 257 | 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 Moolenaar | db86472 | 2021-07-08 11:37:50 +0200 | [diff] [blame] | 262 | w! ++ff=unix |
Dominique Pelle | 81b573d | 2022-03-22 21:14:55 +0000 | [diff] [blame] | 263 | " encryption removed (on MS-Windows the .* matches [unix]) |
Christian Brabandt | 16e26a3 | 2021-07-13 19:09:12 +0200 | [diff] [blame] | 264 | call assert_match('"Xcrypt_sodium.txt".*4000L, 18893B written', execute(':message')) |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 265 | bw! |
| 266 | call delete('Xcrypt_sodium.txt') |
| 267 | set cryptmethod&vim |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 268 | |
| 269 | endfunc |
| 270 | |
| 271 | func 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 Brabandt | 335c584 | 2023-08-09 16:32:28 +0200 | [diff] [blame] | 283 | 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 Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 288 | " encrypted using xchacha20 |
Isao Sato | e6fca0e | 2023-09-24 23:27:25 +0200 | [diff] [blame] | 289 | call assert_match('\[xchacha20v2\]', execute(':messages')) |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 290 | bw! |
Christian Brabandt | 93ebb5e | 2023-08-28 21:17:36 +0200 | [diff] [blame] | 291 | 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 Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 296 | " 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 Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 307 | endfunc |
| 308 | |
| 309 | func Test_uncrypt_xchacha20_3_persistent_undo() |
| 310 | CheckFeature sodium |
| 311 | CheckFeature persistent_undo |
Bram Moolenaar | db86472 | 2021-07-08 11:37:50 +0200 | [diff] [blame] | 312 | |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 313 | for meth in ['xchacha20', 'xchacha20v2'] |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 314 | |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 315 | 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 Brabandt | 8878653 | 2023-08-12 09:41:23 +0200 | [diff] [blame] | 330 | 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 Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 335 | call assert_equal(0, &undofile) |
| 336 | bw! |
Christian Brabandt | 25dec33 | 2023-08-19 22:15:44 +0200 | [diff] [blame] | 337 | 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 Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 342 | " should fail |
| 343 | norm! u |
| 344 | call assert_match('Already at oldest change', execute(':1mess')) |
zeertzjq | 67fe77d | 2025-04-20 10:21:18 +0200 | [diff] [blame] | 345 | call assert_fails('verbose rundo ' .. fnameescape(ufile), 'E822:') |
Christian Brabandt | aae5834 | 2023-04-23 17:50:22 +0100 | [diff] [blame] | 346 | bw! |
| 347 | set undolevels& cryptmethod& undofile& |
| 348 | call delete('Xcrypt_sodium_undo.txt') |
| 349 | |
| 350 | endfor |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 351 | endfunc |
| 352 | |
| 353 | func Test_encrypt_xchacha20_missing() |
| 354 | if has("sodium") |
| 355 | return |
| 356 | endif |
| 357 | sp Xcrypt_sodium_undo.txt |
zeertzjq | 67fe77d | 2025-04-20 10:21:18 +0200 | [diff] [blame] | 358 | call assert_fails(':set cryptmethod=xchacha20', 'E474:') |
| 359 | call assert_fails(':set cryptmethod=xchacha20v2', 'E474:') |
Christian Brabandt | f573c6e | 2021-06-20 14:02:16 +0200 | [diff] [blame] | 360 | bw! |
| 361 | set cm& |
| 362 | endfunc |
| 363 | |
Bram Moolenaar | 987411d | 2019-01-18 22:48:34 +0100 | [diff] [blame] | 364 | func 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= |
| 376 | endfunc |
| 377 | |
| 378 | func Test_crypt_key_mismatch() |
| 379 | set cryptmethod=blowfish |
| 380 | |
Bram Moolenaar | 16abd99 | 2023-07-08 00:54:06 +0100 | [diff] [blame] | 381 | split Xtest_mismatch.txt |
Bram Moolenaar | 987411d | 2019-01-18 22:48:34 +0100 | [diff] [blame] | 382 | 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! |
| 390 | endfunc |
| 391 | |
Bram Moolenaar | 76cb683 | 2020-05-15 22:30:38 +0200 | [diff] [blame] | 392 | func 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') |
| 417 | endfunc |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 418 | |
Christian Brabandt | 19e6c4f | 2023-06-27 18:57:10 +0100 | [diff] [blame] | 419 | func 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! |
| 437 | endfunc |
| 438 | |
Yee Cheng Chin | 6ee7b52 | 2023-10-01 09:13:22 +0200 | [diff] [blame] | 439 | func 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! |
| 460 | endfunc |
| 461 | |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 462 | " vim: shiftwidth=2 sts=2 expandtab |