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

[tor-commits] [onionoo/master] Always add own fingerprint to effective family.



commit a9a0f7054cf66ee51b245b02369119832e330585
Author: Karsten Loesing <karsten.loesing@xxxxxxx>
Date:   Wed Feb 14 22:40:52 2018 +0100

    Always add own fingerprint to effective family.
    
    Some relays include their own fingerprint in the family line of their
    server descriptor and others don't.  To provide consistent data Onionoo
    now adds a relay's own fingerprint to its effective family.
    
    Implements task-25241.
---
 .../org/torproject/onionoo/docs/NodeStatus.java    | 31 +++++++++++-----------
 .../torproject/onionoo/docs/NodeStatusTest.java    | 19 ++++++-------
 2 files changed, 25 insertions(+), 25 deletions(-)

diff --git a/src/main/java/org/torproject/onionoo/docs/NodeStatus.java b/src/main/java/org/torproject/onionoo/docs/NodeStatus.java
index cfd13aa..94da8c8 100644
--- a/src/main/java/org/torproject/onionoo/docs/NodeStatus.java
+++ b/src/main/java/org/torproject/onionoo/docs/NodeStatus.java
@@ -19,6 +19,7 @@ import java.util.SortedMap;
 import java.util.SortedSet;
 import java.util.TreeMap;
 import java.util.TreeSet;
+import java.util.stream.Collectors;
 
 public class NodeStatus extends Document {
 
@@ -54,33 +55,31 @@ public class NodeStatus extends Document {
 
   private String[] declaredFamily;
 
+  @SuppressWarnings("checkstyle:javadocmethod")
   public void setDeclaredFamily(SortedSet<String> declaredFamily) {
-    this.declaredFamily = collectionToStringArray(declaredFamily);
+    SortedSet<String> declaredFamilyIncludingSelf
+        = new TreeSet<>(declaredFamily);
+    declaredFamilyIncludingSelf.add(this.fingerprint);
+    this.declaredFamily = collectionToStringArray(declaredFamilyIncludingSelf);
   }
 
+  @SuppressWarnings("checkstyle:javadocmethod")
   public SortedSet<String> getDeclaredFamily() {
-    return stringArrayToSortedSet(this.declaredFamily);
+    SortedSet<String> declaredFamilyIncludingSelf =
+        stringArrayToSortedSet(this.declaredFamily);
+    declaredFamilyIncludingSelf.add(this.fingerprint);
+    return declaredFamilyIncludingSelf;
   }
 
   private static String[] collectionToStringArray(
       Collection<String> collection) {
-    String[] stringArray = null;
-    if (collection != null && !collection.isEmpty()) {
-      stringArray = new String[collection.size()];
-      int index = 0;
-      for (String string : collection) {
-        stringArray[index++] = string;
-      }
-    }
-    return stringArray;
+    return (null == collection || collection.isEmpty()) ? null
+        : collection.toArray(new String[collection.size()]);
   }
 
   private SortedSet<String> stringArrayToSortedSet(String[] stringArray) {
-    SortedSet<String> sortedSet = new TreeSet<>();
-    if (stringArray != null) {
-      sortedSet.addAll(Arrays.asList(stringArray));
-    }
-    return sortedSet;
+    return stringArray == null ? new TreeSet<>() : Arrays.stream(stringArray)
+        .collect(Collectors.toCollection(TreeSet::new));
   }
 
   /* From network status entries: */
diff --git a/src/test/java/org/torproject/onionoo/docs/NodeStatusTest.java b/src/test/java/org/torproject/onionoo/docs/NodeStatusTest.java
index e2c5dac..2b90ece 100644
--- a/src/test/java/org/torproject/onionoo/docs/NodeStatusTest.java
+++ b/src/test/java/org/torproject/onionoo/docs/NodeStatusTest.java
@@ -53,36 +53,37 @@ public class NodeStatusTest {
   private static final String D = "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD";
   private static final String E = "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE";
   private static final String F = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF";
+  private static final String G = "F2044413DAC2E02E3D6BCF4735A19BCA1DE97281";
   private static final String NICK = "nickname";
 
   @Test
   public void testFamiliesEmpty() {
     assertFamiliesCanBeDeSerialized(
-        new String[] {}, new String[] {}, new String[] {});
+        new String[] { G }, new String[] {}, new String[] {});
   }
 
   @Test
   public void testFamiliesOneNotMutual() {
     assertFamiliesCanBeDeSerialized(
-        new String[] { A }, new String[] {}, new String[] {});
+        new String[] { A, G }, new String[] {}, new String[] {});
   }
 
   @Test
   public void testFamiliesTwoNotMutual() {
     assertFamiliesCanBeDeSerialized(
-        new String[] { A, B }, new String[] {}, new String[] {});
+        new String[] { A, B, G }, new String[] {}, new String[] {});
   }
 
   @Test
   public void testFamiliesOneNotMutualOneMutual() {
     assertFamiliesCanBeDeSerialized(
-        new String[] { A, B }, new String[] { B }, new String[] { B });
+        new String[] { A, B, G }, new String[] { B }, new String[] { B });
   }
 
   @Test
   public void testFamiliesOneMutualOneIndirect() {
     assertFamiliesCanBeDeSerialized(
-        new String[] { A }, new String[] { A }, new String[] { A, B });
+        new String[] { A, G }, new String[] { A }, new String[] { A, B });
   }
 
   @Test
@@ -92,26 +93,26 @@ public class NodeStatusTest {
      * with this relay.  It's a valid case, because B can be in a mutual
      * family relationship with A. */
     assertFamiliesCanBeDeSerialized(
-        new String[] { A, B }, new String[] { A }, new String[] { A, B});
+        new String[] { A, B, G }, new String[] { A }, new String[] { A, B});
   }
 
   @Test
   public void testFamiliesOneNotMutualOneMutualOneIndirect() {
     assertFamiliesCanBeDeSerialized(
-        new String[] { A, B }, new String[] { B }, new String[] { B, C});
+        new String[] { A, B, G }, new String[] { B }, new String[] { B, C});
   }
 
   @Test
   public void testFamiliesTwoNotMutualTwoMutualTwoIndirect() {
     assertFamiliesCanBeDeSerialized(
-        new String[] { A, B, C, D }, new String[] { C, D },
+        new String[] { A, B, C, D, G }, new String[] { C, D },
         new String[] { C, D, E, F });
   }
 
   @Test
   public void testFamiliesNickname() {
     assertFamiliesCanBeDeSerialized(
-        new String[] { NICK }, new String[] {}, new String[] {});
+        new String[] { NICK, G }, new String[] {}, new String[] {});
   }
 
   private static final String KI_LAST_SEEN_1970_NODE_STATUS =

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