[134] | 1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- |
---|
| 2 | * |
---|
| 3 | * Copyright (C) 2007 Andrew Ziem <ahz001@gmail.com> |
---|
| 4 | * Copyright (C) 2007 William Jon McCann <mccann@jhu.edu> |
---|
| 5 | * |
---|
| 6 | * This library is free software; you can redistribute it and/or |
---|
| 7 | * modify it under the terms of the GNU Library General Public |
---|
| 8 | * License as published by the Free Software Foundation; either |
---|
| 9 | * version 2 of the License, or (at your option) any later version. |
---|
| 10 | * |
---|
| 11 | * This library is distributed in the hope that it will be useful, |
---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 14 | * Library General Public License for more details. |
---|
| 15 | * |
---|
| 16 | * You should have received a copy of the GNU Library General Public |
---|
| 17 | * License along with this library; if not, write to the |
---|
| 18 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
---|
| 19 | * Boston, MA 02111-1307, USA. |
---|
| 20 | */ |
---|
| 21 | |
---|
| 22 | #include <stdlib.h> |
---|
| 23 | #include <netinet/in.h> |
---|
| 24 | #include <string.h> |
---|
| 25 | #include <stdio.h> |
---|
| 26 | #include <glib.h> |
---|
| 27 | #include <check.h> |
---|
| 28 | |
---|
| 29 | #include "gdm-address.h" |
---|
| 30 | #include "s-common-address.h" |
---|
| 31 | |
---|
| 32 | static GdmAddress *ga; |
---|
| 33 | static GdmAddress *ga192; |
---|
| 34 | static struct sockaddr sa; |
---|
| 35 | static struct sockaddr_in *s_in; |
---|
| 36 | |
---|
| 37 | #ifdef ENABLE_IPV6 |
---|
| 38 | static GdmAddress *ga6; |
---|
| 39 | static struct sockaddr_storage sa6; |
---|
| 40 | static struct sockaddr_in6 *s_in6; |
---|
| 41 | #endif |
---|
| 42 | |
---|
| 43 | static void |
---|
| 44 | setup (void) |
---|
| 45 | { |
---|
| 46 | s_in = (struct sockaddr_in *) &sa; |
---|
| 47 | s_in->sin_family = AF_INET; |
---|
| 48 | s_in->sin_port = htons (25); |
---|
| 49 | s_in->sin_addr.s_addr = htonl (INADDR_LOOPBACK); |
---|
| 50 | |
---|
| 51 | ga = gdm_address_new_from_sockaddr (&sa, sizeof (sa)); |
---|
| 52 | fail_unless (NULL != ga); |
---|
| 53 | |
---|
| 54 | s_in->sin_addr.s_addr = htonl (0xc0a80001); /* 192.168.0.1 */ |
---|
| 55 | ga192 = gdm_address_new_from_sockaddr (&sa, sizeof (sa)); |
---|
| 56 | fail_unless (NULL != (ga192)); |
---|
| 57 | |
---|
| 58 | #ifdef ENABLE_IPV6 |
---|
| 59 | s_in6 = (struct sockaddr_in6 *) &sa6; |
---|
| 60 | s_in6->sin6_family = AF_INET6; |
---|
| 61 | s_in6->sin6_port = htons (25); |
---|
| 62 | s_in6->sin6_addr = in6addr_loopback; |
---|
| 63 | ga6 = gdm_address_new_from_sockaddr ((struct sockaddr*)&sa6, sizeof(sa6)); |
---|
| 64 | fail_unless (NULL != ga6); |
---|
| 65 | #endif |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | static void |
---|
| 69 | teardown (void) |
---|
| 70 | { |
---|
| 71 | gdm_address_free (ga); |
---|
| 72 | gdm_address_free (ga192); |
---|
| 73 | #ifdef ENABLE_IPV6 |
---|
| 74 | gdm_address_free (ga6); |
---|
| 75 | #endif |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | |
---|
| 79 | /* |
---|
| 80 | * GType gdm_address_get_type (void); |
---|
| 81 | */ |
---|
| 82 | START_TEST (test_gdm_address_get_type) |
---|
| 83 | { |
---|
| 84 | gdm_address_get_type (); |
---|
| 85 | /* it did not crash! :) */ |
---|
| 86 | |
---|
| 87 | } |
---|
| 88 | END_TEST |
---|
| 89 | |
---|
| 90 | |
---|
| 91 | START_TEST (test_gdm_address_new_from_sockaddr) |
---|
| 92 | { |
---|
| 93 | |
---|
| 94 | GdmAddress *_ga; |
---|
| 95 | #ifdef ENABLE_IPV6 |
---|
| 96 | GdmAddress *_ga6; |
---|
| 97 | #endif |
---|
| 98 | |
---|
| 99 | _ga = gdm_address_new_from_sockaddr ((struct sockaddr *) &sa, sizeof (sa)); |
---|
| 100 | fail_unless (NULL != _ga); |
---|
| 101 | gdm_address_free (_ga); |
---|
| 102 | |
---|
| 103 | #ifdef ENABLE_IPV6 |
---|
| 104 | _ga6 = gdm_address_new_from_sockaddr((struct sockaddr *) &sa6, sizeof (sa6)); |
---|
| 105 | fail_unless (NULL != _ga6); |
---|
| 106 | gdm_address_free (_ga6); |
---|
| 107 | #endif |
---|
| 108 | |
---|
| 109 | #ifndef NO_INVALID_INPUT |
---|
| 110 | /* invalid input */ |
---|
| 111 | fail_unless (NULL == gdm_address_new_from_sockaddr ((struct sockaddr *) &sa, 1), NULL ); |
---|
| 112 | fail_unless (NULL == gdm_address_new_from_sockaddr (NULL, 0), NULL); |
---|
| 113 | #endif |
---|
| 114 | } |
---|
| 115 | END_TEST |
---|
| 116 | |
---|
| 117 | |
---|
| 118 | START_TEST (test_gdm_address_get_family_type) |
---|
| 119 | { |
---|
| 120 | fail_unless (AF_INET == gdm_address_get_family_type (ga), NULL); |
---|
| 121 | |
---|
| 122 | #ifdef ENABLE_IPV6 |
---|
| 123 | fail_unless (AF_INET6 == gdm_address_get_family_type (ga6), NULL); |
---|
| 124 | #endif |
---|
| 125 | |
---|
| 126 | #ifndef NO_INVALID_INPUT |
---|
| 127 | /* invalid input */ |
---|
| 128 | fail_unless (-1 == gdm_address_get_family_type (NULL), NULL); |
---|
| 129 | #endif |
---|
| 130 | |
---|
| 131 | } |
---|
| 132 | END_TEST |
---|
| 133 | |
---|
| 134 | |
---|
| 135 | START_TEST (test_gdm_address_is_loopback) |
---|
| 136 | { |
---|
| 137 | fail_unless (TRUE == gdm_address_is_loopback (ga)); |
---|
| 138 | fail_unless (FALSE == gdm_address_is_loopback (ga192)); |
---|
| 139 | |
---|
| 140 | #ifdef ENABLE_IPV6 |
---|
| 141 | fail_unless (TRUE == gdm_address_is_loopback (ga6)); |
---|
| 142 | /* FIXME: add more addresses */ |
---|
| 143 | #endif |
---|
| 144 | |
---|
| 145 | #ifndef NO_INVALID_INPUT |
---|
| 146 | /* invalid input */ |
---|
| 147 | fail_unless (FALSE == gdm_address_is_loopback (NULL)); |
---|
| 148 | #endif |
---|
| 149 | } |
---|
| 150 | END_TEST |
---|
| 151 | |
---|
| 152 | |
---|
| 153 | START_TEST (test_gdm_address_equal) |
---|
| 154 | { |
---|
| 155 | GdmAddress *gdm1; |
---|
| 156 | struct sockaddr sa1; |
---|
| 157 | struct sockaddr_in *sin1; |
---|
| 158 | |
---|
| 159 | /* should be inequal */ |
---|
| 160 | sin1 = (struct sockaddr_in *) &sa1; |
---|
| 161 | sin1->sin_family = AF_INET; |
---|
| 162 | sin1->sin_addr.s_addr = htonl (0xc0a80001); /* 192.168.0.1 */ |
---|
| 163 | gdm1 = gdm_address_new_from_sockaddr (&sa1, sizeof (sa1)); |
---|
| 164 | fail_unless (gdm_address_equal (ga, ga192) == FALSE, NULL); |
---|
| 165 | |
---|
| 166 | /* should be equal */ |
---|
| 167 | fail_unless (TRUE == gdm_address_equal (ga192, gdm1), NULL); |
---|
| 168 | |
---|
| 169 | gdm_address_free (gdm1); |
---|
| 170 | |
---|
| 171 | #ifdef ENABLE_IPV6 |
---|
| 172 | /* should be inequal */ |
---|
| 173 | fail_unless (FALSE == gdm_address_equal (ga6, ga), NULL); |
---|
| 174 | fail_unless (FALSE == gdm_address_equal (ga6, ga192), NULL); |
---|
| 175 | fail_unless (FALSE == gdm_address_equal (ga6, gdm1), NULL); |
---|
| 176 | |
---|
| 177 | /* should be equal */ |
---|
| 178 | /* FIXME: ipv6 version too */ |
---|
| 179 | #endif |
---|
| 180 | |
---|
| 181 | #ifndef NO_INVALID_INPUT |
---|
| 182 | /* invalid input */ |
---|
| 183 | fail_unless (FALSE == gdm_address_equal (NULL, NULL), NULL); |
---|
| 184 | fail_unless (FALSE == gdm_address_equal (ga, NULL), NULL); |
---|
| 185 | fail_unless (FALSE == gdm_address_equal (NULL, ga), NULL); |
---|
| 186 | #endif |
---|
| 187 | } |
---|
| 188 | END_TEST |
---|
| 189 | |
---|
| 190 | |
---|
| 191 | Suite * |
---|
| 192 | suite_common_address (void) |
---|
| 193 | { |
---|
| 194 | Suite *s; |
---|
| 195 | TCase *tc_core; |
---|
| 196 | |
---|
| 197 | s = suite_create ("gdm-address"); |
---|
| 198 | tc_core = tcase_create ("core"); |
---|
| 199 | |
---|
| 200 | tcase_add_checked_fixture (tc_core, setup, teardown); |
---|
| 201 | tcase_add_test (tc_core, test_gdm_address_get_type); |
---|
| 202 | tcase_add_test (tc_core, test_gdm_address_new_from_sockaddr); |
---|
| 203 | tcase_add_test (tc_core, test_gdm_address_get_family_type); |
---|
| 204 | tcase_add_test (tc_core, test_gdm_address_is_loopback); |
---|
| 205 | tcase_add_test (tc_core, test_gdm_address_equal); |
---|
| 206 | suite_add_tcase (s, tc_core); |
---|
| 207 | |
---|
| 208 | return s; |
---|
| 209 | } |
---|