1 | /*
|
---|
2 | * Copyright (c) 2017 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #include <errno.h>
|
---|
30 | #include <inet/endpoint.h>
|
---|
31 | #include <mem.h>
|
---|
32 | #include <pcut/pcut.h>
|
---|
33 | #include <stdlib.h>
|
---|
34 |
|
---|
35 | #include "main.h"
|
---|
36 | #include "../pdu.h"
|
---|
37 | #include "../segment.h"
|
---|
38 |
|
---|
39 | PCUT_INIT;
|
---|
40 |
|
---|
41 | PCUT_TEST_SUITE(pdu);
|
---|
42 |
|
---|
43 | /** Test encode/decode round trip for control PDU */
|
---|
44 | PCUT_TEST(encdec_syn)
|
---|
45 | {
|
---|
46 | tcp_segment_t *seg, *dseg;
|
---|
47 | tcp_pdu_t *pdu;
|
---|
48 | inet_ep2_t epp, depp;
|
---|
49 | errno_t rc;
|
---|
50 |
|
---|
51 | inet_ep2_init(&epp);
|
---|
52 | inet_addr(&epp.local.addr, 1, 2, 3, 4);
|
---|
53 | inet_addr(&epp.remote.addr, 5, 6, 7, 8);
|
---|
54 |
|
---|
55 | seg = tcp_segment_make_ctrl(CTL_SYN);
|
---|
56 | PCUT_ASSERT_NOT_NULL(seg);
|
---|
57 |
|
---|
58 | seg->seq = 20;
|
---|
59 | seg->ack = 19;
|
---|
60 | seg->wnd = 18;
|
---|
61 | seg->up = 17;
|
---|
62 |
|
---|
63 | rc = tcp_pdu_encode(&epp, seg, &pdu);
|
---|
64 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
65 | rc = tcp_pdu_decode(pdu, &depp, &dseg);
|
---|
66 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
67 |
|
---|
68 | test_seg_same(seg, dseg);
|
---|
69 | tcp_segment_delete(seg);
|
---|
70 | }
|
---|
71 |
|
---|
72 | /** Test encode/decode round trip for data PDU */
|
---|
73 | PCUT_TEST(encdec_data)
|
---|
74 | {
|
---|
75 | tcp_segment_t *seg, *dseg;
|
---|
76 | tcp_pdu_t *pdu;
|
---|
77 | inet_ep2_t epp, depp;
|
---|
78 | uint8_t *data;
|
---|
79 | size_t i, dsize;
|
---|
80 | errno_t rc;
|
---|
81 |
|
---|
82 | inet_ep2_init(&epp);
|
---|
83 | inet_addr(&epp.local.addr, 1, 2, 3, 4);
|
---|
84 | inet_addr(&epp.remote.addr, 5, 6, 7, 8);
|
---|
85 |
|
---|
86 | dsize = 15;
|
---|
87 | data = malloc(dsize);
|
---|
88 | PCUT_ASSERT_NOT_NULL(data);
|
---|
89 |
|
---|
90 | for (i = 0; i < dsize; i++)
|
---|
91 | data[i] = (uint8_t) i;
|
---|
92 |
|
---|
93 | seg = tcp_segment_make_data(CTL_SYN, data, dsize);
|
---|
94 | PCUT_ASSERT_NOT_NULL(seg);
|
---|
95 |
|
---|
96 | seg->seq = 20;
|
---|
97 | seg->ack = 19;
|
---|
98 | seg->wnd = 18;
|
---|
99 | seg->up = 17;
|
---|
100 |
|
---|
101 | rc = tcp_pdu_encode(&epp, seg, &pdu);
|
---|
102 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
103 | rc = tcp_pdu_decode(pdu, &depp, &dseg);
|
---|
104 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
105 |
|
---|
106 | test_seg_same(seg, dseg);
|
---|
107 | tcp_segment_delete(seg);
|
---|
108 | free(data);
|
---|
109 | }
|
---|
110 |
|
---|
111 | PCUT_EXPORT(pdu);
|
---|