mirror of
https://github.com/neovim/neovim.git
synced 2026-05-29 16:25:35 +00:00
Problem: `vim.secure.trust()` hashes an unchanged empty buffer as a newline, so trusting an empty file by buffer never works. Solution: Hash unchanged empty-buffers `''` so buffer-based trust matches the on-disk empty file.
100 lines
3.7 KiB
Lua
100 lines
3.7 KiB
Lua
local t = require('test.testutil')
|
|
local n = require('test.functional.testnvim')()
|
|
|
|
local eq = t.eq
|
|
local clear = n.clear
|
|
local command = n.command
|
|
local exec_capture = n.exec_capture
|
|
local matches = t.matches
|
|
local pathsep = n.get_pathsep()
|
|
local is_os = t.is_os
|
|
local fn = n.fn
|
|
|
|
describe(':trust', function()
|
|
local xstate = 'Xstate_ex_trust'
|
|
local test_file = 'Xtest_functional_ex_cmds_trust'
|
|
local empty_file = 'Xtest_functional_ex_cmds_trust_empty'
|
|
|
|
before_each(function()
|
|
n.mkdir_p(xstate .. pathsep .. (is_os('win') and 'nvim-data' or 'nvim'))
|
|
t.write_file(test_file, 'test')
|
|
t.write_file(empty_file, '')
|
|
clear { env = { XDG_STATE_HOME = xstate } }
|
|
end)
|
|
|
|
after_each(function()
|
|
os.remove(test_file)
|
|
os.remove(empty_file)
|
|
n.rmdir(xstate)
|
|
end)
|
|
|
|
--- @param s string
|
|
local function fmt(s)
|
|
return s:format(test_file)
|
|
end
|
|
|
|
it('is not executed when inside false condition', function()
|
|
command(fmt('edit %s'))
|
|
eq('', exec_capture('if 0 | trust | endif'))
|
|
eq(nil, vim.uv.fs_stat(fn.stdpath('state') .. pathsep .. 'trust'))
|
|
end)
|
|
|
|
it('trust then deny then remove a file using current buffer', function()
|
|
local cwd = fn.getcwd()
|
|
local hash = fn.sha256(assert(t.read_file(test_file)))
|
|
|
|
command(fmt('edit %s'))
|
|
matches(fmt('^Allowed in trust database%%: ".*%s"$'), exec_capture('trust'))
|
|
local trust = t.read_file(fn.stdpath('state') .. pathsep .. 'trust')
|
|
eq(string.format('%s %s', hash, cwd .. pathsep .. test_file), vim.trim(trust))
|
|
|
|
matches(fmt('^Denied in trust database%%: ".*%s"$'), exec_capture('trust ++deny'))
|
|
trust = t.read_file(fn.stdpath('state') .. pathsep .. 'trust')
|
|
eq(string.format('! %s', cwd .. pathsep .. test_file), vim.trim(trust))
|
|
|
|
matches(fmt('^Removed from trust database%%: ".*%s"$'), exec_capture('trust ++remove'))
|
|
trust = t.read_file(fn.stdpath('state') .. pathsep .. 'trust')
|
|
eq(string.format(''), vim.trim(trust))
|
|
end)
|
|
|
|
it('trust an empty file using current buffer', function()
|
|
local cwd = fn.getcwd()
|
|
local hash = fn.sha256(assert(t.read_file(empty_file)))
|
|
|
|
command('edit ' .. empty_file)
|
|
matches('^Allowed in trust database%: ".*' .. empty_file .. '"$', exec_capture('trust'))
|
|
local trust = t.read_file(fn.stdpath('state') .. pathsep .. 'trust')
|
|
eq(string.format('%s %s', hash, cwd .. pathsep .. empty_file), vim.trim(trust))
|
|
end)
|
|
|
|
it('deny then trust then remove a file using current buffer', function()
|
|
local cwd = fn.getcwd()
|
|
local hash = fn.sha256(assert(t.read_file(test_file)))
|
|
|
|
command(fmt('edit %s'))
|
|
matches(fmt('^Denied in trust database%%: ".*%s"$'), exec_capture('trust ++deny'))
|
|
local trust = t.read_file(fn.stdpath('state') .. pathsep .. 'trust')
|
|
eq(string.format('! %s', cwd .. pathsep .. test_file), vim.trim(trust))
|
|
|
|
matches(fmt('^Allowed in trust database%%: ".*%s"$'), exec_capture('trust'))
|
|
trust = t.read_file(fn.stdpath('state') .. pathsep .. 'trust')
|
|
eq(string.format('%s %s', hash, cwd .. pathsep .. test_file), vim.trim(trust))
|
|
|
|
matches(fmt('^Removed from trust database%%: ".*%s"$'), exec_capture('trust ++remove'))
|
|
trust = t.read_file(fn.stdpath('state') .. pathsep .. 'trust')
|
|
eq(string.format(''), vim.trim(trust))
|
|
end)
|
|
|
|
it('deny then remove a file using file path', function()
|
|
local cwd = fn.getcwd()
|
|
|
|
matches(fmt('^Denied in trust database%%: ".*%s"$'), exec_capture(fmt('trust ++deny %s')))
|
|
local trust = t.read_file(fn.stdpath('state') .. pathsep .. 'trust')
|
|
eq(string.format('! %s', cwd .. pathsep .. test_file), vim.trim(trust))
|
|
|
|
matches(fmt('^Removed from trust database%%: ".*%s"$'), exec_capture(fmt('trust ++remove %s')))
|
|
trust = t.read_file(fn.stdpath('state') .. pathsep .. 'trust')
|
|
eq(string.format(''), vim.trim(trust))
|
|
end)
|
|
end)
|