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 4096
27 #define SMALLPATH 1
28
TEST_IMPL(tmpdir)29 TEST_IMPL(tmpdir) {
30 char tmpdir[PATHMAX];
31 size_t len;
32 char last;
33 int r;
34
35 /* Test the normal case */
36 len = sizeof tmpdir;
37 tmpdir[0] = '\0';
38
39 ASSERT_OK(strlen(tmpdir));
40 r = uv_os_tmpdir(tmpdir, &len);
41 ASSERT_OK(r);
42 ASSERT_EQ(strlen(tmpdir), len);
43 ASSERT_GT(len, 0);
44 ASSERT_EQ(tmpdir[len], '\0');
45
46 if (len > 1) {
47 last = tmpdir[len - 1];
48 #ifdef _WIN32
49 ASSERT_NE(last, '\\');
50 #else
51 ASSERT_NE(last, '/');
52 #endif
53 }
54
55 /* Test the case where the buffer is too small */
56 len = SMALLPATH;
57 r = uv_os_tmpdir(tmpdir, &len);
58 ASSERT_EQ(r, UV_ENOBUFS);
59 ASSERT_GT(len, SMALLPATH);
60
61 /* Test invalid inputs */
62 r = uv_os_tmpdir(NULL, &len);
63 ASSERT_EQ(r, UV_EINVAL);
64 r = uv_os_tmpdir(tmpdir, NULL);
65 ASSERT_EQ(r, UV_EINVAL);
66 len = 0;
67 r = uv_os_tmpdir(tmpdir, &len);
68 ASSERT_EQ(r, UV_EINVAL);
69
70 #ifdef _WIN32
71 const char *name = "TMP";
72 char tmpdir_win[] = "C:\\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
73 r = uv_os_setenv(name, tmpdir_win);
74 ASSERT_OK(r);
75 char tmpdirx[PATHMAX];
76 size_t lenx = sizeof tmpdirx;
77 r = uv_os_tmpdir(tmpdirx, &lenx);
78 ASSERT_OK(r);
79
80 /* Test empty environment variable */
81 r = uv_os_setenv("TMP", "");
82 ASSERT_EQ(r, 0);
83 len = sizeof tmpdir;
84 r = uv_os_tmpdir(tmpdir, &len);
85 ASSERT_EQ(r, UV_ENOENT);
86 #endif
87
88 return 0;
89 }
90