commit 4d79f79a78af5f1dee293b29c1758f8864d3f6b7 Author: Karsten Loesing karsten.loesing@gmx.net Date: Sun Jun 12 17:00:48 2016 +0200
Parse "package" lines in consensuses and votes.
Implements #19285. --- CHANGELOG.md | 6 +++ .../descriptor/RelayNetworkStatusConsensus.java | 11 +++++ .../descriptor/RelayNetworkStatusVote.java | 10 +++++ .../impl/RelayNetworkStatusConsensusImpl.java | 21 ++++++++++ .../impl/RelayNetworkStatusVoteImpl.java | 21 ++++++++++ .../descriptor/impl/ConsensusBuilder.java | 11 +++++ .../impl/RelayNetworkStatusConsensusImplTest.java | 37 +++++++++++++++++ .../impl/RelayNetworkStatusVoteImplTest.java | 47 ++++++++++++++++++++++ 8 files changed, 164 insertions(+)
diff --git a/CHANGELOG.md b/CHANGELOG.md index ecb1523..3c243d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# Changes in version 1.3.0 - 2016-0x-xx + + * Medium changes + - Parse "package" lines in consensuses and votes. + + # Changes in version 1.2.0 - 2016-05-31
* Medium changes diff --git a/src/org/torproject/descriptor/RelayNetworkStatusConsensus.java b/src/org/torproject/descriptor/RelayNetworkStatusConsensus.java index 624f1cc..90f96d7 100644 --- a/src/org/torproject/descriptor/RelayNetworkStatusConsensus.java +++ b/src/org/torproject/descriptor/RelayNetworkStatusConsensus.java @@ -113,6 +113,17 @@ public interface RelayNetworkStatusConsensus extends Descriptor { public List<String> getRecommendedClientVersions();
/** + * Return a list of software packages and their versions together with a + * URL and one or more digests in the format <code>PackageName Version + * URL DIGESTS</code> that are known by at least three directory + * authorities and agreed upon by the majority of directory authorities, + * or null if the consensus does not contain package information. + * + * @since 1.3.0 + */ + public List<String> getPackageLines(); + + /** * Return known relay flags in this descriptor that were contained in * enough votes for this consensus to be an authoritative opinion for * these relay flags. diff --git a/src/org/torproject/descriptor/RelayNetworkStatusVote.java b/src/org/torproject/descriptor/RelayNetworkStatusVote.java index 5e0c7cd..6e7e1b7 100644 --- a/src/org/torproject/descriptor/RelayNetworkStatusVote.java +++ b/src/org/torproject/descriptor/RelayNetworkStatusVote.java @@ -105,6 +105,16 @@ public interface RelayNetworkStatusVote extends Descriptor { public List<String> getRecommendedClientVersions();
/** + * Return a list of software packages and their versions together with a + * URL and one or more digests in the format <code>PackageName Version + * URL DIGESTS</code> that are known by this directory authority, or + * null if this descriptor does not contain package information. + * + * @since 1.3.0 + */ + public List<String> getPackageLines(); + + /** * Return known relay flags by this authority. * * @since 1.0.0 diff --git a/src/org/torproject/descriptor/impl/RelayNetworkStatusConsensusImpl.java b/src/org/torproject/descriptor/impl/RelayNetworkStatusConsensusImpl.java index 13b9142..091ba09 100644 --- a/src/org/torproject/descriptor/impl/RelayNetworkStatusConsensusImpl.java +++ b/src/org/torproject/descriptor/impl/RelayNetworkStatusConsensusImpl.java @@ -123,6 +123,9 @@ public class RelayNetworkStatusConsensusImpl extends NetworkStatusImpl case "server-versions": this.parseServerVersionsLine(line, parts); break; + case "package": + this.parsePackageLine(line, parts); + break; case "known-flags": this.parseKnownFlagsLine(line, parts); break; @@ -276,6 +279,18 @@ public class RelayNetworkStatusConsensusImpl extends NetworkStatusImpl line, parts); }
+ private void parsePackageLine(String line, String[] parts) + throws DescriptorParseException { + if (parts.length < 5) { + throw new DescriptorParseException("Wrong number of values in line " + + "'" + line + "'."); + } + if (this.packageLines == null) { + this.packageLines = new ArrayList<>(); + } + this.packageLines.add(line.substring("package ".length())); + } + private void parseKnownFlagsLine(String line, String[] parts) throws DescriptorParseException { if (parts.length < 2) { @@ -358,6 +373,12 @@ public class RelayNetworkStatusConsensusImpl extends NetworkStatusImpl Arrays.asList(this.recommendedServerVersions); }
+ private List<String> packageLines; + public List<String> getPackageLines() { + return this.packageLines == null ? null + : new ArrayList<>(this.packageLines); + } + private String[] knownFlags; public SortedSet<String> getKnownFlags() { return new TreeSet<>(Arrays.asList(this.knownFlags)); diff --git a/src/org/torproject/descriptor/impl/RelayNetworkStatusVoteImpl.java b/src/org/torproject/descriptor/impl/RelayNetworkStatusVoteImpl.java index 8d18919..f378a79 100644 --- a/src/org/torproject/descriptor/impl/RelayNetworkStatusVoteImpl.java +++ b/src/org/torproject/descriptor/impl/RelayNetworkStatusVoteImpl.java @@ -113,6 +113,9 @@ public class RelayNetworkStatusVoteImpl extends NetworkStatusImpl case "server-versions": this.parseServerVersionsLine(line, parts); break; + case "package": + this.parsePackageLine(line, parts); + break; case "known-flags": this.parseKnownFlagsLine(line, parts); break; @@ -298,6 +301,18 @@ public class RelayNetworkStatusVoteImpl extends NetworkStatusImpl line, parts); }
+ private void parsePackageLine(String line, String[] parts) + throws DescriptorParseException { + if (parts.length < 5) { + throw new DescriptorParseException("Wrong number of values in line " + + "'" + line + "'."); + } + if (this.packageLines == null) { + this.packageLines = new ArrayList<>(); + } + this.packageLines.add(line.substring("package ".length())); + } + private void parseKnownFlagsLine(String line, String[] parts) throws DescriptorParseException { if (parts.length < 2) { @@ -634,6 +649,12 @@ public class RelayNetworkStatusVoteImpl extends NetworkStatusImpl Arrays.asList(this.recommendedServerVersions); }
+ private List<String> packageLines; + public List<String> getPackageLines() { + return this.packageLines == null ? null + : new ArrayList<>(this.packageLines); + } + private String[] knownFlags; public SortedSet<String> getKnownFlags() { return new TreeSet<>(Arrays.asList(this.knownFlags)); diff --git a/test/org/torproject/descriptor/impl/ConsensusBuilder.java b/test/org/torproject/descriptor/impl/ConsensusBuilder.java index b9638fb..29a2d47 100644 --- a/test/org/torproject/descriptor/impl/ConsensusBuilder.java +++ b/test/org/torproject/descriptor/impl/ConsensusBuilder.java @@ -85,6 +85,14 @@ public class ConsensusBuilder { cb.serverVersionsLine = line; return new RelayNetworkStatusConsensusImpl(cb.buildConsensus(), true); } + private String packageLines = null; + protected static RelayNetworkStatusConsensus + createWithPackageLines(String lines) + throws DescriptorParseException { + ConsensusBuilder cb = new ConsensusBuilder(); + cb.packageLines = lines; + return new RelayNetworkStatusConsensusImpl(cb.buildConsensus(), true); + } private String knownFlagsLine = "known-flags Authority BadExit Exit " + "Fast Guard HSDir Named Running Stable Unnamed V2Dir Valid"; protected static RelayNetworkStatusConsensus @@ -261,6 +269,9 @@ public class ConsensusBuilder { if (this.serverVersionsLine != null) { sb.append(this.serverVersionsLine).append("\n"); } + if (this.packageLines != null) { + sb.append(this.packageLines).append("\n"); + } if (this.knownFlagsLine != null) { sb.append(this.knownFlagsLine).append("\n"); } diff --git a/test/org/torproject/descriptor/impl/RelayNetworkStatusConsensusImplTest.java b/test/org/torproject/descriptor/impl/RelayNetworkStatusConsensusImplTest.java index dbe6cd3..86e4e78 100644 --- a/test/org/torproject/descriptor/impl/RelayNetworkStatusConsensusImplTest.java +++ b/test/org/torproject/descriptor/impl/RelayNetworkStatusConsensusImplTest.java @@ -10,6 +10,7 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue;
import java.util.ArrayList; +import java.util.Arrays; import java.util.List;
@@ -607,6 +608,42 @@ public class RelayNetworkStatusConsensusImplTest { "client-versions ,0.2.2.34"); }
+ @Test() + public void testPackageNone() throws DescriptorParseException { + RelayNetworkStatusConsensus consensus = + ConsensusBuilder.createWithPackageLines(null); + assertNull(consensus.getPackageLines()); + } + + @Test() + public void testPackageOne() throws DescriptorParseException { + String packageLine = "package shouldbesecond 0 http digest=digest"; + RelayNetworkStatusConsensus consensus = + ConsensusBuilder.createWithPackageLines(packageLine); + assertEquals(packageLine.substring("package ".length()), + consensus.getPackageLines().get(0)); + } + + @Test() + public void testPackageTwo() throws DescriptorParseException { + List<String> packageLines = Arrays.asList( + "package shouldbesecond 0 http digest=digest", + "package outoforder 0 http digest=digest"); + RelayNetworkStatusConsensus consensus = + ConsensusBuilder.createWithPackageLines(packageLines.get(0) + + "\n" + packageLines.get(1)); + for (int i = 0; i < packageLines.size(); i++) { + assertEquals(packageLines.get(i).substring("package ".length()), + consensus.getPackageLines().get(i)); + } + } + + @Test(expected = DescriptorParseException.class) + public void testPackageIncomplete() throws DescriptorParseException { + String packageLine = "package shouldbesecond 0 http"; + ConsensusBuilder.createWithPackageLines(packageLine); + } + @Test(expected = DescriptorParseException.class) public void testKnownFlagsNoLine() throws DescriptorParseException { ConsensusBuilder.createWithKnownFlagsLine(null); diff --git a/test/org/torproject/descriptor/impl/RelayNetworkStatusVoteImplTest.java b/test/org/torproject/descriptor/impl/RelayNetworkStatusVoteImplTest.java index 261abd4..f7ddd8c 100644 --- a/test/org/torproject/descriptor/impl/RelayNetworkStatusVoteImplTest.java +++ b/test/org/torproject/descriptor/impl/RelayNetworkStatusVoteImplTest.java @@ -108,6 +108,14 @@ public class RelayNetworkStatusVoteImplTest { vb.serverVersionsLine = line; return new RelayNetworkStatusVoteImpl(vb.buildVote(), true); } + private String packageLines = null; + protected static RelayNetworkStatusVote + createWithPackageLines(String lines) + throws DescriptorParseException { + VoteBuilder vb = new VoteBuilder(); + vb.packageLines = lines; + return new RelayNetworkStatusVoteImpl(vb.buildVote(), true); + } private String knownFlagsLine = "known-flags Authority BadExit Exit " + "Fast Guard HSDir Named Running Stable Unnamed V2Dir Valid"; private static RelayNetworkStatusVote @@ -408,6 +416,9 @@ public class RelayNetworkStatusVoteImplTest { if (this.serverVersionsLine != null) { sb.append(this.serverVersionsLine).append("\n"); } + if (this.packageLines != null) { + sb.append(this.packageLines).append("\n"); + } if (this.knownFlagsLine != null) { sb.append(this.knownFlagsLine).append("\n"); } @@ -777,6 +788,42 @@ public class RelayNetworkStatusVoteImplTest { "client-versions ,0.2.2.34"); }
+ @Test() + public void testPackageNone() throws DescriptorParseException { + RelayNetworkStatusVote vote = + VoteBuilder.createWithPackageLines(null); + assertNull(vote.getPackageLines()); + } + + @Test() + public void testPackageOne() throws DescriptorParseException { + String packageLine = "package shouldbesecond 0 http digest=digest"; + RelayNetworkStatusVote vote = + VoteBuilder.createWithPackageLines(packageLine); + assertEquals(packageLine.substring("package ".length()), + vote.getPackageLines().get(0)); + } + + @Test() + public void testPackageTwo() throws DescriptorParseException { + List<String> packageLines = Arrays.asList( + "package shouldbesecond 0 http digest=digest", + "package outoforder 0 http digest=digest"); + RelayNetworkStatusVote vote = + VoteBuilder.createWithPackageLines(packageLines.get(0) + + "\n" + packageLines.get(1)); + for (int i = 0; i < packageLines.size(); i++) { + assertEquals(packageLines.get(i).substring("package ".length()), + vote.getPackageLines().get(i)); + } + } + + @Test(expected = DescriptorParseException.class) + public void testPackageIncomplete() throws DescriptorParseException { + String packageLine = "package shouldbesecond 0 http"; + ConsensusBuilder.createWithPackageLines(packageLine); + } + @Test(expected = DescriptorParseException.class) public void testKnownFlagsNoLine() throws DescriptorParseException { VoteBuilder.createWithKnownFlagsLine(null);