Goby v2
test.cpp
1 // Copyright 2009-2018 Toby Schneider (http://gobysoft.org/index.wt/people/toby)
2 // GobySoft, LLC (2013-)
3 // Massachusetts Institute of Technology (2007-2014)
4 //
5 //
6 // This file is part of the Goby Underwater Autonomy Project Binaries
7 // ("The Goby Binaries").
8 //
9 // The Goby Binaries are free software: you can redistribute them and/or modify
10 // them under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // The Goby Binaries are distributed in the hope that they will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with Goby. If not, see <http://www.gnu.org/licenses/>.
21 
22 #include "goby/acomms/ip_codecs.h"
23 
24 #include <arpa/inet.h>
25 #include <netinet/in.h>
26 
27 using namespace goby::common::logger;
28 using goby::glog;
29 
30 void ip_test(const std::string& hex, dccl::Codec& dccl_1)
31 {
33  std::string header_data(goby::util::hex_decode(hex));
34  dccl_1.decode(header_data, &iphdr);
35  glog.is(VERBOSE) && glog << "Header is: " << goby::util::hex_encode(header_data) << "\n"
36  << iphdr.DebugString() << std::endl;
37 
38  std::string test;
39  dccl_1.encode(&test, iphdr);
40  glog.is(VERBOSE) && glog << "Re-encoded as: " << goby::util::hex_encode(test) << std::endl;
41  assert(test == header_data);
42 
43  unsigned orig_cs = iphdr.header_checksum();
44  iphdr.set_header_checksum(0);
45  dccl_1.encode(&test, iphdr);
46 
47  assert(goby::acomms::net_checksum(header_data) == 0);
48  assert(goby::acomms::net_checksum(test) == orig_cs);
49 }
50 
51 int main(int argc, char* argv[])
52 {
53  goby::glog.add_stream(goby::common::logger::DEBUG3, &std::cerr);
54  goby::glog.set_name(argv[0]);
55 
56  dccl::FieldCodecManager::add<goby::acomms::IPGatewayEmptyIdentifierCodec<0xF001> >(
57  "ip_gateway_id_codec_1");
58  dccl::FieldCodecManager::add<goby::acomms::NetShortCodec>("net.short");
59  dccl::FieldCodecManager::add<goby::acomms::IPv4AddressCodec>("ip.v4.address");
60  dccl::FieldCodecManager::add<goby::acomms::IPv4FlagsFragOffsetCodec>("ip.v4.flagsfragoffset");
61 
62  dccl::Codec dccl_1("ip_gateway_id_codec_1");
64 
65  // ICMP Ping
66  ip_test("4500001c837e40004001360dc0a80001c0a80004", dccl_1);
67  // UDP
68  ip_test("4500003e2fe8400040116d42c0a88e32c0a88e01", dccl_1);
69  // UDP
70  ip_test("4500004cb803000038111b4d40712005c0a88e32", dccl_1);
71 
72  std::cout << "all tests passed" << std::endl;
73 }
void set_name(const std::string &s)
Set the name of the application that the logger is serving.
Definition: flex_ostream.h:67
Definition: as.cpp:24
common::FlexOstream glog
Access the Goby logger through this object.
void add_stream(logger::Verbosity verbosity=logger::VERBOSE, std::ostream *os=0)
Attach a stream object (e.g. std::cout, std::ofstream, ...) to the logger with desired verbosity...
Definition: flex_ostream.h:96