2024-04-09 04:26:16 -07:00
|
|
|
local t = require('test.unit.testutil')
|
2024-04-08 02:03:20 -07:00
|
|
|
local itp = t.gen_itp(it)
|
2014-08-30 07:47:55 -07:00
|
|
|
|
2024-04-08 02:03:20 -07:00
|
|
|
local cimport = t.cimport
|
|
|
|
local eq = t.eq
|
|
|
|
local ffi = t.ffi
|
|
|
|
local lib = t.lib
|
|
|
|
local NULL = t.NULL
|
|
|
|
local OK = t.OK
|
|
|
|
local FAIL = t.FAIL
|
2014-08-30 07:47:55 -07:00
|
|
|
|
|
|
|
local users = cimport('./src/nvim/os/os.h', 'unistd.h')
|
|
|
|
|
2015-11-17 07:49:12 -07:00
|
|
|
local function garray_new()
|
2014-08-30 07:47:55 -07:00
|
|
|
return ffi.new('garray_T[1]')
|
|
|
|
end
|
|
|
|
|
2015-11-17 07:49:12 -07:00
|
|
|
local function garray_get_len(array)
|
2014-08-30 07:47:55 -07:00
|
|
|
return array[0].ga_len
|
|
|
|
end
|
|
|
|
|
2015-11-17 07:49:12 -07:00
|
|
|
local function garray_get_item(array, index)
|
2014-08-30 07:47:55 -07:00
|
|
|
return (ffi.cast('void **', array[0].ga_data))[index]
|
|
|
|
end
|
|
|
|
|
|
|
|
describe('users function', function()
|
|
|
|
-- will probably not work on windows
|
|
|
|
local current_username = os.getenv('USER')
|
|
|
|
|
|
|
|
describe('os_get_usernames', function()
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('returns FAIL if called with NULL', function()
|
2014-08-30 07:47:55 -07:00
|
|
|
eq(FAIL, users.os_get_usernames(NULL))
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('fills the names garray with os usernames and returns OK', function()
|
2014-08-30 07:47:55 -07:00
|
|
|
local ga_users = garray_new()
|
|
|
|
eq(OK, users.os_get_usernames(ga_users))
|
|
|
|
local user_count = garray_get_len(ga_users)
|
|
|
|
assert.is_true(user_count > 0)
|
|
|
|
local current_username_found = false
|
|
|
|
for i = 0, user_count - 1 do
|
|
|
|
local name = ffi.string((garray_get_item(ga_users, i)))
|
|
|
|
if name == current_username then
|
|
|
|
current_username_found = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert.is_true(current_username_found)
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2022-06-30 04:16:46 -07:00
|
|
|
describe('os_get_username', function()
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('should write the username into the buffer and return OK', function()
|
2014-08-30 07:47:55 -07:00
|
|
|
local name_out = ffi.new('char[100]')
|
2022-06-30 04:16:46 -07:00
|
|
|
eq(OK, users.os_get_username(name_out, 100))
|
2014-08-30 07:47:55 -07:00
|
|
|
eq(current_username, ffi.string(name_out))
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe('os_get_uname', function()
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('should write the username into the buffer and return OK', function()
|
2014-08-30 07:47:55 -07:00
|
|
|
local name_out = ffi.new('char[100]')
|
|
|
|
local user_id = lib.getuid()
|
|
|
|
eq(OK, users.os_get_uname(user_id, name_out, 100))
|
|
|
|
eq(current_username, ffi.string(name_out))
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('should FAIL if the userid is not found', function()
|
2014-08-30 07:47:55 -07:00
|
|
|
local name_out = ffi.new('char[100]')
|
|
|
|
-- hoping nobody has this uid
|
|
|
|
local user_id = 2342
|
|
|
|
eq(FAIL, users.os_get_uname(user_id, name_out, 100))
|
|
|
|
eq('2342', ffi.string(name_out))
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2022-06-30 04:16:46 -07:00
|
|
|
describe('os_get_userdir', function()
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('should return NULL if called with NULL', function()
|
2022-06-30 04:16:46 -07:00
|
|
|
eq(NULL, users.os_get_userdir(NULL))
|
2014-08-30 07:47:55 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('should return $HOME for the current user', function()
|
2014-08-30 07:47:55 -07:00
|
|
|
local home = os.getenv('HOME')
|
2022-06-30 04:16:46 -07:00
|
|
|
eq(home, ffi.string((users.os_get_userdir(current_username))))
|
2014-08-30 07:47:55 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('should return NULL if the user is not found', function()
|
2022-06-30 04:16:46 -07:00
|
|
|
eq(NULL, users.os_get_userdir('neovim_user_not_found_test'))
|
2014-08-30 07:47:55 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
end)
|