1/* Copyright libuv contributors. All rights reserved.
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to
5 * deal in the Software without restriction, including without limitation the
6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 * sell copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19 * IN THE SOFTWARE.
20 */
21
22#include "uv.h"
23#include "task.h"
24#include <string.h>
25
26TEST_IMPL(get_passwd) {
27 uv_passwd_t pwd;
28 size_t len;
29 int r;
30
31 /* Test the normal case */
32 r = uv_os_get_passwd(&pwd);
33 ASSERT(r == 0);
34 len = strlen(pwd.username);
35 ASSERT(len > 0);
36
37#ifdef _WIN32
38 ASSERT(pwd.shell == NULL);
39#else
40 len = strlen(pwd.shell);
41 ASSERT(len > 0);
42#endif
43
44 len = strlen(pwd.homedir);
45 ASSERT(len > 0);
46
47#ifdef _WIN32
48 if (len == 3 && pwd.homedir[1] == ':')
49 ASSERT(pwd.homedir[2] == '\\');
50 else
51 ASSERT(pwd.homedir[len - 1] != '\\');
52#else
53 if (len == 1)
54 ASSERT(pwd.homedir[0] == '/');
55 else
56 ASSERT(pwd.homedir[len - 1] != '/');
57#endif
58
59#ifdef _WIN32
60 ASSERT(pwd.uid == -1);
61 ASSERT(pwd.gid == -1);
62#else
63 ASSERT(pwd.uid >= 0);
64 ASSERT(pwd.gid >= 0);
65#endif
66
67 /* Test uv_os_free_passwd() */
68 uv_os_free_passwd(&pwd);
69
70 ASSERT(pwd.username == NULL);
71 ASSERT(pwd.shell == NULL);
72 ASSERT(pwd.homedir == NULL);
73
74 /* Test a double free */
75 uv_os_free_passwd(&pwd);
76
77 ASSERT(pwd.username == NULL);
78 ASSERT(pwd.shell == NULL);
79 ASSERT(pwd.homedir == NULL);
80
81 /* Test invalid input */
82 r = uv_os_get_passwd(NULL);
83 ASSERT(r == UV_EINVAL);
84
85 return 0;
86}
87