]> granicus.if.org Git - vim/commitdiff
patch 8.1.0773: not all crypt code is tested v8.1.0773
authorBram Moolenaar <Bram@vim.org>
Fri, 18 Jan 2019 21:48:34 +0000 (22:48 +0100)
committerBram Moolenaar <Bram@vim.org>
Fri, 18 Jan 2019 21:48:34 +0000 (22:48 +0100)
Problem:    Not all crypt code is tested.
Solution:   Disable unused crypt code.  Add more test coverage.

src/crypt.c
src/fileio.c
src/proto/crypt.pro
src/structs.h
src/testdir/test_crypt.vim
src/version.c

index 599607078ae6df93ae5ca095dfd733ab266ba47f..f82ee7bdb7c6cc5335b807bafc9aaa5099da7051 100644 (file)
@@ -34,7 +34,9 @@ typedef struct {
     char    *magic;    /* magic bytes stored in file header */
     int            salt_len;   /* length of salt, or 0 when not using salt */
     int            seed_len;   /* length of seed, or 0 when not using salt */
+#ifdef CRYPT_NOT_INPLACE
     int            works_inplace; /* encryption/decryption can be done in-place */
+#endif
     int            whole_undofile; /* whole undo file is encrypted */
 
     /* Optional function pointer for a self-test. */
@@ -80,7 +82,9 @@ static cryptmethod_T cryptmethods[CRYPT_M_COUNT] = {
        "VimCrypt~01!",
        0,
        0,
+#ifdef CRYPT_NOT_INPLACE
        TRUE,
+#endif
        FALSE,
        NULL,
        crypt_zip_init,
@@ -95,7 +99,9 @@ static cryptmethod_T cryptmethods[CRYPT_M_COUNT] = {
        "VimCrypt~02!",
        8,
        8,
+#ifdef CRYPT_NOT_INPLACE
        TRUE,
+#endif
        FALSE,
        blowfish_self_test,
        crypt_blowfish_init,
@@ -110,7 +116,9 @@ static cryptmethod_T cryptmethods[CRYPT_M_COUNT] = {
        "VimCrypt~03!",
        8,
        8,
+#ifdef CRYPT_NOT_INPLACE
        TRUE,
+#endif
        TRUE,
        blowfish_self_test,
        crypt_blowfish_init,
@@ -167,6 +175,7 @@ crypt_method_nr_from_magic(char *ptr, int len)
     return -1;
 }
 
+#ifdef CRYPT_NOT_INPLACE
 /*
  * Return TRUE if the crypt method for "method_nr" can be done in-place.
  */
@@ -175,6 +184,7 @@ crypt_works_inplace(cryptstate_T *state)
 {
     return cryptmethods[state->method_nr].works_inplace;
 }
+#endif
 
 /*
  * Get the crypt method for buffer "buf" as a number.
@@ -366,6 +376,7 @@ crypt_free_state(cryptstate_T *state)
     vim_free(state);
 }
 
+#ifdef CRYPT_NOT_INPLACE
 /*
  * Encode "from[len]" and store the result in a newly allocated buffer, which
  * is stored in "newptr".
@@ -422,6 +433,7 @@ crypt_decode_alloc(
     method->decode_fn(state, ptr, len, *newptr);
     return len;
 }
+#endif
 
 /*
  * Encrypting "from[len]" into "to[len]".
@@ -436,6 +448,7 @@ crypt_encode(
     cryptmethods[state->method_nr].encode_fn(state, from, len, to);
 }
 
+#if 0  // unused
 /*
  * decrypting "from[len]" into "to[len]".
  */
@@ -448,6 +461,7 @@ crypt_decode(
 {
     cryptmethods[state->method_nr].decode_fn(state, from, len, to);
 }
+#endif
 
 /*
  * Simple inplace encryption, modifies "buf[len]" in place.
index f6a799515efad1dea7993184dbcb06cc4e64a775..23fe504527c000118072479d47a07aea4c7b9da7 100644 (file)
@@ -1381,9 +1381,12 @@ retry:
                if (cryptkey != NULL && curbuf->b_cryptstate != NULL
                                                                   && size > 0)
                {
+# ifdef CRYPT_NOT_INPLACE
                    if (crypt_works_inplace(curbuf->b_cryptstate))
                    {
+# endif
                        crypt_decode_inplace(curbuf->b_cryptstate, ptr, size);
+# ifdef CRYPT_NOT_INPLACE
                    }
                    else
                    {
@@ -1434,6 +1437,7 @@ retry:
                        }
                        size = decrypted_size;
                    }
+# endif
                }
 #endif
 
@@ -5768,9 +5772,12 @@ buf_write_bytes(struct bw_info *ip)
     {
        /* Encrypt the data. Do it in-place if possible, otherwise use an
         * allocated buffer. */
+# ifdef CRYPT_NOT_INPLACE
        if (crypt_works_inplace(ip->bw_buffer->b_cryptstate))
        {
+# endif
            crypt_encode_inplace(ip->bw_buffer->b_cryptstate, buf, len);
+# ifdef CRYPT_NOT_INPLACE
        }
        else
        {
@@ -5783,6 +5790,7 @@ buf_write_bytes(struct bw_info *ip)
            vim_free(outbuf);
            return (wlen < len) ? FAIL : OK;
        }
+# endif
     }
 #endif
 
index c944b45cef2210e5b6b535d4670d001ac9b12563..e8ba18897ce2101fafac6e8d90d5d68544377624 100644 (file)
@@ -1,7 +1,6 @@
 /* crypt.c */
 int crypt_method_nr_from_name(char_u *name);
 int crypt_method_nr_from_magic(char *ptr, int len);
-int crypt_works_inplace(cryptstate_T *state);
 int crypt_get_method_nr(buf_T *buf);
 int crypt_whole_undofile(int method_nr);
 int crypt_get_header_len(int method_nr);
@@ -12,10 +11,7 @@ cryptstate_T *crypt_create_from_header(int method_nr, char_u *key, char_u *heade
 cryptstate_T *crypt_create_from_file(FILE *fp, char_u *key);
 cryptstate_T *crypt_create_for_writing(int method_nr, char_u *key, char_u **header, int *header_len);
 void crypt_free_state(cryptstate_T *state);
-long crypt_encode_alloc(cryptstate_T *state, char_u *from, size_t len, char_u **newptr);
-long crypt_decode_alloc(cryptstate_T *state, char_u *ptr, long len, char_u **newptr);
 void crypt_encode(cryptstate_T *state, char_u *from, size_t len, char_u *to);
-void crypt_decode(cryptstate_T *state, char_u *from, size_t len, char_u *to);
 void crypt_encode_inplace(cryptstate_T *state, char_u *buf, size_t len);
 void crypt_decode_inplace(cryptstate_T *state, char_u *buf, size_t len);
 void crypt_free_key(char_u *key);
index 958a5d22e2bcbf0df13d7b98a86aa8c306370c46..5f148a0ff56fb79570f68e27a6d56cc29d5f793c 100644 (file)
@@ -1940,6 +1940,10 @@ typedef struct {
 # define CRYPT_M_BF    1
 # define CRYPT_M_BF2   2
 # define CRYPT_M_COUNT 3 /* number of crypt methods */
+
+// Currently all crypt methods work inplace.  If one is added that isn't then
+// define this.
+//  # define CRYPT_NOT_INPLACE 1
 #endif
 
 
index 4d77a9a9bfe374a18af61daf0a57f149a4cf9dc3..bf1a51111807bbd1c93e30ff295545eeef564a76 100644 (file)
@@ -34,6 +34,7 @@ func Crypt_uncrypt(method)
        \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx']
   call setline(1, text)
   call feedkeys(":X\<CR>foobar\<CR>foobar\<CR>", 'xt')
+  call assert_equal('*****', &key)
   w!
   bwipe!
   call feedkeys(":split Xtest.txt\<CR>foobar\<CR>", 'xt')
@@ -81,3 +82,32 @@ endfunc
 func Test_uncrypt_blowfish2()
   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", "!@#$%^&*()_+=-`~"])
 endfunc
+
+func Test_uncrypt_unknown_method()
+  split Xuncrypt_unknown.txt
+  set bin noeol key= fenc=latin1
+  call setline(1, "VimCrypt~93!\u001e\u00d1")
+  w!
+  bwipe!
+  set nobin
+  call assert_fails(":split Xuncrypt_unknown.txt", 'E821:')
+
+  bwipe!
+  call delete('Xuncrypt_unknown.txt')
+  set key=
+endfunc
+
+func Test_crypt_key_mismatch()
+  set cryptmethod=blowfish
+
+  split Xtest.txt
+  call setline(1, 'nothing')
+  call feedkeys(":X\<CR>foobar\<CR>nothing\<CR>", 'xt')
+  call assert_match("Keys don't match!", execute(':2messages'))
+  call assert_equal('', &key)
+  call feedkeys("\<CR>\<CR>", 'xt')
+
+  set cryptmethod&
+  bwipe!
+endfunc
+
index 84d583146b759b6816a3192329f03fdc8f1328ec..ac81d2edbe978939e4346dd284d1445c12c8a234 100644 (file)
@@ -791,6 +791,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    773,
 /**/
     772,
 /**/