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 | |
Evgenii Stepanov | acd6f4f | 2018-11-06 16:48:27 -0800 | [diff] [blame] | 23 | #include "utils.h" |
| 24 | |
Elliott Hughes | 04a83a4 | 2012-08-16 15:59:12 -0700 | [diff] [blame] | 25 | TEST(getcwd, auto_full) { |
| 26 | // If we let the library do all the work, everything's fine. |
| 27 | errno = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 28 | char* cwd = getcwd(nullptr, 0); |
| 29 | ASSERT_TRUE(cwd != nullptr); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 30 | ASSERT_EQ(0, errno); |
Elliott Hughes | 04a83a4 | 2012-08-16 15:59:12 -0700 | [diff] [blame] | 31 | ASSERT_GE(strlen(cwd), 1U); |
| 32 | free(cwd); |
| 33 | } |
| 34 | |
| 35 | TEST(getcwd, auto_reasonable) { |
| 36 | // If we ask the library to allocate a reasonable buffer, everything's fine. |
| 37 | errno = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 38 | char* cwd = getcwd(nullptr, PATH_MAX); |
| 39 | ASSERT_TRUE(cwd != nullptr); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 40 | ASSERT_EQ(0, errno); |
Elliott Hughes | 04a83a4 | 2012-08-16 15:59:12 -0700 | [diff] [blame] | 41 | ASSERT_GE(strlen(cwd), 1U); |
| 42 | free(cwd); |
| 43 | } |
| 44 | |
| 45 | TEST(getcwd, auto_too_small) { |
| 46 | // If we ask the library to allocate a too-small buffer, ERANGE. |
| 47 | errno = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 48 | char* cwd = getcwd(nullptr, 1); |
| 49 | ASSERT_TRUE(cwd == nullptr); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 50 | ASSERT_EQ(ERANGE, errno); |
Elliott Hughes | 04a83a4 | 2012-08-16 15:59:12 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | TEST(getcwd, auto_too_large) { |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame^] | 54 | SKIP_WITH_HWASAN << "allocation size too large"; |
Elliott Hughes | 04a83a4 | 2012-08-16 15:59:12 -0700 | [diff] [blame] | 55 | // If we ask the library to allocate an unreasonably large buffer, ERANGE. |
| 56 | errno = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 57 | char* cwd = getcwd(nullptr, static_cast<size_t>(-1)); |
| 58 | ASSERT_TRUE(cwd == nullptr); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 59 | ASSERT_EQ(ENOMEM, errno); |
Elliott Hughes | 04a83a4 | 2012-08-16 15:59:12 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 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)); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 67 | ASSERT_TRUE(cwd == nullptr); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 68 | ASSERT_EQ(ERANGE, errno); |
Elliott Hughes | 04a83a4 | 2012-08-16 15:59:12 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 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); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 76 | ASSERT_TRUE(cwd == nullptr); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 77 | ASSERT_EQ(EINVAL, errno); |
Elliott Hughes | 04a83a4 | 2012-08-16 15:59:12 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 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); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 85 | ASSERT_EQ(0, errno); |
Elliott Hughes | 04a83a4 | 2012-08-16 15:59:12 -0700 | [diff] [blame] | 86 | ASSERT_GE(strlen(cwd), 1U); |
| 87 | delete[] cwd; |
| 88 | } |