blob: 26ea75f596458be355f918a3fc9f2c4df1619f7f [file] [log] [blame]
Elliott Hughes04a83a42012-08-16 15:59:12 -07001/*
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
Florian Mayer750dcd32022-04-15 15:54:47 -070023#include <android-base/test_utils.h>
24
Evgenii Stepanovacd6f4f2018-11-06 16:48:27 -080025#include "utils.h"
26
Elliott Hughes04a83a42012-08-16 15:59:12 -070027TEST(getcwd, auto_full) {
28 // If we let the library do all the work, everything's fine.
29 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -070030 char* cwd = getcwd(nullptr, 0);
31 ASSERT_TRUE(cwd != nullptr);
Elliott Hughes95646e62023-09-21 14:11:19 -070032 ASSERT_ERRNO(0);
Elliott Hughes04a83a42012-08-16 15:59:12 -070033 ASSERT_GE(strlen(cwd), 1U);
34 free(cwd);
35}
36
37TEST(getcwd, auto_reasonable) {
38 // If we ask the library to allocate a reasonable buffer, everything's fine.
39 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -070040 char* cwd = getcwd(nullptr, PATH_MAX);
41 ASSERT_TRUE(cwd != nullptr);
Elliott Hughes95646e62023-09-21 14:11:19 -070042 ASSERT_ERRNO(0);
Elliott Hughes04a83a42012-08-16 15:59:12 -070043 ASSERT_GE(strlen(cwd), 1U);
44 free(cwd);
45}
46
47TEST(getcwd, auto_too_small) {
48 // If we ask the library to allocate a too-small buffer, ERANGE.
49 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -070050 char* cwd = getcwd(nullptr, 1);
51 ASSERT_TRUE(cwd == nullptr);
Elliott Hughes95646e62023-09-21 14:11:19 -070052 ASSERT_ERRNO(ERANGE);
Elliott Hughes04a83a42012-08-16 15:59:12 -070053}
54
55TEST(getcwd, auto_too_large) {
Elliott Hughesbcaa4542019-03-08 15:20:23 -080056 SKIP_WITH_HWASAN << "allocation size too large";
Elliott Hughes04a83a42012-08-16 15:59:12 -070057 // If we ask the library to allocate an unreasonably large buffer, ERANGE.
58 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -070059 char* cwd = getcwd(nullptr, static_cast<size_t>(-1));
60 ASSERT_TRUE(cwd == nullptr);
Elliott Hughes95646e62023-09-21 14:11:19 -070061 ASSERT_ERRNO(ENOMEM);
Elliott Hughes04a83a42012-08-16 15:59:12 -070062}
63
64TEST(getcwd, manual_too_small) {
65 // If we allocate a too-small buffer, ERANGE.
66 char tiny_buf[1];
67 errno = 0;
68 char* cwd = getcwd(tiny_buf, sizeof(tiny_buf));
Yi Kong32bc0fc2018-08-02 17:31:13 -070069 ASSERT_TRUE(cwd == nullptr);
Elliott Hughes95646e62023-09-21 14:11:19 -070070 ASSERT_ERRNO(ERANGE);
Elliott Hughes04a83a42012-08-16 15:59:12 -070071}
72
73TEST(getcwd, manual_zero) {
74 // If we allocate a zero-length buffer, EINVAL.
75 char tiny_buf[1];
76 errno = 0;
77 char* cwd = getcwd(tiny_buf, 0);
Yi Kong32bc0fc2018-08-02 17:31:13 -070078 ASSERT_TRUE(cwd == nullptr);
Elliott Hughes95646e62023-09-21 14:11:19 -070079 ASSERT_ERRNO(EINVAL);
Elliott Hughes04a83a42012-08-16 15:59:12 -070080}
81
82TEST(getcwd, manual_path_max) {
83 char* buf = new char[PATH_MAX];
84 errno = 0;
85 char* cwd = getcwd(buf, PATH_MAX);
86 ASSERT_TRUE(cwd == buf);
Elliott Hughes95646e62023-09-21 14:11:19 -070087 ASSERT_ERRNO(0);
Elliott Hughes04a83a42012-08-16 15:59:12 -070088 ASSERT_GE(strlen(cwd), 1U);
89 delete[] cwd;
90}