generic tcptrace flows value comparison testing (difference currently)

This commit is contained in:
Raphael Bauduin 2015-05-26 12:00:03 -07:00
parent 83788f5c08
commit ac1e857f79
3 changed files with 41 additions and 11 deletions

View File

@ -5,6 +5,8 @@ from subprocess import check_output
import csv import csv
from io import StringIO from io import StringIO
import re
@ -22,7 +24,16 @@ class TcptraceData:
def get(self, flow, column): def get(self, flow, column):
if flow>self.number_of_flows-1: if flow>self.number_of_flows-1:
raise Exception("Bad flow index") raise Exception("Bad flow index")
return self.flows[flow][self.headers.index(column)] value = self.flows[flow][self.headers.index(column)]
if re.search("[a-zA-Z]", value):
return value
elif re.search("\.", value):
return float(value)
elif re.search("^[0-9]+$", value):
return int(value)
else:
return value
# returns first packet time of flow # returns first packet time of flow
def first_packet(self, flow): def first_packet(self, flow):
return float(self.flows[flow][self.header_index("first_packet")])-float(self.flows[0][self.header_index("first_packet")]) return float(self.flows[flow][self.header_index("first_packet")])-float(self.flows[0][self.header_index("first_packet")])

View File

@ -20,22 +20,27 @@ class Validation:
# checks a value passed is greater or equal (generic) # checks a value passed is greater or equal (generic)
class MinValueValidation(Validation): class MinValueValidation(Validation):
def validate(self, value): def validate(self, value):
self.value = value
return self.compared<=value return self.compared<=value
# checks a value passed is greater or equal (generic) # checks a value passed is greater or equal (generic)
class MaxValueValidation(Validation): class MaxValueValidation(Validation):
def validate(self, value): def validate(self, value):
self.value = value
return self.compared>=value return self.compared>=value
# checks a value passed is greater or equal (generic) # checks a value passed is greater or equal (generic)
class ExactValueValidation(Validation): class ExactValueValidation(Validation):
def validate(self, value): def validate(self, value):
self.value = value
return self.compared==value return self.compared==value
# individual flow validation (specific) # individual flow validation (specific)
# gets flow_spec = (index, flows) where index is the index of the flow to validate, and flows is the array of flows # gets flow_spec = (index, flows) where index is the index of the flow to validate, and flows is the array of flows
class MinDelayValidation(Validation): class MinDelayValidation(Validation):
def validate(self, flow_spec): def validate(self, flow_spec):
(index,trace) = flow_spec (yml,trace) = flow_spec
index=yml["index"]
val = trace.first_packet(index)-trace.first_packet(0) val = trace.first_packet(index)-trace.first_packet(0)
self.value = val
return self.compared<=val return self.compared<=val
# individual flow validation (specific) # individual flow validation (specific)
@ -45,10 +50,19 @@ class MinDelayValidation(Validation):
# - flows is the array of flows # - flows is the array of flows
class MinDelayBetweenValidation(Validation): class MinDelayBetweenValidation(Validation):
def validate(self, flow_spec): def validate(self, flow_spec):
([index0, index1] ,trace) = flow_spec (yml ,trace) = flow_spec
[index0, index1] = yml["index"]
val = trace.first_packet(index1)-trace.first_packet(index0) val = trace.first_packet(index1)-trace.first_packet(index0)
self.value = val
return self.compared<=val return self.compared<=val
class AttributeMinimumDifferenceValidation(Validation):
def validate(self, flow_spec):
(yml ,trace) = flow_spec
[index0, index1] = yml["index"]
val = trace.get(index1, yml["attribute"]) - trace.get(index0, yml["attribute"])
self.value = val
return self.compared<=val
# Base class testing tcptrace results # Base class testing tcptrace results
@ -64,21 +78,19 @@ class TcptraceTest:
def validate(self): def validate(self):
is_ok = True is_ok = True
self.logs = "" self.logs = ""
print self.yml
for val in self.yml: for val in self.yml:
print val["name"]
tested_value = self.get_tested_value(val) tested_value = self.get_tested_value(val)
klass_name=val["name"].title().replace("_","")+"Validation" klass_name=val["name"].title().replace("_","")+"Validation"
tester_klass=globals()[klass_name] tester_klass=globals()[klass_name]
tester = tester_klass(val) tester = tester_klass(val)
try: try:
if tester.validate(tested_value): if tester.validate(tested_value):
self.logs=self.logs+ ("" if self.logs=="" else "\n ")+ " -" +tester.name()+" OK\n" self.logs=self.logs+ " " + " OK :" + val["desc"] +" - " + tester.name()+ " value : " + str(tester.value) +" vs " + str(val["target"]) + "\n"
else: else:
self.logs=self.logs+ ("" if self.logs=="" else "\n ")+ " -" +tester.name()+" FAILS "+ val["desc"] +"\n" self.logs=self.logs+ " " + " FAIL:" + val["desc"] +" - " + tester.name()+ " value : " + str(tester.value) +" vs " + str(val["target"]) + "\n"
is_ok = False is_ok = False
except Exception as e: except Exception as e:
self.logs=self.logs+ ("" if self.logs=="" else "\n ")+ " -" +tester.name()+" FAILS (EXCEPTION:"+ str(e) + ") "+ val["desc"] +"\n" self.logs=self.logs+ ("" if self.logs=="" else "\n ")+ " EXCP:" + val["desc"] +" - " + tester.name()+ " " + str(e) + "\n"
return is_ok return is_ok
def name(self): def name(self):
return self.__class__.__name__ return self.__class__.__name__
@ -94,7 +106,7 @@ class NumberOfFlowsTest(TcptraceTest):
# index can be a compound value, as in the case of min_delay_between where it is an array of indexes # index can be a compound value, as in the case of min_delay_between where it is an array of indexes
class FlowsTest(TcptraceTest): class FlowsTest(TcptraceTest):
def get_tested_value(self, yml): def get_tested_value(self, yml):
return (yml["index"],self.trace) return (yml,self.trace)
# Runs tests based on tcptrace # Runs tests based on tcptrace
@ -111,7 +123,7 @@ class TcptraceChecker:
klass = globals()[name] klass = globals()[name]
r = klass(test, self.trace) r = klass(test, self.trace)
if r.validate(): if r.validate():
self.logs = self.logs + " *" + r.name() + " SUCCESS\n" self.logs = self.logs + " *" + self.test_id + " " + r.name() + " SUCCESS\n"
self.logs = self.logs + r.logs self.logs = self.logs + r.logs
else: else:
self.logs = self.logs + " *" + self.test_id + " " + r.name() + " FAIL\n" self.logs = self.logs + " *" + self.test_id + " " + r.name() + " FAIL\n"

View File

@ -8,7 +8,7 @@ tcptrace:
target: 4 target: 4
desc: "Open maximum 4 flows" desc: "Open maximum 4 flows"
- name: "exact_value" - name: "exact_value"
target: 5 target: 4
desc: "Open exactly 4 flows" desc: "Open exactly 4 flows"
# This tests the flows opened by the connection. See class FlowsTest # This tests the flows opened by the connection. See class FlowsTest
- test: "flows" - test: "flows"
@ -26,3 +26,10 @@ tcptrace:
- 2 - 2
target: 1 target: 1
desc: "Minimum 1 second delay between opening of second and third flow" desc: "Minimum 1 second delay between opening of second and third flow"
- name: "attribute_minimum_difference"
attribute: "first_packet"
index:
- 0
- 1
target: 2
desc: "first packet delay 2nd flow"