Add support for fetching remote deprecation.

This commit is contained in:
Greyson Parrelli
2020-09-08 18:03:56 -04:00
committed by GitHub
parent c946a7a1d5
commit 2784285d47
21 changed files with 559 additions and 39 deletions

View File

@@ -0,0 +1,26 @@
package org.thoughtcrime.securesms.testutil;
import org.thoughtcrime.securesms.logging.Log;
public class EmptyLogger extends Log.Logger {
@Override
public void v(String tag, String message, Throwable t) { }
@Override
public void d(String tag, String message, Throwable t) { }
@Override
public void i(String tag, String message, Throwable t) { }
@Override
public void w(String tag, String message, Throwable t) { }
@Override
public void e(String tag, String message, Throwable t) { }
@Override
public void wtf(String tag, String message, Throwable t) { }
@Override
public void blockUntilAllWritesFinished() { }
}

View File

@@ -0,0 +1,88 @@
package org.thoughtcrime.securesms.util;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.thoughtcrime.securesms.logging.Log;
import org.thoughtcrime.securesms.testutil.EmptyLogger;
import java.util.Arrays;
import java.util.Collection;
import static junit.framework.TestCase.assertEquals;
@RunWith(Parameterized.class)
public class RemoteExpirationTest_getTimeUntilDeprecation {
private final String json;
private final long currentDate;
private final String currentVersion;
private final long timeUntilExpiration;
@Parameterized.Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
// Null json, invalid
{ null, DateUtils.parseIso8601("2020-01-01T00:00:00Z"), "1.1.0", -1 },
// Empty json, no expiration
{ "[]", DateUtils.parseIso8601("2020-01-01T00:00:00Z"), "1.1.0", -1 },
// Badly formatted minVersion, no expiration
{ "[ {\"minVersion\": \"1.1\", \"iso8601\": \"2020-01-01T00:00:01Z\"} ]", DateUtils.parseIso8601("2020-01-01T00:00:00Z"), "1.1.1", -1 },
// Badly formatted date, no expiration
{ "[ {\"minVersion\": \"1.1.1\", \"iso8601\": \"20-01T00:00:01Z\"} ]", DateUtils.parseIso8601("2020-01-01T00:00:00Z"), "1.1.1", -1 },
// Missing minVersion, no expiration
{ "[ {\"iso8601\": \"20-01T00:00:01Z\"} ]", DateUtils.parseIso8601("2020-01-01T00:00:00Z"), "1.1.1", -1 },
// Missing date, no expiration
{ "[ {\"minVersion\": \"1.1.1\"} ]", DateUtils.parseIso8601("2020-01-01T00:00:00Z"), "1.1.1", -1 },
// Missing expiration and date, no expiration
{ "[ {} ]", DateUtils.parseIso8601("2020-01-01T00:00:00Z"), "1.1.1", -1 },
// Invalid inner object, no expiration
{ "[ { \"a\": 1 } ]", DateUtils.parseIso8601("2020-01-01T00:00:00Z"), "1.1.1", -1 },
// Invalid json, no expiration
{ "[ {", DateUtils.parseIso8601("2020-01-01T00:00:00Z"), "1.1.1", -1 },
// We meet the min version, no expiration
{ "[ {\"minVersion\": \"1.1.1\", \"iso8601\": \"2020-01-01T00:00:01Z\"} ]", DateUtils.parseIso8601("2020-01-01T00:00:00Z"), "1.1.1", -1 },
// We exceed the min version, no expiration
{ "[ {\"minVersion\": \"1.1.1\", \"iso8601\": \"2020-01-01T00:00:01Z\"} ]", DateUtils.parseIso8601("2020-01-01T00:00:00Z"), "1.1.2", -1 },
// We expire in 1 second
{ "[ {\"minVersion\": \"1.1.1\", \"iso8601\": \"2020-01-01T00:00:01Z\"} ]", DateUtils.parseIso8601("2020-01-01T00:00:00Z"), "1.1.0", 1000 },
// We have already expired
{ "[ {\"minVersion\": \"1.1.1\", \"iso8601\": \"2020-01-01T00:00:01Z\"} ]", DateUtils.parseIso8601("2020-01-01T00:00:02Z"), "1.1.0", 0 },
// Use the closest expiration when multiple ones are listed
{ "[ {\"minVersion\": \"1.1.2\", \"iso8601\": \"2020-02-01T00:00:00Z\"}," +
"{\"minVersion\": \"1.1.3\", \"iso8601\": \"2020-03-01T00:00:00Z\"}," +
"{\"minVersion\": \"1.1.1\", \"iso8601\": \"2020-01-01T00:00:01Z\"} ]", DateUtils.parseIso8601("2020-01-01T00:00:00Z"), "1.1.0", 1000 },
});
}
public RemoteExpirationTest_getTimeUntilDeprecation(String json, long currentDate, String currentVersion, long timeUntilExpiration) {
this.json = json;
this.currentDate = currentDate;
this.currentVersion = currentVersion;
this.timeUntilExpiration = timeUntilExpiration;
}
@BeforeClass
public static void setup() {
Log.initialize(new EmptyLogger());
}
@Test
public void getTimeUntilExpiration() {
assertEquals(timeUntilExpiration, RemoteDeprecation.getTimeUntilDeprecation(json, currentDate, currentVersion));
}
}

View File

@@ -0,0 +1,46 @@
package org.thoughtcrime.securesms.util;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.util.Arrays;
import java.util.Collection;
import static junit.framework.TestCase.assertEquals;
@RunWith(Parameterized.class)
public class SemanticVersionTest_compareTo {
private final SemanticVersion first;
private final SemanticVersion second;
private final int output;
@Parameterized.Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
{ new SemanticVersion(1, 0, 0), new SemanticVersion(0, 1, 0), 1 },
{ new SemanticVersion(1, 0, 0), new SemanticVersion(0, 0, 1), 1 },
{ new SemanticVersion(1, 0, 0), new SemanticVersion(0, 0, 0), 1 },
{ new SemanticVersion(0, 1, 0), new SemanticVersion(0, 0, 1), 1 },
{ new SemanticVersion(0, 1, 0), new SemanticVersion(0, 0, 0), 1 },
{ new SemanticVersion(0, 0, 1), new SemanticVersion(0, 0, 0), 1 },
{ new SemanticVersion(1, 1, 0), new SemanticVersion(1, 0, 0), 1 },
{ new SemanticVersion(1, 1, 1), new SemanticVersion(1, 1, 0), 1 },
{ new SemanticVersion(0, 0, 1), new SemanticVersion(1, 0, 0), -1 },
{ new SemanticVersion(1, 1, 1), new SemanticVersion(1, 1, 1), 0 },
{ new SemanticVersion(0, 0, 0), new SemanticVersion(0, 0, 0), 0 },
});
}
public SemanticVersionTest_compareTo(SemanticVersion first, SemanticVersion second, int output) {
this.first = first;
this.second = second;
this.output = output;
}
@Test
public void compareTo() {
assertEquals(output, first.compareTo(second));
}
}

View File

@@ -0,0 +1,41 @@
package org.thoughtcrime.securesms.util;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.thoughtcrime.securesms.linkpreview.LinkPreviewUtil;
import java.util.Arrays;
import java.util.Collection;
import static junit.framework.TestCase.assertEquals;
@RunWith(Parameterized.class)
public class SemanticVersionTest_parse {
private final String input;
private final SemanticVersion output;
@Parameterized.Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
{ "0.0.0", new SemanticVersion(0, 0, 0)},
{ "1.2.3", new SemanticVersion(1, 2, 3)},
{ "111.222.333", new SemanticVersion(111, 222, 333)},
{ "v1.2.3", null },
{ "1.2.3x", null },
{ "peter.ben.parker", null },
{ "", null}
});
}
public SemanticVersionTest_parse(String input, SemanticVersion output) {
this.input = input;
this.output = output;
}
@Test
public void parse() {
assertEquals(output, SemanticVersion.parse(input));
}
}