Commit 4d79f79a authored by Karsten Loesing's avatar Karsten Loesing
Browse files

Parse "package" lines in consensuses and votes.

Implements #19285.
parent fecd53b1
# 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
......
......@@ -112,6 +112,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
......
......@@ -104,6 +104,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.
*
......
......@@ -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));
......
......@@ -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));
......
......@@ -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");
}
......
......@@ -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);
......
......@@ -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);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment