Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright (C) 2013 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include <gtest/gtest.h>
     18 
     19 #include <sys/types.h>
     20 #include <sys/socket.h>
     21 #include <netdb.h>
     22 #include <netinet/in.h>
     23 
     24 TEST(netdb, getaddrinfo_NULL_hints) {
     25   addrinfo* ai = NULL;
     26   ASSERT_EQ(0, getaddrinfo("localhost", "9999", NULL, &ai));
     27   freeaddrinfo(ai);
     28 }
     29 
     30 TEST(netdb, getnameinfo_salen) {
     31   sockaddr_storage ss;
     32   memset(&ss, 0, sizeof(ss));
     33   sockaddr* sa = reinterpret_cast<sockaddr*>(&ss);
     34   char tmp[16];
     35 
     36   ss.ss_family = AF_INET;
     37   socklen_t too_much = sizeof(ss);
     38   socklen_t just_right = sizeof(sockaddr_in);
     39   socklen_t too_little = sizeof(sockaddr_in) - 1;
     40 
     41   ASSERT_EQ(0, getnameinfo(sa, too_much, tmp, sizeof(tmp), NULL, 0, NI_NUMERICHOST));
     42   ASSERT_STREQ("0.0.0.0", tmp);
     43   ASSERT_EQ(0, getnameinfo(sa, just_right, tmp, sizeof(tmp), NULL, 0, NI_NUMERICHOST));
     44   ASSERT_STREQ("0.0.0.0", tmp);
     45   ASSERT_EQ(EAI_FAMILY, getnameinfo(sa, too_little, tmp, sizeof(tmp), NULL, 0, NI_NUMERICHOST));
     46 
     47   ss.ss_family = AF_INET6;
     48   just_right = sizeof(sockaddr_in6);
     49   too_little = sizeof(sockaddr_in6) - 1;
     50   too_much = just_right + 1;
     51 
     52   ASSERT_EQ(0, getnameinfo(sa, too_much, tmp, sizeof(tmp), NULL, 0, NI_NUMERICHOST));
     53   ASSERT_STREQ("::", tmp);
     54   ASSERT_EQ(0, getnameinfo(sa, just_right, tmp, sizeof(tmp), NULL, 0, NI_NUMERICHOST));
     55   ASSERT_STREQ("::", tmp);
     56   ASSERT_EQ(EAI_FAMILY, getnameinfo(sa, too_little, tmp, sizeof(tmp), NULL, 0, NI_NUMERICHOST));
     57 }
     58