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