Home | History | Annotate | Line # | Download | only in test
      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 
     29 TEST_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_OK(strlen(homedir));
     38   r = uv_os_homedir(homedir, &len);
     39   ASSERT_OK(r);
     40   ASSERT_EQ(strlen(homedir), len);
     41   ASSERT_GT(len, 0);
     42   ASSERT_EQ(homedir[len], '\0');
     43 
     44 #ifdef _WIN32
     45   if (len == 3 && homedir[1] == ':')
     46     ASSERT_EQ(homedir[2], '\\');
     47   else
     48     ASSERT_NE(homedir[len - 1], '\\');
     49 #else
     50   if (len == 1)
     51     ASSERT_EQ(homedir[0], '/');
     52   else
     53     ASSERT_NE(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_EQ(r, UV_ENOBUFS);
     60   ASSERT_GT(len, SMALLPATH);
     61 
     62   /* Test invalid inputs */
     63   r = uv_os_homedir(NULL, &len);
     64   ASSERT_EQ(r, UV_EINVAL);
     65   r = uv_os_homedir(homedir, NULL);
     66   ASSERT_EQ(r, UV_EINVAL);
     67   len = 0;
     68   r = uv_os_homedir(homedir, &len);
     69   ASSERT_EQ(r, UV_EINVAL);
     70 
     71 #ifdef _WIN32
     72   /* Test empty environment variable */
     73   r = uv_os_setenv("USERPROFILE", "");
     74   ASSERT_EQ(r, 0);
     75   len = sizeof homedir;
     76   r = uv_os_homedir(homedir, &len);
     77   ASSERT_EQ(r, UV_ENOENT);
     78 #endif
     79 
     80   return 0;
     81 }
     82