Skip to content

Commit 8477859

Browse files
authored
Merge pull request #184 from dedis/amine
Refactoring of contracts/evoting/types/ballots_test.go :
2 parents 96559a3 + ea40b0d commit 8477859

File tree

2 files changed

+119
-84
lines changed

2 files changed

+119
-84
lines changed

contracts/evoting/types/ballots.go

+26-6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ import (
1010
"golang.org/x/xerrors"
1111
)
1212

13+
const (
14+
selectID = "select"
15+
rankID = "rank"
16+
textID = "text"
17+
)
18+
1319
// Ballot contains all information about a simple ballot
1420
type Ballot struct {
1521

@@ -81,7 +87,7 @@ func (b *Ballot) Unmarshal(marshalledBallot string, election Election) error {
8187

8288
switch question[0] {
8389

84-
case "select":
90+
case selectID:
8591
selections := strings.Split(question[2], ",")
8692

8793
selectQ := Select{
@@ -100,7 +106,7 @@ func (b *Ballot) Unmarshal(marshalledBallot string, election Election) error {
100106
b.SelectResultIDs = append(b.SelectResultIDs, ID(questionID))
101107
b.SelectResult = append(b.SelectResult, results)
102108

103-
case "rank":
109+
case rankID:
104110
ranks := strings.Split(question[2], ",")
105111

106112
rankQ := Rank{
@@ -118,7 +124,7 @@ func (b *Ballot) Unmarshal(marshalledBallot string, election Election) error {
118124
b.RankResultIDs = append(b.RankResultIDs, ID(questionID))
119125
b.RankResult = append(b.RankResult, results)
120126

121-
case "text":
127+
case textID:
122128
texts := strings.Split(question[2], ",")
123129

124130
textQ := Text{
@@ -311,21 +317,21 @@ func (s *Subject) MaxEncodedSize() int {
311317

312318
//TODO : optimise by computing max size according to number of choices and maxN
313319
for _, rank := range s.Ranks {
314-
size += len("rank::")
320+
size += len(rank.GetID() + "::")
315321
size += len(rank.ID)
316322
// at most 3 bytes (128) + ',' per choice
317323
size += len(rank.Choices) * 4
318324
}
319325

320326
for _, selection := range s.Selects {
321-
size += len("select::")
327+
size += len(selection.GetID() + "::")
322328
size += len(selection.ID)
323329
// 1 bytes (0/1) + ',' per choice
324330
size += len(selection.Choices) * 2
325331
}
326332

327333
for _, text := range s.Texts {
328-
size += len("text::")
334+
size += len(text.GetID() + "::")
329335
size += len(text.ID)
330336

331337
// at most 4 bytes per character + ',' per answer
@@ -401,6 +407,7 @@ type Question interface {
401407
GetMaxN() uint
402408
GetMinN() uint
403409
GetChoicesLength() int
410+
GetID() string
404411
}
405412

406413
func isValid(q Question) bool {
@@ -418,6 +425,11 @@ type Select struct {
418425
Choices []string
419426
}
420427

428+
// GetID implements Question
429+
func (s Select) GetID() string {
430+
return selectID
431+
}
432+
421433
// GetMaxN implements Question
422434
func (s Select) GetMaxN() uint {
423435
return s.MaxN
@@ -478,6 +490,10 @@ type Rank struct {
478490
Choices []string
479491
}
480492

493+
func (r Rank) GetID() string {
494+
return rankID
495+
}
496+
481497
// GetMaxN implements Question
482498
func (r Rank) GetMaxN() uint {
483499
return r.MaxN
@@ -548,6 +564,10 @@ type Text struct {
548564
Choices []string
549565
}
550566

567+
func (t Text) GetID() string {
568+
return textID
569+
}
570+
551571
// GetMaxN implements Question
552572
func (t Text) GetMaxN() uint {
553573
return t.MaxN

contracts/evoting/types/ballots_test.go

+93-78
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,32 @@ import (
88
"github.com/stretchr/testify/require"
99
)
1010

11-
var ballot1 = string("select:" + encodedQuestionID(1) + ":1,0,1\n" +
12-
"rank:" + encodedQuestionID(2) + ":1,2,0,,\n" +
13-
"select:" + encodedQuestionID(3) + ":1,0,1,1\n" +
14-
"text:" + encodedQuestionID(4) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n\n")
15-
16-
var ballot2 = string("select:" + encodedQuestionID(1) + ":0,0,0\n" +
17-
"rank:" + encodedQuestionID(2) + ":128,128,128,128\n" +
18-
"select:" + encodedQuestionID(3) + ":0,0,0,0,0\n" +
19-
"text:" + encodedQuestionID(4) + ":xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx," +
11+
const (
12+
selectIDTest = "select:"
13+
rankIDTest = "rank:"
14+
textIDTest = "text:"
15+
unmarshalingRankID = "could not unmarshal rank answers: "
16+
unmarshalingTextID = "could not unmarshal text answers: "
17+
)
18+
19+
// Creating a ballot for the first question, which is a select question.
20+
var ballot1 = string(selectIDTest + encodedQuestionID(1) + ":1,0,1\n" +
21+
rankIDTest + encodedQuestionID(2) + ":1,2,0,,\n" +
22+
selectIDTest + encodedQuestionID(3) + ":1,0,1,1\n" +
23+
textIDTest + encodedQuestionID(4) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n\n")
24+
25+
// Creating a ballot with the following questions:
26+
// 1. Select one of three options
27+
// 2. Rank four options
28+
// 3. Select one of five options
29+
// 4. Write two text answers
30+
// 5. Write one text answer
31+
var ballot2 = string(selectIDTest + encodedQuestionID(1) + ":0,0,0\n" +
32+
rankIDTest + encodedQuestionID(2) + ":128,128,128,128\n" +
33+
selectIDTest + encodedQuestionID(3) + ":0,0,0,0,0\n" +
34+
textIDTest + encodedQuestionID(4) + ":xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx," +
2035
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n" +
21-
"text:" + encodedQuestionID(5) + ":xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,,\n\n")
36+
textIDTest + encodedQuestionID(5) + ":xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,,\n\n")
2237

2338
func encodedQuestionID(i int) ID {
2439
return ID(base64.StdEncoding.EncodeToString([]byte("Q" + strconv.Itoa(i))))
@@ -105,28 +120,28 @@ func TestBallot_Unmarshal(t *testing.T) {
105120
require.EqualError(t, err, "a line in the ballot has length != 3: x")
106121

107122
// with ID not encoded in base64
108-
ballotWrongID := string("select:" + "aaa" + ":1,0,1\n" +
109-
"rank:" + encodedQuestionID(2) + ":1,2,0,,\n" +
110-
"select:" + encodedQuestionID(3) + ":1,0,1,1\n" +
111-
"text:" + encodedQuestionID(4) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n\n")
123+
ballotWrongID := string(selectIDTest + "aaa" + ":1,0,1\n" +
124+
rankIDTest + encodedQuestionID(2) + ":1,2,0,,\n" +
125+
selectIDTest + encodedQuestionID(3) + ":1,0,1,1\n" +
126+
textIDTest + encodedQuestionID(4) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n\n")
112127

113128
err = b.Unmarshal(ballotWrongID, election)
114129
require.EqualError(t, err, "could not decode question ID: illegal base64 data at input byte 0")
115130

116131
// with question ID not from the election
117-
ballotUnknownID := string("select:" + encodedQuestionID(0) + ":1,0,1\n" +
118-
"rank:" + encodedQuestionID(2) + ":1,2,0,,\n" +
119-
"select:" + encodedQuestionID(3) + ":1,0,1,1\n" +
120-
"text:" + encodedQuestionID(4) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n\n")
132+
ballotUnknownID := string(selectIDTest + encodedQuestionID(0) + ":1,0,1\n" +
133+
rankIDTest + encodedQuestionID(2) + ":1,2,0,,\n" +
134+
selectIDTest + encodedQuestionID(3) + ":1,0,1,1\n" +
135+
textIDTest + encodedQuestionID(4) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n\n")
121136

122137
err = b.Unmarshal(ballotUnknownID, election)
123138
require.EqualError(t, err, "wrong question ID: the question doesn't exist")
124139

125140
// with too many answers in select question
126-
ballotWrongSelect := string("select:" + encodedQuestionID(1) + ":1,0,1,0,0\n" +
127-
"rank:" + encodedQuestionID(2) + ":1,2,0,,\n" +
128-
"select:" + encodedQuestionID(3) + ":1,0,1,1\n" +
129-
"text:" + encodedQuestionID(4) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n\n")
141+
ballotWrongSelect := string(selectIDTest + encodedQuestionID(1) + ":1,0,1,0,0\n" +
142+
rankIDTest + encodedQuestionID(2) + ":1,2,0,,\n" +
143+
selectIDTest + encodedQuestionID(3) + ":1,0,1,1\n" +
144+
textIDTest + encodedQuestionID(4) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n\n")
130145

131146
election.BallotSize = len(ballotWrongSelect)
132147

@@ -136,10 +151,10 @@ func TestBallot_Unmarshal(t *testing.T) {
136151
" of answers: expected 3 got 5")
137152

138153
// with wrong format answers in select question
139-
ballotWrongSelect = string("select:" + encodedQuestionID(1) + ":1,0,wrong\n" +
140-
"rank:" + encodedQuestionID(2) + ":1,2,0,,\n" +
141-
"select:" + encodedQuestionID(3) + ":1,0,1,1\n" +
142-
"text:" + encodedQuestionID(4) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n\n")
154+
ballotWrongSelect = string(selectIDTest + encodedQuestionID(1) + ":1,0,wrong\n" +
155+
rankIDTest + encodedQuestionID(2) + ":1,2,0,,\n" +
156+
selectIDTest + encodedQuestionID(3) + ":1,0,1,1\n" +
157+
textIDTest + encodedQuestionID(4) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n\n")
143158

144159
election.BallotSize = len(ballotWrongSelect)
145160

@@ -149,10 +164,10 @@ func TestBallot_Unmarshal(t *testing.T) {
149164
"ParseBool: parsing \"wrong\": invalid syntax")
150165

151166
// with too many selected answers in select question
152-
ballotWrongSelect = string("select:" + encodedQuestionID(1) + ":1,1,1\n" +
153-
"rank:" + encodedQuestionID(2) + ":1,2,0,,\n" +
154-
"select:" + encodedQuestionID(3) + ":1,0,1,1\n" +
155-
"text:" + encodedQuestionID(4) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n\n")
167+
ballotWrongSelect = string(selectIDTest + encodedQuestionID(1) + ":1,1,1\n" +
168+
rankIDTest + encodedQuestionID(2) + ":1,2,0,,\n" +
169+
selectIDTest + encodedQuestionID(3) + ":1,0,1,1\n" +
170+
textIDTest + encodedQuestionID(4) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n\n")
156171

157172
election.BallotSize = len(ballotWrongSelect)
158173

@@ -161,10 +176,10 @@ func TestBallot_Unmarshal(t *testing.T) {
161176
"failed to check number of answers: question Q1 has too many selected answers")
162177

163178
// with not enough selected answers in select question
164-
ballotWrongSelect = string("select:" + encodedQuestionID(1) + ":1,0,0\n" +
165-
"rank:" + encodedQuestionID(2) + ":1,2,0,,\n" +
166-
"select:" + encodedQuestionID(3) + ":1,0,1,1\n" +
167-
"text:" + encodedQuestionID(4) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n\n")
179+
ballotWrongSelect = string(selectIDTest + encodedQuestionID(1) + ":1,0,0\n" +
180+
rankIDTest + encodedQuestionID(2) + ":1,2,0,,\n" +
181+
selectIDTest + encodedQuestionID(3) + ":1,0,1,1\n" +
182+
textIDTest + encodedQuestionID(4) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n\n")
168183

169184
election.BallotSize = len(ballotWrongSelect)
170185

@@ -173,114 +188,114 @@ func TestBallot_Unmarshal(t *testing.T) {
173188
"failed to check number of answers: question Q1 has not enough selected answers")
174189

175190
// with not enough answers in rank question
176-
ballotWrongRank := string("select:" + encodedQuestionID(1) + ":1,0,1\n" +
177-
"rank:" + encodedQuestionID(2) + ":1,2,0\n" +
178-
"select:" + encodedQuestionID(3) + ":1,0,1,1\n" +
179-
"text:" + encodedQuestionID(4) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n\n")
191+
ballotWrongRank := string(selectIDTest + encodedQuestionID(1) + ":1,0,1\n" +
192+
rankIDTest + encodedQuestionID(2) + ":1,2,0\n" +
193+
selectIDTest + encodedQuestionID(3) + ":1,0,1,1\n" +
194+
textIDTest + encodedQuestionID(4) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n\n")
180195

181196
err = b.Unmarshal(ballotWrongRank, election)
182197
require.EqualError(t, err, "could not unmarshal rank answers: question"+
183198
" Q2 has a wrong number of answers: expected 5 got 3")
184199

185200
// with wrong format answers in rank question
186-
ballotWrongRank = string("select:" + encodedQuestionID(1) + ":1,0,1\n" +
187-
"rank:" + encodedQuestionID(2) + ":1,x,0,,\n" +
188-
"select:" + encodedQuestionID(3) + ":1,0,1,1\n" +
189-
"text:" + encodedQuestionID(4) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n\n")
201+
ballotWrongRank = string(selectIDTest + encodedQuestionID(1) + ":1,0,1\n" +
202+
rankIDTest + encodedQuestionID(2) + ":1,x,0,,\n" +
203+
selectIDTest + encodedQuestionID(3) + ":1,0,1,1\n" +
204+
textIDTest + encodedQuestionID(4) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n\n")
190205

191206
election.BallotSize = len(ballotWrongRank)
192207

193208
err = b.Unmarshal(ballotWrongRank, election)
194-
require.EqualError(t, err, "could not unmarshal rank answers: "+
209+
require.EqualError(t, err, unmarshalingRankID+
195210
"could not parse rank value for Q.Q2: strconv.ParseInt: parsing \"x\": invalid syntax")
196211

197212
// with too many selected answers in rank question
198-
ballotWrongRank = string("select:" + encodedQuestionID(1) + ":1,0,1\n" +
199-
"rank:" + encodedQuestionID(2) + ":1,2,0,3,4\n" +
200-
"select:" + encodedQuestionID(3) + ":1,0,1,1\n" +
201-
"text:" + encodedQuestionID(4) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n\n")
213+
ballotWrongRank = string(selectIDTest + encodedQuestionID(1) + ":1,0,1\n" +
214+
rankIDTest + encodedQuestionID(2) + ":1,2,0,3,4\n" +
215+
selectIDTest + encodedQuestionID(3) + ":1,0,1,1\n" +
216+
textIDTest + encodedQuestionID(4) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n\n")
202217

203218
election.BallotSize = len(ballotWrongRank)
204219

205220
err = b.Unmarshal(ballotWrongRank, election)
206-
require.EqualError(t, err, "could not unmarshal rank answers: "+
221+
require.EqualError(t, err, unmarshalingRankID+
207222
"invalid rank not in range [0, MaxN[: 3")
208223

209224
// with valid ranks but one is selected twice
210-
ballotWrongRank = string("select:" + encodedQuestionID(1) + ":1,0,1\n" +
211-
"rank:" + encodedQuestionID(2) + ":1,2,0,2,2\n" +
212-
"select:" + encodedQuestionID(3) + ":1,0,1,1\n" +
213-
"text:" + encodedQuestionID(4) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n\n")
225+
ballotWrongRank = string(selectIDTest + encodedQuestionID(1) + ":1,0,1\n" +
226+
rankIDTest + encodedQuestionID(2) + ":1,2,0,2,2\n" +
227+
selectIDTest + encodedQuestionID(3) + ":1,0,1,1\n" +
228+
textIDTest + encodedQuestionID(4) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n\n")
214229

215230
election.BallotSize = len(ballotWrongRank)
216231

217232
err = b.Unmarshal(ballotWrongRank, election)
218-
require.EqualError(t, err, "could not unmarshal rank answers: "+
233+
require.EqualError(t, err, unmarshalingRankID+
219234
"failed to check number of answers: question Q2 has too many selected answers")
220235

221236
// with not enough selected answers in rank question
222-
ballotWrongRank = string("select:" + encodedQuestionID(1) + ":1,0,1\n" +
223-
"rank:" + encodedQuestionID(2) + ":1,,0,,\n" +
224-
"select:" + encodedQuestionID(3) + ":1,0,1,1\n" +
225-
"text:" + encodedQuestionID(4) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n\n")
237+
ballotWrongRank = string(selectIDTest + encodedQuestionID(1) + ":1,0,1\n" +
238+
rankIDTest + encodedQuestionID(2) + ":1,,0,,\n" +
239+
selectIDTest + encodedQuestionID(3) + ":1,0,1,1\n" +
240+
textIDTest + encodedQuestionID(4) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n\n")
226241

227242
election.BallotSize = len(ballotWrongRank)
228243

229244
err = b.Unmarshal(ballotWrongRank, election)
230-
require.EqualError(t, err, "could not unmarshal rank answers: "+
245+
require.EqualError(t, err, unmarshalingRankID+
231246
"failed to check number of answers: question"+
232247
" Q2 has not enough selected answers")
233248

234249
// with not enough answers in text question
235-
ballotWrongText := string("select:" + encodedQuestionID(1) + ":1,0,1\n" +
236-
"rank:" + encodedQuestionID(2) + ":1,2,0,,\n" +
237-
"select:" + encodedQuestionID(3) + ":1,0,1,1\n" +
238-
"text:" + encodedQuestionID(4) + ":Y2VzdG1vaUVtaQ==\n\n")
250+
ballotWrongText := string(selectIDTest + encodedQuestionID(1) + ":1,0,1\n" +
251+
rankIDTest + encodedQuestionID(2) + ":1,2,0,,\n" +
252+
selectIDTest + encodedQuestionID(3) + ":1,0,1,1\n" +
253+
textIDTest + encodedQuestionID(4) + ":Y2VzdG1vaUVtaQ==\n\n")
239254

240255
election.BallotSize = len(ballotWrongText)
241256

242257
err = b.Unmarshal(ballotWrongText, election)
243-
require.EqualError(t, err, "could not unmarshal text answers: "+
258+
require.EqualError(t, err, unmarshalingTextID+
244259
"question Q4 has a wrong number of answers: expected 2 got 1")
245260

246261
// with wrong encoding in text question
247-
ballotWrongText = string("select:" + encodedQuestionID(1) + ":1,0,1\n" +
248-
"rank:" + encodedQuestionID(2) + ":1,2,0,,\n" +
249-
"select:" + encodedQuestionID(3) + ":1,0,1,1\n" +
250-
"text:" + encodedQuestionID(4) + ":wrongEncoding,Y2VzdG1vaUVtaQ==\n\n")
262+
ballotWrongText = string(selectIDTest + encodedQuestionID(1) + ":1,0,1\n" +
263+
rankIDTest + encodedQuestionID(2) + ":1,2,0,,\n" +
264+
selectIDTest + encodedQuestionID(3) + ":1,0,1,1\n" +
265+
textIDTest + encodedQuestionID(4) + ":wrongEncoding,Y2VzdG1vaUVtaQ==\n\n")
251266

252267
election.BallotSize = len(ballotWrongText)
253268

254269
err = b.Unmarshal(ballotWrongText, election)
255-
require.EqualError(t, err, "could not unmarshal text answers: "+
270+
require.EqualError(t, err, unmarshalingTextID+
256271
"could not decode text for Q.Q4: illegal base64 data at input byte 12")
257272

258273
// with too many selected answers in text question
259274
election.Configuration.Scaffold[0].Texts[0].MaxN = 1
260275

261-
ballotWrongText = string("select:" + encodedQuestionID(1) + ":1,0,1\n" +
262-
"rank:" + encodedQuestionID(2) + ":1,2,0,,\n" +
263-
"select:" + encodedQuestionID(3) + ":1,0,1,1\n" +
264-
"text:" + encodedQuestionID(4) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n\n")
276+
ballotWrongText = string(selectIDTest + encodedQuestionID(1) + ":1,0,1\n" +
277+
rankIDTest + encodedQuestionID(2) + ":1,2,0,,\n" +
278+
selectIDTest + encodedQuestionID(3) + ":1,0,1,1\n" +
279+
textIDTest + encodedQuestionID(4) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n\n")
265280

266281
election.BallotSize = len(ballotWrongText)
267282

268283
err = b.Unmarshal(ballotWrongText, election)
269-
require.EqualError(t, err, "could not unmarshal text answers: "+
284+
require.EqualError(t, err, unmarshalingTextID+
270285
"failed to check number of answers: question Q4 has too many selected answers")
271286

272287
election.Configuration.Scaffold[0].Texts[0].MaxN = 2
273288

274289
// with not enough elected answers in text question
275-
ballotWrongText = string("select:" + encodedQuestionID(1) + ":1,0,1\n" +
276-
"rank:" + encodedQuestionID(2) + ":1,2,0,,\n" +
277-
"select:" + encodedQuestionID(3) + ":1,0,1,1\n" +
278-
"text:" + encodedQuestionID(4) + ":,Y2VzdG1vaUVtaQ==\n\n")
290+
ballotWrongText = string(selectIDTest + encodedQuestionID(1) + ":1,0,1\n" +
291+
rankIDTest + encodedQuestionID(2) + ":1,2,0,,\n" +
292+
selectIDTest + encodedQuestionID(3) + ":1,0,1,1\n" +
293+
textIDTest + encodedQuestionID(4) + ":,Y2VzdG1vaUVtaQ==\n\n")
279294

280295
election.BallotSize = len(ballotWrongText)
281296

282297
err = b.Unmarshal(ballotWrongText, election)
283-
require.EqualError(t, err, "could not unmarshal text answers: "+
298+
require.EqualError(t, err, unmarshalingTextID+
284299
"failed to check number of answers: question Q4 has not enough selected answers")
285300

286301
// with unknown question type

0 commit comments

Comments
 (0)