[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [exonerator/master] Avoid adding superfluous brackets in suggestion links.



commit fa80fff479cfd10359283e4e40e23b59bc109b50
Author: iwakeh <iwakeh@xxxxxxxxxxxxxx>
Date:   Tue Jan 9 09:25:13 2018 +0000

    Avoid adding superfluous brackets in suggestion links.
    
    Also add a test checking for superfluous brackets.
    Fixes task-26032.
---
 .../metrics/exonerator/ExoneraTorServlet.java      |  8 +--
 .../metrics/exonerator/ExoneraTorServletTest.java  | 58 ++++++++++++++++++++++
 2 files changed, 62 insertions(+), 4 deletions(-)

diff --git a/src/main/java/org/torproject/metrics/exonerator/ExoneraTorServlet.java b/src/main/java/org/torproject/metrics/exonerator/ExoneraTorServlet.java
index 8f114eb..659f494 100644
--- a/src/main/java/org/torproject/metrics/exonerator/ExoneraTorServlet.java
+++ b/src/main/java/org/torproject/metrics/exonerator/ExoneraTorServlet.java
@@ -466,7 +466,7 @@ public class ExoneraTorServlet extends HttpServlet {
         contactLink);
   }
 
-  private void writeSummaryAddressesInSameNetwork(PrintWriter out,
+  void writeSummaryAddressesInSameNetwork(PrintWriter out,
       ResourceBundle rb, String relayIp, String timestampStr, String langStr,
       List<String> addressesInSameNetwork) throws IOException {
     Object[][] panelItems = new Object[addressesInSameNetwork.size()][];
@@ -475,10 +475,10 @@ public class ExoneraTorServlet extends HttpServlet {
       String link;
       String address;
       if (addressInSameNetwork.contains(":")) {
+        address = addressInSameNetwork.replaceAll("[\\[\\]]", "");
         link = String.format("/?ip=[%s]&timestamp=%s&lang=%s",
-            addressInSameNetwork.replaceAll(":", "%3A"), timestampStr,
-            langStr);
-        address = "[" + addressInSameNetwork + "]";
+            address.replaceAll(":", "%3A"), timestampStr, langStr);
+        address = "[" + address + "]";
       } else {
         link = String.format("/?ip=%s&timestamp=%s&lang=%s",
             addressInSameNetwork, timestampStr, langStr);
diff --git a/src/test/java/org/torproject/metrics/exonerator/ExoneraTorServletTest.java b/src/test/java/org/torproject/metrics/exonerator/ExoneraTorServletTest.java
index 81d6d3c..34dfd42 100644
--- a/src/test/java/org/torproject/metrics/exonerator/ExoneraTorServletTest.java
+++ b/src/test/java/org/torproject/metrics/exonerator/ExoneraTorServletTest.java
@@ -4,9 +4,17 @@
 package org.torproject.metrics.exonerator;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
 
 import org.junit.Test;
 
+import java.io.PrintWriter;
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.util.Arrays;
+import java.util.Locale;
+import java.util.ResourceBundle;
+
 public class ExoneraTorServletTest {
 
   private static final String[][] ipTestData
@@ -32,5 +40,55 @@ public class ExoneraTorServletTest {
       assertEquals(data[1], ExoneraTorServlet.parseIpParameter(data[0]));
     }
   }
+
+  @Test
+  public void testNearbyIpV6Response() throws Exception {
+    ExoneraTorServlet es = new ExoneraTorServlet();
+    ResourceBundle rb = ResourceBundle
+        .getBundle("ExoneraTor", Locale.forLanguageTag("en"));
+    for (QueryResponse qr : qrs) {
+      StringWriter sw = new StringWriter();
+      es.writeSummaryAddressesInSameNetwork(new PrintWriter(sw), rb,
+          qr.queryAddress, qr.queryDate, "en",
+          Arrays.asList(qr.nearbyAddresses));
+      String errorMsg = "Test data:" + QueryResponse.toJson(qr)
+          + "\nresult:\n" + sw.toString();
+      assertTrue(errorMsg,
+          sw.toString().contains("Result is negative"));
+      assertTrue(errorMsg,
+          sw.toString().contains("ip=[2a06%3Ae80%3A1%3A%3A10]&"));
+      assertTrue(errorMsg,
+          sw.toString().contains("ip=[2a06%3Ae80%3A1%3A%3A15]&"));
+    }
+  }
+
+  private QueryResponse[] qrs = new QueryResponse[]{
+      QueryResponse.fromJson(new StringReader(
+          "{\"version\":\"1.0\","
+          + "\"query_address\":\"2a06:e80:1::11\","
+          + "\"query_date\":\"2016-12-12\","
+          + "\"first_date_in_database\":\"2016-01-01\","
+          + "\"last_date_in_database\":\"2016-12-31\","
+          + "\"relevant_statuses\":false,"
+          + "\"nearby_addresses\":[\"2a06:e80:1::10\","
+          + "\"2a06:e80:1::15\"]}")),
+      QueryResponse.fromJson(new StringReader(
+          "{\"version\":\"1.0\","
+          + "\"query_address\":\"2a06:e80:1::11\","
+          + "\"query_date\":\"2016-12-12\","
+          + "\"first_date_in_database\":\"2016-01-01\","
+          + "\"last_date_in_database\":\"2016-12-31\","
+          + "\"relevant_statuses\":false,"
+          + "\"nearby_addresses\":[\"[2a06:e80:1::10]\","
+          + "\"2a06:e80:1::15\"]}")),
+      QueryResponse.fromJson(new StringReader(
+          "{\"version\":\"1.0\","
+          + "\"query_address\":\"2a06:e80:1::11\","
+          + "\"query_date\":\"2016-12-12\","
+          + "\"first_date_in_database\":\"2016-01-01\","
+          + "\"last_date_in_database\":\"2016-12-31\","
+          + "\"relevant_statuses\":false,"
+          + "\"nearby_addresses\":[\"[2a06:e80:1::10]\","
+          + "\"[2a06:e80:1::15]\"]}"))};
 }
 



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits