robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
14.4 KiB415 linesraw
1local t = require('test.unit.testutil')
2local describe = t.describe
3local itp = t.gen_itp(t.it)
4
5local cimport = t.cimport
6local eq = t.eq
7local neq = t.neq
8local ffi = t.ffi
9local cstr = t.cstr
10local to_cstr = t.to_cstr
11local NULL = t.NULL
12local OK = 0
13
14local cimp = cimport('./src/nvim/os/os.h')
15
16describe('env.c', function()
17  local function os_env_exists(name, nonempty)
18    return cimp.os_env_exists(to_cstr(name), nonempty)
19  end
20
21  local function os_setenv(name, value, override)
22    return cimp.os_setenv(to_cstr(name), to_cstr(value), override)
23  end
24
25  local function os_unsetenv(name)
26    return cimp.os_unsetenv(to_cstr(name))
27  end
28
29  local function os_getenv(name)
30    local rval = cimp.os_getenv(to_cstr(name))
31    if rval ~= NULL then
32      return ffi.string(rval)
33    else
34      return NULL
35    end
36  end
37
38  local function os_getenv_buf(name, buf, bufsize)
39    local rval = cimp.os_getenv_buf(to_cstr(name), buf, bufsize)
40    if rval ~= NULL then
41      return ffi.string(rval)
42    else
43      return NULL
44    end
45  end
46
47  local function os_getenv_noalloc(name)
48    local rval = cimp.os_getenv_noalloc(to_cstr(name))
49    if rval ~= NULL then
50      return ffi.string(rval)
51    else
52      return NULL
53    end
54  end
55
56  itp('os_env_exists(..., false)', function()
57    eq(false, os_env_exists('', false))
58    eq(false, os_env_exists('      ', false))
59    eq(false, os_env_exists('\t', false))
60    eq(false, os_env_exists('\n', false))
61    eq(false, os_env_exists('AaあB <= very weird name...', false))
62
63    local varname = 'NVIM_UNIT_TEST_os_env_exists'
64    eq(false, os_env_exists(varname, false))
65    eq(OK, os_setenv(varname, 'foo bar baz ...', 1))
66    eq(true, os_env_exists(varname, false))
67    eq(OK, os_setenv(varname, 'f', 1))
68    eq(true, os_env_exists(varname, true))
69  end)
70
71  itp('os_env_exists(..., true)', function()
72    eq(false, os_env_exists('', true))
73    eq(false, os_env_exists('      ', true))
74    eq(false, os_env_exists('\t', true))
75    eq(false, os_env_exists('\n', true))
76    eq(false, os_env_exists('AaあB <= very weird name...', true))
77
78    local varname = 'NVIM_UNIT_TEST_os_env_defined'
79    eq(false, os_env_exists(varname, true))
80    eq(OK, os_setenv(varname, '', 1))
81    eq(false, os_env_exists(varname, true))
82    eq(OK, os_setenv(varname, 'foo bar baz ...', 1))
83    eq(true, os_env_exists(varname, true))
84    eq(OK, os_setenv(varname, 'f', 1))
85    eq(true, os_env_exists(varname, true))
86  end)
87
88  describe('os_setenv', function()
89    itp('sets an env var and returns success', function()
90      local name = 'NVIM_UNIT_TEST_SETENV_1N'
91      local value = 'NVIM_UNIT_TEST_SETENV_1V'
92      eq(nil, os.getenv(name))
93      eq(OK, os_setenv(name, value, 1))
94      eq(value, os.getenv(name))
95
96      -- Set empty, then set non-empty, then retrieve.
97      eq(OK, os_setenv(name, '', 1))
98      eq('', os.getenv(name))
99      eq(OK, os_setenv(name, 'non-empty', 1))
100      eq('non-empty', os.getenv(name))
101    end)
102
103    itp('`overwrite` behavior', function()
104      local name = 'NVIM_UNIT_TEST_SETENV_2N'
105      local value = 'NVIM_UNIT_TEST_SETENV_2V'
106      local value_updated = 'NVIM_UNIT_TEST_SETENV_2V_UPDATED'
107      eq(OK, os_setenv(name, value, 0))
108      eq(value, os.getenv(name))
109      eq(OK, os_setenv(name, value_updated, 0))
110      eq(value, os.getenv(name))
111      eq(OK, os_setenv(name, value_updated, 1))
112      eq(value_updated, os.getenv(name))
113    end)
114  end)
115
116  describe('os_setenv_append_path', function()
117    itp('appends :/foo/bar to $PATH', function()
118      local original_path = os.getenv('PATH')
119      eq(true, cimp.os_setenv_append_path(to_cstr('/foo/bar/baz.exe')))
120      eq(original_path .. ':/foo/bar', os.getenv('PATH'))
121    end)
122
123    itp('avoids redundant separator when appending to $PATH #7377', function()
124      os_setenv('PATH', '/a/b/c:', true)
125      eq(true, cimp.os_setenv_append_path(to_cstr('/foo/bar/baz.exe')))
126      -- Must not have duplicate separators. #7377
127      eq('/a/b/c:/foo/bar', os.getenv('PATH'))
128    end)
129
130    itp('returns false if `fname` is not absolute', function()
131      local original_path = os.getenv('PATH')
132      eq(false, cimp.os_setenv_append_path(to_cstr('foo/bar/baz.exe')))
133      eq(original_path, os.getenv('PATH'))
134    end)
135  end)
136
137  describe('os_shell_is_cmdexe', function()
138    itp('returns true for expected names', function()
139      eq(true, cimp.os_shell_is_cmdexe(to_cstr('cmd.exe')))
140      eq(true, cimp.os_shell_is_cmdexe(to_cstr('cmd')))
141      eq(true, cimp.os_shell_is_cmdexe(to_cstr('CMD.EXE')))
142      eq(true, cimp.os_shell_is_cmdexe(to_cstr('CMD')))
143
144      os_setenv('COMSPEC', '/foo/bar/cmd.exe', 0)
145      eq(true, cimp.os_shell_is_cmdexe(to_cstr('$COMSPEC')))
146      os_setenv('COMSPEC', [[C:\system32\cmd.exe]], 0)
147      eq(true, cimp.os_shell_is_cmdexe(to_cstr('$COMSPEC')))
148    end)
149    itp('returns false for unexpected names', function()
150      eq(false, cimp.os_shell_is_cmdexe(to_cstr('')))
151      eq(false, cimp.os_shell_is_cmdexe(to_cstr('powershell')))
152      eq(false, cimp.os_shell_is_cmdexe(to_cstr(' cmd.exe ')))
153      eq(false, cimp.os_shell_is_cmdexe(to_cstr('cm')))
154      eq(false, cimp.os_shell_is_cmdexe(to_cstr('md')))
155      eq(false, cimp.os_shell_is_cmdexe(to_cstr('cmd.ex')))
156
157      os_setenv('COMSPEC', '/foo/bar/cmd', 0)
158      eq(false, cimp.os_shell_is_cmdexe(to_cstr('$COMSPEC')))
159    end)
160  end)
161
162  describe('os_getenv', function()
163    itp('reads an env var', function()
164      local name = 'NVIM_UNIT_TEST_GETENV_1N'
165      local value = 'NVIM_UNIT_TEST_GETENV_1V'
166      eq(NULL, os_getenv(name))
167      -- Use os_setenv because Lua doesn't have setenv.
168      os_setenv(name, value, 1)
169      eq(value, os_getenv(name))
170
171      -- Shortest non-empty value
172      os_setenv(name, 'z', 1)
173      eq('z', os_getenv(name))
174
175      -- Get a big value.
176      local bigval = ('x'):rep(256)
177      eq(OK, os_setenv(name, bigval, 1))
178      eq(bigval, os_getenv(name))
179
180      -- Set non-empty, then set empty.
181      eq(OK, os_setenv(name, 'non-empty', 1))
182      eq('non-empty', os_getenv(name))
183      eq(OK, os_setenv(name, '', 1))
184      eq(NULL, os_getenv(name))
185    end)
186
187    itp('returns NULL if the env var is not found', function()
188      eq(NULL, os_getenv('NVIM_UNIT_TEST_GETENV_NOTFOUND'))
189    end)
190  end)
191
192  describe('os_getenv_buf', function()
193    itp('reads an env var into given buffer', function()
194      local name = 'NVIM_UNIT_TEST_GETENV_1N'
195      local value = 'NVIM_UNIT_TEST_GETENV_1V'
196      local bufsize = 200
197      local buf = cstr(bufsize, '')
198      eq(NULL, os_getenv_buf(name, buf, bufsize))
199      -- Use os_setenv because Lua doesn't have setenv.
200      os_setenv(name, value, 1)
201      eq(value, os_getenv_buf(name, buf, bufsize))
202
203      -- Shortest non-empty value
204      os_setenv(name, 'z', 1)
205      eq('z', os_getenv_buf(name, buf, bufsize))
206
207      -- Variable size above `bufsize` gets truncated
208      local verybigval = ('y'):rep(bufsize + 10)
209      local trunc = string.sub(verybigval, 0, bufsize - 1)
210      eq(OK, os_setenv(name, verybigval, 1))
211      eq(trunc, os_getenv_buf(name, buf, bufsize))
212    end)
213  end)
214
215  describe('os_getenv_noalloc', function()
216    itp('reads an env var without memory allocation', function()
217      local name = 'NVIM_UNIT_TEST_GETENV_1N'
218      local value = 'NVIM_UNIT_TEST_GETENV_1V'
219      eq(NULL, os_getenv_noalloc(name))
220      -- Use os_setenv because Lua doesn't have setenv.
221      os_setenv(name, value, 1)
222      eq(value, os_getenv_noalloc(name))
223
224      -- Shortest non-empty value
225      os_setenv(name, 'z', 1)
226      eq('z', os_getenv_noalloc(name))
227
228      local bigval = ('x'):rep(256)
229      eq(OK, os_setenv(name, bigval, 1))
230      eq(bigval, os_getenv_noalloc(name))
231
232      -- Variable size above NameBuff size gets truncated
233      -- This assumes MAXPATHL is 4096 bytes.
234      local verybigval = ('y'):rep(4096)
235      local trunc = string.sub(verybigval, 0, 4095)
236      eq(OK, os_setenv(name, verybigval, 1))
237      eq(trunc, os_getenv_noalloc(name))
238
239      -- Set non-empty, then set empty.
240      eq(OK, os_setenv(name, 'non-empty', 1))
241      eq('non-empty', os_getenv_noalloc(name))
242      eq(OK, os_setenv(name, '', 1))
243      eq(NULL, os_getenv_noalloc(name))
244    end)
245
246    itp('returns NULL if the env var is not found', function()
247      eq(NULL, os_getenv_noalloc('NVIM_UNIT_TEST_GETENV_NOTFOUND'))
248    end)
249  end)
250
251  itp('os_unsetenv', function()
252    local name = 'TEST_UNSETENV'
253    local value = 'TESTVALUE'
254    os_setenv(name, value, 1)
255    eq(OK, os_unsetenv(name))
256    neq(value, os_getenv(name))
257    -- Depending on the platform the var might be unset or set as ''
258    t.ok(os_getenv(name) == nil or os_getenv(name) == '')
259    if os_getenv(name) == nil then
260      eq(false, os_env_exists(name, false))
261    end
262  end)
263
264  describe('os_getenvname_at_index', function()
265    itp('returns names of environment variables', function()
266      local test_name = 'NVIM_UNIT_TEST_GETENVNAME_AT_INDEX_1N'
267      local test_value = 'NVIM_UNIT_TEST_GETENVNAME_AT_INDEX_1V'
268      os_setenv(test_name, test_value, 1)
269      local i = 0
270      local names = {}
271      local found_name = false
272      local name = cimp.os_getenvname_at_index(i)
273      while name ~= NULL do
274        table.insert(names, ffi.string(name))
275        if (ffi.string(name)) == test_name then
276          found_name = true
277        end
278        i = i + 1
279        name = cimp.os_getenvname_at_index(i)
280      end
281      eq(true, #names > 0)
282      eq(true, found_name)
283    end)
284
285    itp('returns NULL if the index is out of bounds', function()
286      local huge = ffi.new('size_t', 10000)
287      local maxuint32 = ffi.new('size_t', 4294967295)
288      eq(NULL, cimp.os_getenvname_at_index(huge))
289      eq(NULL, cimp.os_getenvname_at_index(maxuint32))
290
291      if ffi.abi('64bit') then
292        -- couldn't use a bigger number because it gets converted to
293        -- double somewhere, should be big enough anyway
294        -- maxuint64 = ffi.new 'size_t', 18446744073709551615
295        local maxuint64 = ffi.new('size_t', 18446744073709000000)
296        eq(NULL, cimp.os_getenvname_at_index(maxuint64))
297      end
298    end)
299  end)
300
301  describe('os_get_pid', function()
302    itp('returns the process ID', function()
303      local stat_file = io.open('/proc/self/stat')
304      if stat_file then
305        local stat_str = stat_file:read('*l')
306        stat_file:close()
307        local pid = tonumber((stat_str:match('%d+')))
308        eq(pid, tonumber(cimp.os_get_pid()))
309      else
310        -- /proc is not available on all systems, test if pid is nonzero.
311        eq(true, (cimp.os_get_pid() > 0))
312      end
313    end)
314  end)
315
316  describe('os_get_hostname', function()
317    itp('returns the hostname', function()
318      local handle = io.popen('hostname')
319      local hostname = handle:read('*l')
320      handle:close()
321      local hostname_buf = cstr(255, '')
322      cimp.os_get_hostname(hostname_buf, 255)
323      eq(hostname, (ffi.string(hostname_buf)))
324    end)
325  end)
326
327  describe('expand_env_esc', function()
328    itp('expands environment variables', function()
329      local name = 'NVIM_UNIT_TEST_EXPAND_ENV_ESCN'
330      local value = 'NVIM_UNIT_TEST_EXPAND_ENV_ESCV'
331      os_setenv(name, value, 1)
332      -- TODO(bobtwinkles) This only tests Unix expansions. There should be a
333      -- test for Windows as well
334      local input1 = to_cstr('$NVIM_UNIT_TEST_EXPAND_ENV_ESCN/test')
335      local input2 = to_cstr('${NVIM_UNIT_TEST_EXPAND_ENV_ESCN}/test')
336      local output_buff1 = cstr(255, '')
337      local output_buff2 = cstr(255, '')
338      local output_expected = 'NVIM_UNIT_TEST_EXPAND_ENV_ESCV/test'
339      cimp.expand_env_esc(input1, output_buff1, 255, NULL, true, NULL)
340      cimp.expand_env_esc(input2, output_buff2, 255, NULL, true, NULL)
341      eq(output_expected, ffi.string(output_buff1))
342      eq(output_expected, ffi.string(output_buff2))
343    end)
344
345    itp('expands ~ once when `one` is true', function()
346      local input = '~/foo ~ foo'
347      local homedir = cstr(255, '')
348      cimp.expand_env_esc(to_cstr('~'), homedir, 255, NULL, true, NULL)
349      local output_expected = ffi.string(homedir) .. '/foo ~ foo'
350      local output = cstr(255, '')
351      cimp.expand_env_esc(to_cstr(input), output, 255, NULL, true, NULL)
352      eq(ffi.string(output), ffi.string(output_expected))
353    end)
354
355    itp('expands ~ every time when `one` is false', function()
356      local input = to_cstr('~/foo ~ foo')
357      local dst = cstr(255, '')
358      cimp.expand_env_esc(to_cstr('~'), dst, 255, NULL, true, NULL)
359      local homedir = ffi.string(dst)
360      local output_expected = homedir .. '/foo ' .. homedir .. ' foo'
361      local output = cstr(255, '')
362      cimp.expand_env_esc(input, output, 255, NULL, false, NULL)
363      eq(output_expected, ffi.string(output))
364    end)
365
366    itp('does not crash #3725', function()
367      local name_out = ffi.new('char[100]')
368      cimp.os_get_username(name_out, 100)
369      local curuser = ffi.string(name_out)
370
371      local src =
372        to_cstr('~' .. curuser .. '/Vcs/django-rest-framework/rest_framework/renderers.py')
373      local dst = cstr(256, '~' .. curuser)
374      cimp.expand_env_esc(src, dst, 256, NULL, false, NULL)
375      local len = string.len(ffi.string(dst))
376      t.ok(len > 56)
377      t.ok(len < 256)
378    end)
379
380    itp('respects `dstlen` without expansion', function()
381      local input = to_cstr('this is a very long thing that will not fit')
382      -- The buffer is long enough to actually contain the full input in case the
383      -- test fails, but we don't tell expand_env_esc that
384      local output = cstr(255, '')
385      cimp.expand_env_esc(input, output, 5, NULL, true, NULL)
386      -- Make sure the first few characters are copied properly and that there is a
387      -- terminating null character
388      for i = 0, 3 do
389        eq(input[i], output[i])
390      end
391      eq(0, output[4])
392    end)
393
394    itp('respects `dstlen` with expansion', function()
395      local varname = to_cstr('NVIM_UNIT_TEST_EXPAND_ENV_ESC_DSTLENN')
396      local varval = to_cstr('NVIM_UNIT_TEST_EXPAND_ENV_ESC_DSTLENV')
397      cimp.os_setenv(varname, varval, 1)
398      -- TODO(bobtwinkles) This test uses unix-specific environment variable accessing,
399      -- should have some alternative for windows
400      local input = to_cstr('$NVIM_UNIT_TEST_EXPAND_ENV_ESC_DSTLENN/even more stuff')
401      -- The buffer is long enough to actually contain the full input in case the
402      -- test fails, but we don't tell expand_env_esc that
403      local output = cstr(255, '')
404      cimp.expand_env_esc(input, output, 5, NULL, true, NULL)
405      -- Make sure the first few characters are copied properly and that there is a
406      -- terminating null character
407      -- expand_env_esc SHOULD NOT expand the variable if there is not enough space to
408      -- contain the result
409      for i = 0, 3 do
410        eq(input[i], output[i])
411      end
412      eq(0, output[4])
413    end)
414  end)
415end)