commit 9ccb934fac5827ef663e6740f64c4e06c6cb795e Author: Karsten Loesing karsten.loesing@gmx.net Date: Sat Feb 22 09:13:33 2020 +0100
Avoid invoking overridable methods from constructors.
Fixes #33205. --- CHANGELOG.md | 3 ++ .../descriptor/impl/BridgeNetworkStatusImpl.java | 4 +-- .../descriptor/impl/NetworkStatusImpl.java | 7 ++--- .../impl/RelayNetworkStatusConsensusImpl.java | 3 +- .../descriptor/impl/RelayNetworkStatusImpl.java | 3 +- .../impl/RelayNetworkStatusVoteImpl.java | 35 +++++++--------------- .../impl/RelayNetworkStatusVoteImplTest.java | 13 ++++++++ 7 files changed, 36 insertions(+), 32 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md index bb8bb89..d58e92b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changes in version 2.1?.? - 2020-0?-??
+ * Minor changes + - Avoid invoking overridable methods from constructors. +
# Changes in version 2.10.0 - 2020-01-15
diff --git a/src/main/java/org/torproject/descriptor/impl/BridgeNetworkStatusImpl.java b/src/main/java/org/torproject/descriptor/impl/BridgeNetworkStatusImpl.java index d3d546b..b9241a8 100644 --- a/src/main/java/org/torproject/descriptor/impl/BridgeNetworkStatusImpl.java +++ b/src/main/java/org/torproject/descriptor/impl/BridgeNetworkStatusImpl.java @@ -22,8 +22,8 @@ public class BridgeNetworkStatusImpl extends NetworkStatusImpl protected BridgeNetworkStatusImpl(byte[] rawDescriptorBytes, int[] offsetAndLength, File descriptorFile, String fileName) throws DescriptorParseException { - super(rawDescriptorBytes, offsetAndLength, descriptorFile, - false, false); + super(rawDescriptorBytes, offsetAndLength, descriptorFile, false); + this.splitAndParseParts(false); this.setPublishedMillisFromFileName(fileName); }
diff --git a/src/main/java/org/torproject/descriptor/impl/NetworkStatusImpl.java b/src/main/java/org/torproject/descriptor/impl/NetworkStatusImpl.java index b994016..4b160f5 100644 --- a/src/main/java/org/torproject/descriptor/impl/NetworkStatusImpl.java +++ b/src/main/java/org/torproject/descriptor/impl/NetworkStatusImpl.java @@ -26,14 +26,13 @@ public abstract class NetworkStatusImpl extends DescriptorImpl { protected Map<Integer, String> flagStrings = new HashMap<>();
protected NetworkStatusImpl(byte[] rawDescriptorBytes, int[] offsetAndLength, - File descriptorFile, boolean containsDirSourceEntries, - boolean blankLinesAllowed) throws DescriptorParseException { + File descriptorFile, boolean blankLinesAllowed) + throws DescriptorParseException { super(rawDescriptorBytes, offsetAndLength, descriptorFile, blankLinesAllowed); - this.splitAndParseParts(containsDirSourceEntries); }
- private void splitAndParseParts(boolean containsDirSourceEntries) + protected final void splitAndParseParts(boolean containsDirSourceEntries) throws DescriptorParseException { int firstRIndex = this.findFirstIndexOfKey(Key.R); int firstDirectorySignatureIndex = this.findFirstIndexOfKey( diff --git a/src/main/java/org/torproject/descriptor/impl/RelayNetworkStatusConsensusImpl.java b/src/main/java/org/torproject/descriptor/impl/RelayNetworkStatusConsensusImpl.java index 79f0756..367865e 100644 --- a/src/main/java/org/torproject/descriptor/impl/RelayNetworkStatusConsensusImpl.java +++ b/src/main/java/org/torproject/descriptor/impl/RelayNetworkStatusConsensusImpl.java @@ -25,7 +25,8 @@ public class RelayNetworkStatusConsensusImpl extends NetworkStatusImpl protected RelayNetworkStatusConsensusImpl(byte[] consensusBytes, int[] offsetAndLimit, File descriptorFile) throws DescriptorParseException { - super(consensusBytes, offsetAndLimit, descriptorFile, true, false); + super(consensusBytes, offsetAndLimit, descriptorFile, false); + this.splitAndParseParts(true); Set<Key> exactlyOnceKeys = EnumSet.of( Key.VOTE_STATUS, Key.CONSENSUS_METHOD, Key.VALID_AFTER, Key.FRESH_UNTIL, Key.VALID_UNTIL, Key.VOTING_DELAY, Key.KNOWN_FLAGS); diff --git a/src/main/java/org/torproject/descriptor/impl/RelayNetworkStatusImpl.java b/src/main/java/org/torproject/descriptor/impl/RelayNetworkStatusImpl.java index 0fba932..5de8f70 100644 --- a/src/main/java/org/torproject/descriptor/impl/RelayNetworkStatusImpl.java +++ b/src/main/java/org/torproject/descriptor/impl/RelayNetworkStatusImpl.java @@ -21,7 +21,8 @@ public class RelayNetworkStatusImpl extends NetworkStatusImpl
protected RelayNetworkStatusImpl(byte[] statusBytes, int[] offsetAndLength, File descriptorFile) throws DescriptorParseException { - super(statusBytes, offsetAndLength, descriptorFile, false, true); + super(statusBytes, offsetAndLength, descriptorFile, true); + this.splitAndParseParts(false); Set<Key> exactlyOnceKeys = EnumSet.of( Key.NETWORK_STATUS_VERSION, Key.DIR_SOURCE, Key.FINGERPRINT, Key.CONTACT, Key.DIR_SIGNING_KEY, Key.PUBLISHED); diff --git a/src/main/java/org/torproject/descriptor/impl/RelayNetworkStatusVoteImpl.java b/src/main/java/org/torproject/descriptor/impl/RelayNetworkStatusVoteImpl.java index 56a9e21..7e8d816 100644 --- a/src/main/java/org/torproject/descriptor/impl/RelayNetworkStatusVoteImpl.java +++ b/src/main/java/org/torproject/descriptor/impl/RelayNetworkStatusVoteImpl.java @@ -26,7 +26,8 @@ public class RelayNetworkStatusVoteImpl extends NetworkStatusImpl protected RelayNetworkStatusVoteImpl(byte[] voteBytes, int[] offsetAndLength, File descriptorFile) throws DescriptorParseException { - super(voteBytes, offsetAndLength, descriptorFile, false, false); + super(voteBytes, offsetAndLength, descriptorFile, false); + this.splitAndParseParts(false); Set<Key> exactlyOnceKeys = EnumSet.of( Key.VOTE_STATUS, Key.PUBLISHED, Key.VALID_AFTER, Key.FRESH_UNTIL, Key.VALID_UNTIL, Key.VOTING_DELAY, Key.KNOWN_FLAGS, Key.DIR_SOURCE, @@ -52,20 +53,6 @@ public class RelayNetworkStatusVoteImpl extends NetworkStatusImpl
protected void parseHeader(int offset, int length) throws DescriptorParseException { - /* Initialize flag-thresholds values here for the case that the vote - * doesn't contain those values. Initializing them in the constructor - * or when declaring variables wouldn't work, because those parts are - * evaluated later and would overwrite everything we parse here. */ - this.stableUptime = -1L; - this.stableMtbf = -1L; - this.fastBandwidth = -1L; - this.guardWfu = -1.0; - this.guardTk = -1L; - this.guardBandwidthIncludingExits = -1L; - this.guardBandwidthExcludingExits = -1L; - this.enoughMtbfInfo = -1; - this.ignoringAdvertisedBws = -1; - Scanner scanner = this.newScanner(offset, length).useDelimiter(NL); Key nextCrypto = Key.EMPTY; StringBuilder crypto = null; @@ -847,63 +834,63 @@ public class RelayNetworkStatusVoteImpl extends NetworkStatusImpl return new TreeSet<>(Arrays.asList(this.knownFlags)); }
- private long stableUptime; + private long stableUptime = -1L;
@Override public long getStableUptime() { return this.stableUptime; }
- private long stableMtbf; + private long stableMtbf = -1L;
@Override public long getStableMtbf() { return this.stableMtbf; }
- private long fastBandwidth; + private long fastBandwidth = -1L;
@Override public long getFastBandwidth() { return this.fastBandwidth; }
- private double guardWfu; + private double guardWfu = -1.0;
@Override public double getGuardWfu() { return this.guardWfu; }
- private long guardTk; + private long guardTk = -1L;
@Override public long getGuardTk() { return this.guardTk; }
- private long guardBandwidthIncludingExits; + private long guardBandwidthIncludingExits = -1L;
@Override public long getGuardBandwidthIncludingExits() { return this.guardBandwidthIncludingExits; }
- private long guardBandwidthExcludingExits; + private long guardBandwidthExcludingExits = -1L;
@Override public long getGuardBandwidthExcludingExits() { return this.guardBandwidthExcludingExits; }
- private int enoughMtbfInfo; + private int enoughMtbfInfo = -1;
@Override public int getEnoughMtbfInfo() { return this.enoughMtbfInfo; }
- private int ignoringAdvertisedBws; + private int ignoringAdvertisedBws = -1;
@Override public int getIgnoringAdvertisedBws() { diff --git a/src/test/java/org/torproject/descriptor/impl/RelayNetworkStatusVoteImplTest.java b/src/test/java/org/torproject/descriptor/impl/RelayNetworkStatusVoteImplTest.java index 6735b61..a9104b4 100644 --- a/src/test/java/org/torproject/descriptor/impl/RelayNetworkStatusVoteImplTest.java +++ b/src/test/java/org/torproject/descriptor/impl/RelayNetworkStatusVoteImplTest.java @@ -721,6 +721,8 @@ public class RelayNetworkStatusVoteImplTest { assertEquals(3, vote.getDirKeyCertificateVersion()); assertEquals("80550987E1D626E3EBA5E5E75A458DE0626D088C", vote.getIdentity()); + assertTrue(vote.isSharedRandParticipate()); + assertEquals(8, vote.getSharedRandCurrentNumReveals()); assertEquals(1303882477000L, /* 2011-04-27 05:34:37 */ vote.getDirKeyPublishedMillis()); assertEquals(1335504877000L, /* 2012-04-27 05:34:37 */ @@ -745,6 +747,8 @@ public class RelayNetworkStatusVoteImplTest { + "F3Yh\nrXVaaoP07r6Ta+s0g1Zijm3lms50Nk/4tV2p8Y63c3F4Q3DAnK40Oi" + "kfOIwEj+Ny\n+zBRQssP3hPhTPOj/A7o3mZZwtL6x1sxpeu/nME1l5E=\n" + "-----END SIGNATURE-----\n", signature.getSignature()); + assertEquals(184320, vote.getGuardBandwidthExcludingExits()); + assertEquals(1, vote.getEnoughMtbfInfo()); assertTrue(vote.getUnrecognizedLines().isEmpty()); }
@@ -1549,6 +1553,15 @@ public class RelayNetworkStatusVoteImplTest { }
@Test + public void testSharedRandCurrentValueLineEmpty() + throws DescriptorParseException { + RelayNetworkStatusVote vote = + VoteBuilder.createWithSharedRandCurrentValueLine(null); + assertEquals(-1, vote.getSharedRandCurrentNumReveals()); + assertNull(vote.getSharedRandCurrentValue()); + } + + @Test public void testSharedRandCurrentNoNumReveals() throws DescriptorParseException { this.thrown.expect(DescriptorParseException.class);
tor-commits@lists.torproject.org