• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <gtest/gtest.h>
18 
19 #include <errno.h>
20 #include <limits.h>
21 #include <unistd.h>
22 
23 #include "utils.h"
24 
TEST(getcwd,auto_full)25 TEST(getcwd, auto_full) {
26   // If we let the library do all the work, everything's fine.
27   errno = 0;
28   char* cwd = getcwd(nullptr, 0);
29   ASSERT_TRUE(cwd != nullptr);
30   ASSERT_EQ(0, errno);
31   ASSERT_GE(strlen(cwd), 1U);
32   free(cwd);
33 }
34 
TEST(getcwd,auto_reasonable)35 TEST(getcwd, auto_reasonable) {
36   // If we ask the library to allocate a reasonable buffer, everything's fine.
37   errno = 0;
38   char* cwd = getcwd(nullptr, PATH_MAX);
39   ASSERT_TRUE(cwd != nullptr);
40   ASSERT_EQ(0, errno);
41   ASSERT_GE(strlen(cwd), 1U);
42   free(cwd);
43 }
44 
TEST(getcwd,auto_too_small)45 TEST(getcwd, auto_too_small) {
46   // If we ask the library to allocate a too-small buffer, ERANGE.
47   errno = 0;
48   char* cwd = getcwd(nullptr, 1);
49   ASSERT_TRUE(cwd == nullptr);
50   ASSERT_EQ(ERANGE, errno);
51 }
52 
TEST(getcwd,auto_too_large)53 TEST(getcwd, auto_too_large) {
54   SKIP_WITH_HWASAN << "allocation size too large";
55   // If we ask the library to allocate an unreasonably large buffer, ERANGE.
56   errno = 0;
57   char* cwd = getcwd(nullptr, static_cast<size_t>(-1));
58   ASSERT_TRUE(cwd == nullptr);
59   ASSERT_EQ(ENOMEM, errno);
60 }
61 
TEST(getcwd,manual_too_small)62 TEST(getcwd, manual_too_small) {
63   // If we allocate a too-small buffer, ERANGE.
64   char tiny_buf[1];
65   errno = 0;
66   char* cwd = getcwd(tiny_buf, sizeof(tiny_buf));
67   ASSERT_TRUE(cwd == nullptr);
68   ASSERT_EQ(ERANGE, errno);
69 }
70 
TEST(getcwd,manual_zero)71 TEST(getcwd, manual_zero) {
72   // If we allocate a zero-length buffer, EINVAL.
73   char tiny_buf[1];
74   errno = 0;
75   char* cwd = getcwd(tiny_buf, 0);
76   ASSERT_TRUE(cwd == nullptr);
77   ASSERT_EQ(EINVAL, errno);
78 }
79 
TEST(getcwd,manual_path_max)80 TEST(getcwd, manual_path_max) {
81   char* buf = new char[PATH_MAX];
82   errno = 0;
83   char* cwd = getcwd(buf, PATH_MAX);
84   ASSERT_TRUE(cwd == buf);
85   ASSERT_EQ(0, errno);
86   ASSERT_GE(strlen(cwd), 1U);
87   delete[] cwd;
88 }
89