aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/test/java/org/gnunet/voting/TestVotingCrypto.java97
1 files changed, 97 insertions, 0 deletions
diff --git a/src/test/java/org/gnunet/voting/TestVotingCrypto.java b/src/test/java/org/gnunet/voting/TestVotingCrypto.java
new file mode 100644
index 0000000..1ccdf92
--- /dev/null
+++ b/src/test/java/org/gnunet/voting/TestVotingCrypto.java
@@ -0,0 +1,97 @@
1/*
2 This file is part of GNUnet.
3 (C) 2014 Christian Grothoff (and other contributing authors)
4
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19 */
20
21package org.gnunet.voting;
22
23import org.gnunet.secretsharing.*;
24import org.gnunet.secretsharing.callbacks.DecryptCallback;
25import org.gnunet.secretsharing.callbacks.SecretReadyCallback;
26import org.gnunet.testing.TestingFixture;
27import org.gnunet.testing.TestingSubsystem;
28import org.gnunet.util.*;
29import org.gnunet.util.crypto.EcdhePrivateKey;
30import org.gnunet.util.crypto.EcdsaPrivateKey;
31import org.gnunet.util.crypto.EcdsaPublicKey;
32import org.junit.Assert;
33import org.junit.Test;
34
35public class TestVotingCrypto extends TestingFixture {
36 @Test
37 public void testVoteTally() {
38 Configuration armConf = new Configuration();
39 armConf.setValueString("arm", "DEFAULTSERVICES", "consensus set");
40 Program.configureLogging("DEBUG");
41 // for testing, we vote with the same voter identity multiple times,
42 // does not matter here ...
43 final EcdsaPrivateKey privateKey = EcdsaPrivateKey.createRandom();
44 final EcdsaPublicKey publicKey = privateKey.getPublicKey();
45 final TestingSubsystem ts = new TestingSubsystem("arm", armConf.writeTemp().getAbsolutePath());
46 KeyGeneration kg = new KeyGeneration(ts.getConfiguration(), new PeerIdentity[0],
47 HashCode.random(), AbsoluteTime.now(), AbsoluteTime.now().add(RelativeTime.fromSeconds(5)),
48 1, new SecretReadyCallback() {
49 @Override
50 public void onSecretReady(Share share) {
51 Assert.assertNotNull(share);
52 EncryptedVote encryptedVote1 = EncryptedVote.fromChoice(1, share.publicKey, privateKey, publicKey);
53 EncryptedVote encryptedVote2 = EncryptedVote.fromChoice(0, share.publicKey, privateKey, publicKey);
54 EncryptedVote encryptedVote3 = EncryptedVote.fromChoice(0, share.publicKey, privateKey, publicKey);
55
56 Ciphertext sum = Ciphertext.identity().multiply(encryptedVote1.v.multiply(encryptedVote2.v).multiply(encryptedVote3.v));
57
58 Decryption decryption = new Decryption(ts.getConfiguration(), share, sum,
59 AbsoluteTime.now(), AbsoluteTime.now().add(RelativeTime.fromSeconds(5)), new DecryptCallback() {
60
61 @Override
62 public void onResult(Plaintext plaintext) {
63 Assert.assertNotNull(plaintext);
64 long x = plaintext.bruteForceDiscreteLog(3);
65 // one yes, two no
66 Assert.assertEquals(-1, x);
67 }
68 });
69 }
70 });
71 }
72
73 @Test
74 public void testSerialization() {
75 Configuration armConf = new Configuration();
76 armConf.setValueString("arm", "DEFAULTSERVICES", "consensus set");
77 Program.configureLogging("DEBUG");
78 final TestingSubsystem ts = new TestingSubsystem("arm", armConf.writeTemp().getAbsolutePath());
79 final EcdsaPrivateKey privateKey = EcdsaPrivateKey.createRandom();
80 final EcdsaPublicKey publicKey = privateKey.getPublicKey();
81 KeyGeneration kg = new KeyGeneration(ts.getConfiguration(), new PeerIdentity[0],
82 HashCode.random(), AbsoluteTime.now(), AbsoluteTime.now().add(RelativeTime.fromSeconds(5)),
83 1, new SecretReadyCallback() {
84 @Override
85 public void onSecretReady(Share share) {
86 Assert.assertNotNull(share);
87 EncryptedVote encryptedVote1 = EncryptedVote.fromChoice(1, share.publicKey, privateKey, publicKey);
88 Configuration c = new Configuration();
89 encryptedVote1.writeToConfiguration(c);
90 EncryptedVote encryptedVote2 = EncryptedVote.parseFromConfiguration(c, publicKey);
91 Assert.assertEquals(encryptedVote1, encryptedVote2);
92 }
93
94 });
95 }
96}
97