t_address.cpp

Go to the documentation of this file.
00001 /*****************************************************************************\
00002 *                                                                             *
00003 *  Name   : test_address                                                      *
00004 *  Author : Chris Koeritz                                                     *
00005 *                                                                             *
00006 *  Purpose:                                                                   *
00007 *                                                                             *
00008 *    Tests some attributes of the network_address class for correctness.      *
00009 *                                                                             *
00010 *******************************************************************************
00011 * Copyright (c) 2001-$now By Author.  This program is free software; you can  *
00012 * redistribute it and/or modify it under the terms of the GNU General Public  *
00013 * License as published by the Free Software Foundation; either version 2 of   *
00014 * the License or (at your option) any later version.  This is online at:      *
00015 *     http://www.fsf.org/copyleft/gpl.html                                    *
00016 * Please send any updates to: fred@gruntose.com                               *
00017 \*****************************************************************************/
00018 
00019 #include <basis/byte_array.h>
00020 #include <basis/istring.h>
00021 #include <opsystem/application_shell.h>
00022 #include <loggers/console_logger.h>
00023 #include <data_struct/static_memory_gremlin.h>
00024 #include <sockets/address.h>
00025 
00026 #include <stdio.h>
00027 #include <string.h>
00028 
00029 class test_address : public application_shell
00030 {
00031 public:
00032   test_address() : application_shell(class_name()) {}
00033   IMPLEMENT_CLASS_NAME("test_address");
00034   virtual int execute();
00035 };
00036 
00037 int test_address::execute()
00038 {
00039   // testing is_valid_internet_address here...
00040 
00041   bool all_zero = false;
00042   byte_array ip_form;
00043   istring to_test = "12.5.55.37";
00044   if (!internet_address::is_valid_internet_address(to_test, ip_form, all_zero))
00045     deadly_error(class_name(), "1st address",
00046         "failed to say address was valid");
00047   if (all_zero)
00048     deadly_error(class_name(), "1st address", "said address was all zeros");
00049   if ( (ip_form[0] != 12) || (ip_form[1] != 5) || (ip_form[2] != 55)
00050       || (ip_form[3] != 37) )
00051     deadly_error(class_name(), "1st address", "address had incorrect contents");
00052 
00053   to_test = "12.5.55.372";
00054   if (internet_address::is_valid_internet_address(to_test, ip_form, all_zero))
00055     deadly_error(class_name(), "2nd address", "failed to say address was invalid");
00056 
00057   to_test = "12.5.55.37.3";
00058   if (internet_address::is_valid_internet_address(to_test, ip_form, all_zero))
00059     deadly_error(class_name(), "3rd address", "failed to say address was invalid");
00060 
00061   to_test = "12.5.55";
00062   if (internet_address::is_valid_internet_address(to_test, ip_form, all_zero))
00063     deadly_error(class_name(), "4th address", "failed to say address was invalid");
00064 
00065   to_test = "0.0.0.0";
00066   if (!internet_address::is_valid_internet_address(to_test, ip_form, all_zero))
00067     deadly_error(class_name(), "5th address", "failed to say address was valid");
00068   if (!all_zero)
00069     deadly_error(class_name(), "5th address", "said address was not all zeros");
00070   if ( (ip_form[0] != 0) || (ip_form[1] != 0) || (ip_form[2] != 0)
00071       || (ip_form[3] != 0) )
00072     deadly_error(class_name(), "5th address", "address had incorrect contents");
00073 
00074   to_test = "0.0.0.1";
00075   if (!internet_address::is_valid_internet_address(to_test, ip_form, all_zero))
00076     deadly_error(class_name(), "6th address", "failed to say address was valid");
00077   if (all_zero)
00078     deadly_error(class_name(), "6th address", "said address was all zeros");
00079   if ( (ip_form[0] != 0) || (ip_form[1] != 0) || (ip_form[2] != 0)
00080       || (ip_form[3] != 1) )
00081     deadly_error(class_name(), "6th address", "address had incorrect contents");
00082 
00083   to_test = "0.0.0.";
00084   if (internet_address::is_valid_internet_address(to_test, ip_form, all_zero))
00085     deadly_error(class_name(), "7th address", "failed to say address was invalid");
00086 
00087   to_test = "0.0.0";
00088   if (internet_address::is_valid_internet_address(to_test, ip_form, all_zero))
00089     deadly_error(class_name(), "7th address", "failed to say address was invalid");
00090 
00091   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";
00092   istring found;
00093   if (!internet_address::has_ip_address(to_test, found))
00094     deadly_error(class_name(), "8th address", "failed to find ip address");
00095   if (found != "23.15.123.5")
00096     deadly_error(class_name(), "8th address", istring("ip address found was wrong: ") + found + ", should have been 23.15.123.5");
00097 
00098   to_test = "furples 92.23.1 9123.5.3 12398. 23 11 1 1 0202 2.4.1.5";
00099   if (!internet_address::has_ip_address(to_test, found))
00100     deadly_error(class_name(), "9th address", "failed to find ip address");
00101   if (found != "2.4.1.5")
00102     deadly_error(class_name(), "9th address", istring("ip address found was wrong: ") + found + ", should have been 2.4.1.5");
00103 
00104   to_test = "12.5.55.2\"";
00105   if (!internet_address::has_ip_address(to_test, found))
00106     deadly_error(class_name(), "10th address", "failed to find ip address");
00107   if (found != "12.5.55.2")
00108     deadly_error(class_name(), "10th address", istring("ip address found was wrong: ") + found + ", should have been 12.5.55.2");
00109 
00110   to_test = "12.5.55.2";
00111   if (!internet_address::has_ip_address(to_test, found))
00112     deadly_error(class_name(), "11th address", "failed to find ip address");
00113   if (found != "12.5.55.2")
00114     deadly_error(class_name(), "11th address", istring("ip address found was wrong: ") + found + ", should have been 12.5.55.2");
00115 
00116 //some more tests would be nice...
00117 
00118   log("network_address:: works for those functions tested.");
00119   return 0;
00120 }
00121 
00122 HOOPLE_MAIN(test_address, )
00123 

Generated on Fri Nov 28 04:29:34 2008 for HOOPLE Libraries by  doxygen 1.5.1