|
| 1 | +package org.ipfs.api; |
| 2 | + |
| 3 | +import org.junit.*; |
| 4 | + |
| 5 | +import java.io.*; |
| 6 | +import java.util.*; |
| 7 | +import java.util.function.*; |
| 8 | +import java.util.stream.*; |
| 9 | + |
| 10 | +public class MultiAddressTests { |
| 11 | + |
| 12 | + @Test |
| 13 | + public void fails() { |
| 14 | + List<String> parsed = Stream.of( |
| 15 | + "/ip4", |
| 16 | + "/ip4/::1", |
| 17 | + "/ip4/fdpsofodsajfdoisa", |
| 18 | + "/ip6", |
| 19 | + "/udp", |
| 20 | + "/tcp", |
| 21 | + "/sctp", |
| 22 | + "/udp/65536", |
| 23 | + "/tcp/65536", |
| 24 | + "/onion/9imaq4ygg2iegci7:80", |
| 25 | + "/onion/aaimaq4ygg2iegci7:80", |
| 26 | + "/onion/timaq4ygg2iegci7:0", |
| 27 | + "/onion/timaq4ygg2iegci7:-1", |
| 28 | + "/onion/timaq4ygg2iegci7", |
| 29 | + "/onion/timaq4ygg2iegci@:666", |
| 30 | + "/udp/1234/sctp", |
| 31 | + "/udp/1234/udt/1234", |
| 32 | + "/udp/1234/utp/1234", |
| 33 | + "/ip4/127.0.0.1/udp/jfodsajfidosajfoidsa", |
| 34 | + "/ip4/127.0.0.1/udp", |
| 35 | + "/ip4/127.0.0.1/tcp/jfodsajfidosajfoidsa", |
| 36 | + "/ip4/127.0.0.1/tcp", |
| 37 | + "/ip4/127.0.0.1/ipfs", |
| 38 | + "/ip4/127.0.0.1/ipfs/tcp" |
| 39 | + ).flatMap(s -> { |
| 40 | + try { |
| 41 | + new MultiAddress(s); |
| 42 | + return Stream.of(s); |
| 43 | + } catch (Exception e) { |
| 44 | + return Stream.empty(); |
| 45 | + } |
| 46 | + }).collect(Collectors.toList()); |
| 47 | + if (parsed.size() > 0) |
| 48 | + throw new IllegalStateException("Parsed invalid MultiAddresses: "+parsed); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void succeeds() { |
| 53 | + List<String> failed = Stream.of( |
| 54 | + "/ip4/1.2.3.4", |
| 55 | + "/ip4/0.0.0.0", |
| 56 | + "/ip6/::1", |
| 57 | + "/ip6/2601:9:4f81:9700:803e:ca65:66e8:c21", |
| 58 | + "/onion/timaq4ygg2iegci7:1234", |
| 59 | + "/onion/timaq4ygg2iegci7:80/http", |
| 60 | + "/udp/0", |
| 61 | + "/tcp/0", |
| 62 | + "/sctp/0", |
| 63 | + "/udp/1234", |
| 64 | + "/tcp/1234", |
| 65 | + "/sctp/1234", |
| 66 | + "/udp/65535", |
| 67 | + "/tcp/65535", |
| 68 | + "/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC", |
| 69 | + "/udp/1234/sctp/1234", |
| 70 | + "/udp/1234/udt", |
| 71 | + "/udp/1234/utp", |
| 72 | + "/tcp/1234/http", |
| 73 | + "/tcp/1234/https", |
| 74 | + "/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234", |
| 75 | + "/ip4/127.0.0.1/udp/1234", |
| 76 | + "/ip4/127.0.0.1/udp/0", |
| 77 | + "/ip4/127.0.0.1/tcp/1234", |
| 78 | + "/ip4/127.0.0.1/tcp/1234/", |
| 79 | + "/ip4/127.0.0.1/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC", |
| 80 | + "/ip4/127.0.0.1/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234" |
| 81 | + ).flatMap(s -> { |
| 82 | + try { |
| 83 | + new MultiAddress(s); |
| 84 | + return Stream.empty(); |
| 85 | + } catch (Exception e) { |
| 86 | + e.printStackTrace(); |
| 87 | + return Stream.of(s); |
| 88 | + } |
| 89 | + }).collect(Collectors.toList()); |
| 90 | + if (failed.size() > 0) |
| 91 | + throw new IllegalStateException("Failed to construct MultiAddresses: "+failed); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void equalsTests() { |
| 96 | + MultiAddress m1 = new MultiAddress("/ip4/127.0.0.1/udp/1234"); |
| 97 | + MultiAddress m2 = new MultiAddress("/ip4/127.0.0.1/tcp/1234"); |
| 98 | + MultiAddress m3 = new MultiAddress("/ip4/127.0.0.1/tcp/1234"); |
| 99 | + MultiAddress m4 = new MultiAddress("/ip4/127.0.0.1/tcp/1234/"); |
| 100 | + |
| 101 | + if (m1.equals(m2)) |
| 102 | + throw new IllegalStateException("Should be unequal!"); |
| 103 | + |
| 104 | + if (m2.equals(m1)) |
| 105 | + throw new IllegalStateException("Should be unequal!"); |
| 106 | + |
| 107 | + if (!m2.equals(m3)) |
| 108 | + throw new IllegalStateException("Should be equal!"); |
| 109 | + |
| 110 | + if (!m3.equals(m2)) |
| 111 | + throw new IllegalStateException("Should be equal!"); |
| 112 | + |
| 113 | + if (!m1.equals(m1)) |
| 114 | + throw new IllegalStateException("Should be equal!"); |
| 115 | + |
| 116 | + if (!m2.equals(m4)) |
| 117 | + throw new IllegalStateException("Should be equal!"); |
| 118 | + |
| 119 | + if (!m4.equals(m3)) |
| 120 | + throw new IllegalStateException("Should be equal!"); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void stringToBytes() { |
| 125 | + BiConsumer<String, String> test = (s, h) -> { |
| 126 | + if (!Arrays.equals(new MultiAddress(s).getBytes(), fromHex(h))) throw new IllegalStateException(s + " bytes != " + new MultiAddress(fromHex(h))); |
| 127 | + }; |
| 128 | + |
| 129 | + test.accept("/ip4/127.0.0.1/udp/1234", "047f0000011104d2"); |
| 130 | + test.accept("/ip4/127.0.0.1/tcp/4321", "047f0000010610e1"); |
| 131 | + test.accept("/ip4/127.0.0.1/udp/1234/ip4/127.0.0.1/tcp/4321", "047f0000011104d2047f0000010610e1"); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + public void bytesToString() { |
| 136 | + BiConsumer<String, String> test = (s, h) -> { |
| 137 | + if (!s.equals(new MultiAddress(fromHex(h)).toString())) throw new IllegalStateException(s + " != " + new MultiAddress(fromHex(h))); |
| 138 | + }; |
| 139 | + |
| 140 | + test.accept("/ip4/127.0.0.1/udp/1234", "047f0000011104d2"); |
| 141 | + test.accept("/ip4/127.0.0.1/tcp/4321", "047f0000010610e1"); |
| 142 | + test.accept("/ip4/127.0.0.1/udp/1234/ip4/127.0.0.1/tcp/4321", "047f0000011104d2047f0000010610e1"); |
| 143 | + } |
| 144 | + |
| 145 | + public static byte[] fromHex(String hex) { |
| 146 | + if (hex.length() % 2 != 0) |
| 147 | + throw new IllegalStateException("Uneven number of hex digits!"); |
| 148 | + ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
| 149 | + for (int i=0; i < hex.length()-1; i+= 2) |
| 150 | + bout.write(Integer.valueOf(hex.substring(i, i+2), 16)); |
| 151 | + return bout.toByteArray(); |
| 152 | + } |
| 153 | +} |
0 commit comments