brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.7 KiB · c9c9787 Raw
93 lines · cpp
1//===-- Unittests for inet_aton -------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "src/arpa/inet/htonl.h"10#include "src/arpa/inet/inet_aton.h"11#include "test/UnitTest/Test.h"12 13namespace LIBC_NAMESPACE_DECL {14 15TEST(LlvmLibcInetAton, ValidTest) {16  in_addr a;17 18  // a.b.c.d19  a.s_addr = 0;20  ASSERT_EQ(1, inet_aton("127.1.2.4", &a));21  ASSERT_EQ(htonl(0x7f010204), a.s_addr);22 23  // a.b.c24  a.s_addr = 0;25  ASSERT_EQ(1, inet_aton("127.1.4", &a));26  ASSERT_EQ(htonl(0x7f010004), a.s_addr);27 28  // a.b29  a.s_addr = 0;30  ASSERT_EQ(1, inet_aton("127.1", &a));31  ASSERT_EQ(htonl(0x7f000001), a.s_addr);32 33  // a34  a.s_addr = 0;35  ASSERT_EQ(1, inet_aton("0x7f000001", &a));36  ASSERT_EQ(htonl(0x7f000001), a.s_addr);37 38  // Hex (0x) and mixed-case hex digits.39  a.s_addr = 0;40  ASSERT_EQ(1, inet_aton("0xFf.0.0.1", &a));41  ASSERT_EQ(htonl(0xff000001), a.s_addr);42 43  // Hex (0X) and mixed-case hex digits.44  a.s_addr = 0;45  ASSERT_EQ(1, inet_aton("0XfF.0.0.1", &a));46  ASSERT_EQ(htonl(0xff000001), a.s_addr);47 48  // Octal.49  a.s_addr = 0;50  ASSERT_EQ(1, inet_aton("0177.0.0.1", &a));51  ASSERT_EQ(htonl(0x7f000001), a.s_addr);52 53  a.s_addr = 0;54  ASSERT_EQ(1, inet_aton("036", &a));55  ASSERT_EQ(htonl(036U), a.s_addr);56}57 58TEST(LlvmLibcInetAton, InvalidTest) {59  ASSERT_EQ(0, inet_aton("", nullptr));           // Empty.60  ASSERT_EQ(0, inet_aton("x", nullptr));          // Leading junk.61  ASSERT_EQ(0, inet_aton("127.0.0.1x", nullptr)); // Trailing junk.62  ASSERT_EQ(0, inet_aton("09.0.0.1", nullptr));   // Invalid octal.63  ASSERT_EQ(0, inet_aton("0xg.0.0.1", nullptr));  // Invalid hex.64  ASSERT_EQ(0, inet_aton("1.2.3.4.5", nullptr));  // Too many dots.65  ASSERT_EQ(0, inet_aton("1.2.3.4.", nullptr));   // Trailing dot.66 67  // Out of range a.b.c.d form.68  ASSERT_EQ(0, inet_aton("999.0.0.1", nullptr));69  ASSERT_EQ(0, inet_aton("0.999.0.1", nullptr));70  ASSERT_EQ(0, inet_aton("0.0.999.1", nullptr));71  ASSERT_EQ(0, inet_aton("0.0.0.999", nullptr));72 73  // Out of range a.b.c form.74  ASSERT_EQ(0, inet_aton("256.0.0", nullptr));75  ASSERT_EQ(0, inet_aton("0.256.0", nullptr));76  ASSERT_EQ(0, inet_aton("0.0.0x10000", nullptr));77 78  // Out of range a.b form.79  ASSERT_EQ(0, inet_aton("256.0", nullptr));80  ASSERT_EQ(0, inet_aton("0.0x1000000", nullptr));81 82  // Out of range a form.83  ASSERT_EQ(0, inet_aton("0x100000000", nullptr));84 85  // 64-bit overflow.86  ASSERT_EQ(0, inet_aton("0x10000000000000000", nullptr));87 88  // Out of range octal.89  ASSERT_EQ(0, inet_aton("0400.0.0.1", nullptr));90}91 92} // namespace LIBC_NAMESPACE_DECL93