[tor-commits] [metrics-lib/master] Accept negative uptimes and uptimes > MAXINT.
karsten at torproject.org
karsten at torproject.org
Tue Apr 17 16:26:41 UTC 2012
commit bd21da9b4370afe385dc2c9a88a07f15dd3a7892
Author: Karsten Loesing <karsten.loesing at gmx.net>
Date: Tue Apr 17 18:20:54 2012 +0200
Accept negative uptimes and uptimes > MAXINT.
---
.../torproject/descriptor/ServerDescriptor.java | 5 +++--
.../descriptor/impl/ServerDescriptorImpl.java | 10 ++++------
.../descriptor/impl/ServerDescriptorImplTest.java | 8 ++++----
3 files changed, 11 insertions(+), 12 deletions(-)
diff --git a/src/org/torproject/descriptor/ServerDescriptor.java b/src/org/torproject/descriptor/ServerDescriptor.java
index b6bc9de..453d038 100644
--- a/src/org/torproject/descriptor/ServerDescriptor.java
+++ b/src/org/torproject/descriptor/ServerDescriptor.java
@@ -60,9 +60,10 @@ public interface ServerDescriptor extends Descriptor {
* published. */
public boolean isHibernating();
- /* Return the number of seconds that this relay has been running, or -1
+ /* Return the number of seconds that this relay has been running (which
+ * might even be negative in a few descriptors due to a bug), or null
* if the descriptor does not contain an uptime line. */
- public int getUptime();
+ public Long getUptime();
/* Return the onion key in PEM format, or null if the descriptor
* doesn't contain a signing key (which is the case in sanitized bridge
diff --git a/src/org/torproject/descriptor/impl/ServerDescriptorImpl.java b/src/org/torproject/descriptor/impl/ServerDescriptorImpl.java
index 68b0e70..d7049eb 100644
--- a/src/org/torproject/descriptor/impl/ServerDescriptorImpl.java
+++ b/src/org/torproject/descriptor/impl/ServerDescriptorImpl.java
@@ -258,10 +258,8 @@ public class ServerDescriptorImpl extends DescriptorImpl
}
boolean isValid = false;
try {
- this.uptime = Integer.parseInt(partsNoOpt[1]);
- if (this.uptime >= 0) {
- isValid = true;
- }
+ this.uptime = Long.parseLong(partsNoOpt[1]);
+ isValid = true;
} catch (NumberFormatException e) {
/* Handle below. */
}
@@ -536,8 +534,8 @@ public class ServerDescriptorImpl extends DescriptorImpl
return this.hibernating;
}
- private int uptime = -1;
- public int getUptime() {
+ private Long uptime;
+ public Long getUptime() {
return this.uptime;
}
diff --git a/test/org/torproject/descriptor/impl/ServerDescriptorImplTest.java b/test/org/torproject/descriptor/impl/ServerDescriptorImplTest.java
index 1407195..1995697 100644
--- a/test/org/torproject/descriptor/impl/ServerDescriptorImplTest.java
+++ b/test/org/torproject/descriptor/impl/ServerDescriptorImplTest.java
@@ -293,7 +293,7 @@ public class ServerDescriptorImplTest {
assertEquals(1325390599000L, descriptor.getPublishedMillis());
assertEquals("D8733048FC8EC9102466AD8F3098622BF1BF71FD",
descriptor.getFingerprint());
- assertEquals(48, (int) descriptor.getUptime());
+ assertEquals(48, descriptor.getUptime().longValue());
assertEquals(51200, (int) descriptor.getBandwidthRate());
assertEquals(51200, (int) descriptor.getBandwidthBurst());
assertEquals(53470, (int) descriptor.getBandwidthObserved());
@@ -529,14 +529,14 @@ public class ServerDescriptorImplTest {
public void testUptimeMissing() throws DescriptorParseException {
ServerDescriptor descriptor = DescriptorBuilder.
createWithUptimeLine(null);
- assertEquals(-1, (int) descriptor.getUptime());
+ assertNull(descriptor.getUptime());
}
@Test()
public void testUptimeOpt() throws DescriptorParseException {
ServerDescriptor descriptor = DescriptorBuilder.
createWithUptimeLine("opt uptime 48");
- assertEquals(48, (int) descriptor.getUptime());
+ assertEquals(48, descriptor.getUptime().longValue());
}
@Test(expected = DescriptorParseException.class)
@@ -544,7 +544,7 @@ public class ServerDescriptorImplTest {
DescriptorBuilder.createWithUptimeLine("uptime fourty-eight");
}
- @Test(expected = DescriptorParseException.class)
+ @Test()
public void testUptimeMinusOne() throws DescriptorParseException {
DescriptorBuilder.createWithUptimeLine("uptime -1");
}
More information about the tor-commits
mailing list