users_spec.lua 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. local t = require('test.unit.testutil')
  2. local itp = t.gen_itp(it)
  3. local cimport = t.cimport
  4. local eq = t.eq
  5. local ffi = t.ffi
  6. local lib = t.lib
  7. local NULL = t.NULL
  8. local OK = t.OK
  9. local FAIL = t.FAIL
  10. local users = cimport('./src/nvim/os/os.h', 'unistd.h')
  11. local function garray_new()
  12. return ffi.new('garray_T[1]')
  13. end
  14. local function garray_get_len(array)
  15. return array[0].ga_len
  16. end
  17. local function garray_get_item(array, index)
  18. return (ffi.cast('void **', array[0].ga_data))[index]
  19. end
  20. describe('users function', function()
  21. -- will probably not work on windows
  22. local current_username = os.getenv('USER')
  23. describe('os_get_usernames', function()
  24. itp('returns FAIL if called with NULL', function()
  25. eq(FAIL, users.os_get_usernames(NULL))
  26. end)
  27. itp('fills the names garray with os usernames and returns OK', function()
  28. local ga_users = garray_new()
  29. eq(OK, users.os_get_usernames(ga_users))
  30. local user_count = garray_get_len(ga_users)
  31. assert.is_true(user_count > 0)
  32. local current_username_found = false
  33. for i = 0, user_count - 1 do
  34. local name = ffi.string((garray_get_item(ga_users, i)))
  35. if name == current_username then
  36. current_username_found = true
  37. end
  38. end
  39. assert.is_true(current_username_found)
  40. end)
  41. end)
  42. describe('os_get_username', function()
  43. itp('should write the username into the buffer and return OK', function()
  44. local name_out = ffi.new('char[100]')
  45. eq(OK, users.os_get_username(name_out, 100))
  46. eq(current_username, ffi.string(name_out))
  47. end)
  48. end)
  49. describe('os_get_uname', function()
  50. itp('should write the username into the buffer and return OK', function()
  51. local name_out = ffi.new('char[100]')
  52. local user_id = lib.getuid()
  53. eq(OK, users.os_get_uname(user_id, name_out, 100))
  54. eq(current_username, ffi.string(name_out))
  55. end)
  56. itp('should FAIL if the userid is not found', function()
  57. local name_out = ffi.new('char[100]')
  58. -- hoping nobody has this uid
  59. local user_id = 2342
  60. eq(FAIL, users.os_get_uname(user_id, name_out, 100))
  61. eq('2342', ffi.string(name_out))
  62. end)
  63. end)
  64. describe('os_get_userdir', function()
  65. itp('should return NULL if called with NULL', function()
  66. eq(NULL, users.os_get_userdir(NULL))
  67. end)
  68. itp('should return $HOME for the current user', function()
  69. local home = os.getenv('HOME')
  70. eq(home, ffi.string((users.os_get_userdir(current_username))))
  71. end)
  72. itp('should return NULL if the user is not found', function()
  73. eq(NULL, users.os_get_userdir('neovim_user_not_found_test'))
  74. end)
  75. end)
  76. end)