diff options
author | Florian Dold <florian.dold@gmail.com> | 2013-10-15 10:49:30 +0000 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2013-10-15 10:49:30 +0000 |
commit | 4cbf5731c126695bbd964552b92e7ae9151561e1 (patch) | |
tree | 5c7bbdd7ddf08dad3a67e29190e246055db01775 /src/test/java/org | |
parent | 3b0a0f129a4e78671d6ccfaf7cb1b74295846375 (diff) | |
download | gnunet-java-4cbf5731c126695bbd964552b92e7ae9151561e1.tar.gz gnunet-java-4cbf5731c126695bbd964552b92e7ae9151561e1.zip |
- started implementing new crypto
- $-expansion and XDG in config
Diffstat (limited to 'src/test/java/org')
-rw-r--r-- | src/test/java/org/gnunet/mesh/MeshTest.java | 2 | ||||
-rw-r--r-- | src/test/java/org/gnunet/util/ConfigDollarTest.java | 44 | ||||
-rw-r--r-- | src/test/java/org/gnunet/util/CryptoECCTest.java | 81 | ||||
-rw-r--r-- | src/test/java/org/gnunet/util/EddsaTest.java | 39 |
4 files changed, 84 insertions, 82 deletions
diff --git a/src/test/java/org/gnunet/mesh/MeshTest.java b/src/test/java/org/gnunet/mesh/MeshTest.java index f8694de..a89bd63 100644 --- a/src/test/java/org/gnunet/mesh/MeshTest.java +++ b/src/test/java/org/gnunet/mesh/MeshTest.java | |||
@@ -31,7 +31,7 @@ public class MeshTest extends TestingFixture { | |||
31 | final Configuration cfg = ts.getConfiguration(); | 31 | final Configuration cfg = ts.getConfiguration(); |
32 | 32 | ||
33 | final MessageHandler1 mh = new MessageHandler1(); | 33 | final MessageHandler1 mh = new MessageHandler1(); |
34 | // FIXME: use CryptoECC instead of Core once available and compatible | 34 | // FIXME: use CryptoEcc instead of Core once available and compatible |
35 | Core.withPeerIdentity(cfg, new PeerIdentityContinuation() { | 35 | Core.withPeerIdentity(cfg, new PeerIdentityContinuation() { |
36 | @Override | 36 | @Override |
37 | public void cont(PeerIdentity peerIdentity) { | 37 | public void cont(PeerIdentity peerIdentity) { |
diff --git a/src/test/java/org/gnunet/util/ConfigDollarTest.java b/src/test/java/org/gnunet/util/ConfigDollarTest.java new file mode 100644 index 0000000..31a30fa --- /dev/null +++ b/src/test/java/org/gnunet/util/ConfigDollarTest.java | |||
@@ -0,0 +1,44 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2012, 2013 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 | |||
21 | package org.gnunet.util; | ||
22 | |||
23 | |||
24 | import org.junit.Assert; | ||
25 | import org.junit.Test; | ||
26 | |||
27 | public class ConfigDollarTest { | ||
28 | |||
29 | @Test | ||
30 | public void test_dollar() { | ||
31 | Configuration cfg = new Configuration(); | ||
32 | cfg.setValueString("PATHS", "FOO", "/bin/true"); | ||
33 | Assert.assertEquals("hello, world!", cfg.expandDollar("hello, world!")); | ||
34 | Assert.assertEquals("", cfg.expandDollar("${}")); | ||
35 | Assert.assertEquals("/bin/true", cfg.expandDollar("${FOO}")); | ||
36 | Assert.assertEquals("/bin/true/gnu", cfg.expandDollar("${FOO}/gnu")); | ||
37 | Assert.assertEquals("/bin/true/gnu", cfg.expandDollar("$FOO/gnu")); | ||
38 | Assert.assertEquals("/bin/true", cfg.expandDollar("${BLUB:-/bin}/true")); | ||
39 | Assert.assertEquals("/bin/true/gnu", cfg.expandDollar("${BLUB:-${FOO}}/gnu")); | ||
40 | Assert.assertEquals("", cfg.expandDollar("${{{}}")); | ||
41 | Assert.assertEquals("/bin/true", cfg.expandDollar("${XXX:-${XXX:-${XXX:-/bin}}}/true")); | ||
42 | Assert.assertEquals("/bincd/true", cfg.expandDollar("${XXX:-${XXX:-${XXX:-/bin}c}d}/true")); | ||
43 | } | ||
44 | } | ||
diff --git a/src/test/java/org/gnunet/util/CryptoECCTest.java b/src/test/java/org/gnunet/util/CryptoECCTest.java deleted file mode 100644 index 1dda545..0000000 --- a/src/test/java/org/gnunet/util/CryptoECCTest.java +++ /dev/null | |||
@@ -1,81 +0,0 @@ | |||
1 | package org.gnunet.util; | ||
2 | |||
3 | |||
4 | import org.junit.Assert; | ||
5 | import org.junit.Test; | ||
6 | |||
7 | import java.util.Random; | ||
8 | |||
9 | public class CryptoECCTest { | ||
10 | /** | ||
11 | * Check that signed messages can be verified correctly. | ||
12 | */ | ||
13 | @Test | ||
14 | public void test_sign_success() { | ||
15 | Random r = new Random(); | ||
16 | // the test uses random data, repeat it multiple times! | ||
17 | for (int i = 0; i < 5; i++) { | ||
18 | byte[] msg = new byte[16]; | ||
19 | r.nextBytes(msg); | ||
20 | |||
21 | CryptoECC.PrivateKey privateKey = new CryptoECC.PrivateKey(); | ||
22 | privateKey.d = new byte[32]; | ||
23 | r.nextBytes(privateKey.d); | ||
24 | CryptoECC.PublicSignKey publicKey = CryptoECC.computePublicKey(privateKey); | ||
25 | System.out.println("gen"); | ||
26 | CryptoECC.Signature sig = CryptoECC.sign(msg, privateKey, publicKey); | ||
27 | System.out.println("sign"); | ||
28 | boolean valid = CryptoECC.verify(sig, msg, publicKey); | ||
29 | System.out.println("verify"); | ||
30 | Assert.assertTrue(valid); | ||
31 | } | ||
32 | } | ||
33 | |||
34 | /** | ||
35 | * Check that signature verification fails for manipulated data. | ||
36 | */ | ||
37 | @Test | ||
38 | public void test_sign_failure() { | ||
39 | Random r = new Random(); | ||
40 | // the test uses random data, repeat it multiple times! | ||
41 | for (int i = 0; i < 5; i++) { | ||
42 | byte[] msg = new byte[16]; | ||
43 | r.nextBytes(msg); | ||
44 | CryptoECC.PrivateKey privateKey = new CryptoECC.PrivateKey(); | ||
45 | privateKey.d = new byte[32]; | ||
46 | r.nextBytes(privateKey.d); | ||
47 | CryptoECC.PublicSignKey publicKey = CryptoECC.computePublicKey(privateKey); | ||
48 | System.out.println("gen"); | ||
49 | CryptoECC.Signature sig = CryptoECC.sign(msg, privateKey, publicKey); | ||
50 | System.out.println("sign"); | ||
51 | // corrupt the message | ||
52 | msg[0] = (byte) (msg[0] + 1); | ||
53 | boolean valid = CryptoECC.verify(sig, msg, publicKey); | ||
54 | System.out.println("verify"); | ||
55 | Assert.assertFalse(valid); | ||
56 | } | ||
57 | } | ||
58 | |||
59 | /** | ||
60 | * Check whether ecdh key coincide | ||
61 | */ | ||
62 | @Test | ||
63 | public void test_ecdh() { | ||
64 | Random r = new Random(); | ||
65 | |||
66 | CryptoECC.PrivateKey privateAlice = new CryptoECC.PrivateKey(); | ||
67 | privateAlice.d = new byte[32]; | ||
68 | r.nextBytes(privateAlice.d); | ||
69 | CryptoECC.PublicSignKey publicAlice = CryptoECC.computePublicKey(privateAlice); | ||
70 | |||
71 | CryptoECC.PrivateKey privateBob = new CryptoECC.PrivateKey(); | ||
72 | privateBob.d = new byte[32]; | ||
73 | r.nextBytes(privateBob.d); | ||
74 | CryptoECC.PublicSignKey publicBob = CryptoECC.computePublicKey(privateBob); | ||
75 | |||
76 | HashCode ssAlice = CryptoECC.ecdh(privateAlice, publicBob); | ||
77 | HashCode ssBob = CryptoECC.ecdh(privateBob, publicAlice); | ||
78 | |||
79 | Assert.assertArrayEquals(ssAlice.data, ssBob.data); | ||
80 | } | ||
81 | } | ||
diff --git a/src/test/java/org/gnunet/util/EddsaTest.java b/src/test/java/org/gnunet/util/EddsaTest.java new file mode 100644 index 0000000..302751b --- /dev/null +++ b/src/test/java/org/gnunet/util/EddsaTest.java | |||
@@ -0,0 +1,39 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2012, 2013 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 | |||
21 | package org.gnunet.util; | ||
22 | |||
23 | |||
24 | import org.gnunet.util.crypto.EddsaPrivateKey; | ||
25 | import org.gnunet.util.crypto.EddsaPublicKey; | ||
26 | import org.gnunet.util.crypto.EddsaSignature; | ||
27 | import org.junit.Assert; | ||
28 | import org.junit.Test; | ||
29 | |||
30 | public class EddsaTest { | ||
31 | @Test | ||
32 | public void test_eddsa_sign_success() { | ||
33 | byte[] data = "foo".getBytes(); | ||
34 | EddsaPrivateKey privateKey = EddsaPrivateKey.createRandom(); | ||
35 | EddsaPublicKey publicKey = privateKey.getPublicKey(); | ||
36 | EddsaSignature signature = privateKey.sign(publicKey, 0, data); | ||
37 | Assert.assertTrue(signature.verify(data, 0, publicKey)); | ||
38 | } | ||
39 | } | ||