104a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes/* 204a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes * Copyright (C) 2012 The Android Open Source Project 304a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes * 404a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License"); 504a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes * you may not use this file except in compliance with the License. 604a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes * You may obtain a copy of the License at 704a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes * 804a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 904a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes * 1004a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes * Unless required by applicable law or agreed to in writing, software 1104a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 1204a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1304a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes * See the License for the specific language governing permissions and 1404a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes * limitations under the License. 1504a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes */ 1604a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes 1704a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes#include <gtest/gtest.h> 1804a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes 1904a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes#include <errno.h> 2004a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes#include <limits.h> 2104a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes#include <unistd.h> 2204a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes 2304a83a48ed89f433c78e31106ed50059764797a0Elliott HughesTEST(getcwd, auto_full) { 2404a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes // If we let the library do all the work, everything's fine. 2504a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes errno = 0; 2604a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes char* cwd = getcwd(NULL, 0); 2704a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes ASSERT_TRUE(cwd != NULL); 285e3fc43ddeada547a155c6f561a12ff0b16e02d3Elliott Hughes ASSERT_EQ(0, errno); 2904a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes ASSERT_GE(strlen(cwd), 1U); 3004a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes free(cwd); 3104a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes} 3204a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes 3304a83a48ed89f433c78e31106ed50059764797a0Elliott HughesTEST(getcwd, auto_reasonable) { 3404a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes // If we ask the library to allocate a reasonable buffer, everything's fine. 3504a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes errno = 0; 3604a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes char* cwd = getcwd(NULL, PATH_MAX); 3704a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes ASSERT_TRUE(cwd != NULL); 385e3fc43ddeada547a155c6f561a12ff0b16e02d3Elliott Hughes ASSERT_EQ(0, errno); 3904a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes ASSERT_GE(strlen(cwd), 1U); 4004a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes free(cwd); 4104a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes} 4204a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes 4304a83a48ed89f433c78e31106ed50059764797a0Elliott HughesTEST(getcwd, auto_too_small) { 4404a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes // If we ask the library to allocate a too-small buffer, ERANGE. 4504a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes errno = 0; 4604a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes char* cwd = getcwd(NULL, 1); 4704a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes ASSERT_TRUE(cwd == NULL); 485e3fc43ddeada547a155c6f561a12ff0b16e02d3Elliott Hughes ASSERT_EQ(ERANGE, errno); 4904a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes} 5004a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes 5104a83a48ed89f433c78e31106ed50059764797a0Elliott HughesTEST(getcwd, auto_too_large) { 5204a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes // If we ask the library to allocate an unreasonably large buffer, ERANGE. 5304a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes errno = 0; 5404a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes char* cwd = getcwd(NULL, static_cast<size_t>(-1)); 5504a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes ASSERT_TRUE(cwd == NULL); 565e3fc43ddeada547a155c6f561a12ff0b16e02d3Elliott Hughes ASSERT_EQ(ENOMEM, errno); 5704a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes} 5804a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes 5904a83a48ed89f433c78e31106ed50059764797a0Elliott HughesTEST(getcwd, manual_too_small) { 6004a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes // If we allocate a too-small buffer, ERANGE. 6104a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes char tiny_buf[1]; 6204a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes errno = 0; 6304a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes char* cwd = getcwd(tiny_buf, sizeof(tiny_buf)); 6404a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes ASSERT_TRUE(cwd == NULL); 655e3fc43ddeada547a155c6f561a12ff0b16e02d3Elliott Hughes ASSERT_EQ(ERANGE, errno); 6604a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes} 6704a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes 6804a83a48ed89f433c78e31106ed50059764797a0Elliott HughesTEST(getcwd, manual_zero) { 6904a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes // If we allocate a zero-length buffer, EINVAL. 7004a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes char tiny_buf[1]; 7104a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes errno = 0; 7204a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes char* cwd = getcwd(tiny_buf, 0); 7304a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes ASSERT_TRUE(cwd == NULL); 745e3fc43ddeada547a155c6f561a12ff0b16e02d3Elliott Hughes ASSERT_EQ(EINVAL, errno); 7504a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes} 7604a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes 7704a83a48ed89f433c78e31106ed50059764797a0Elliott HughesTEST(getcwd, manual_path_max) { 7804a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes char* buf = new char[PATH_MAX]; 7904a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes errno = 0; 8004a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes char* cwd = getcwd(buf, PATH_MAX); 8104a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes ASSERT_TRUE(cwd == buf); 825e3fc43ddeada547a155c6f561a12ff0b16e02d3Elliott Hughes ASSERT_EQ(0, errno); 8304a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes ASSERT_GE(strlen(cwd), 1U); 8404a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes delete[] cwd; 8504a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes} 86