Add complete integration tests and real call to ExaBGP for configurartion checks

This commit is contained in:
Pavel Odintsov 2015-07-13 14:36:50 +02:00
parent 00532de6aa
commit dd42422ec1
3 changed files with 94 additions and 70 deletions

View File

@ -9,66 +9,3 @@
#include "log4cpp/PatternLayout.hh"
#include "log4cpp/Priority.hh"
/*
g++ fast_library.cpp -c -o fast_library.o
gcc libpatricia/patricia.c -c -o patricia.o
g++ bgp_flow_spec.cpp fast_library.o patricia.o -lboost_system -lboost_regex -llog4cpp
*/
// For library compilation
log4cpp::Category& logger = log4cpp::Category::getRoot();
int main() {
bgp_flow_spec_action_t my_action;
//my_action.set_type(FLOW_SPEC_ACTION_ACCEPT);
my_action.set_type(FLOW_SPEC_ACTION_RATE_LIMIT);
my_action.set_rate_limit(1024);
exabgp_flow_spec_rule_t exabgp_rule;
exabgp_rule.add_protocol(FLOW_SPEC_PROTOCOL_UDP);
exabgp_rule.add_source_port(53);
exabgp_rule.add_destination_port(80);
exabgp_rule.add_packet_length(777);
exabgp_rule.add_packet_length(1122);
exabgp_rule.add_fragmentation_flag(FLOW_SPEC_IS_A_FRAGMENT);
exabgp_rule.add_fragmentation_flag(FLOW_SPEC_DONT_FRAGMENT);
exabgp_rule.set_destination_subnet( convert_subnet_from_string_to_binary_with_cidr_format("127.0.0.0/24") );
exabgp_rule.set_source_subnet( convert_subnet_from_string_to_binary_with_cidr_format("4.0.0.0/24") );
exabgp_rule.set_action( my_action );
exabgp_rule.announce_is_correct();
//std::cout << exabgp_rule.serialize();
std::cout << exabgp_rule.serialize_complete_exabgp_configuration();
// /usr/src/exabgp/sbin/exabgp --test flow_spec.conf 2> /dev/null ; echo $?
/*
Example output:
flow {
match {
source 4.0.0.0/24;
destination 127.0.0.0/24;
protocol [ udp ];
source-port [ =53 ];
destination-port [ =80 ];
packet-length [ =777 =1122 ];
fragment [ is-fragment dont-fragment ];
}
then {
rate-limit 1024;
}
}
*/
}
void exabgp_flow_spec_rule_ban_manage(std::string action, flow_spec_rule_t flow_spec_rule) {
// "announce flow route {\\n match {\\n source 10.0.0.1/32;\\nsource-port =" + str(i) +
// ";\\n destination 1.2.3.4/32;\\n }\\n then {\\n discard;\\n }\\n }\\n\n")
}

View File

@ -311,9 +311,9 @@ class exabgp_flow_spec_rule_t : public flow_spec_rule_t {
<< four_spaces << four_spaces << "ipv6 flow;" << "\n"
<< four_spaces << "}" << "\n";
buffer << "flow {\n";
buffer << "flow {" << "\n";
buffer << this->serialize();
buffer << "}\n";
buffer << "}" << "\n";
buffer << "}" << "\n";
@ -323,11 +323,24 @@ class exabgp_flow_spec_rule_t : public flow_spec_rule_t {
std::string serialize() {
std::ostringstream buffer;
buffer
<< "route {" << "\n"
<< this->serialize_match()
<< this->serialize_then()
<< "\n" << "}" << "\n";
buffer << "route {";
if (enabled_indents) {
buffer << "\n";
}
buffer << this->serialize_match();
buffer << this->serialize_then();
if (enabled_indents) {
buffer << "\n";
}
buffer << "}";
if (enabled_indents) {
buffer << "\n";
}
return buffer.str();
}
@ -487,4 +500,10 @@ class exabgp_flow_spec_rule_t : public flow_spec_rule_t {
bool enabled_indents;
};
void exabgp_flow_spec_rule_ban_manage(std::string action, flow_spec_rule_t flow_spec_rule) {
// "announce flow route {\\n match {\\n source 10.0.0.1/32;\\nsource-port =" + str(i) +
// ";\\n destination 1.2.3.4/32;\\n }\\n then {\\n discard;\\n }\\n }\\n\n")
}
#endif

View File

@ -4,6 +4,8 @@
#include "fast_library.h"
#include "bgp_flow_spec.h"
#include <fstream>
#include "log4cpp/Category.hh"
#include "log4cpp/Appender.hh"
#include "log4cpp/FileAppender.hh"
@ -219,6 +221,72 @@ TEST(BgpFlowSpec, serialize_then_first) {
EXPECT_EQ( exabgp_rule.serialize_then(), "then {rate-limit 1024;}");
}
TEST(BgpFlowSpec, serialize_whole) {
bgp_flow_spec_action_t my_action;
//my_action.set_type(FLOW_SPEC_ACTION_ACCEPT);
my_action.set_type(FLOW_SPEC_ACTION_RATE_LIMIT);
my_action.set_rate_limit(1024);
exabgp_flow_spec_rule_t exabgp_rule;
exabgp_rule.add_protocol(FLOW_SPEC_PROTOCOL_UDP);
exabgp_rule.add_source_port(53);
exabgp_rule.add_destination_port(80);
exabgp_rule.add_packet_length(777);
exabgp_rule.add_packet_length(1122);
exabgp_rule.add_fragmentation_flag(FLOW_SPEC_IS_A_FRAGMENT);
exabgp_rule.add_fragmentation_flag(FLOW_SPEC_DONT_FRAGMENT);
exabgp_rule.set_destination_subnet( convert_subnet_from_string_to_binary_with_cidr_format("127.0.0.0/24") );
exabgp_rule.set_source_subnet( convert_subnet_from_string_to_binary_with_cidr_format("4.0.0.0/24") );
exabgp_rule.set_action( my_action );
exabgp_rule.disable_indents();
EXPECT_EQ( exabgp_rule.serialize(), "route {match {source 4.0.0.0/24;destination 127.0.0.0/24;protocol [ udp ];source-port [ =53 ];destination-port [ =80 ];packet-length [ =777 =1122 ];fragment [ is-fragment dont-fragment ];}then {rate-limit 1024;}}");
}
TEST(BgpFlowSpec, serialize_with_real_exabgp) {
bgp_flow_spec_action_t my_action;
//my_action.set_type(FLOW_SPEC_ACTION_ACCEPT);
my_action.set_type(FLOW_SPEC_ACTION_RATE_LIMIT);
my_action.set_rate_limit(1024);
exabgp_flow_spec_rule_t exabgp_rule;
exabgp_rule.add_protocol(FLOW_SPEC_PROTOCOL_UDP);
exabgp_rule.add_source_port(53);
exabgp_rule.add_destination_port(80);
exabgp_rule.add_packet_length(777);
exabgp_rule.add_packet_length(1122);
exabgp_rule.add_fragmentation_flag(FLOW_SPEC_IS_A_FRAGMENT);
exabgp_rule.add_fragmentation_flag(FLOW_SPEC_DONT_FRAGMENT);
exabgp_rule.set_destination_subnet( convert_subnet_from_string_to_binary_with_cidr_format("127.0.0.0/24") );
exabgp_rule.set_source_subnet( convert_subnet_from_string_to_binary_with_cidr_format("4.0.0.0/24") );
exabgp_rule.set_action( my_action );
//exabgp_rule.disable_indents();
std::string exabgp_configuration = exabgp_rule.serialize_complete_exabgp_configuration();
std::ofstream config_file;
config_file.open("/tmp/exabgp_test_config.conf", std::ios::trunc);
if (config_file.is_open()) {
config_file << exabgp_configuration;
config_file.close();
}
int system_ret_code = system("/usr/src/exabgp/sbin/exabgp --test /tmp/exabgp_test_config.conf 2>/dev/null");
EXPECT_EQ( system_ret_code, 0 );
}
// Flow Spec actions tests
TEST(BgpFlowSpecAction, rate_limit) {