xorp

test_utils.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 
00003 // Copyright (c) 2001-2011 XORP, Inc and Others
00004 //
00005 // This program is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License, Version 2, June
00007 // 1991 as published by the Free Software Foundation. Redistribution
00008 // and/or modification of this program under the terms of any other
00009 // version of the GNU General Public License is not permitted.
00010 // 
00011 // This program is distributed in the hope that it will be useful, but
00012 // WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
00014 // see the GNU General Public License, Version 2, a copy of which can be
00015 // found in the XORP LICENSE.gpl file.
00016 // 
00017 // XORP Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
00018 // http://xorp.net
00019 
00020 // $XORP: xorp/rip/test_utils.hh,v 1.16 2008/10/02 21:58:18 bms Exp $
00021 
00022 #ifndef __RIP_TEST_UTILS_HH__
00023 #define __RIP_TEST_UTILS_HH__
00024 
00025 #include "libxorp/xorp.h"
00026 #include "libxorp/ipv4.hh"
00027 #include "libxorp/ipv6.hh"
00028 #include "libxorp/ipnet.hh"
00029 
00030 
00031 
00032 
00033 #include "peer.hh"
00034 #include "route_db.hh"
00035 
00036 
00037 template <typename A>
00038 inline A random_addr();
00039 
00043 template <>
00044 IPv4
00045 random_addr<IPv4>()
00046 {
00047     uint32_t h = htonl(xorp_random());
00048     return IPv4(h);
00049 }
00050 
00054 template <>
00055 IPv6
00056 random_addr<IPv6>()
00057 {
00058     uint32_t x[4];
00059     x[0] = htonl(xorp_random());
00060     x[1] = htonl(xorp_random());
00061     x[2] = htonl(xorp_random());
00062     x[3] = htonl(xorp_random());
00063     return IPv6(x);
00064 }
00065 
00069 template <typename A>
00070 void
00071 make_nets(set<IPNet<A> >& nets, uint32_t n_nets)
00072 {
00073     // attempt at deterministic nets sequence
00074     while (nets.size() != n_nets) {
00075     A addr = random_addr<A>();
00076     IPNet<A> net = IPNet<A>(addr, 1 + xorp_random() % A::ADDR_BITLEN);
00077     nets.insert(net);
00078     }
00079 }
00080 
00084 template <typename A>
00085 struct SplitNets : public unary_function<A,void>
00086 {
00087     SplitNets(set<IPNet<A> >& a, set<IPNet<A> >& b) : _s1(a), _s2(b), _n(0)
00088     {}
00089 
00090     void operator() (const IPNet<A>& net)
00091     {
00092     if (_n % 2) {
00093         _s1.insert(net);
00094     } else {
00095         _s2.insert(net);
00096     }
00097     _n++;
00098     }
00099 protected:
00100     set<IPNet<A> >&     _s1;
00101     set<IPNet<A> >&     _s2;
00102     uint32_t        _n;
00103 };
00104 
00108 template <typename A>
00109 struct RouteInjector : public unary_function<A,void>
00110 {
00111     RouteInjector(RouteDB<A>& r, const A& my_addr, const string& ifname,
00112           const string& vifname, uint32_t cost,
00113           Peer<A>* peer)
00114     : _r(r), _m(my_addr), _ifname(ifname), _vifname(vifname),
00115       _c(cost), _p(peer), _injected(0) {}
00116 
00117     void operator() (const IPNet<A>& net)
00118     {
00119     if (_r.update_route(net, _m, _ifname, _vifname, _c, 0, _p,
00120                 PolicyTags(), false) == true) {
00121         _injected++;
00122     } else {
00123         fprintf(stderr, "Failed to update %s\n", net.str().c_str());
00124     }
00125     }
00126     uint32_t injected() const { return _injected; }
00127 
00128 protected:
00129     RouteDB<A>& _r;
00130     A       _m;
00131     string  _ifname;
00132     string  _vifname;
00133     uint32_t    _c;
00134     Peer<A>*    _p;
00135     uint32_t    _injected;
00136 };
00137 
00139 //
00140 // Verbosity level control
00141 //
00142 
00143 static bool s_verbose = false;
00144 bool verbose()                  { return s_verbose; }
00145 void set_verbose(bool v)        { s_verbose = v; }
00146 
00147 #define verbose_log(x...) _verbose_log(__FILE__,__LINE__, x)
00148 
00149 #define _verbose_log(file, line, x...)                  \
00150 do {                                    \
00151     if (verbose()) {                            \
00152     printf("From %s:%d: ", file, line);             \
00153     printf(x);                          \
00154     fflush(stdout);                         \
00155     }                                   \
00156 } while(0)
00157 
00158 #endif // __RIP_TEST_UTILS_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations