Elliott Hughes | 04a83a4 | 2012-08-16 15:59:12 -0700 | [diff] [blame] | 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 | |
Florian Mayer | 750dcd3 | 2022-04-15 15:54:47 -0700 | [diff] [blame] | 23 | #include <android-base/test_utils.h> |
| 24 | |
Evgenii Stepanov | acd6f4f | 2018-11-06 16:48:27 -0800 | [diff] [blame] | 25 | #include "utils.h" |
| 26 | |
Elliott Hughes | 04a83a4 | 2012-08-16 15:59:12 -0700 | [diff] [blame] | 27 | TEST(getcwd, auto_full) { |
| 28 | // If we let the library do all the work, everything's fine. |
| 29 | errno = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 30 | char* cwd = getcwd(nullptr, 0); |
| 31 | ASSERT_TRUE(cwd != nullptr); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 32 | ASSERT_ERRNO(0); |
Elliott Hughes | 04a83a4 | 2012-08-16 15:59:12 -0700 | [diff] [blame] | 33 | ASSERT_GE(strlen(cwd), 1U); |
| 34 | free(cwd); |
| 35 | } |
| 36 | |
| 37 | TEST(getcwd, auto_reasonable) { |
| 38 | // If we ask the library to allocate a reasonable buffer, everything's fine. |
| 39 | errno = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 40 | char* cwd = getcwd(nullptr, PATH_MAX); |
| 41 | ASSERT_TRUE(cwd != nullptr); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 42 | ASSERT_ERRNO(0); |
Elliott Hughes | 04a83a4 | 2012-08-16 15:59:12 -0700 | [diff] [blame] | 43 | ASSERT_GE(strlen(cwd), 1U); |
| 44 | free(cwd); |
| 45 | } |
| 46 | |
| 47 | TEST(getcwd, auto_too_small) { |
| 48 | // If we ask the library to allocate a too-small buffer, ERANGE. |
| 49 | errno = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 50 | char* cwd = getcwd(nullptr, 1); |
| 51 | ASSERT_TRUE(cwd == nullptr); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 52 | ASSERT_ERRNO(ERANGE); |
Elliott Hughes | 04a83a4 | 2012-08-16 15:59:12 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | TEST(getcwd, auto_too_large) { |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 56 | SKIP_WITH_HWASAN << "allocation size too large"; |
Elliott Hughes | 04a83a4 | 2012-08-16 15:59:12 -0700 | [diff] [blame] | 57 | // If we ask the library to allocate an unreasonably large buffer, ERANGE. |
| 58 | errno = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 59 | char* cwd = getcwd(nullptr, static_cast<size_t>(-1)); |
| 60 | ASSERT_TRUE(cwd == nullptr); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 61 | ASSERT_ERRNO(ENOMEM); |
Elliott Hughes | 04a83a4 | 2012-08-16 15:59:12 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | TEST(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 Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 69 | ASSERT_TRUE(cwd == nullptr); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 70 | ASSERT_ERRNO(ERANGE); |
Elliott Hughes | 04a83a4 | 2012-08-16 15:59:12 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | TEST(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 Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 78 | ASSERT_TRUE(cwd == nullptr); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 79 | ASSERT_ERRNO(EINVAL); |
Elliott Hughes | 04a83a4 | 2012-08-16 15:59:12 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | TEST(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 Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 87 | ASSERT_ERRNO(0); |
Elliott Hughes | 04a83a4 | 2012-08-16 15:59:12 -0700 | [diff] [blame] | 88 | ASSERT_GE(strlen(cwd), 1U); |
| 89 | delete[] cwd; |
| 90 | } |