diff options
5 files changed, 101 insertions, 8 deletions
diff --git a/app/src/androidTest/java/se/leap/bitmaskclient/test/TestCalendarProvider.java b/app/src/androidTest/java/se/leap/bitmaskclient/test/TestCalendarProvider.java new file mode 100644 index 00000000..82ea8b59 --- /dev/null +++ b/app/src/androidTest/java/se/leap/bitmaskclient/test/TestCalendarProvider.java @@ -0,0 +1,26 @@ +package se.leap.bitmaskclient.test; + +import java.util.Calendar; + +import se.leap.bitmaskclient.eip.CalendarProviderInterface; + +/** + * Created by cyberta on 13.09.17. + */ + +class TestCalendarProvider implements CalendarProviderInterface { + + private long currentTimeInMillis = 0; + + public TestCalendarProvider(long currentTimeInMillis) { + this.currentTimeInMillis = currentTimeInMillis; + } + + @Override + public Calendar getCalendar() { + Calendar calendar = Calendar.getInstance(); + calendar.setTimeInMillis(currentTimeInMillis); + return calendar; + } + +} diff --git a/app/src/androidTest/java/se/leap/bitmaskclient/test/TestVpnCertificateValidator.java b/app/src/androidTest/java/se/leap/bitmaskclient/test/TestVpnCertificateValidator.java index 672607be..2704bcad 100644 --- a/app/src/androidTest/java/se/leap/bitmaskclient/test/TestVpnCertificateValidator.java +++ b/app/src/androidTest/java/se/leap/bitmaskclient/test/TestVpnCertificateValidator.java @@ -30,10 +30,11 @@ import se.leap.bitmaskclient.eip.*; /** * @author parmegv + * //FIXME: The class VpnCertificateValidator should be tested with unit tests! */ -public class testVpnCertificateValidator extends InstrumentationTestCase { +public class TestVpnCertificateValidator extends InstrumentationTestCase { - String certificate_valid_from_jan2015_to_nov2022 = ""; + String certificate_valid_from_nov2012_to_nov2022 = ""; Context context; FromAssets assets; @@ -43,16 +44,47 @@ public class testVpnCertificateValidator extends InstrumentationTestCase { context = getInstrumentation().getContext(); assets = new FromAssets(context); JSONObject secrets = new JSONObject(assets.toString(TestConstants.SECRETS_FILE)); - certificate_valid_from_jan2015_to_nov2022 = secrets.getString(Provider.CA_CERT); + certificate_valid_from_nov2012_to_nov2022 = secrets.getString(Provider.CA_CERT); super.setUp(); } + + //TODO: This test proves that the validation method is weird. Valid dates range between Nov. 2oo6 and Nov. 2017 instead of Nov. 2012 and Nov.2022 public void testIsValid() { - VpnCertificateValidator validator = new VpnCertificateValidator(certificate_valid_from_jan2015_to_nov2022); - setTime(2015, 1, 6); + VpnCertificateValidator validator = new VpnCertificateValidator(certificate_valid_from_nov2012_to_nov2022); + Calendar calendar = Calendar.getInstance(); + + calendar.set(Calendar.YEAR, 2006); + calendar.set(Calendar.MONTH, Calendar.NOVEMBER); + calendar.set(Calendar.DAY_OF_MONTH, 6); calendar.set(Calendar.YEAR, 2006); + validator.setCalendarProvider(new TestCalendarProvider(calendar.getTimeInMillis())); + assertFalse(validator.isValid()); + + calendar.set(Calendar.YEAR, 2010); + calendar.set(Calendar.MONTH, Calendar.NOVEMBER); + calendar.set(Calendar.DAY_OF_MONTH, 6); + validator.setCalendarProvider(new TestCalendarProvider(calendar.getTimeInMillis())); + assertTrue(validator.isValid()); + + calendar.set(Calendar.YEAR, 2011); + calendar.set(Calendar.MONTH, Calendar.NOVEMBER); + calendar.set(Calendar.DAY_OF_MONTH, 6); + validator.setCalendarProvider(new TestCalendarProvider(calendar.getTimeInMillis())); assertTrue(validator.isValid()); - setTime(2020, 1, 6); + + calendar.set(Calendar.YEAR, 2017); + calendar.set(Calendar.MONTH, Calendar.NOVEMBER); + calendar.set(Calendar.DAY_OF_MONTH, 5); + validator.setCalendarProvider(new TestCalendarProvider(calendar.getTimeInMillis())); + assertTrue(validator.isValid()); + + calendar.set(Calendar.YEAR, 2017); + calendar.set(Calendar.MONTH, Calendar.NOVEMBER); + calendar.set(Calendar.DAY_OF_MONTH, 6); + validator.setCalendarProvider(new TestCalendarProvider(calendar.getTimeInMillis())); assertFalse(validator.isValid()); + + } private void setTime(int year, int month, int day) { diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/CalendarProvider.java b/app/src/main/java/se/leap/bitmaskclient/eip/CalendarProvider.java new file mode 100644 index 00000000..a9aec6b8 --- /dev/null +++ b/app/src/main/java/se/leap/bitmaskclient/eip/CalendarProvider.java @@ -0,0 +1,14 @@ +package se.leap.bitmaskclient.eip; + +import java.util.Calendar; + +/** + * Created by cyberta on 13.09.17. + */ + +class CalendarProvider implements CalendarProviderInterface { + + public Calendar getCalendar() { + return Calendar.getInstance(); + } +} diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/CalendarProviderInterface.java b/app/src/main/java/se/leap/bitmaskclient/eip/CalendarProviderInterface.java new file mode 100644 index 00000000..a20f5fab --- /dev/null +++ b/app/src/main/java/se/leap/bitmaskclient/eip/CalendarProviderInterface.java @@ -0,0 +1,11 @@ +package se.leap.bitmaskclient.eip; + +import java.util.Calendar; + +/** + * Created by cyberta on 13.09.17. + */ + +public interface CalendarProviderInterface { + Calendar getCalendar(); +} diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/VpnCertificateValidator.java b/app/src/main/java/se/leap/bitmaskclient/eip/VpnCertificateValidator.java index 8fce6a37..b7c26761 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/VpnCertificateValidator.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/VpnCertificateValidator.java @@ -25,9 +25,15 @@ public class VpnCertificateValidator { public final static String TAG = VpnCertificateValidator.class.getSimpleName(); private String certificate; + protected CalendarProviderInterface calendarProvider; public VpnCertificateValidator(String certificate) { this.certificate = certificate; + calendarProvider = new CalendarProvider(); + } + + public void setCalendarProvider(CalendarProviderInterface calendarProvider) { + this.calendarProvider = calendarProvider; } public boolean isValid() { @@ -37,6 +43,10 @@ public class VpnCertificateValidator { } else return true; } + + /* FIXME: the validation seems to be syntactically wrong. + * if the valid time span of a certificate is between 01.01.14 and 01.01.16 this method would return true for current dates between 01.01.13 and 01.01.15!!! + */ private boolean isValid(X509Certificate certificate) { Calendar offset_date = calculateOffsetCertificateValidity(certificate); try { @@ -51,9 +61,9 @@ public class VpnCertificateValidator { private Calendar calculateOffsetCertificateValidity(X509Certificate certificate) { long preventive_time = Math.abs(certificate.getNotBefore().getTime() - certificate.getNotAfter().getTime()) / 2; - long current_date_millis = Calendar.getInstance().getTimeInMillis(); + long current_date_millis = calendarProvider.getCalendar().getTimeInMillis(); - Calendar limit_date = Calendar.getInstance(); + Calendar limit_date = calendarProvider.getCalendar(); limit_date.setTimeInMillis(current_date_millis + preventive_time); return limit_date; } |