summaryrefslogtreecommitdiff
path: root/libc/test/src/unistd/gethostname_test.cpp
blob: a0e57ff0df333e2c5783366b9f205c6dc7a0da7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//===-- Unittests for gethostname -----------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/unistd/gethostname.h"

#include "test/UnitTest/ErrnoCheckingTest.h"
#include "test/UnitTest/Test.h"

using LlvmLibcGetHostNameTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;

TEST(LlvmLibcGetHostNameTest, GetCurrHostName) {
  char hostbuffer[1024];
  int ret = LIBC_NAMESPACE::gethostname(hostbuffer, sizeof(hostbuffer));
  ASSERT_NE(ret, -1);
  ASSERT_ERRNO_SUCCESS();

  ret = LIBC_NAMESPACE::gethostname(hostbuffer, 0);
  ASSERT_EQ(ret, -1);
  ASSERT_ERRNO_EQ(ENAMETOOLONG);

  // test for invalid pointer
  char *nptr = nullptr;
  ret = LIBC_NAMESPACE::gethostname(nptr, 1);
  ASSERT_EQ(ret, -1);
  ASSERT_ERRNO_EQ(EFAULT);
}