forked from edagar/censorship-analyser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
169 lines (129 loc) · 4.84 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import subprocess
from random import choice, randint
from time import sleep
import yaml
from ooni.otime import timestamp
import const
class Test(object):
def __init__(self, testfile, args=[]):
self.testfile = testfile
self.args = args
self.output = None
self.status = None
self.errorMessage = None
self.parser = None
self.report = None
self.reportName = None
def run(self):
self.reportName = "report-%s-%s.yamloo" % (self.testfile, timestamp())
self.output = runTest(self)
self.parseResults()
def parseResults(self):
self.parser = TestParser(self)
self.parser.parseReport()
def printResults(self):
self.parser.printResults()
def getResults(self):
return {
"Status": self.status,
"ErrorMessage": self.errorMessage,
}
class SiteProbe(Test):
def __init__(self, testfile=const.PROBE_TEST, target=const.TOR_SITE_URL):
super(SiteProbe, self).__init__(testfile=testfile, args = ["-u", target])
self.target = target
class TCPTest(Test):
def __init__(self, testfile=const.TCP_TEST, target=const.TOR_DOMAIN, port="443"):
super(TCPTest, self).__init__(testfile=testfile, args=["-t", target, "-p", port])
self.target = target
class PingTest(Test):
def __init__(self, testfile=const.PING_TEST,target=None):
args = ["-t", target] if target is not None else []
super(PingTest, self).__init__(testfile=testfile, args=args)
self.target = target
self.packets = None
def parsePackets(self, report):
try:
return 'echo-reply' in report['ReceivedPackets'][0][0]['summary']
except:
return False
def parseResults(self):
self.parser = TestParser(self)
self.parser.loadReport()
if self.report['TestStatus'] == 'OK':
self.packets = self.report['packets']
if self.parsePackets(self.report):
self.status = "OK"
return
self.status = "FAILED"
self.errorMessage = "Host unreachable"
raise TestException(self)
class DNSTest(Test):
def __init__(self, testfile=const.DNS_TEST, target=const.TOR_DOMAIN):
super(DNSTest, self).__init__(testfile=testfile, args=["-t", target])
self.target = target
class Traceroute(Test):
def __init__(self, testfile=const.TRACEROUTE_TEST, target=None):
args = ["-b", target] if target is not None else []
super(Traceroute, self).__init__(testfile=testfile,args=args)
self.target = target
class TestParser(object):
def __init__(self, test):
self.test = test
def loadReport(self):
with open(self.test.reportName, 'r') as f:
entries = yaml.safe_load_all(f)
headers = entries.next()
self.test.report = entries.next()
def parseReport(self):
self.loadReport()
self.test.status = self.test.report['TestStatus']
if not self.test.status == "OK":
self.test.errorMessage = self.test.report['TestException']
raise TestException(self.test)
def printResults(self):
print "Test: %s" % self.test.testfile
if hasattr(self.test, "target") and self.test.target is not None:
print "Target: %s" % self.test.target
results = self.test.getResults()
for key, value in results.iteritems():
if key and value:
print "%s: %s" % (key, value)
class TestCase(list):
def __init__(self, tests=[], sleep_interval=const.SLEEP_INTERVAL):
super(TestCase, self).__init__(tests)
self.sleepInterval = sleep_interval
def run(self):
tests = testCaseGenerator(list(self))
for test in tests:
try:
test.run()
except TestException, e:
print e
sleep(randint(self.sleepInterval[0], self.sleepInterval[1]))
def printResults(self):
for test in self:
test.printResults()
print
def getFailed(self):
return [test for test in self if test.status != "OK"]
def testCaseGenerator(seq):
for x in range(len(seq)):
test = choice(seq)
seq.remove(test)
yield test
def runTest(test):
binary = const.OONI_BINARY
args = [binary, "-o", test.reportName, "-n", test.testfile]
if test.args:
args += test.args
print "Running test %s" % test.testfile
popen = subprocess.Popen(args, stdout=subprocess.PIPE)
popen.wait()
output = popen.stdout.read()
return output
class TestException(Exception):
def __init__(self, test):
self.testInstance = test
def __str__(self):
return "%s: %s (%s)" % (self.testInstance.testfile, self.testInstance.status, self.testInstance.errorMessage)