blob: 97b1d4f294f00d6be903260890729d77045f06b9 [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
Evgenii Stepanovacd6f4f2018-11-06 16:48:27 -080023#include "utils.h"
24
Elliott Hughes04a83a42012-08-16 15:59:12 -070025TEST(getcwd, auto_full) {
26 // If we let the library do all the work, everything's fine.
27 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -070028 char* cwd = getcwd(nullptr, 0);
29 ASSERT_TRUE(cwd != nullptr);
Elliott Hughes5e3fc432013-02-11 16:36:48 -080030 ASSERT_EQ(0, errno);
Elliott Hughes04a83a42012-08-16 15:59:12 -070031 ASSERT_GE(strlen(cwd), 1U);
32 free(cwd);
33}
34
35TEST(getcwd, auto_reasonable) {
36 // If we ask the library to allocate a reasonable buffer, everything's fine.
37 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -070038 char* cwd = getcwd(nullptr, PATH_MAX);
39 ASSERT_TRUE(cwd != nullptr);
Elliott Hughes5e3fc432013-02-11 16:36:48 -080040 ASSERT_EQ(0, errno);
Elliott Hughes04a83a42012-08-16 15:59:12 -070041 ASSERT_GE(strlen(cwd), 1U);
42 free(cwd);
43}
44
45TEST(getcwd, auto_too_small) {
46 // If we ask the library to allocate a too-small buffer, ERANGE.
47 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -070048 char* cwd = getcwd(nullptr, 1);
49 ASSERT_TRUE(cwd == nullptr);
Elliott Hughes5e3fc432013-02-11 16:36:48 -080050 ASSERT_EQ(ERANGE, errno);
Elliott Hughes04a83a42012-08-16 15:59:12 -070051}
52
53TEST(getcwd, auto_too_large) {
Elliott Hughesbcaa4542019-03-08 15:20:23 -080054 SKIP_WITH_HWASAN << "allocation size too large";
Elliott Hughes04a83a42012-08-16 15:59:12 -070055 // If we ask the library to allocate an unreasonably large buffer, ERANGE.
56 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -070057 char* cwd = getcwd(nullptr, static_cast<size_t>(-1));
58 ASSERT_TRUE(cwd == nullptr);
Elliott Hughes5e3fc432013-02-11 16:36:48 -080059 ASSERT_EQ(ENOMEM, errno);
Elliott Hughes04a83a42012-08-16 15:59:12 -070060}
61
62TEST(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));
Yi Kong32bc0fc2018-08-02 17:31:13 -070067 ASSERT_TRUE(cwd == nullptr);
Elliott Hughes5e3fc432013-02-11 16:36:48 -080068 ASSERT_EQ(ERANGE, errno);
Elliott Hughes04a83a42012-08-16 15:59:12 -070069}
70
71TEST(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);
Yi Kong32bc0fc2018-08-02 17:31:13 -070076 ASSERT_TRUE(cwd == nullptr);
Elliott Hughes5e3fc432013-02-11 16:36:48 -080077 ASSERT_EQ(EINVAL, errno);
Elliott Hughes04a83a42012-08-16 15:59:12 -070078}
79
80TEST(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);
Elliott Hughes5e3fc432013-02-11 16:36:48 -080085 ASSERT_EQ(0, errno);
Elliott Hughes04a83a42012-08-16 15:59:12 -070086 ASSERT_GE(strlen(cwd), 1U);
87 delete[] cwd;
88}