Add test for DI constructability of SearchPage
[Sone.git] / src / test / java / net / pterodactylus / sone / freenet / KeyTest.java
1 package net.pterodactylus.sone.freenet;
2
3 import static freenet.support.Base64.encode;
4 import static net.pterodactylus.sone.freenet.Key.from;
5 import static net.pterodactylus.sone.freenet.Key.routingKey;
6 import static org.hamcrest.MatcherAssert.assertThat;
7 import static org.hamcrest.Matchers.is;
8
9 import java.net.MalformedURLException;
10
11 import freenet.keys.FreenetURI;
12
13 import org.junit.Test;
14
15 /**
16  * Unit test for {@link Key}.
17  *
18  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
19  */
20 public class KeyTest {
21
22         private final FreenetURI uri;
23         private final Key key;
24
25         public KeyTest() throws MalformedURLException {
26                 uri = new FreenetURI(
27                                 "SSK@NfUYvxDwU9vqb2mh-qdT~DYJ6U0XNbxMGGoLe0aCHJs,Miglsgix0VR56ZiPl4NgjnUd~UdrnHqIvXJ3KKHmxmI,AQACAAE/some-site-12/foo/bar.html");
28                 key = from(uri);
29         }
30
31         @Test
32         public void keyCanBeCreatedFromFreenetUri() throws MalformedURLException {
33                 assertThat(key.getRoutingKey(),
34                                 is("NfUYvxDwU9vqb2mh-qdT~DYJ6U0XNbxMGGoLe0aCHJs"));
35                 assertThat(key.getCryptoKey(),
36                                 is("Miglsgix0VR56ZiPl4NgjnUd~UdrnHqIvXJ3KKHmxmI"));
37                 assertThat(key.getExtra(), is("AQACAAE"));
38         }
39
40         @Test
41         public void keyCanBeConvertedToUsk() throws MalformedURLException {
42                 FreenetURI uskUri = key.toUsk("other-site", 15, "some", "path.html");
43                 assertThat(uskUri.toString(),
44                                 is("USK@NfUYvxDwU9vqb2mh-qdT~DYJ6U0XNbxMGGoLe0aCHJs,Miglsgix0VR56ZiPl4NgjnUd~UdrnHqIvXJ3KKHmxmI,AQACAAE/other-site/15/some/path.html"));
45         }
46
47         @Test
48         public void keyCanBeConvertedToSskWithoutEdition()
49         throws MalformedURLException {
50                 FreenetURI uskUri = key.toSsk("other-site", "some", "path.html");
51                 assertThat(uskUri.toString(),
52                                 is("SSK@NfUYvxDwU9vqb2mh-qdT~DYJ6U0XNbxMGGoLe0aCHJs,Miglsgix0VR56ZiPl4NgjnUd~UdrnHqIvXJ3KKHmxmI,AQACAAE/other-site/some/path.html"));
53         }
54
55         @Test
56         public void keyCanBeConvertedToSskWithEdition()
57         throws MalformedURLException {
58                 FreenetURI uskUri = key.toSsk("other-site", 15, "some", "path.html");
59                 assertThat(uskUri.toString(),
60                                 is("SSK@NfUYvxDwU9vqb2mh-qdT~DYJ6U0XNbxMGGoLe0aCHJs,Miglsgix0VR56ZiPl4NgjnUd~UdrnHqIvXJ3KKHmxmI,AQACAAE/other-site-15/some/path.html"));
61         }
62
63         @Test
64         public void routingKeyIsExtractCorrectly() {
65                 assertThat(routingKey(uri),
66                                 is("NfUYvxDwU9vqb2mh-qdT~DYJ6U0XNbxMGGoLe0aCHJs"));
67         }
68
69 }