-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathcommon.py
More file actions
79 lines (66 loc) · 2.24 KB
/
common.py
File metadata and controls
79 lines (66 loc) · 2.24 KB
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
""" ./QA/common.py - common code for QA """
import contextlib
import json
import os
import shutil
import socket
import subprocess
def execute(args):
"""Execute a specified command"""
subprocess.run(args)
def execute_jafar_and_miniooni(ooni_exe, outfile, experiment, tag, args):
"""Executes jafar and miniooni. Returns the test keys."""
tmpoutfile = "/tmp/{}".format(outfile)
with contextlib.suppress(FileNotFoundError):
os.remove(tmpoutfile) # just in case
execute(
[
# Disable ASLR
"setarch",
"--addr-no-randomize",
# Run cachegrind
"valgrind",
"--tool=cachegrind",
"--I1=32768,8,64",
"--D1=32768,8,64",
"--LL=8388608,16,64",
"--cachegrind-out-file=cachegrind.out",
# Run jafar
"./jafar",
"-main-command",
"./QA/minioonilike.py {} -n -o '{}' --home /tmp {}".format(
ooni_exe, tmpoutfile, experiment
),
"-main-user",
"nobody", # should be present on Unix
"-tag",
tag,
]
+ args
)
shutil.copy(tmpoutfile, outfile)
result = read_result(outfile)
assert isinstance(result, dict)
assert isinstance(result["test_keys"], dict)
return result["test_keys"]
def read_result(outfile):
"""Reads the result of an experiment"""
return json.load(open(outfile, "rb"))
def test_keys(result):
"""Returns just the test keys of a specific result"""
return result["test_keys"]
def check_maybe_binary_value(value):
"""Make sure a maybe binary value is correct"""
assert isinstance(value, str) or (
isinstance(value, dict)
and value["format"] == "base64"
and isinstance(value["data"], str)
)
def with_free_port(func):
"""This function executes |func| passing it a port number on localhost
which is bound but not listening for new connections"""
# See <https://stackoverflow.com/a/45690594>
with contextlib.closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock:
sock.bind(("127.0.0.1", 0))
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
func(sock.getsockname()[1])