feisty meow concerns codebase  2.140
test_address.cpp
Go to the documentation of this file.
1 /*
2 * Name : test_address
3 * Author : Chris Koeritz
4 * Purpose: Tests some attributes of the network_address class for correctness.
5 **
6 * Copyright (c) 2001-$now By Author. This program is free software; you can *
7 * redistribute it and/or modify it under the terms of the GNU General Public *
8 * License as published by the Free Software Foundation; either version 2 of *
9 * the License or (at your option) any later version. This is online at: *
10 * http://www.fsf.org/copyleft/gpl.html *
11 * Please send any updates to: fred@gruntose.com *
12 */
13 
15 #include <basis/byte_array.h>
16 #include <basis/astring.h>
20 #include <unit_test/unit_base.h>
21 
22 using namespace application;
23 using namespace basis;
24 //using namespace filesystem;
25 using namespace loggers;
26 using namespace mathematics;
27 using namespace sockets;
28 using namespace structures;
29 using namespace textual;
30 using namespace timely;
31 using namespace unit_test;
32 
33 //#include <stdio.h>
34 //#include <string.h>
35 
36 #define LOG(to_print) EMERGENCY_LOG(program_wide_logger().get(), astring(to_print))
37 
38 class test_address : public virtual unit_base, virtual public application_shell
39 {
40 public:
41  test_address() {}
42  DEFINE_CLASS_NAME("test_address");
43  virtual int execute();
44 };
45 
46 int test_address::execute()
47 {
48  FUNCDEF("execute");
49  // testing is_valid_internet_address here...
50 
51  bool all_zero = false;
52  byte_array ip_form;
53  astring to_test = "12.5.55.37";
54  if (!internet_address::is_valid_internet_address(to_test, ip_form, all_zero))
55  deadly_error(class_name(), "1st address",
56  "failed to say address was valid");
57  if (all_zero)
58  deadly_error(class_name(), "1st address", "said address was all zeros");
59  if ( (ip_form[0] != 12) || (ip_form[1] != 5) || (ip_form[2] != 55)
60  || (ip_form[3] != 37) )
61  deadly_error(class_name(), "1st address", "address had incorrect contents");
62 
63  to_test = "12.5.55.372";
64  if (internet_address::is_valid_internet_address(to_test, ip_form, all_zero))
65  deadly_error(class_name(), "2nd address", "failed to say address was invalid");
66 
67  to_test = "12.5.55.37.3";
68  if (internet_address::is_valid_internet_address(to_test, ip_form, all_zero))
69  deadly_error(class_name(), "3rd address", "failed to say address was invalid");
70 
71  to_test = "12.5.55";
72  if (internet_address::is_valid_internet_address(to_test, ip_form, all_zero))
73  deadly_error(class_name(), "4th address", "failed to say address was invalid");
74 
75  to_test = "0.0.0.0";
76  if (!internet_address::is_valid_internet_address(to_test, ip_form, all_zero))
77  deadly_error(class_name(), "5th address", "failed to say address was valid");
78  if (!all_zero)
79  deadly_error(class_name(), "5th address", "said address was not all zeros");
80  if ( (ip_form[0] != 0) || (ip_form[1] != 0) || (ip_form[2] != 0)
81  || (ip_form[3] != 0) )
82  deadly_error(class_name(), "5th address", "address had incorrect contents");
83 
84  to_test = "0.0.0.1";
85  if (!internet_address::is_valid_internet_address(to_test, ip_form, all_zero))
86  deadly_error(class_name(), "6th address", "failed to say address was valid");
87  if (all_zero)
88  deadly_error(class_name(), "6th address", "said address was all zeros");
89  if ( (ip_form[0] != 0) || (ip_form[1] != 0) || (ip_form[2] != 0)
90  || (ip_form[3] != 1) )
91  deadly_error(class_name(), "6th address", "address had incorrect contents");
92 
93  to_test = "0.0.0.";
94  if (internet_address::is_valid_internet_address(to_test, ip_form, all_zero))
95  deadly_error(class_name(), "7th address", "failed to say address was invalid");
96 
97  to_test = "0.0.0";
98  if (internet_address::is_valid_internet_address(to_test, ip_form, all_zero))
99  deadly_error(class_name(), "7th address", "failed to say address was invalid");
100 
101  to_test = "this may have. an ip address in it somewhere... 92.21. 23.123. 1235.6.3 9 oops hey where is it. 23.51.2 2.4 1.2.343 09023.2.3. marbles 23.15.123.5 manus kobble 23.1.5.2 sturp nort ation";
102  astring found;
103  if (!internet_address::has_ip_address(to_test, found))
104  deadly_error(class_name(), "8th address", "failed to find ip address");
105  ASSERT_EQUAL(found, astring("23.15.123.5"), "8th address: ip address found should be correct");
106 
107  to_test = "furples 92.23.1 9123.5.3 12398. 23 11 1 1 0202 2.4.1.5";
108  if (!internet_address::has_ip_address(to_test, found))
109  deadly_error(class_name(), "9th address", "failed to find ip address");
110  if (found != astring("2.4.1.5"))
111  deadly_error(class_name(), "9th address", astring("ip address found was wrong: ") + found + ", should have been 2.4.1.5");
112 
113  to_test = "12.5.55.2\"";
114  if (!internet_address::has_ip_address(to_test, found))
115  deadly_error(class_name(), "10th address", "failed to find ip address");
116  if (found != astring("12.5.55.2"))
117  deadly_error(class_name(), "10th address", astring("ip address found was wrong: ") + found + ", should have been 12.5.55.2");
118 
119  to_test = "12.5.55.2";
120  if (!internet_address::has_ip_address(to_test, found))
121  deadly_error(class_name(), "11th address", "failed to find ip address");
122  if (found != astring("12.5.55.2"))
123  deadly_error(class_name(), "11th address", astring("ip address found was wrong: ") + found + ", should have been 12.5.55.2");
124 
125  return final_report();
126 }
127 
128 HOOPLE_MAIN(test_address, )
129 
The application_shell is a base object for console programs.
Provides a dynamically resizable ASCII character string.
Definition: astring.h:35
A very common template for a dynamic array of bytes.
Definition: byte_array.h:36
#define deadly_error(c, f, i)
#define DEFINE_CLASS_NAME(objname)
Defines the name of a class by providing a couple standard methods.
Definition: enhance_cpp.h:45
#define FUNCDEF(func_in)
FUNCDEF sets the name of a function (and plugs it into the callstack).
Definition: enhance_cpp.h:57
Provides macros that implement the 'main' program of an application.
#define HOOPLE_MAIN(obj_name, obj_args)
options that should work for most unix and linux apps.
Definition: hoople_main.h:61
Implements an application lock to ensure only one is running at once.
The guards collection helps in testing preconditions and reporting errors.
Definition: array.h:30
A logger that sends to the console screen using the standard output device.
An extension to floating point primitives providing approximate equality.
Definition: averager.h:21
Provides access to the operating system's socket methods.
Definition: base_address.h:26
A dynamic container class that holds any kind of object via pointers.
Definition: amorph.h:55
#include <time.h>
Definition: earth_time.cpp:37
Useful support functions for unit testing, especially within hoople.
Definition: unit_base.cpp:35
#define ASSERT_EQUAL(a, b, test_name)
Definition: unit_base.h:38