From 59cb041d0a56d8555857da7e063ec61504ee1fa7 Mon Sep 17 00:00:00 2001
From: Bram Moolenaar <Bram@vim.org>
Date: Wed, 18 Dec 2019 22:26:31 +0100
Subject: [PATCH] patch 8.2.0023: command line editing not sufficiently tested
Problem:    Command line editing not sufficiently tested.
Solution:   Add more tests. (Dominique Pelle, closes #5374)
Reference:https://github.com/vim/vim/commit/59cb041d0a56d8555857da7e063ec61504ee1fa7
Conflict:don't change src/testdir/test_cmdline.vim file. isrc/testdir/test_ex_mode.vim file 
needs to be introduced to fix the CVE-2022-0359，so only change src/testdir/test_ex_mode.vim
---
 src/testdir/Make_all.mak     |  1 +
 src/testdir/test_alot.vim    |  1 +
 src/testdir/test_ex_mode.vim | 54 ++++++++++++++++++++++++++++++++++++
 3 files changed, 56 insertions(+)
 create mode 100644 src/testdir/test_ex_mode.vim
diff --git a/src/testdir/Make_all.mak b/src/testdir/Make_all.mak
index 665bcc7..05e7a2c 100644
--- a/src/testdir/Make_all.mak
+++ b/src/testdir/Make_all.mak
@@ -102,6 +102,7 @@ NEW_TESTS = \
 	test_ex_equal \
 	test_ex_undo \
 	test_ex_z \
+	test_ex_mode \
 	test_excmd \
 	test_exec_while_if \
 	test_execute_func \
diff --git a/src/testdir/test_alot.vim b/src/testdir/test_alot.vim
index 894ec58..25241b2 100644
--- a/src/testdir/test_alot.vim
+++ b/src/testdir/test_alot.vim
@@ -13,6 +13,7 @@ source test_delete.vim
 source test_ex_equal.vim
 source test_ex_undo.vim
 source test_ex_z.vim
+source test_ex_mode.vim
 source test_execute_func.vim
 source test_expand.vim
 source test_expand_dllpath.vim
diff --git a/src/testdir/test_ex_mode.vim b/src/testdir/test_ex_mode.vim
new file mode 100644
index 0000000..00a35a3
--- /dev/null
+++ b/src/testdir/test_ex_mode.vim
@@ -0,0 +1,54 @@
+" Test editing line in Ex mode (see :help Q and :help gQ).
+
+" Helper function to test editing line in Q Ex mode
+func Ex_Q(cmd)
+  " Is there a simpler way to test editing Ex line?
+  call feedkeys("Q"
+        \    .. "let s:test_ex =<< END\<CR>"
+        \    .. a:cmd .. "\<CR>"
+        \    .. "END\<CR>"
+        \    .. "visual\<CR>", 'tx')
+  return s:test_ex[0]
+endfunc
+
+" Helper function to test editing line in gQ Ex mode
+func Ex_gQ(cmd)
+  call feedkeys("gQ" .. a:cmd .. "\<C-b>\"\<CR>", 'tx')
+  let ret = @:[1:] " Remove leading quote.
+  call feedkeys("visual\<CR>", 'tx')
+  return ret
+endfunc
+
+" Helper function to test editing line with both Q and gQ Ex mode.
+func Ex(cmd)
+ return [Ex_Q(a:cmd), Ex_gQ(a:cmd)]
+endfunc
+
+" Test editing line in Ex mode (both Q and gQ)
+func Test_ex_mode()
+  let encoding_save = &encoding
+  set sw=2
+
+  for e in ['utf8', 'latin1']
+    exe 'set encoding=' . e
+
+    call assert_equal(['bar', 'bar'],             Ex("foo bar\<C-u>bar"), e)
+    call assert_equal(["1\<C-u>2", "1\<C-u>2"],   Ex("1\<C-v>\<C-u>2"), e)
+    call assert_equal(["1\<C-b>2\<C-e>3", '213'], Ex("1\<C-b>2\<C-e>3"), e)
+    call assert_equal(['0123', '2013'],           Ex("01\<Home>2\<End>3"), e)
+    call assert_equal(['0123', '0213'],           Ex("01\<Left>2\<Right>3"), e)
+    call assert_equal(['01234', '0342'],          Ex("012\<Left>\<Left>\<Insert>3\<Insert>4"), e)
+    call assert_equal(["foo bar\<C-w>", 'foo '],  Ex("foo bar\<C-w>"), e)
+    call assert_equal(['foo', 'foo'],             Ex("fooba\<Del>\<Del>"), e)
+    call assert_equal(["foo\tbar", 'foobar'],     Ex("foo\<Tab>bar"), e)
+    call assert_equal(["abbrev\t", 'abbreviate'], Ex("abbrev\<Tab>"), e)
+    call assert_equal(['    1', "1\<C-t>\<C-t>"], Ex("1\<C-t>\<C-t>"), e)
+    call assert_equal(['  1', "1\<C-t>\<C-t>"],   Ex("1\<C-t>\<C-t>\<C-d>"), e)
+    call assert_equal(['  foo', '    foo'],       Ex("    foo\<C-d>"), e)
+    call assert_equal(['foo', '    foo0'],        Ex("    foo0\<C-d>"), e)
+    call assert_equal(['foo', '    foo^'],        Ex("    foo^\<C-d>"), e)
+  endfor
+
+  set sw&
+  let &encoding = encoding_save
+endfunc
-- 
2.27.0
