Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
DisSys Inc.
bsc-shamir
Commits
039e64d1
Commit
039e64d1
authored
Jun 03, 2020
by
Anders Jensen Løvig
Browse files
Fix election tests
parent
f44211b4
Changes
1
Hide whitespace changes
Inline
Side-by-side
election/election_test.go
View file @
039e64d1
...
...
@@ -2,6 +2,7 @@ package election
import
(
"bsc-shamir/crypto/common"
"bsc-shamir/network"
"crypto/rand"
"fmt"
"io/ioutil"
...
...
@@ -196,44 +197,31 @@ func BenchmarkCombineRequired(b *testing.B) {
// Actual tests
func
setupElection
()
*
Election
{
// netConfig, err := network.LoadConfig()
netConfig
:=
&
network
.
Config
{}
netConfig
.
RootPath
=
"../config/cert/root.crt"
netConfig
.
CertPath
=
"../config/cert/server/s2.crt"
netConfig
.
KeyPath
=
"../config/cert/server/s2.key"
config
:=
&
Config
{
Server
:
network
.
NewServer
(
netConfig
),
ServerID
:
"1"
,
Voters
:
5
,
Servers
:
3
,
RequiredServers
:
2
,
Deadline
:
time
.
Now
()
.
Add
(
1
*
time
.
Minute
),
ResultCallback
:
func
(
result
*
Result
)
{},
// CloseSleep: 0 * time.Second, // Disable close delay for testing
}
return
NewElection
(
config
)
}
func
testCloseCondition
(
t
*
testing
.
T
,
expectedReason
Reason
,
expectedPhase
Phase
,
fun
func
(
*
Election
))
*
Election
{
log
.
SetOutput
(
ioutil
.
Discard
)
close
:=
make
(
chan
bool
,
1
)
timeout
:=
make
(
chan
bool
,
1
)
func
testCloseCondition
(
t
*
testing
.
T
,
expectedPhase
Phase
,
fun
func
(
*
Election
)
<-
chan
bool
)
*
Election
{
// log.SetOutput(ioutil.Discard)
e
:=
setupElection
()
// e.closeCallback = func(reason Reason) {
// if reason != expectedReason {
// t.Errorf("Unexpected close reason\n Expected: %s\n Actual: %s\n", expectedReason, reason)
// }
// close <- true
// }
go
func
()
{
time
.
Sleep
(
2
*
time
.
Second
)
timeout
<-
true
}()
e
.
Deadline
=
time
.
Now
()
.
Add
(
1
*
time
.
Second
)
_
=
e
.
Start
()
fun
(
e
)
select
{
case
<-
close
:
case
<-
timeout
:
t
.
Error
(
"Timeout"
)
case
<-
time
.
After
(
2
*
time
.
Second
)
:
// Give time for test
case
<-
fun
(
e
)
:
}
if
e
.
Status
.
Phase
!=
expectedPhase
{
...
...
@@ -244,78 +232,115 @@ func testCloseCondition(t *testing.T, expectedReason Reason, expectedPhase Phase
}
func
TestCloseDeadline
(
t
*
testing
.
T
)
{
_
=
testCloseCondition
(
t
,
ReasonDeadline
,
PhaseClosed
,
func
(
e
*
Election
)
{})
_
=
testCloseCondition
(
t
,
PhaseClosed
,
func
(
e
*
Election
)
<-
chan
bool
{
done
:=
make
(
chan
bool
,
1
)
e
.
Deadline
=
time
.
Now
()
.
Add
(
1
*
time
.
Second
)
_
=
e
.
Start
()
go
func
()
{
time
.
Sleep
(
1200
*
time
.
Millisecond
)
done
<-
true
}()
return
done
})
}
func
TestCloseAgreement
(
t
*
testing
.
T
)
{
_
=
testCloseCondition
(
t
,
ReasonAgreement
,
PhaseTallying
,
func
(
e
*
Election
)
{
_
=
testCloseCondition
(
t
,
PhaseTallying
,
func
(
e
*
Election
)
<-
chan
bool
{
done
:=
make
(
chan
bool
,
1
)
e
.
Status
.
Phase
=
PhaseCollecting
ballots
:=
CreateBallots
(
3
,
createXS
(
1
),
big
.
NewInt
(
1
))
e
.
handleBallot
(
UniqueID
(
strconv
.
Itoa
(
1
)),
ballots
[
"1"
])
e
.
handleClosing
(
"2"
)
e
.
handleClosing
(
"3"
)
done
<-
true
return
done
})
}
func
TestCloseAllVotes
(
t
*
testing
.
T
)
{
_
=
testCloseCondition
(
t
,
ReasonVotes
,
PhaseClosed
,
func
(
e
*
Election
)
{
_
=
testCloseCondition
(
t
,
PhaseClosed
,
func
(
e
*
Election
)
<-
chan
bool
{
done
:=
make
(
chan
bool
,
1
)
e
.
Status
.
Phase
=
PhaseCollecting
for
i
:=
1
;
i
<=
e
.
Participants
.
Voters
;
i
++
{
ballots
:=
CreateBallots
(
3
,
createXS
(
1
),
big
.
NewInt
(
1
))
e
.
handleBallot
(
UniqueID
(
strconv
.
Itoa
(
i
)),
ballots
[
"1"
])
}
done
<-
true
return
done
})
}
func
TestTallyDeadline
(
t
*
testing
.
T
)
{
_
=
testCloseCondition
(
t
,
ReasonDeadline
,
PhaseTallying
,
func
(
e
*
Election
)
{
_
=
testCloseCondition
(
t
,
PhaseTallying
,
func
(
e
*
Election
)
<-
chan
bool
{
done
:=
make
(
chan
bool
,
1
)
e
.
Deadline
=
time
.
Now
()
.
Add
(
1
*
time
.
Second
)
_
=
e
.
Start
()
ballots
:=
CreateBallots
(
3
,
createXS
(
1
),
big
.
NewInt
(
1
))
e
.
handleBallot
(
UniqueID
(
strconv
.
Itoa
(
1
)),
ballots
[
"1"
])
go
func
()
{
time
.
Sleep
(
1200
*
time
.
Millisecond
)
done
<-
true
}()
// By closing another server, we should go directly to tallying when
// deadline is reached
e
.
handleClosing
(
"2"
)
return
done
})
}
func
TestNoVotesResult
(
t
*
testing
.
T
)
{
done
:=
make
(
chan
bool
,
1
)
timeout
:=
make
(
chan
bool
,
1
)
e
:=
testCloseCondition
(
t
,
ReasonDeadline
,
PhaseClosed
,
func
(
e
*
Election
)
{})
e
.
resultCallback
=
func
(
result
*
Result
)
{
if
result
!=
nil
{
t
.
Error
(
"Got a result wile none was expected"
)
_
=
testCloseCondition
(
t
,
PhaseResult
,
func
(
e
*
Election
)
<-
chan
bool
{
done
:=
make
(
chan
bool
,
1
)
e
.
resultCallback
=
func
(
result
*
Result
)
{
if
result
!=
nil
{
t
.
Error
(
"Got a result wile none was expected"
)
}
done
<-
true
}
done
<-
true
}
go
func
()
{
time
.
Sleep
(
2
*
time
.
Second
)
timeout
<-
true
}()
e
.
Deadline
=
time
.
Now
()
.
Add
(
1000
*
time
.
Millisecond
)
_
=
e
.
Start
()
e
.
handleClosing
(
"2"
)
e
.
handleClosing
(
"2"
)
select
{
case
<-
done
:
case
<-
timeout
:
t
.
Error
(
"Timeout"
)
}
if
e
.
Status
.
Phase
!=
PhaseResult
{
t
.
Errorf
(
"Unexpected phase
\n
Expected: %s
\n
Actual: %s
\n
"
,
PhaseResult
,
e
.
Status
.
Phase
)
}
return
done
})
}
func
TestTallyAfterClose
(
t
*
testing
.
T
)
{
_
=
testCloseCondition
(
t
,
ReasonDeadline
,
PhaseTallying
,
func
(
e
*
Election
)
{
_
=
testCloseCondition
(
t
,
PhaseTallying
,
func
(
e
*
Election
)
<-
chan
bool
{
done
:=
make
(
chan
bool
,
1
)
e
.
Deadline
=
time
.
Now
()
.
Add
(
1000
*
time
.
Millisecond
)
_
=
e
.
Start
()
ballots
:=
CreateBallots
(
3
,
createXS
(
1
),
big
.
NewInt
(
1
))
e
.
handleBallot
(
UniqueID
(
strconv
.
Itoa
(
1
)),
ballots
[
"1"
])
e
.
handleClosing
(
"2"
)
go
func
()
{
time
.
Sleep
(
1200
*
time
.
Millisecond
)
done
<-
true
}()
return
done
})
}
func
TestOnlyVoteOnce
(
t
*
testing
.
T
)
{
e
:=
setupElection
()
_
=
e
.
Sta
rt
()
e
.
Sta
tus
.
Phase
=
PhaseCollecting
ballots
:=
CreateBallots
(
3
,
createXS
(
1
),
big
.
NewInt
(
1
))
...
...
@@ -340,7 +365,10 @@ func TestHandleTally(t *testing.T) {
Commits
:
ballots
[
"4"
]
.
Commits
,
}
_
=
testCloseCondition
(
t
,
ReasonAgreement
,
PhaseResult
,
func
(
e
*
Election
)
{
_
=
testCloseCondition
(
t
,
PhaseResult
,
func
(
e
*
Election
)
<-
chan
bool
{
done
:=
make
(
chan
bool
,
1
)
e
.
Status
.
Phase
=
PhaseCollecting
e
.
handleBallot
(
"4"
,
ballots
[
"1"
])
e
.
handleTally
(
"2"
,
tally1
)
if
e
.
tallyBox
.
Size
()
!=
0
{
...
...
@@ -359,5 +387,8 @@ func TestHandleTally(t *testing.T) {
fmt
.
Println
(
"Phase:"
,
e
.
Status
.
Phase
)
e
.
handleTally
(
"3"
,
tally2
)
fmt
.
Println
(
"Phase:"
,
e
.
Status
.
Phase
)
done
<-
true
return
done
})
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment