commit 1e942ab15d636ca3307fe5327896afff807b52f8 Author: iwakeh iwakeh@users.ourproject.org Date: Thu Nov 26 10:00:00 2015 +0000
Prevent disappearance of torperf annotation. --- .../descriptor/impl/TorperfResultImpl.java | 12 ++- .../descriptor/impl/TorperfResultImplTest.java | 86 ++++++++++++++++++++ 2 files changed, 96 insertions(+), 2 deletions(-)
diff --git a/src/org/torproject/descriptor/impl/TorperfResultImpl.java b/src/org/torproject/descriptor/impl/TorperfResultImpl.java index d865802..6da985f 100644 --- a/src/org/torproject/descriptor/impl/TorperfResultImpl.java +++ b/src/org/torproject/descriptor/impl/TorperfResultImpl.java @@ -27,6 +27,7 @@ public class TorperfResultImpl extends DescriptorImpl List<Descriptor> parsedDescriptors = new ArrayList<Descriptor>(); String descriptorString = new String(rawDescriptorBytes); Scanner s = new Scanner(descriptorString).useDelimiter("\n"); + String typeAnnotation = ""; while (s.hasNext()) { String line = s.next(); if (line.startsWith("@type torperf ")) { @@ -40,9 +41,12 @@ public class TorperfResultImpl extends DescriptorImpl throw new DescriptorParseException("Unsupported version in " + " line '" + line + "'."); } + typeAnnotation = line + "\n"; } else { - parsedDescriptors.add(new TorperfResultImpl(line.getBytes(), + parsedDescriptors.add(new TorperfResultImpl( + (typeAnnotation + line).getBytes(), failUnrecognizedDescriptorLines)); + typeAnnotation = ""; } } return parsedDescriptors; @@ -55,8 +59,12 @@ public class TorperfResultImpl extends DescriptorImpl this.parseTorperfResultLine(new String(rawDescriptorBytes)); }
- private void parseTorperfResultLine(String line) + private void parseTorperfResultLine(String inputLine) throws DescriptorParseException { + String line = inputLine; + while (line.startsWith("@") && line.contains("\n")) { + line = line.split("\n")[1]; + } if (line.isEmpty()) { throw new DescriptorParseException("Blank lines are not allowed."); } diff --git a/test/org/torproject/descriptor/impl/TorperfResultImplTest.java b/test/org/torproject/descriptor/impl/TorperfResultImplTest.java new file mode 100644 index 0000000..be9221f --- /dev/null +++ b/test/org/torproject/descriptor/impl/TorperfResultImplTest.java @@ -0,0 +1,86 @@ +/* Copyright 2015 The Tor Project + * See LICENSE for licensing information */ +package org.torproject.descriptor.impl; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.util.List; + +import org.junit.Test; +import org.torproject.descriptor.Descriptor; + +public class TorperfResultImplTest { + + @Test() + public void testAnnotatedInput() throws Exception{ + TorperfResultImpl result = (TorperfResultImpl) + (TorperfResultImpl.parseTorperfResults((torperfAnnotation + input) + .getBytes("US-ASCII"), false).get(0)); + assertEquals("Expected one annotation.", 1, + result.getAnnotations().size()); + assertEquals(torperfAnnotation.substring(0, 17), + result.getAnnotations().get(0)); + int count = 0; + for (Long l: result.getDataPercentiles().values()) { + assertNotNull(l); + assertEquals(l.longValue(), deciles[count++]); + } + } + + @Test() + public void testPartiallyAnnotatedInput() throws Exception{ + byte[] asciiBytes = (torperfAnnotation + + input + input + input).getBytes("US-ASCII"); + List<Descriptor> result = TorperfResultImpl.parseTorperfResults( + asciiBytes, false); + assertEquals("Expected one annotation.", 1, + ((TorperfResultImpl)(result.get(0))).getAnnotations().size()); + assertEquals(3, result.size()); + assertEquals("Expected zero annotations.", 0, + ((TorperfResultImpl)(result.get(1))).getAnnotations().size()); + assertEquals("Expected zero annotations.", 0, + ((TorperfResultImpl)(result.get(2))).getAnnotations().size()); + } + + @Test() + public void testAllAnnotatedInput() throws Exception{ + byte[] asciiBytes = (torperfAnnotation + input + + torperfAnnotation + input + + torperfAnnotation + input).getBytes("US-ASCII"); + List<Descriptor> result = TorperfResultImpl.parseTorperfResults( + asciiBytes, false); + assertEquals("Expected one annotation.", 1, + ((TorperfResultImpl)(result.get(0))).getAnnotations().size()); + assertEquals(3, result.size()); + assertEquals("Expected one annotation.", 1, + ((TorperfResultImpl)(result.get(1))).getAnnotations().size()); + assertEquals("Expected one annotation.", 1, + ((TorperfResultImpl)(result.get(2))).getAnnotations().size()); + } + + private static long[] deciles = new long[] { + 1441065602980L, 1441065603030L, 1441065603090L, 1441065603120L, + 1441065603230L, 1441065603250L, 1441065603310L, 1441065603370L, + 1441065603370L }; + + private static final String torperfAnnotation = "@type torperf 1.0\n"; + + private static final String input = + "BUILDTIMES=0.872834920883,1.09103679657,1.49180984497 " + + "CIRC_ID=1228 CONNECT=1441065601.86 DATACOMPLETE=1441065603.39 " + + "DATAPERC10=1441065602.98 DATAPERC20=1441065603.03 " + + "DATAPERC30=1441065603.09 DATAPERC40=1441065603.12 " + + "DATAPERC50=1441065603.23 DATAPERC60=1441065603.25 " + + "DATAPERC70=1441065603.31 DATAPERC80=1441065603.37 " + + "DATAPERC90=1441065603.37 DATAREQUEST=1441065602.38 " + + "DATARESPONSE=1441065602.84 DIDTIMEOUT=0 FILESIZE=51200 " + + "LAUNCH=1441065361.30 NEGOTIATE=1441065601.86 " + + "PATH=$C4C9C332D25B3546BEF4E1250CF410E97EF996E6," + + "$C43FA6474A9F071E9120DF63ED6EB8FDBA105234," + + "$7C0AA4E3B73E407E9F5FEB1912F8BE26D8AA124D QUANTILE=0.800000 " + + "READBYTES=51416 REQUEST=1441065601.86 RESPONSE=1441065602.38 " + + "SOCKET=1441065601.86 SOURCE=moria START=1441065601.86 " + + "TIMEOUT=1500 USED_AT=1441065603.40 USED_BY=2475 WRITEBYTES=75\n"; +} +
tor-commits@lists.torproject.org