Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/C/Android/bionic/bionic/tests/     Datei vom 26.5.2026 mit Größe 2 kB image not shown  

Quelle  getcwd_test.cpp   Sprache: C

 

/*
 * 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>

#include <android-base/test_utils.h>

#include "utils.h"

TEST(getcwd, auto_full) {
  // If we let the library do all the work, everything's fine.
  errno = 0;
  char* cwd = getcwd(nullptr, 0);
  ASSERT_TRUE(cwd != nullptr);
  ASSERT_ERRNO(0);
  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;
  char* cwd = getcwd(nullptr, PATH_MAX);
  ASSERT_TRUE(cwd != nullptr);
  ASSERT_ERRNO(0);
  ASSERT_GE(strlen(cwd), 1U);
  free(cwd);
}

TEST(getcwd, auto_too_small) {
  // If we ask the library to allocate a too-small buffer, ERANGE.
  ASSERT_ERRNO_FAILURE(ERANGE, nullptr, getcwd(nullptr, 1));
}

TEST(getcwd, auto_too_large) {
  SKIP_WITH_HWASAN << "allocation size too large";
  // If we ask the library to allocate an unreasonably large buffer, ERANGE.
  ASSERT_ERRNO_FAILURE(ENOMEM, nullptr, getcwd(nullptr, static_cast<size_t>(-1)));
}

TEST(getcwd, manual_too_small) {
  // If we allocate a too-small buffer, ERANGE.
  char tiny_buf[1];
  ASSERT_ERRNO_FAILURE(ERANGE, nullptr, getcwd(tiny_buf, sizeof(tiny_buf)));
}

TEST(getcwd, manual_zero) {
  // If we allocate a zero-length buffer, EINVAL.
  char tiny_buf[1];
  ASSERT_ERRNO_FAILURE(EINVAL, nullptr, getcwd(tiny_buf, 0));
}

TEST(getcwd, manual_path_max) {
  char* buf = new char[PATH_MAX];
  char* cwd;
  ASSERT_ERRNO_SUCCESS(0, buf, cwd = getcwd(buf, PATH_MAX));
  ASSERT_GE(strlen(cwd), 1U);
  delete[] cwd;
}

Messung
CHZG
909710095

¤ Dauer der Verarbeitung: 0.3 Sekunden  ¤

*© Formatika GbR, Deutschland