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 | |
| 3 | if !has('cryptv') |
Bram Moolenaar | b0f94c1 | 2019-06-13 22:19:53 +0200 | [diff] [blame] | 4 | throw 'Skipped, encryption feature missing' |
Bram Moolenaar | 1eceada | 2016-09-26 20:14:56 +0200 | [diff] [blame] | 5 | endif |
Bram Moolenaar | 680e015 | 2016-09-25 20:54:11 +0200 | [diff] [blame] | 6 | |
| 7 | func Common_head_only(text) |
| 8 | " This was crashing Vim |
| 9 | split Xtest.txt |
| 10 | call setline(1, a:text) |
| 11 | wq |
| 12 | call feedkeys(":split Xtest.txt\<CR>foobar\<CR>", "tx") |
| 13 | call delete('Xtest.txt') |
| 14 | call assert_match('VimCrypt', getline(1)) |
| 15 | bwipe! |
| 16 | endfunc |
| 17 | |
| 18 | func Test_head_only_2() |
| 19 | call Common_head_only('VimCrypt~02!abc') |
| 20 | endfunc |
| 21 | |
| 22 | func Test_head_only_3() |
| 23 | call Common_head_only('VimCrypt~03!abc') |
| 24 | endfunc |
Bram Moolenaar | 1777785 | 2016-09-27 21:30:22 +0200 | [diff] [blame] | 25 | |
| 26 | func Crypt_uncrypt(method) |
| 27 | exe "set cryptmethod=" . a:method |
| 28 | " If the blowfish test fails 'cryptmethod' will be 'zip' now. |
| 29 | call assert_equal(a:method, &cryptmethod) |
| 30 | |
| 31 | split Xtest.txt |
| 32 | let text = ['01234567890123456789012345678901234567', |
| 33 | \ 'line 2 foo bar blah', |
| 34 | \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'] |
| 35 | call setline(1, text) |
| 36 | call feedkeys(":X\<CR>foobar\<CR>foobar\<CR>", 'xt') |
Bram Moolenaar | 987411d | 2019-01-18 22:48:34 +0100 | [diff] [blame] | 37 | call assert_equal('*****', &key) |
Bram Moolenaar | 1777785 | 2016-09-27 21:30:22 +0200 | [diff] [blame] | 38 | w! |
| 39 | bwipe! |
| 40 | call feedkeys(":split Xtest.txt\<CR>foobar\<CR>", 'xt') |
| 41 | call assert_equal(text, getline(1, 3)) |
| 42 | set key= cryptmethod& |
| 43 | bwipe! |
| 44 | call delete('Xtest.txt') |
| 45 | endfunc |
| 46 | |
| 47 | func Test_crypt_zip() |
| 48 | call Crypt_uncrypt('zip') |
| 49 | endfunc |
| 50 | |
| 51 | func Test_crypt_blowfish() |
| 52 | call Crypt_uncrypt('blowfish') |
| 53 | endfunc |
| 54 | |
| 55 | func Test_crypt_blowfish2() |
| 56 | call Crypt_uncrypt('blowfish2') |
| 57 | endfunc |
| 58 | |
| 59 | func Uncrypt_stable(method, crypted_text, key, uncrypted_text) |
| 60 | split Xtest.txt |
| 61 | set bin noeol key= fenc=latin1 |
| 62 | exe "set cryptmethod=" . a:method |
| 63 | call setline(1, a:crypted_text) |
| 64 | w! |
| 65 | bwipe! |
| 66 | set nobin |
| 67 | call feedkeys(":split Xtest.txt\<CR>" . a:key . "\<CR>", 'xt') |
| 68 | call assert_equal(a:uncrypted_text, getline(1, len(a:uncrypted_text))) |
| 69 | bwipe! |
| 70 | call delete('Xtest.txt') |
| 71 | set key= |
| 72 | endfunc |
| 73 | |
| 74 | func Test_uncrypt_zip() |
| 75 | 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"]) |
| 76 | endfunc |
| 77 | |
| 78 | func Test_uncrypt_blowfish() |
| 79 | 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"]) |
| 80 | endfunc |
| 81 | |
| 82 | func Test_uncrypt_blowfish2() |
| 83 | 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", "!@#$%^&*()_+=-`~"]) |
| 84 | endfunc |
Bram Moolenaar | 987411d | 2019-01-18 22:48:34 +0100 | [diff] [blame] | 85 | |
| 86 | func Test_uncrypt_unknown_method() |
| 87 | split Xuncrypt_unknown.txt |
| 88 | set bin noeol key= fenc=latin1 |
| 89 | call setline(1, "VimCrypt~93!\u001e\u00d1") |
| 90 | w! |
| 91 | bwipe! |
| 92 | set nobin |
| 93 | call assert_fails(":split Xuncrypt_unknown.txt", 'E821:') |
| 94 | |
| 95 | bwipe! |
| 96 | call delete('Xuncrypt_unknown.txt') |
| 97 | set key= |
| 98 | endfunc |
| 99 | |
| 100 | func Test_crypt_key_mismatch() |
| 101 | set cryptmethod=blowfish |
| 102 | |
| 103 | split Xtest.txt |
| 104 | call setline(1, 'nothing') |
| 105 | call feedkeys(":X\<CR>foobar\<CR>nothing\<CR>", 'xt') |
| 106 | call assert_match("Keys don't match!", execute(':2messages')) |
| 107 | call assert_equal('', &key) |
| 108 | call feedkeys("\<CR>\<CR>", 'xt') |
| 109 | |
| 110 | set cryptmethod& |
| 111 | bwipe! |
| 112 | endfunc |
| 113 | |