22using ElectionGuard . Encryption . Utils ;
33using NUnit . Framework ;
44
5- namespace ElectionGuard . Encrypt . Tests
5+ namespace ElectionGuard . Encryption . Tests
66{
77 [ TestFixture ]
88 public class TestEncrypt
@@ -88,7 +88,7 @@ public void Test_Encrypt_Ballot_Undervote_Succeeds()
8888 var mediator = new EncryptionMediator ( internalManifest , context , device ) ;
8989
9090 // Act
91- var ballot = BallotGenerator . GetFakeBallot ( internalManifest , 1 ) ;
91+ var ballot = BallotGenerator . GetFakeBallot ( internalManifest ) ;
9292 var ciphertext = mediator . Encrypt ( ballot ) ;
9393
9494 // Assert
@@ -116,6 +116,40 @@ public void Test_Encrypt_Ballot_Overvote_Succeeds()
116116 Assert . That ( ciphertext . IsValidEncryption ( context . ManifestHash , keypair . PublicKey , context . CryptoExtendedBaseHash ) ) ;
117117 }
118118
119+ [ Test ]
120+ public void Test_EncryptAndDecryptOfBallot_WithMultipleVotesAllowed ( )
121+ {
122+ // Arrange
123+ var keypair = ElGamalKeyPair . FromSecret ( Constants . TWO_MOD_Q ) ;
124+ var manifest = ManifestGenerator . GetManifestFromFile ( ) ;
125+ var internalManifest = new InternalManifest ( manifest ) ;
126+ var context = new CiphertextElectionContext (
127+ 1UL , 1UL , keypair . PublicKey , Constants . TWO_MOD_Q , internalManifest . ManifestHash ) ;
128+ var device = new EncryptionDevice ( 12345UL , 23456UL , 34567UL , "Location" ) ;
129+ var mediator = new EncryptionMediator ( internalManifest , context , device ) ;
130+ const string styleId = "congress-district-7-arlington-pismo-beach" ;
131+ var ballot = BallotGenerator . GetFakeBallotWithContest ( internalManifest , "pismo-beach-school-board-contest" , 3 , styleId ) ;
132+
133+ // Act
134+ var ciphertext = mediator . Encrypt ( ballot ) ;
135+
136+ // Assert
137+ Assert . AreEqual ( 5 , ciphertext . ContestsSize ) ;
138+ var pismoBeach = FindContestOrDefault ( ciphertext , i => i . ObjectId == "pismo-beach-school-board-contest" ) ;
139+ Assert . IsNotNull ( pismoBeach ) ;
140+ Assert . AreEqual ( 1L , DecryptSelection ( pismoBeach , keypair , 0 ) ) ;
141+ Assert . AreEqual ( 1L , DecryptSelection ( pismoBeach , keypair , 1 ) ) ;
142+ Assert . AreEqual ( 1L , DecryptSelection ( pismoBeach , keypair , 2 ) ) ;
143+ Assert . AreEqual ( 0L , DecryptSelection ( pismoBeach , keypair , 3 ) ) ;
144+ Assert . AreEqual ( 0L , DecryptSelection ( pismoBeach , keypair , 4 ) ) ;
145+ }
146+
147+ private static ulong ? DecryptSelection ( CiphertextBallotContest pismoBeach , ElGamalKeyPair keypair , int index )
148+ {
149+ var selection = pismoBeach . GetSelectionAt ( ( ulong ) index ) ;
150+ return selection . Ciphertext . Decrypt ( keypair . SecretKey ) ;
151+ }
152+
119153 [ Test ]
120154 public void Test_Compact_Encrypt_Ballot_Simple_Succeeds ( )
121155 {
@@ -168,7 +202,7 @@ public void Test_EncryptMediator_Hashes_Match()
168202 var mediator = new EncryptionMediator ( internalManifest , context , device ) ;
169203 Assert . IsNotNull ( mediator ) ; // should not be null if it gets created
170204 }
171- catch ( Exception ex )
205+ catch ( Exception )
172206 {
173207 // if there is an exception then the manifest hash would not be equal
174208 Assert . AreNotEqual ( context . ManifestHash . ToHex ( ) , internalManifest . ManifestHash . ToHex ( ) ) ;
@@ -189,13 +223,27 @@ public void Test_EncryptMediator_Hashes_Dont_Match()
189223 var mediator = new EncryptionMediator ( internalManifest , context , device ) ;
190224 Assert . IsNull ( mediator ) ; // should not be created, so null at best
191225 }
192- catch ( Exception ex )
226+ catch ( Exception )
193227 {
194228 // if there is an exception then the manifest hash would not be equal
195229 Assert . AreNotEqual ( context . ManifestHash . ToHex ( ) , internalManifest . ManifestHash . ToHex ( ) ) ;
196230 }
197231 }
198232
199233
234+ private static CiphertextBallotContest FindContestOrDefault ( CiphertextBallot ballot , Func < CiphertextBallotContest , bool > func )
235+ {
236+ var contestSize = ( int ) ballot . ContestsSize ;
237+ for ( int i = 0 ; i < contestSize ; i ++ )
238+ {
239+ var contest = ballot . GetContestAt ( ( ulong ) i ) ;
240+ if ( func ( contest ) )
241+ {
242+ return contest ;
243+ }
244+ }
245+
246+ return null ;
247+ }
200248 }
201249}
0 commit comments