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

« back to all changes in this revision

Viewing changes to openpgm/pgm/packet_test_unittest.c

  • 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 PGM packet handling.
 
4
 *
 
5
 * Copyright (c) 2009-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
 
 
23
#include <signal.h>
 
24
#include <stdbool.h>
 
25
#include <stdlib.h>
 
26
#include <glib.h>
 
27
#include <check.h>
 
28
 
 
29
#ifdef _WIN32
 
30
#       define PGM_CHECK_NOFORK         1
 
31
#endif
 
32
 
 
33
 
 
34
/* mock state */
 
35
 
 
36
#define PACKET_DEBUG
 
37
#include "packet_test.c"
 
38
 
 
39
 
 
40
static
 
41
struct pgm_sk_buff_t*
 
42
generate_raw_pgm (void)
 
43
{
 
44
        const char source[] = "i am not a string";
 
45
        const guint source_len = sizeof(source);
 
46
        struct pgm_sk_buff_t* skb;
 
47
        GError* err = NULL;
 
48
 
 
49
        skb = pgm_alloc_skb (1500);
 
50
        skb->sock               = (pgm_sock_t*)0x1;
 
51
        skb->tstamp             = 0x1;
 
52
        skb->data               = skb->head;
 
53
        skb->len                = sizeof(struct pgm_ip) + sizeof(struct pgm_header) + sizeof(struct pgm_data) + source_len;
 
54
        skb->tail               = (guint8*)skb->data + skb->len;
 
55
 
 
56
/* add IP header */
 
57
        struct pgm_ip* iphdr = skb->data;
 
58
        iphdr->ip_hl            = sizeof(struct pgm_ip) / 4;
 
59
        iphdr->ip_v             = 4;
 
60
        iphdr->ip_tos           = 0;
 
61
        iphdr->ip_len           = g_htons (skb->len);
 
62
        iphdr->ip_id            = 0;
 
63
        iphdr->ip_off           = 0;
 
64
        iphdr->ip_ttl           = 16;
 
65
        iphdr->ip_p             = IPPROTO_PGM;
 
66
        iphdr->ip_sum           = 0;
 
67
        iphdr->ip_src.s_addr    = inet_addr ("127.0.0.1");
 
68
        iphdr->ip_dst.s_addr    = inet_addr ("127.0.0.2");
 
69
 
 
70
/* add PGM header */
 
71
        struct pgm_header* pgmhdr = (gpointer)(iphdr + 1);
 
72
        pgmhdr->pgm_sport       = g_htons ((guint16)1000);
 
73
        pgmhdr->pgm_dport       = g_htons ((guint16)7500);
 
74
        pgmhdr->pgm_type        = PGM_ODATA;
 
75
        pgmhdr->pgm_options     = 0;
 
76
        pgmhdr->pgm_gsi[0]      = 1;
 
77
        pgmhdr->pgm_gsi[1]      = 2;
 
78
        pgmhdr->pgm_gsi[2]      = 3;
 
79
        pgmhdr->pgm_gsi[3]      = 4;
 
80
        pgmhdr->pgm_gsi[4]      = 5;
 
81
        pgmhdr->pgm_gsi[5]      = 6;
 
82
        pgmhdr->pgm_tsdu_length = g_htons (source_len);
 
83
 
 
84
/* add ODATA header */
 
85
        struct pgm_data* datahdr = (gpointer)(pgmhdr + 1);
 
86
        datahdr->data_sqn       = g_htonl ((guint32)0);
 
87
        datahdr->data_trail     = g_htonl ((guint32)-1);
 
88
 
 
89
/* add payload */
 
90
        gpointer data = (gpointer)(datahdr + 1);
 
91
        memcpy (data, source, source_len);
 
92
 
 
93
/* finally PGM checksum */
 
94
        pgmhdr->pgm_checksum    = 0;
 
95
        pgmhdr->pgm_checksum    = pgm_csum_fold (pgm_csum_partial (pgmhdr, sizeof(struct pgm_header) + sizeof(struct pgm_data) + source_len, 0));
 
96
 
 
97
/* and IP checksum */
 
98
        iphdr->ip_sum           = pgm_inet_checksum (skb->head, skb->len, 0);
 
99
 
 
100
        return skb;
 
101
}
 
102
 
 
103
 
 
104
/* mock functions for external references */
 
105
 
 
106
size_t
 
107
pgm_pkt_offset (
 
108
        const bool                      can_fragment,
 
109
        const sa_family_t               pgmcc_family    /* 0 = disable */
 
110
        )
 
111
{
 
112
        return 0;
 
113
}
 
114
 
 
115
PGM_GNUC_INTERNAL
 
116
int
 
117
pgm_get_nprocs (void)
 
118
{
 
119
        return 1;
 
120
}
 
121
 
 
122
/* target:
 
123
 *      gboolean
 
124
 *      pgm_print_packet (
 
125
 *              gpointer                        data,
 
126
 *              gsize                           len
 
127
 *      )
 
128
 */
 
129
 
 
130
START_TEST (test_print_packet_pass_001)
 
131
{
 
132
        struct pgm_sk_buff_t* skb = generate_raw_pgm ();
 
133
        pgm_print_packet (skb->head, skb->len);
 
134
}
 
135
END_TEST
 
136
 
 
137
START_TEST (test_print_packet_fail_001)
 
138
{
 
139
        pgm_print_packet (NULL, 0);
 
140
        fail ("reached");
 
141
}
 
142
END_TEST
 
143
 
 
144
 
 
145
static
 
146
Suite*
 
147
make_test_suite (void)
 
148
{
 
149
        Suite* s;
 
150
 
 
151
        s = suite_create (__FILE__);
 
152
 
 
153
        TCase* tc_print_packet = tcase_create ("print-packet");
 
154
        suite_add_tcase (s, tc_print_packet);
 
155
        tcase_add_test (tc_print_packet, test_print_packet_pass_001);
 
156
#ifndef PGM_CHECK_NOFORK
 
157
        tcase_add_test_raise_signal (tc_print_packet, test_print_packet_fail_001, SIGABRT);
 
158
#endif
 
159
        return s;
 
160
}
 
161
 
 
162
static
 
163
Suite*
 
164
make_master_suite (void)
 
165
{
 
166
        Suite* s = suite_create ("Master");
 
167
        return s;
 
168
}
 
169
 
 
170
int
 
171
main (void)
 
172
{
 
173
        SRunner* sr = srunner_create (make_master_suite ());
 
174
        srunner_add_suite (sr, make_test_suite ());
 
175
        srunner_run_all (sr, CK_ENV);
 
176
        int number_failed = srunner_ntests_failed (sr);
 
177
        srunner_free (sr);
 
178
        return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
 
179
}
 
180
 
 
181
/* eof */