~ubuntu-branches/ubuntu/precise/libpgm/precise

« back to all changes in this revision

Viewing changes to openpgm/pgm/.svn/text-base/getnetbyname_unittest.c.svn-base

  • Committer: Bazaar Package Importer
  • Author(s): Gabriel de Perthuis
  • Date: 2011-04-07 16:48:52 UTC
  • Revision ID: james.westby@ubuntu.com-20110407164852-8uamem42ojeptj6l
Tags: upstream-5.1.116~dfsg
ImportĀ upstreamĀ versionĀ 5.1.116~dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* vim:ts=8:sts=8:sw=4:noai:noexpandtab
 
2
 *
 
3
 * unit tests for portable function to enumerate network names.
 
4
 *
 
5
 * Copyright (c) 2010 Miru Limited.
 
6
 *
 
7
 * This library is free software; you can redistribute it and/or
 
8
 * modify it under the terms of the GNU Lesser General Public
 
9
 * License as published by the Free Software Foundation; either
 
10
 * version 2.1 of the License, or (at your option) any later version.
 
11
 *
 
12
 * This library is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
 * Lesser General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU Lesser General Public
 
18
 * License along with this library; if not, write to the Free Software
 
19
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
20
 */
 
21
 
 
22
#ifndef _WIN32
 
23
#       include <sys/socket.h>
 
24
#       include <netdb.h>
 
25
#else
 
26
#       include <ws2tcpip.h>
 
27
#endif
 
28
 
 
29
#include <glib.h>
 
30
#include <check.h>
 
31
 
 
32
 
 
33
/* mock state */
 
34
 
 
35
#ifndef _WIN32
 
36
#       define COMPARE_GETNETENT
 
37
#endif
 
38
 
 
39
#define GETNETBYNAME_DEBUG
 
40
#include "getnetbyname.c"
 
41
 
 
42
PGM_GNUC_INTERNAL
 
43
int
 
44
pgm_get_nprocs (void)
 
45
{
 
46
        return 1;
 
47
}
 
48
 
 
49
static
 
50
void
 
51
mock_setup (void)
 
52
{
 
53
}
 
54
 
 
55
static
 
56
void
 
57
mock_teardown (void)
 
58
{
 
59
}
 
60
 
 
61
/* target:
 
62
 *      struct netent*
 
63
 *      pgm_getnetbyname (
 
64
 *              const char*     name
 
65
 *      )
 
66
 */
 
67
 
 
68
START_TEST (test_getnetbyname_pass_001)
 
69
{
 
70
        const char loopback[] = "loopback";
 
71
 
 
72
        fail_if (NULL == pgm_getnetbyname (loopback), "getnetbyname failed");
 
73
}
 
74
END_TEST
 
75
 
 
76
START_TEST (test_getnetbyname_fail_001)
 
77
{
 
78
        fail_unless (NULL == pgm_getnetbyname (NULL), "getnetbyname failed");
 
79
}
 
80
END_TEST
 
81
 
 
82
START_TEST (test_getnetbyname_fail_002)
 
83
{
 
84
        const char unknown[] = "qwertyuiop";
 
85
 
 
86
        fail_unless (NULL == pgm_getnetbyname (unknown), "getnetbyname failed");
 
87
}
 
88
END_TEST
 
89
 
 
90
/* target:
 
91
 *      struct pgm_netent_t*
 
92
 *      _pgm_compat_getnetent (void)
 
93
 */
 
94
 
 
95
START_TEST (test_getnetent_pass_001)
 
96
{
 
97
        int i = 1;
 
98
        struct pgm_netent_t* ne;
 
99
#ifdef COMPARE_GETNETENT
 
100
        struct netent* nne;
 
101
#endif
 
102
 
 
103
        _pgm_compat_setnetent();
 
104
#ifdef COMPARE_GETNETENT
 
105
        setnetent (0);
 
106
#endif
 
107
        while (ne = _pgm_compat_getnetent()) {
 
108
                char buffer[1024];
 
109
                char **p;
 
110
 
 
111
#ifdef COMPARE_GETNETENT
 
112
                nne = getnetent();
 
113
                if (NULL == nne)
 
114
                        g_warning ("native ne = (null");
 
115
#endif
 
116
 
 
117
/* official network name */
 
118
                fail_if (NULL == ne->n_name, "no official name");
 
119
                g_debug ("%-6dn_name = %s", i++, ne->n_name);
 
120
#ifdef COMPARE_GETNETENT
 
121
                if (NULL != nne)
 
122
                        fail_unless (0 == strcmp (ne->n_name, nne->n_name), "official name mismatch");
 
123
#endif
 
124
 
 
125
/* alias list */
 
126
                fail_if (NULL == ne->n_aliases, "invalid aliases pointer");
 
127
                p = ne->n_aliases;
 
128
                if (*p) {
 
129
                        strcpy (buffer, *p++);
 
130
                        while (*p) {
 
131
                                strcat (buffer, ", ");
 
132
                                strcat (buffer, *p++);
 
133
                        }
 
134
                } else
 
135
                        strcpy (buffer, "(nil)");
 
136
                g_debug ("      n_aliases = %s", buffer);
 
137
#ifdef COMPARE_GETNETENT
 
138
                if (NULL != nne) {
 
139
                        char nbuffer[1024];
 
140
 
 
141
                        fail_if (NULL == nne->n_aliases, "invalid aliases pointer");
 
142
                        p = nne->n_aliases;
 
143
                        if (*p) {
 
144
                                strcpy (nbuffer, *p++);
 
145
                                while (*p) {
 
146
                                        strcat (nbuffer, ", ");
 
147
                                        strcat (nbuffer, *p++);
 
148
                                }
 
149
                        } else
 
150
                                strcpy (nbuffer, "(nil)");
 
151
                        fail_unless (0 == strcmp (buffer, nbuffer), "aliases mismatch");
 
152
                }
 
153
#endif
 
154
 
 
155
/* net address type */
 
156
                fail_unless (AF_INET == ne->n_net.ss_family || AF_INET6 == ne->n_net.ss_family, "invalid address family");
 
157
                if (AF_INET == ne->n_net.ss_family) {
 
158
                        struct sockaddr_in sa;
 
159
                        struct in_addr net;
 
160
 
 
161
                        g_debug ("      n_addrtype = AF_INET");
 
162
#ifdef COMPARE_GETNETENT
 
163
                        fail_unless (ne->n_net.ss_family == nne->n_addrtype, "address family mismatch");
 
164
#endif
 
165
/* network number */
 
166
                        memcpy (&sa, &ne->n_net, sizeof (sa));
 
167
                        fail_unless (0 == getnameinfo ((struct sockaddr*)&sa, sizeof (sa),
 
168
                                                       buffer, sizeof (buffer),
 
169
                                                       NULL, 0,
 
170
                                                       NI_NUMERICHOST), "getnameinfo failed");
 
171
                        g_debug ("      n_net = %s", buffer);
 
172
#ifdef COMPARE_GETNETENT
 
173
                        net = pgm_inet_makeaddr (nne->n_net, 0);
 
174
                        fail_unless (0 == memcmp (&sa.sin_addr, &net, sizeof (struct in_addr)), "network address mismatch");
 
175
#endif
 
176
                } else {
 
177
                        struct sockaddr_in6 sa6;
 
178
                        g_debug ("      n_addrtype = AF_INET6");
 
179
#ifdef COMPARE_GETNETENT
 
180
                        if (ne->n_net.ss_family != nne->n_addrtype)
 
181
                                g_warning ("native address type not AF_INET6");
 
182
#endif
 
183
                        memcpy (&sa6, &ne->n_net, sizeof (sa6));
 
184
                        fail_unless (0 == getnameinfo ((struct sockaddr*)&sa6, sizeof (sa6),
 
185
                                                       buffer, sizeof (buffer),
 
186
                                                       NULL, 0,
 
187
                                                       NI_NUMERICHOST), "getnameinfo failed");
 
188
                        g_debug ("      n_net = %s", buffer);
 
189
                }
 
190
        }
 
191
        _pgm_compat_endnetent();
 
192
#ifdef COMPARE_GETNETENT
 
193
        endnetent();
 
194
#endif
 
195
}
 
196
END_TEST
 
197
 
 
198
 
 
199
static
 
200
Suite*
 
201
make_test_suite (void)
 
202
{
 
203
        Suite* s;
 
204
 
 
205
        s = suite_create (__FILE__);
 
206
        TCase* tc_getnetbyname = tcase_create ("getnetbyname");
 
207
        suite_add_tcase (s, tc_getnetbyname);
 
208
        tcase_add_checked_fixture (tc_getnetbyname, mock_setup, mock_teardown);
 
209
        tcase_add_test (tc_getnetbyname, test_getnetbyname_pass_001);
 
210
        tcase_add_test (tc_getnetbyname, test_getnetbyname_fail_001);
 
211
        tcase_add_test (tc_getnetbyname, test_getnetbyname_fail_002);
 
212
        tcase_add_test (tc_getnetbyname, test_getnetent_pass_001);
 
213
        return s;
 
214
}
 
215
 
 
216
static
 
217
Suite*
 
218
make_master_suite (void)
 
219
{
 
220
        Suite* s = suite_create ("Master");
 
221
        return s;
 
222
}
 
223
 
 
224
int
 
225
main (void)
 
226
{
 
227
#ifdef _WIN32
 
228
        WORD wVersionRequested = MAKEWORD (2, 2);
 
229
        WSADATA wsaData;
 
230
        g_assert (0 == WSAStartup (wVersionRequested, &wsaData));
 
231
        g_assert (LOBYTE (wsaData.wVersion) == 2 && HIBYTE (wsaData.wVersion) == 2);
 
232
#endif
 
233
        pgm_messages_init();
 
234
        SRunner* sr = srunner_create (make_master_suite ());
 
235
        srunner_add_suite (sr, make_test_suite ());
 
236
        srunner_run_all (sr, CK_ENV);
 
237
        int number_failed = srunner_ntests_failed (sr);
 
238
        srunner_free (sr);
 
239
        pgm_messages_shutdown();
 
240
#ifdef _WIN32
 
241
        WSACleanup();
 
242
#endif
 
243
        return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
 
244
}
 
245
 
 
246
/* eof */