[tor-commits] [metrics-lib/master] task-17824 addendum.

karsten at torproject.org karsten at torproject.org
Fri Dec 25 09:02:44 UTC 2015


commit 5b73e0eb9afe09b0f9e73773b4310644de987837
Author: iwakeh <iwakeh at users.ourproject.org>
Date:   Tue Dec 22 22:00:00 2015 +0000

    task-17824 addendum.
---
 .../descriptor/impl/DirSourceEntryImpl.java        |   35 +++--
 .../impl/DirectoryKeyCertificateImpl.java          |   53 ++++----
 .../descriptor/impl/DirectorySignatureImpl.java    |   41 +++---
 .../descriptor/impl/DownloadCoordinatorImpl.java   |   75 ++++++-----
 .../descriptor/impl/ExtraInfoDescriptorImpl.java   |   62 ++++-----
 .../descriptor/impl/MicrodescriptorImpl.java       |   72 +++++-----
 .../descriptor/impl/NetworkStatusEntryImpl.java    |   25 ++--
 .../descriptor/impl/NetworkStatusImpl.java         |   27 ++--
 .../torproject/descriptor/impl/ParseHelper.java    |   12 ++
 .../descriptor/impl/RelayDirectoryImpl.java        |   64 +++++----
 .../descriptor/impl/RelayNetworkStatusImpl.java    |   61 +++++----
 .../impl/RelayNetworkStatusVoteImpl.java           |   14 +-
 .../descriptor/impl/ServerDescriptorImpl.java      |  137 ++++++++++----------
 13 files changed, 377 insertions(+), 301 deletions(-)

diff --git a/src/org/torproject/descriptor/impl/DirSourceEntryImpl.java b/src/org/torproject/descriptor/impl/DirSourceEntryImpl.java
index 3a59d5b..3ea0179 100644
--- a/src/org/torproject/descriptor/impl/DirSourceEntryImpl.java
+++ b/src/org/torproject/descriptor/impl/DirSourceEntryImpl.java
@@ -80,25 +80,34 @@ public class DirSourceEntryImpl implements DirSourceEntry {
     boolean skipCrypto = false;
     while (s.hasNext()) {
       String line = s.next();
-      if (line.startsWith("dir-source")) {
+      String[] parts = line.split(" ");
+      switch (parts[0]) {
+      case "dir-source":
         this.parseDirSourceLine(line);
-      } else if (line.startsWith("contact")) {
+        break;
+      case "contact":
         this.parseContactLine(line);
-      } else if (line.startsWith("vote-digest")) {
+        break;
+      case "vote-digest":
         this.parseVoteDigestLine(line);
-      } else if (line.startsWith("-----BEGIN")) {
+        break;
+      case "-----BEGIN":
         skipCrypto = true;
-      } else if (line.startsWith("-----END")) {
+        break;
+      case "-----END":
         skipCrypto = false;
-      } else if (!skipCrypto) {
-        if (this.failUnrecognizedDescriptorLines) {
-          throw new DescriptorParseException("Unrecognized line '"
-              + line + "' in dir-source entry.");
-        } else {
-          if (this.unrecognizedLines == null) {
-            this.unrecognizedLines = new ArrayList<>();
+        break;
+      default:
+        if (!skipCrypto) {
+          if (this.failUnrecognizedDescriptorLines) {
+            throw new DescriptorParseException("Unrecognized line '"
+                + line + "' in dir-source entry.");
+          } else {
+            if (this.unrecognizedLines == null) {
+              this.unrecognizedLines = new ArrayList<>();
+            }
+            this.unrecognizedLines.add(line);
           }
-          this.unrecognizedLines.add(line);
         }
       }
     }
diff --git a/src/org/torproject/descriptor/impl/DirectoryKeyCertificateImpl.java b/src/org/torproject/descriptor/impl/DirectoryKeyCertificateImpl.java
index 2b7d3ea..1039bbd 100644
--- a/src/org/torproject/descriptor/impl/DirectoryKeyCertificateImpl.java
+++ b/src/org/torproject/descriptor/impl/DirectoryKeyCertificateImpl.java
@@ -98,33 +98,34 @@ public class DirectoryKeyCertificateImpl extends DescriptorImpl
         this.parseDirKeyCertificationLine(line, parts);
         nextCrypto = "dir-key-certification";
         break;
+      case "-----BEGIN":
+        crypto = new StringBuilder();
+        crypto.append(line).append("\n");
+        break;
+      case "-----END":
+        crypto.append(line).append("\n");
+        String cryptoString = crypto.toString();
+        crypto = null;
+        switch (nextCrypto) {
+        case "dir-identity-key":
+          this.dirIdentityKey = cryptoString;
+          break;
+        case "dir-signing-key":
+          this.dirSigningKey = cryptoString;
+          break;
+        case "dir-key-crosscert":
+          this.dirKeyCrosscert = cryptoString;
+          break;
+        case "dir-key-certification":
+          this.dirKeyCertification = cryptoString;
+          break;
+        default:
+          throw new DescriptorParseException("Unrecognized crypto "
+              + "block in directory key certificate.");
+        }
+        nextCrypto = "";
       default:
-        if (line.startsWith("-----BEGIN")) {
-          crypto = new StringBuilder();
-          crypto.append(line).append("\n");
-        } else if (line.startsWith("-----END")) {
-          crypto.append(line).append("\n");
-          String cryptoString = crypto.toString();
-          crypto = null;
-          switch (nextCrypto) {
-          case "dir-identity-key":
-            this.dirIdentityKey = cryptoString;
-            break;
-          case "dir-signing-key":
-            this.dirSigningKey = cryptoString;
-            break;
-          case "dir-key-crosscert":
-            this.dirKeyCrosscert = cryptoString;
-            break;
-          case "dir-key-certification":
-            this.dirKeyCertification = cryptoString;
-            break;
-          default:
-            throw new DescriptorParseException("Unrecognized crypto "
-                + "block in directory key certificate.");
-          }
-          nextCrypto = "";
-        } else if (crypto != null) {
+        if (crypto != null) {
           crypto.append(line).append("\n");
         } else {
           if (this.failUnrecognizedDescriptorLines) {
diff --git a/src/org/torproject/descriptor/impl/DirectorySignatureImpl.java b/src/org/torproject/descriptor/impl/DirectorySignatureImpl.java
index 8cf6589..18de918 100644
--- a/src/org/torproject/descriptor/impl/DirectorySignatureImpl.java
+++ b/src/org/torproject/descriptor/impl/DirectorySignatureImpl.java
@@ -40,13 +40,19 @@ public class DirectorySignatureImpl implements DirectorySignature {
     StringBuilder crypto = null;
     while (s.hasNext()) {
       String line = s.next();
-      if (line.startsWith("directory-signature ")) {
-        String[] parts = line.split(" ", -1);
+      String[] parts = line.split(" ", -1);
+      String keyword = parts[0];
+      switch (keyword) {
+      case "directory-signature":
         int algorithmOffset = 0;
-        if (parts.length == 4) {
+        switch (parts.length) {
+        case 4:
           this.algorithm = parts[1];
           algorithmOffset = 1;
-        } else if (parts.length != 3) {
+          break;
+        case 3:
+          break;
+        default:
           throw new DescriptorParseException("Illegal line '" + line
               + "'.");
         }
@@ -54,25 +60,30 @@ public class DirectorySignatureImpl implements DirectorySignature {
             parts[1 + algorithmOffset]);
         this.signingKeyDigest = ParseHelper.parseTwentyByteHexString(
             line, parts[2 + algorithmOffset]);
-      } else if (line.startsWith("-----BEGIN")) {
+        break;
+      case "-----BEGIN":
         crypto = new StringBuilder();
         crypto.append(line).append("\n");
-      } else if (line.startsWith("-----END")) {
+        break;
+      case "-----END":
         crypto.append(line).append("\n");
         String cryptoString = crypto.toString();
         crypto = null;
         this.signature = cryptoString;
-      } else if (crypto != null) {
-        crypto.append(line).append("\n");
-      } else {
-        if (this.failUnrecognizedDescriptorLines) {
-          throw new DescriptorParseException("Unrecognized line '"
-              + line + "' in dir-source entry.");
+        break;
+      default:
+        if (crypto != null) {
+          crypto.append(line).append("\n");
         } else {
-          if (this.unrecognizedLines == null) {
-            this.unrecognizedLines = new ArrayList<>();
+          if (this.failUnrecognizedDescriptorLines) {
+            throw new DescriptorParseException("Unrecognized line '"
+                + line + "' in dir-source entry.");
+          } else {
+            if (this.unrecognizedLines == null) {
+              this.unrecognizedLines = new ArrayList<>();
+            }
+            this.unrecognizedLines.add(line);
           }
-          this.unrecognizedLines.add(line);
         }
       }
     }
diff --git a/src/org/torproject/descriptor/impl/DownloadCoordinatorImpl.java b/src/org/torproject/descriptor/impl/DownloadCoordinatorImpl.java
index e95f9da..6a73090 100644
--- a/src/org/torproject/descriptor/impl/DownloadCoordinatorImpl.java
+++ b/src/org/torproject/descriptor/impl/DownloadCoordinatorImpl.java
@@ -203,48 +203,51 @@ public class DownloadCoordinatorImpl implements DownloadCoordinator {
     if (response.getException() != null) {
       this.runningDirectories.remove(nickname);
     }
-    if (response.getDescriptorType().equals("consensus")) {
-      this.requestingConsensuses.remove(nickname);
-      if (response.getResponseCode() == 200 &&
-          response.getDescriptors() != null) {
-        if (this.includeCurrentReferencedVotes) {
-          /* TODO Only add votes if the consensus is not older than one
-           * hour.  Or does that make no sense? */
-          for (Descriptor parsedDescriptor : response.getDescriptors()) {
-            if (!(parsedDescriptor instanceof
-                RelayNetworkStatusConsensus)) {
-              continue;
-            }
-            RelayNetworkStatusConsensus parsedConsensus =
-                (RelayNetworkStatusConsensus) parsedDescriptor;
-            for (DirSourceEntry dirSource :
-                parsedConsensus.getDirSourceEntries().values()) {
-              String identity = dirSource.getIdentity();
-              if (!this.missingVotes.contains(identity)) {
-                boolean alreadyRequested = false;
-                for (Set<String> requestedBefore :
-                    this.requestedVotes.values()) {
-                  if (requestedBefore.contains(identity)) {
-                    alreadyRequested = true;
-                    break;
+    switch (response.getDescriptorType()) {
+      case "consensus":
+        this.requestingConsensuses.remove(nickname);
+        if (response.getResponseCode() == 200 &&
+            response.getDescriptors() != null) {
+          if (this.includeCurrentReferencedVotes) {
+            /* TODO Only add votes if the consensus is not older than one
+             * hour.  Or does that make no sense? */
+            for (Descriptor parsedDescriptor :
+                response.getDescriptors()) {
+              if (!(parsedDescriptor instanceof
+                  RelayNetworkStatusConsensus)) {
+                continue;
+              }
+              RelayNetworkStatusConsensus parsedConsensus =
+                  (RelayNetworkStatusConsensus) parsedDescriptor;
+              for (DirSourceEntry dirSource :
+                  parsedConsensus.getDirSourceEntries().values()) {
+                String identity = dirSource.getIdentity();
+                if (!this.missingVotes.contains(identity)) {
+                  boolean alreadyRequested = false;
+                  for (Set<String> requestedBefore :
+                      this.requestedVotes.values()) {
+                    if (requestedBefore.contains(identity)) {
+                      alreadyRequested = true;
+                      break;
+                    }
+                  }
+                  if (!alreadyRequested) {
+                    this.missingVotes.add(identity);
                   }
-                }
-                if (!alreadyRequested) {
-                  this.missingVotes.add(identity);
                 }
               }
             }
+            /* TODO Later, add referenced server descriptors. */
           }
-          /* TODO Later, add referenced server descriptors. */
+        } else {
+          this.missingConsensus = true;
+        }
+        break;
+      case "vote":
+        String requestedVote = requestingVotes.remove(nickname);
+        if (response.getResponseCode() != 200) {
+          this.missingVotes.add(requestedVote);
         }
-      } else {
-        this.missingConsensus = true;
-      }
-    } else if (response.getDescriptorType().equals("vote")) {
-      String requestedVote = requestingVotes.remove(nickname);
-      if (response.getResponseCode() != 200) {
-        this.missingVotes.add(requestedVote);
-      }
     }
     if (response.getRequestEnd() != 0L) {
       this.descriptorQueue.add(response);
diff --git a/src/org/torproject/descriptor/impl/ExtraInfoDescriptorImpl.java b/src/org/torproject/descriptor/impl/ExtraInfoDescriptorImpl.java
index 4fcf4c9..3255bd9 100644
--- a/src/org/torproject/descriptor/impl/ExtraInfoDescriptorImpl.java
+++ b/src/org/torproject/descriptor/impl/ExtraInfoDescriptorImpl.java
@@ -238,40 +238,42 @@ public abstract class ExtraInfoDescriptorImpl extends DescriptorImpl
       case "router-digest-sha256":
         this.parseRouterDigestSha256Line(line, lineNoOpt, partsNoOpt);
         break;
-      default:
-        if (line.startsWith("-----BEGIN")) {
-          cryptoLines = new ArrayList<>();
-          cryptoLines.add(line);
-        } else if (line.startsWith("-----END")) {
-          cryptoLines.add(line);
-          StringBuilder sb = new StringBuilder();
-          for (String cryptoLine : cryptoLines) {
-            sb.append("\n").append(cryptoLine);
-          }
-          String cryptoString = sb.toString().substring(1);
-          switch (nextCrypto) {
-          case "router-signature":
-            this.routerSignature = cryptoString;
-            break;
-          case "identity-ed25519":
-            this.identityEd25519 = cryptoString;
-            this.parseIdentityEd25519CryptoBlock(cryptoString);
-            break;
-          default:
-            if (this.failUnrecognizedDescriptorLines) {
-              throw new DescriptorParseException("Unrecognized crypto "
-                  + "block '" + cryptoString + "' in extra-info "
-                  + "descriptor.");
-            } else {
-              if (this.unrecognizedLines == null) {
-                this.unrecognizedLines = new ArrayList<>();
-              }
-              this.unrecognizedLines.addAll(cryptoLines);
+      case "-----BEGIN":
+        cryptoLines = new ArrayList<>();
+        cryptoLines.add(line);
+        break;
+      case "-----END":
+        cryptoLines.add(line);
+        StringBuilder sb = new StringBuilder();
+        for (String cryptoLine : cryptoLines) {
+          sb.append("\n").append(cryptoLine);
+        }
+        String cryptoString = sb.toString().substring(1);
+        switch (nextCrypto) {
+        case "router-signature":
+          this.routerSignature = cryptoString;
+          break;
+        case "identity-ed25519":
+          this.identityEd25519 = cryptoString;
+          this.parseIdentityEd25519CryptoBlock(cryptoString);
+          break;
+        default:
+          if (this.failUnrecognizedDescriptorLines) {
+            throw new DescriptorParseException("Unrecognized crypto "
+                + "block '" + cryptoString + "' in extra-info "
+                + "descriptor.");
+          } else {
+            if (this.unrecognizedLines == null) {
+              this.unrecognizedLines = new ArrayList<>();
             }
+            this.unrecognizedLines.addAll(cryptoLines);
           }
           cryptoLines = null;
           nextCrypto = "";
-        } else if (cryptoLines != null) {
+        }
+        break;
+      default:
+        if (cryptoLines != null) {
           cryptoLines.add(line);
         } else {
           ParseHelper.parseKeyword(line, partsNoOpt[0]);
diff --git a/src/org/torproject/descriptor/impl/MicrodescriptorImpl.java b/src/org/torproject/descriptor/impl/MicrodescriptorImpl.java
index d303fa0..cf8f3e6 100644
--- a/src/org/torproject/descriptor/impl/MicrodescriptorImpl.java
+++ b/src/org/torproject/descriptor/impl/MicrodescriptorImpl.java
@@ -89,22 +89,24 @@ public class MicrodescriptorImpl extends DescriptorImpl
       case "id":
         this.parseIdLine(line, parts);
         break;
+      case "-----BEGIN":
+        crypto = new StringBuilder();
+        crypto.append(line).append("\n");
+        break;
+      case "-----END":
+        crypto.append(line).append("\n");
+        String cryptoString = crypto.toString();
+        crypto = null;
+        if (nextCrypto.equals("onion-key")) {
+          this.onionKey = cryptoString;
+        } else {
+          throw new DescriptorParseException("Unrecognized crypto "
+              + "block in microdescriptor.");
+        }
+        nextCrypto = "";
+        break;
       default:
-        if (line.startsWith("-----BEGIN")) {
-          crypto = new StringBuilder();
-          crypto.append(line).append("\n");
-        } else if (line.startsWith("-----END")) {
-          crypto.append(line).append("\n");
-          String cryptoString = crypto.toString();
-          crypto = null;
-          if (nextCrypto.equals("onion-key")) {
-            this.onionKey = cryptoString;
-          } else {
-            throw new DescriptorParseException("Unrecognized crypto "
-                + "block in microdescriptor.");
-          }
-          nextCrypto = "";
-        } else if (crypto != null) {
+        if (crypto != null) {
           crypto.append(line).append("\n");
         } else {
           ParseHelper.parseKeyword(line, parts[0]);
@@ -191,15 +193,20 @@ public class MicrodescriptorImpl extends DescriptorImpl
     boolean isValid = true;
     if (parts.length != 3) {
       isValid = false;
-    } else if (!parts[1].equals("accept") && !parts[1].equals("reject")) {
-      isValid = false;
-    } else {
-      String[] ports = parts[2].split(",", -1);
-      for (int i = 0; i < ports.length; i++) {
-        if (ports[i].length() < 1) {
-          isValid = false;
-          break;
+    } else  {
+      switch (parts[1]) {
+      case "accept":
+      case "reject":
+        String[] ports = parts[2].split(",", -1);
+        for (int i = 0; i < ports.length; i++) {
+          if (ports[i].length() < 1) {
+            isValid = false;
+            break;
+          }
         }
+        break;
+      default:
+        isValid = false;
       }
     }
     if (!isValid) {
@@ -211,14 +218,19 @@ public class MicrodescriptorImpl extends DescriptorImpl
       throws DescriptorParseException {
     if (parts.length != 3) {
       throw new DescriptorParseException("Illegal line '" + line + "'.");
-    } else if ("ed25519".equals(parts[1])) {
-      ParseHelper.parseThirtyTwoByteBase64String(line, parts[2]);
-      this.ed25519Identity = parts[2];
-    } else if ("rsa1024".equals(parts[1])) {
-      ParseHelper.parseTwentyByteBase64String(line, parts[2]);
-      this.rsa1024Identity = parts[2];
     } else {
-      throw new DescriptorParseException("Illegal line '" + line + "'.");
+        switch (parts[1]) {
+        case "ed25519":
+          ParseHelper.parseThirtyTwoByteBase64String(line, parts[2]);
+          this.ed25519Identity = parts[2];
+          break;
+        case "rsa1024":
+          ParseHelper.parseTwentyByteBase64String(line, parts[2]);
+          this.rsa1024Identity = parts[2];
+          break;
+        default:
+          throw new DescriptorParseException("Illegal line '" + line + "'.");
+        }
     }
   }
 
diff --git a/src/org/torproject/descriptor/impl/NetworkStatusEntryImpl.java b/src/org/torproject/descriptor/impl/NetworkStatusEntryImpl.java
index c5f0704..a17973e 100644
--- a/src/org/torproject/descriptor/impl/NetworkStatusEntryImpl.java
+++ b/src/org/torproject/descriptor/impl/NetworkStatusEntryImpl.java
@@ -213,18 +213,23 @@ public class NetworkStatusEntryImpl implements NetworkStatusEntry {
     boolean isValid = true;
     if (parts.length != 3) {
       isValid = false;
-    } else if (!parts[1].equals("accept") && !parts[1].equals("reject")) {
-      isValid = false;
     } else {
-      this.defaultPolicy = parts[1];
-      this.portList = parts[2];
-      String[] ports = parts[2].split(",", -1);
-      for (int i = 0; i < ports.length; i++) {
-        if (ports[i].length() < 1) {
-          isValid = false;
-          break;
+        switch (parts[1]) {
+          case "accept":
+          case "reject":
+            this.defaultPolicy = parts[1];
+            this.portList = parts[2];
+            String[] ports = parts[2].split(",", -1);
+            for (int i = 0; i < ports.length; i++) {
+              if (ports[i].length() < 1) {
+                isValid = false;
+                break;
+              }
+            }
+            break;
+          default:
+            isValid = false;
         }
-      }
     }
     if (!isValid) {
       throw new DescriptorParseException("Illegal line '" + line + "'.");
diff --git a/src/org/torproject/descriptor/impl/NetworkStatusImpl.java b/src/org/torproject/descriptor/impl/NetworkStatusImpl.java
index a9c2483..9a950cf 100644
--- a/src/org/torproject/descriptor/impl/NetworkStatusImpl.java
+++ b/src/org/torproject/descriptor/impl/NetworkStatusImpl.java
@@ -177,19 +177,22 @@ public abstract class NetworkStatusImpl extends DescriptorImpl {
   protected String[] parseClientOrServerVersions(String line,
       String[] parts) throws DescriptorParseException {
     String[] result = null;
-    if (parts.length > 2) {
-      throw new DescriptorParseException("Illegal versions line '" + line
-          + "'.");
-    } else if (parts.length == 2) {
-      result = parts[1].split(",", -1);
-      for (String version : result) {
-        if (version.length() < 1) {
-          throw new DescriptorParseException("Illegal versions line '"
-              + line + "'.");
+    switch (parts.length) {
+      case 1:
+        result = new String[0];
+        break;
+      case 2:
+        result = parts[1].split(",", -1);
+        for (String version : result) {
+          if (version.length() < 1) {
+            throw new DescriptorParseException("Illegal versions line '"
+                + line + "'.");
+          }
         }
-      }
-    } else if (parts.length == 1) {
-      result = new String[0];
+        break;
+      default:
+        throw new DescriptorParseException("Illegal versions line '" + line
+            + "'.");
     }
     return result;
   }
diff --git a/src/org/torproject/descriptor/impl/ParseHelper.java b/src/org/torproject/descriptor/impl/ParseHelper.java
index 43a44d1..6ae549b 100644
--- a/src/org/torproject/descriptor/impl/ParseHelper.java
+++ b/src/org/torproject/descriptor/impl/ParseHelper.java
@@ -263,6 +263,18 @@ public class ParseHelper {
     return nickname;
   }
 
+  public static boolean parseBoolean(String b, String line)
+      throws DescriptorParseException {
+    switch (b) {
+    case "1":
+      return true;
+    case "0":
+      return false;
+    default:
+      throw new DescriptorParseException("Illegal line '" + line + "'.");
+    }
+  }
+
   private static Pattern twentyByteBase64Pattern =
       Pattern.compile("^[0-9a-zA-Z+/]{27}$");
   public static String parseTwentyByteBase64String(String line,
diff --git a/src/org/torproject/descriptor/impl/RelayDirectoryImpl.java b/src/org/torproject/descriptor/impl/RelayDirectoryImpl.java
index ac17f54..873326d 100644
--- a/src/org/torproject/descriptor/impl/RelayDirectoryImpl.java
+++ b/src/org/torproject/descriptor/impl/RelayDirectoryImpl.java
@@ -227,23 +227,25 @@ public class RelayDirectoryImpl extends DescriptorImpl
       case "router-status":
         routerStatusLine = line;
         break;
+      case "-----BEGIN":
+        crypto = new StringBuilder();
+        crypto.append(line).append("\n");
+        break;
+      case "-----END":
+        crypto.append(line).append("\n");
+        String cryptoString = crypto.toString();
+        crypto = null;
+        if (nextCrypto.equals("dir-signing-key") &&
+            this.dirSigningKey == null) {
+          this.dirSigningKey = cryptoString;
+        } else {
+          throw new DescriptorParseException("Unrecognized crypto "
+              + "block in v1 directory.");
+        }
+        nextCrypto = "";
+        break;
       default:
-        if (line.startsWith("-----BEGIN")) {
-          crypto = new StringBuilder();
-          crypto.append(line).append("\n");
-        } else if (line.startsWith("-----END")) {
-          crypto.append(line).append("\n");
-          String cryptoString = crypto.toString();
-          crypto = null;
-          if (nextCrypto.equals("dir-signing-key") &&
-              this.dirSigningKey == null) {
-            this.dirSigningKey = cryptoString;
-          } else {
-            throw new DescriptorParseException("Unrecognized crypto "
-                + "block in v1 directory.");
-          }
-          nextCrypto = "";
-        } else if (crypto != null) {
+        if (crypto != null) {
           crypto.append(line).append("\n");
         } else {
           if (this.failUnrecognizedDescriptorLines) {
@@ -313,13 +315,16 @@ public class RelayDirectoryImpl extends DescriptorImpl
           line.substring("opt ".length()) : line;
       String[] partsNoOpt = lineNoOpt.split("[ \t]+");
       String keyword = partsNoOpt[0];
-      if (keyword.equals("directory-signature")) {
+      switch (keyword) {
+      case "directory-signature":
         this.parseDirectorySignatureLine(line, lineNoOpt, partsNoOpt);
         nextCrypto = "directory-signature";
-      } else if (line.startsWith("-----BEGIN")) {
+        break;
+      case "-----BEGIN":
         crypto = new StringBuilder();
         crypto.append(line).append("\n");
-      } else if (line.startsWith("-----END")) {
+        break;
+      case "-----END":
         crypto.append(line).append("\n");
         String cryptoString = crypto.toString();
         crypto = null;
@@ -330,16 +335,19 @@ public class RelayDirectoryImpl extends DescriptorImpl
               + "block in v2 network status.");
         }
         nextCrypto = "";
-      } else if (crypto != null) {
-        crypto.append(line).append("\n");
-      } else if (this.failUnrecognizedDescriptorLines) {
-        throw new DescriptorParseException("Unrecognized line '" + line
-            + "' in v2 network status.");
-      } else {
-        if (this.unrecognizedLines == null) {
-          this.unrecognizedLines = new ArrayList<>();
+        break;
+      default:
+        if (crypto != null) {
+          crypto.append(line).append("\n");
+        } else if (this.failUnrecognizedDescriptorLines) {
+          throw new DescriptorParseException("Unrecognized line '" + line
+              + "' in v2 network status.");
+        } else {
+          if (this.unrecognizedLines == null) {
+            this.unrecognizedLines = new ArrayList<>();
+          }
+          this.unrecognizedLines.add(line);
         }
-        this.unrecognizedLines.add(line);
       }
     }
   }
diff --git a/src/org/torproject/descriptor/impl/RelayNetworkStatusImpl.java b/src/org/torproject/descriptor/impl/RelayNetworkStatusImpl.java
index 7788040..9ac3284 100644
--- a/src/org/torproject/descriptor/impl/RelayNetworkStatusImpl.java
+++ b/src/org/torproject/descriptor/impl/RelayNetworkStatusImpl.java
@@ -126,22 +126,23 @@ public class RelayNetworkStatusImpl extends NetworkStatusImpl
       case "dir-options":
         this.parseDirOptionsLine(line, parts);
         break;
+      case "-----BEGIN":
+        crypto = new StringBuilder();
+        crypto.append(line).append("\n");
+        break;
+      case "-----END":
+        crypto.append(line).append("\n");
+        String cryptoString = crypto.toString();
+        crypto = null;
+        if (nextCrypto.equals("dir-signing-key")) {
+          this.dirSigningKey = cryptoString;
+        } else {
+          throw new DescriptorParseException("Unrecognized crypto "
+              + "block in v2 network status.");
+        }
+        nextCrypto = "";
       default:
-        if (line.startsWith("-----BEGIN")) {
-          crypto = new StringBuilder();
-          crypto.append(line).append("\n");
-        } else if (line.startsWith("-----END")) {
-          crypto.append(line).append("\n");
-          String cryptoString = crypto.toString();
-          crypto = null;
-          if (nextCrypto.equals("dir-signing-key")) {
-            this.dirSigningKey = cryptoString;
-          } else {
-            throw new DescriptorParseException("Unrecognized crypto "
-                + "block in v2 network status.");
-          }
-          nextCrypto = "";
-        } else if (crypto != null) {
+        if (crypto != null) {
           crypto.append(line).append("\n");
         } else if (this.failUnrecognizedDescriptorLines) {
           throw new DescriptorParseException("Unrecognized line '" + line
@@ -172,13 +173,16 @@ public class RelayNetworkStatusImpl extends NetworkStatusImpl
       String line = s.next();
       String[] parts = line.split("[ \t]+");
       String keyword = parts[0];
-      if (keyword.equals("directory-signature")) {
+      switch (keyword) {
+      case "directory-signature":
         this.parseDirectorySignatureLine(line, parts);
         nextCrypto = "directory-signature";
-      } else if (line.startsWith("-----BEGIN")) {
+        break;
+      case "-----BEGIN":
         crypto = new StringBuilder();
         crypto.append(line).append("\n");
-      } else if (line.startsWith("-----END")) {
+        break;
+      case "-----END":
         crypto.append(line).append("\n");
         String cryptoString = crypto.toString();
         crypto = null;
@@ -189,16 +193,19 @@ public class RelayNetworkStatusImpl extends NetworkStatusImpl
               + "block in v2 network status.");
         }
         nextCrypto = "";
-      } else if (crypto != null) {
-        crypto.append(line).append("\n");
-      } else if (this.failUnrecognizedDescriptorLines) {
-        throw new DescriptorParseException("Unrecognized line '" + line
-            + "' in v2 network status.");
-      } else {
-        if (this.unrecognizedLines == null) {
-          this.unrecognizedLines = new ArrayList<>();
+        break;
+      default:
+        if (crypto != null) {
+          crypto.append(line).append("\n");
+        } else if (this.failUnrecognizedDescriptorLines) {
+          throw new DescriptorParseException("Unrecognized line '" + line
+              + "' in v2 network status.");
+        } else {
+          if (this.unrecognizedLines == null) {
+            this.unrecognizedLines = new ArrayList<>();
+          }
+          this.unrecognizedLines.add(line);
         }
-        this.unrecognizedLines.add(line);
       }
     }
   }
diff --git a/src/org/torproject/descriptor/impl/RelayNetworkStatusVoteImpl.java b/src/org/torproject/descriptor/impl/RelayNetworkStatusVoteImpl.java
index d74ef65..c16a9f9 100644
--- a/src/org/torproject/descriptor/impl/RelayNetworkStatusVoteImpl.java
+++ b/src/org/torproject/descriptor/impl/RelayNetworkStatusVoteImpl.java
@@ -148,12 +148,14 @@ public class RelayNetworkStatusVoteImpl extends NetworkStatusImpl
       case "dir-key-crosscert":
       case "dir-key-certification":
         break;
+      case "-----BEGIN":
+        skipCrypto = true;
+        break;
+      case "-----END":
+        skipCrypto = false;
+        break;
       default:
-        if (line.startsWith("-----BEGIN")) {
-          skipCrypto = true;
-        } else if (line.startsWith("-----END")) {
-          skipCrypto = false;
-        } else if (!skipCrypto) {
+        if (!skipCrypto) {
           if (this.failUnrecognizedDescriptorLines) {
             throw new DescriptorParseException("Unrecognized line '"
                 + line + "' in vote.");
@@ -313,6 +315,8 @@ public class RelayNetworkStatusVoteImpl extends NetworkStatusImpl
         case "ignoring-advertised-bws":
           this.ignoringAdvertisedBws = Integer.parseInt(e.getValue());
           break;
+        default:
+          // empty
         }
       }
     } catch (NumberFormatException ex) {
diff --git a/src/org/torproject/descriptor/impl/ServerDescriptorImpl.java b/src/org/torproject/descriptor/impl/ServerDescriptorImpl.java
index 84ad2a1..8b6e983 100644
--- a/src/org/torproject/descriptor/impl/ServerDescriptorImpl.java
+++ b/src/org/torproject/descriptor/impl/ServerDescriptorImpl.java
@@ -171,51 +171,53 @@ public abstract class ServerDescriptorImpl extends DescriptorImpl
         this.parseNtorOnionKeyCrosscert(line, lineNoOpt, partsNoOpt);
         nextCrypto = "ntor-onion-key-crosscert";
         break;
-      default:
-        if (line.startsWith("-----BEGIN")) {
-          cryptoLines = new ArrayList<>();
-          cryptoLines.add(line);
-        } else if (line.startsWith("-----END")) {
-          cryptoLines.add(line);
-          StringBuilder sb = new StringBuilder();
-          for (String cryptoLine : cryptoLines) {
-            sb.append("\n").append(cryptoLine);
-          }
-          String cryptoString = sb.toString().substring(1);
-          switch (nextCrypto) {
-          case "onion-key":
-            this.onionKey = cryptoString;
-            break;
-          case "signing-key":
-            this.signingKey = cryptoString;
-            break;
-          case "router-signature":
-            this.routerSignature = cryptoString;
-            break;
-          case "identity-ed25519":
-            this.identityEd25519 = cryptoString;
-            this.parseIdentityEd25519CryptoBlock(cryptoString);
-            break;
-          case "onion-key-crosscert":
-            this.onionKeyCrosscert = cryptoString;
-            break;
-          case "ntor-onion-key-crosscert":
-            this.ntorOnionKeyCrosscert = cryptoString;
-            break;
-          default:
-            if (this.failUnrecognizedDescriptorLines) {
-              throw new DescriptorParseException("Unrecognized crypto "
-                  + "block '" + cryptoString + "' in server descriptor.");
-            } else {
-              if (this.unrecognizedLines == null) {
-                this.unrecognizedLines = new ArrayList<>();
-              }
-              this.unrecognizedLines.addAll(cryptoLines);
+      case "-----BEGIN":
+        cryptoLines = new ArrayList<>();
+        cryptoLines.add(line);
+        break;
+      case "-----END":
+        cryptoLines.add(line);
+        StringBuilder sb = new StringBuilder();
+        for (String cryptoLine : cryptoLines) {
+          sb.append("\n").append(cryptoLine);
+        }
+        String cryptoString = sb.toString().substring(1);
+        switch (nextCrypto) {
+        case "onion-key":
+          this.onionKey = cryptoString;
+          break;
+        case "signing-key":
+          this.signingKey = cryptoString;
+          break;
+        case "router-signature":
+          this.routerSignature = cryptoString;
+          break;
+        case "identity-ed25519":
+          this.identityEd25519 = cryptoString;
+          this.parseIdentityEd25519CryptoBlock(cryptoString);
+          break;
+        case "onion-key-crosscert":
+          this.onionKeyCrosscert = cryptoString;
+          break;
+        case "ntor-onion-key-crosscert":
+          this.ntorOnionKeyCrosscert = cryptoString;
+          break;
+        default:
+          if (this.failUnrecognizedDescriptorLines) {
+            throw new DescriptorParseException("Unrecognized crypto "
+                + "block '" + cryptoString + "' in server descriptor.");
+          } else {
+            if (this.unrecognizedLines == null) {
+              this.unrecognizedLines = new ArrayList<>();
             }
+            this.unrecognizedLines.addAll(cryptoLines);
           }
-          cryptoLines = null;
-          nextCrypto = "";
-        } else if (cryptoLines != null) {
+        }
+        cryptoLines = null;
+        nextCrypto = "";
+        break;
+      default:
+        if (cryptoLines != null) {
           cryptoLines.add(line);
         } else {
           ParseHelper.parseKeyword(line, partsNoOpt[0]);
@@ -317,13 +319,7 @@ public abstract class ServerDescriptorImpl extends DescriptorImpl
     if (partsNoOpt.length != 2) {
       throw new DescriptorParseException("Illegal line '" + line + "'.");
     }
-    if (partsNoOpt[1].equals("1")) {
-      this.hibernating = true;
-    } else if (partsNoOpt[1].equals("0")) {
-      this.hibernating = false;
-    } else {
-      throw new DescriptorParseException("Illegal line '" + line + "'.");
-    }
+    this.hibernating = ParseHelper.parseBoolean(partsNoOpt[1], line);
   }
 
   private void parseUptimeLine(String line, String lineNoOpt,
@@ -438,13 +434,7 @@ public abstract class ServerDescriptorImpl extends DescriptorImpl
     if (partsNoOpt.length != 2) {
       throw new DescriptorParseException("Illegal line '" + line + "'.");
     }
-    if (partsNoOpt[1].equals("1")) {
-      this.usesEnhancedDnsLogic = true;
-    } else if (partsNoOpt[1].equals("0")) {
-      this.usesEnhancedDnsLogic = false;
-    } else {
-      throw new DescriptorParseException("Illegal line '" + line + "'.");
-    }
+    this.usesEnhancedDnsLogic = ParseHelper.parseBoolean(partsNoOpt[1], line);
   }
 
   private void parseCachesExtraInfoLine(String line, String lineNoOpt,
@@ -490,10 +480,15 @@ public abstract class ServerDescriptorImpl extends DescriptorImpl
       String[] partsNoOpt) throws DescriptorParseException {
     int linkIndex = -1, circuitIndex = -1;
     for (int i = 1; i < partsNoOpt.length; i++) {
-      if (partsNoOpt[i].equals("Link")) {
+      switch (partsNoOpt[i]) {
+      case "Link":
         linkIndex = i;
-      } else if (partsNoOpt[i].equals("Circuit")) {
+        break;
+      case "Circuit":
         circuitIndex = i;
+        break;
+      default:
+        // empty
       }
     }
     if (linkIndex < 0 || circuitIndex < 0 || circuitIndex < linkIndex) {
@@ -555,19 +550,23 @@ public abstract class ServerDescriptorImpl extends DescriptorImpl
     boolean isValid = true;
     if (partsNoOpt.length != 3) {
       isValid = false;
-    } else if (!partsNoOpt[1].equals("accept") &&
-        !partsNoOpt[1].equals("reject")) {
-      isValid = false;
     } else {
-      this.ipv6DefaultPolicy = partsNoOpt[1];
-      this.ipv6PortList = partsNoOpt[2];
-      String[] ports = partsNoOpt[2].split(",", -1);
-      for (int i = 0; i < ports.length; i++) {
-        if (ports[i].length() < 1) {
-          isValid = false;
+        switch (partsNoOpt[1]) {
+        case "accept":
+        case "reject":
+          this.ipv6DefaultPolicy = partsNoOpt[1];
+          this.ipv6PortList = partsNoOpt[2];
+          String[] ports = partsNoOpt[2].split(",", -1);
+          for (int i = 0; i < ports.length; i++) {
+            if (ports[i].length() < 1) {
+              isValid = false;
+              break;
+            }
+          }
           break;
+        default:
+          isValid = false;
         }
-      }
     }
     if (!isValid) {
       throw new DescriptorParseException("Illegal line '" + line + "'.");





More information about the tor-commits mailing list