blob: db100991ab5f31a23d32a84788ff844860b46467 [file] [log] [blame]
Bram Moolenaar680e0152016-09-25 20:54:11 +02001" Tests for encryption.
Bram Moolenaar1eceada2016-09-26 20:14:56 +02002
3if !has('cryptv')
Bram Moolenaarb0f94c12019-06-13 22:19:53 +02004 throw 'Skipped, encryption feature missing'
Bram Moolenaar1eceada2016-09-26 20:14:56 +02005endif
Bram Moolenaar680e0152016-09-25 20:54:11 +02006
7func 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!
16endfunc
17
18func Test_head_only_2()
19 call Common_head_only('VimCrypt~02!abc')
20endfunc
21
22func Test_head_only_3()
23 call Common_head_only('VimCrypt~03!abc')
24endfunc
Bram Moolenaar17777852016-09-27 21:30:22 +020025
26func 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 Moolenaar987411d2019-01-18 22:48:34 +010037 call assert_equal('*****', &key)
Bram Moolenaar17777852016-09-27 21:30:22 +020038 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')
45endfunc
46
47func Test_crypt_zip()
48 call Crypt_uncrypt('zip')
49endfunc
50
51func Test_crypt_blowfish()
52 call Crypt_uncrypt('blowfish')
53endfunc
54
55func Test_crypt_blowfish2()
56 call Crypt_uncrypt('blowfish2')
57endfunc
58
59func 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=
72endfunc
73
74func 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"])
76endfunc
77
78func 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"])
80endfunc
81
82func 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", "!@#$%^&*()_+=-`~"])
84endfunc
Bram Moolenaar987411d2019-01-18 22:48:34 +010085
86func 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=
98endfunc
99
100func 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!
112endfunc
113