commit 17e9149f0733f854737d14233e3537a4ee25e4d2 Author: Karsten Loesing karsten.loesing@gmx.net Date: Thu Nov 8 14:18:18 2012 -0500
Add support for parsing bridge-ip-versions lines. --- .../torproject/descriptor/ExtraInfoDescriptor.java | 6 +++++ .../descriptor/impl/ExtraInfoDescriptorImpl.java | 14 +++++++++++ .../impl/ExtraInfoDescriptorImplTest.java | 24 ++++++++++++++++++++ 3 files changed, 44 insertions(+), 0 deletions(-)
diff --git a/src/org/torproject/descriptor/ExtraInfoDescriptor.java b/src/org/torproject/descriptor/ExtraInfoDescriptor.java index 0e19425..e57febe 100644 --- a/src/org/torproject/descriptor/ExtraInfoDescriptor.java +++ b/src/org/torproject/descriptor/ExtraInfoDescriptor.java @@ -242,6 +242,12 @@ public interface ExtraInfoDescriptor extends Descriptor { * of 8, or null if no bridge statistics are included. */ public SortedMap<String, Integer> getBridgeIps();
+ /* Return statistics on client IP addresses with map keys being protocol + * family, e.g., "v4" or "v6", and map values being the number of unique + * IP addresses rounded up to the nearest multiple of 8, or null if no + * bridge IP version statistics are included. */ + public SortedMap<String, Integer> getBridgeIpVersions(); + /* Return the (possibly empty) list of transports supported by this * bridge. */ public List<String> getTransports(); diff --git a/src/org/torproject/descriptor/impl/ExtraInfoDescriptorImpl.java b/src/org/torproject/descriptor/impl/ExtraInfoDescriptorImpl.java index d84a5e2..d7dfd79 100644 --- a/src/org/torproject/descriptor/impl/ExtraInfoDescriptorImpl.java +++ b/src/org/torproject/descriptor/impl/ExtraInfoDescriptorImpl.java @@ -168,6 +168,8 @@ public class ExtraInfoDescriptorImpl extends DescriptorImpl this.parseBridgeStatsEndLine(line, lineNoOpt, partsNoOpt); } else if (keyword.equals("bridge-ips")) { this.parseBridgeStatsIpsLine(line, lineNoOpt, partsNoOpt); + } else if (keyword.equals("bridge-ip-versions")) { + this.parseBridgeIpVersionsLine(line, lineNoOpt, partsNoOpt); } else if (keyword.equals("transport")) { this.parseTransportLine(line, lineNoOpt, partsNoOpt); } else if (keyword.equals("router-signature")) { @@ -581,6 +583,12 @@ public class ExtraInfoDescriptorImpl extends DescriptorImpl partsNoOpt, 1, 2); }
+ private void parseBridgeIpVersionsLine(String line, String lineNoOpt, + String[] partsNoOpt) throws DescriptorParseException { + this.bridgeIpVersions = ParseHelper.parseCommaSeparatedKeyValueList(line, + partsNoOpt, 1, 2); + } + private void parseTransportLine(String line, String lineNoOpt, String[] partsNoOpt) throws DescriptorParseException { if (partsNoOpt.length < 2) { @@ -897,6 +905,12 @@ public class ExtraInfoDescriptorImpl extends DescriptorImpl new TreeMap<String, Integer>(this.bridgeIps); }
+ private SortedMap<String, Integer> bridgeIpVersions; + public SortedMap<String, Integer> getBridgeIpVersions() { + return this.bridgeIpVersions == null ? null : + new TreeMap<String, Integer>(this.bridgeIpVersions); + } + private List<String> transports = new ArrayList<String>(); public List<String> getTransports() { return new ArrayList<String>(this.transports); diff --git a/test/org/torproject/descriptor/impl/ExtraInfoDescriptorImplTest.java b/test/org/torproject/descriptor/impl/ExtraInfoDescriptorImplTest.java index a2becd2..e1b19f8 100644 --- a/test/org/torproject/descriptor/impl/ExtraInfoDescriptorImplTest.java +++ b/test/org/torproject/descriptor/impl/ExtraInfoDescriptorImplTest.java @@ -641,6 +641,14 @@ public class ExtraInfoDescriptorImplTest { return DescriptorBuilder.createWithBridgeStatsLines( bsb.buildBridgeStatsLines()); } + private String bridgeIpVersionsLine = "bridge-ip-versions v4=8,v6=16"; + private static ExtraInfoDescriptor createWithBridgeIpVersionsLine( + String line) throws DescriptorParseException { + BridgeStatsBuilder bsb = new BridgeStatsBuilder(); + bsb.bridgeIpVersionsLine = line; + return DescriptorBuilder.createWithBridgeStatsLines( + bsb.buildBridgeStatsLines()); + } private static ExtraInfoDescriptor createWithDefaultLines() throws DescriptorParseException { return DescriptorBuilder.createWithBridgeStatsLines( @@ -654,6 +662,9 @@ public class ExtraInfoDescriptorImplTest { if (this.bridgeIpsLine != null) { sb.append(this.bridgeIpsLine + "\n"); } + if (this.bridgeIpVersionsLine != null) { + sb.append(this.bridgeIpVersionsLine + "\n"); + } String lines = sb.toString(); if (lines.endsWith("\n")) { lines = lines.substring(0, lines.length() - 1); @@ -1255,6 +1266,11 @@ public class ExtraInfoDescriptorImplTest { assertEquals(24, ips.get("ir").intValue()); assertEquals(16, ips.get("sy").intValue()); assertFalse(ips.containsKey("no")); + SortedMap<String, Integer> ver = descriptor.getBridgeIpVersions(); + assertNotNull(ver); + assertEquals(8, ver.get("v4").intValue()); + assertEquals(16, ver.get("v6").intValue()); + assertFalse(ver.containsKey("v8")); }
@Test(expected = DescriptorParseException.class) @@ -1269,6 +1285,14 @@ public class ExtraInfoDescriptorImplTest { throws DescriptorParseException { BridgeStatsBuilder.createWithBridgeIpsLine("bridge-ips ir=24.5"); } + + @Test(expected = DescriptorParseException.class) + public void testBridgeIpVersionsDouble() + throws DescriptorParseException { + BridgeStatsBuilder.createWithBridgeIpVersionsLine( + "bridge-ip-versions v4=24.5"); + } + @Test() public void testRouterSignatureOpt() throws DescriptorParseException {
tor-commits@lists.torproject.org