[46a8c3cd] | 1 | /*
|
---|
| 2 | * Copyright (c) 2019 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 <inet/endpoint.h>
|
---|
| 30 | #include <pcut/pcut.h>
|
---|
| 31 | #include <str.h>
|
---|
| 32 | #include "../msg.h"
|
---|
| 33 | #include "../pdu.h"
|
---|
| 34 |
|
---|
| 35 | PCUT_INIT;
|
---|
| 36 |
|
---|
| 37 | PCUT_TEST_SUITE(pdu);
|
---|
| 38 |
|
---|
| 39 | /** Test creating and deleting dummy PDU */
|
---|
| 40 | PCUT_TEST(new_delete)
|
---|
| 41 | {
|
---|
| 42 | udp_pdu_t *pdu;
|
---|
| 43 |
|
---|
| 44 | pdu = udp_pdu_new();
|
---|
| 45 | udp_pdu_delete(pdu);
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | /** Test encoding and decoding back IPv4 PDU */
|
---|
| 49 | PCUT_TEST(encode_decode_v4)
|
---|
| 50 | {
|
---|
| 51 | inet_ep2_t epp;
|
---|
| 52 | udp_msg_t *msg;
|
---|
| 53 | inet_ep2_t depp;
|
---|
| 54 | udp_msg_t *dmsg;
|
---|
| 55 | udp_pdu_t *pdu;
|
---|
| 56 | const char *msgstr = "Hello";
|
---|
| 57 | errno_t rc;
|
---|
| 58 |
|
---|
| 59 | inet_ep2_init(&epp);
|
---|
| 60 | inet_addr(&epp.local.addr, 192, 168, 0, 1);
|
---|
| 61 | epp.local.port = 1;
|
---|
| 62 | inet_addr(&epp.remote.addr, 192, 168, 0, 2);
|
---|
| 63 | epp.remote.port = 2;
|
---|
| 64 |
|
---|
| 65 | msg = udp_msg_new();
|
---|
| 66 | PCUT_ASSERT_NOT_NULL(msg);
|
---|
| 67 | msg->data_size = str_size(msgstr) + 1;
|
---|
| 68 | msg->data = str_dup(msgstr);
|
---|
| 69 |
|
---|
| 70 | rc = udp_pdu_encode(&epp, msg, &pdu);
|
---|
| 71 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 72 | rc = udp_pdu_decode(pdu, &depp, &dmsg);
|
---|
| 73 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 74 |
|
---|
| 75 | /* Compare epp and depp. Local and remote should be swapped. */
|
---|
| 76 | PCUT_ASSERT_TRUE(inet_addr_compare(&epp.local.addr, &depp.remote.addr));
|
---|
| 77 | PCUT_ASSERT_EQUALS(epp.local.port, depp.remote.port);
|
---|
| 78 | PCUT_ASSERT_TRUE(inet_addr_compare(&epp.remote.addr, &depp.local.addr));
|
---|
| 79 | PCUT_ASSERT_EQUALS(epp.remote.port, depp.local.port);
|
---|
| 80 |
|
---|
| 81 | /* Compare msg and dmsg */
|
---|
| 82 | PCUT_ASSERT_EQUALS(msg->data_size, dmsg->data_size);
|
---|
| 83 | PCUT_ASSERT_TRUE(memcmp(msg->data, dmsg->data, dmsg->data_size) == 0);
|
---|
| 84 |
|
---|
| 85 | udp_pdu_delete(pdu);
|
---|
| 86 | udp_msg_delete(msg);
|
---|
| 87 | udp_msg_delete(dmsg);
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | PCUT_TEST(encode_decode_v6)
|
---|
| 91 | {
|
---|
| 92 | inet_ep2_t epp;
|
---|
| 93 | udp_msg_t *msg;
|
---|
| 94 | inet_ep2_t depp;
|
---|
| 95 | udp_msg_t *dmsg;
|
---|
| 96 | udp_pdu_t *pdu;
|
---|
| 97 | const char *msgstr = "Hello";
|
---|
| 98 | errno_t rc;
|
---|
| 99 |
|
---|
| 100 | inet_ep2_init(&epp);
|
---|
| 101 | inet_addr6(&epp.local.addr, 0xfc00, 0, 0, 0, 0, 0, 0, 1);
|
---|
| 102 | epp.local.port = 1;
|
---|
| 103 | inet_addr6(&epp.remote.addr, 0xfc00, 0, 0, 0, 0, 0, 0, 2);
|
---|
| 104 | epp.remote.port = 2;
|
---|
| 105 |
|
---|
| 106 | msg = udp_msg_new();
|
---|
| 107 | PCUT_ASSERT_NOT_NULL(msg);
|
---|
| 108 | msg->data_size = str_size(msgstr) + 1;
|
---|
| 109 | msg->data = str_dup(msgstr);
|
---|
| 110 |
|
---|
| 111 | rc = udp_pdu_encode(&epp, msg, &pdu);
|
---|
| 112 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 113 | rc = udp_pdu_decode(pdu, &depp, &dmsg);
|
---|
| 114 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 115 |
|
---|
| 116 | /* Compare epp and depp. Local and remote should be swapped. */
|
---|
| 117 | PCUT_ASSERT_TRUE(inet_addr_compare(&epp.local.addr, &depp.remote.addr));
|
---|
| 118 | PCUT_ASSERT_EQUALS(epp.local.port, depp.remote.port);
|
---|
| 119 | PCUT_ASSERT_TRUE(inet_addr_compare(&epp.remote.addr, &depp.local.addr));
|
---|
| 120 | PCUT_ASSERT_EQUALS(epp.remote.port, depp.local.port);
|
---|
| 121 |
|
---|
| 122 | /* Compare msg and dmsg */
|
---|
| 123 | PCUT_ASSERT_EQUALS(msg->data_size, dmsg->data_size);
|
---|
| 124 | PCUT_ASSERT_TRUE(memcmp(msg->data, dmsg->data, dmsg->data_size) == 0);
|
---|
| 125 |
|
---|
| 126 | udp_pdu_delete(pdu);
|
---|
| 127 | udp_msg_delete(msg);
|
---|
| 128 | udp_msg_delete(dmsg);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | PCUT_EXPORT(pdu);
|
---|