aboutsummaryrefslogtreecommitdiff
path: root/src/test/python/test_voting_single.py
blob: 630f92e9de0420f33b4005809d583b5c2a3920a1 (plain) (blame)
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
"""
Test the voting implementation with a single authority.
"""
import os
import subprocess
import time


NUM_AUTHORITIES = 1
NUM_VOTERS = 1


def wait_for_after(ts):
  now = time.time()
  if now < ts:
    time.sleep(ts - now)

def get_config(section, option, filename=None, expand=False):
  args = ["gnunet-config"]
  if filename is not None:
    args.extend(["-c", filename])
  args.extend(["-s", section])
  args.extend(["-o", option])
  if expand:
    args.extend(["-f"])
  return subprocess.check_output(args).strip()


def create_identity(name, config=None):
  args = ["gnunet-identity", "-C", name]
  if config is not None:
    args.extend(["-c", config])
  subprocess.check_call(args)

def get_identity_pubkey(name, config=None):
  args = ["gnunet-identity", "-d", name]
  if config is not None:
    args.extend(["-c", config])
  out = subprocess.check_output(args)
  components = out.split("-")
  return components[-1].strip()

testdir = subprocess.check_output(["mktemp", "-d", "test-voting-XXXXXXXXXX.d", "--tmpdir"])
testdir = testdir.strip()
ballot = os.path.join(testdir, "ballot")
print "testdir", testdir


testbed_conf = "test_voting.conf"
env = os.environ.copy()
env["GNUNET_TESTING_PREFIX"] = testdir
testbed = subprocess.Popen(["gnunet-testbed-profiler", "-n", "-c", testbed_conf, "-p", "1"], env=env)

ballot_filename = os.path.join(testdir, "ballot")

conf = []
conf.append(os.path.join(testdir, "0", "config"))

for c in conf:
  while not os.path.exists(c):
    print "waiting for creation of", c
    time.sleep(0.1)

print "test dir:", testdir


# start authority
# FIXME: using gnunet-arm for this might be nicer,
auth = subprocess.Popen(["gnunet-daemon-ballot-tally", "-c", conf[0]])

private_key_filename = get_config("peer", "private_key", conf[0], expand=True)

public_key = subprocess.check_output(["gnunet-ecc", "--print-public-key", private_key_filename]).strip()

print "public key", public_key

create_identity("voter0", conf[0])
create_identity("issuer", conf[0])
create_identity("groupca", conf[0])

print "created identities"

ballot = open(ballot_filename, "w")

now = int(time.time())
TS_KEYGEN_START = now + 10
TS_KEYGEN_END = now + 20
TS_START = now + 20
TS_CLOSING = now + 25
TS_CONCLUDE = now + 45
TS_QUERY = now + 65
TS_END = now + 65

ballot.write("[authorities]\n")
ballot.write("auth0 = %s\n" % public_key)
ballot.write("[election]\n")
ballot.write("TOPIC = mytopic\n")
ballot.write("THRESHOLD = 1\n")
ballot.write("CHOICES = yes//no\n")
ballot.write("GROUP = mygroup\n")
ballot.write("CA_PUB = %s\n" % get_identity_pubkey("groupca", conf[0]))
ballot.write("TIMESTAMP_KEYGEN_START = %s\n" % TS_KEYGEN_START)
ballot.write("TIMESTAMP_KEYGEN_END = %s\n" % TS_KEYGEN_END)
ballot.write("TIMESTAMP_START = %s\n" % TS_START)
ballot.write("TIMESTAMP_CLOSING = %s\n" % TS_CLOSING)
ballot.write("TIMESTAMP_CONCLUDE = %s\n" % TS_CONCLUDE)
ballot.write("TIMESTAMP_QUERY = %s\n" % TS_QUERY)
ballot.write("TIMESTAMP_END = %s\n" % TS_END)

ballot.close()

groupcert_filename = os.path.join(testdir, "v0-cert")
groupcert_file = open(groupcert_filename, "w")

v0_pub = get_identity_pubkey("voter0", conf[0])

subprocess.check_call(["gnunet-ballot-group-certify", "-c", conf[0],
  "-g", "mygroup", "-e", "groupca", "-m", v0_pub], stdout=groupcert_file)

groupcert_file.close()

# register the ballot with authorities
subprocess.check_call(["gnunet-ballot", "-LINFO", "-i", ballot_filename, "-e", "issuer", "-c", conf[0]])

# register the ballot with authorities
subprocess.check_call(["gnunet-ballot", "-LINFO", "-r", ballot_filename, "-c", conf[0]])

wait_for_after(TS_KEYGEN_END)

print "getting threshold public key"

# get the threshold public key
subprocess.check_call(["gnunet-ballot", "-LDEBUG", "-k", ballot_filename, "-c", conf[0]])

print "threshold public key retrieved"

# add voter's group information
subprocess.check_call(["gnunet-ballot", "-g", groupcert_filename, ballot_filename, "-c", conf[0]])

# actually vote ...
subprocess.check_call(["gnunet-ballot", "-x", "yes", ballot_filename, "-e", "voter0", "-c", conf[0]])

wait_for_after(TS_START)

# submit the ballot with the vote
subprocess.check_call(["gnunet-ballot", "-s", ballot_filename, "-c", conf[0]])

wait_for_after(TS_QUERY)

# query the result
subprocess.check_call(["gnunet-ballot", "-q", ballot_filename, "-c", conf[0]])

# FIXME: cleanup