2012-08-17 00:59:12 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2022-04-16 00:54:47 +02:00
|
|
|
#include <android-base/test_utils.h>
|
|
|
|
|
2018-11-07 01:48:27 +01:00
|
|
|
#include "utils.h"
|
|
|
|
|
2012-08-17 00:59:12 +02:00
|
|
|
TEST(getcwd, auto_full) {
|
|
|
|
// If we let the library do all the work, everything's fine.
|
|
|
|
errno = 0;
|
2018-08-03 02:31:13 +02:00
|
|
|
char* cwd = getcwd(nullptr, 0);
|
|
|
|
ASSERT_TRUE(cwd != nullptr);
|
2023-09-21 23:11:19 +02:00
|
|
|
ASSERT_ERRNO(0);
|
2012-08-17 00:59:12 +02:00
|
|
|
ASSERT_GE(strlen(cwd), 1U);
|
|
|
|
free(cwd);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(getcwd, auto_reasonable) {
|
|
|
|
// If we ask the library to allocate a reasonable buffer, everything's fine.
|
|
|
|
errno = 0;
|
2018-08-03 02:31:13 +02:00
|
|
|
char* cwd = getcwd(nullptr, PATH_MAX);
|
|
|
|
ASSERT_TRUE(cwd != nullptr);
|
2023-09-21 23:11:19 +02:00
|
|
|
ASSERT_ERRNO(0);
|
2012-08-17 00:59:12 +02:00
|
|
|
ASSERT_GE(strlen(cwd), 1U);
|
|
|
|
free(cwd);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(getcwd, auto_too_small) {
|
|
|
|
// If we ask the library to allocate a too-small buffer, ERANGE.
|
|
|
|
errno = 0;
|
2018-08-03 02:31:13 +02:00
|
|
|
char* cwd = getcwd(nullptr, 1);
|
|
|
|
ASSERT_TRUE(cwd == nullptr);
|
2023-09-21 23:11:19 +02:00
|
|
|
ASSERT_ERRNO(ERANGE);
|
2012-08-17 00:59:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(getcwd, auto_too_large) {
|
2019-03-09 00:20:23 +01:00
|
|
|
SKIP_WITH_HWASAN << "allocation size too large";
|
2012-08-17 00:59:12 +02:00
|
|
|
// If we ask the library to allocate an unreasonably large buffer, ERANGE.
|
|
|
|
errno = 0;
|
2018-08-03 02:31:13 +02:00
|
|
|
char* cwd = getcwd(nullptr, static_cast<size_t>(-1));
|
|
|
|
ASSERT_TRUE(cwd == nullptr);
|
2023-09-21 23:11:19 +02:00
|
|
|
ASSERT_ERRNO(ENOMEM);
|
2012-08-17 00:59:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(getcwd, manual_too_small) {
|
|
|
|
// If we allocate a too-small buffer, ERANGE.
|
|
|
|
char tiny_buf[1];
|
|
|
|
errno = 0;
|
|
|
|
char* cwd = getcwd(tiny_buf, sizeof(tiny_buf));
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_TRUE(cwd == nullptr);
|
2023-09-21 23:11:19 +02:00
|
|
|
ASSERT_ERRNO(ERANGE);
|
2012-08-17 00:59:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(getcwd, manual_zero) {
|
|
|
|
// If we allocate a zero-length buffer, EINVAL.
|
|
|
|
char tiny_buf[1];
|
|
|
|
errno = 0;
|
|
|
|
char* cwd = getcwd(tiny_buf, 0);
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_TRUE(cwd == nullptr);
|
2023-09-21 23:11:19 +02:00
|
|
|
ASSERT_ERRNO(EINVAL);
|
2012-08-17 00:59:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(getcwd, manual_path_max) {
|
|
|
|
char* buf = new char[PATH_MAX];
|
|
|
|
errno = 0;
|
|
|
|
char* cwd = getcwd(buf, PATH_MAX);
|
|
|
|
ASSERT_TRUE(cwd == buf);
|
2023-09-21 23:11:19 +02:00
|
|
|
ASSERT_ERRNO(0);
|
2012-08-17 00:59:12 +02:00
|
|
|
ASSERT_GE(strlen(cwd), 1U);
|
|
|
|
delete[] cwd;
|
|
|
|
}
|