1/* Copyright libuv project 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
26#define PATHMAX 1024
27#define SMALLPATH 1
28
29TEST_IMPL(homedir) {
30 char homedir[PATHMAX];
31 size_t len;
32 int r;
33
34 /* Test the normal case */
35 len = sizeof homedir;
36 homedir[0] = '\0';
37 ASSERT(strlen(homedir) == 0);
38 r = uv_os_homedir(homedir, &len);
39 ASSERT(r == 0);
40 ASSERT(strlen(homedir) == len);
41 ASSERT(len > 0);
42 ASSERT(homedir[len] == '\0');
43
44#ifdef _WIN32
45 if (len == 3 && homedir[1] == ':')
46 ASSERT(homedir[2] == '\\');
47 else
48 ASSERT(homedir[len - 1] != '\\');
49#else
50 if (len == 1)
51 ASSERT(homedir[0] == '/');
52 else
53 ASSERT(homedir[len - 1] != '/');
54#endif
55
56 /* Test the case where the buffer is too small */
57 len = SMALLPATH;
58 r = uv_os_homedir(homedir, &len);
59 ASSERT(r == UV_ENOBUFS);
60 ASSERT(len > SMALLPATH);
61
62 /* Test invalid inputs */
63 r = uv_os_homedir(NULL, &len);
64 ASSERT(r == UV_EINVAL);
65 r = uv_os_homedir(homedir, NULL);
66 ASSERT(r == UV_EINVAL);
67 len = 0;
68 r = uv_os_homedir(homedir, &len);
69 ASSERT(r == UV_EINVAL);
70
71 return 0;
72}
73