xref: /vim-8.2.3635/src/testdir/test_crypt.vim (revision ceb56ddb)
1" Tests for encryption.
2
3source check.vim
4CheckFeature cryptv
5
6func 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!
15endfunc
16
17func Test_head_only_2()
18  call Common_head_only('VimCrypt~02!abc')
19endfunc
20
21func Test_head_only_3()
22  call Common_head_only('VimCrypt~03!abc')
23endfunc
24
25func Crypt_uncrypt(method)
26  exe "set cryptmethod=" . a:method
27  " If the blowfish test fails 'cryptmethod' will be 'zip' now.
28  call assert_equal(a:method, &cryptmethod)
29
30  split Xtest.txt
31  let text = ['01234567890123456789012345678901234567',
32	\ 'line 2  foo bar blah',
33	\ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx']
34  call setline(1, text)
35  call feedkeys(":X\<CR>foobar\<CR>foobar\<CR>", 'xt')
36  call assert_equal('*****', &key)
37  w!
38  bwipe!
39  call feedkeys(":split Xtest.txt\<CR>foobar\<CR>", 'xt')
40  call assert_equal(text, getline(1, 3))
41  set key= cryptmethod&
42  bwipe!
43  call delete('Xtest.txt')
44endfunc
45
46func Test_crypt_zip()
47  call Crypt_uncrypt('zip')
48endfunc
49
50func Test_crypt_blowfish()
51  call Crypt_uncrypt('blowfish')
52endfunc
53
54func Test_crypt_blowfish2()
55  call Crypt_uncrypt('blowfish2')
56endfunc
57
58func Uncrypt_stable(method, crypted_text, key, uncrypted_text)
59  split Xtest.txt
60  set bin noeol key= fenc=latin1
61  exe "set cryptmethod=" . a:method
62  call setline(1, a:crypted_text)
63  w!
64  bwipe!
65  set nobin
66  call feedkeys(":split Xtest.txt\<CR>" . a:key . "\<CR>", 'xt')
67  call assert_equal(a:uncrypted_text, getline(1, len(a:uncrypted_text)))
68  bwipe!
69  call delete('Xtest.txt')
70  set key=
71endfunc
72
73func Test_uncrypt_zip()
74  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"])
75endfunc
76
77func Test_uncrypt_blowfish()
78  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"])
79endfunc
80
81func Test_uncrypt_blowfish2()
82  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", "!@#$%^&*()_+=-`~"])
83endfunc
84
85func Test_uncrypt_unknown_method()
86  split Xuncrypt_unknown.txt
87  set bin noeol key= fenc=latin1
88  call setline(1, "VimCrypt~93!\u001e\u00d1")
89  w!
90  bwipe!
91  set nobin
92  call assert_fails(":split Xuncrypt_unknown.txt", 'E821:')
93
94  bwipe!
95  call delete('Xuncrypt_unknown.txt')
96  set key=
97endfunc
98
99func Test_crypt_key_mismatch()
100  set cryptmethod=blowfish
101
102  split Xtest.txt
103  call setline(1, 'nothing')
104  call feedkeys(":X\<CR>foobar\<CR>nothing\<CR>", 'xt')
105  call assert_match("Keys don't match!", execute(':2messages'))
106  call assert_equal('', &key)
107  call feedkeys("\<CR>\<CR>", 'xt')
108
109  set cryptmethod&
110  bwipe!
111endfunc
112
113func Test_crypt_set_key_changes_buffer()
114
115  new Xtest1.txt
116  call setline(1, 'nothing')
117  set cryptmethod=blowfish2
118  call feedkeys(":X\<CR>foobar\<CR>foobar\<CR>", 'xt')
119  call assert_fails(":q", "E37:")
120  w
121  set key=anotherkey
122  call assert_fails(":bw")
123  w
124  call feedkeys(":X\<CR>foobar\<CR>foobar\<CR>", 'xt')
125  call assert_fails(":bw")
126  w
127  let winnr = winnr()
128  wincmd p
129  call setwinvar(winnr, '&key', 'yetanotherkey')
130  wincmd p
131  call assert_fails(":bw")
132  w
133
134  set cryptmethod&
135  set key=
136  bwipe!
137  call delete('Xtest1.txt')
138endfunc
139