From b090a12b7f1a9aa32b3b6898b61a92fe083ae8dc Mon Sep 17 00:00:00 2001 From: akwizgran <michael@briarproject.org> Date: Sat, 15 Dec 2012 22:24:51 +0000 Subject: [PATCH] Accept phone numbers starting with +. --- .../sf/briar/plugins/modem/CountryCodes.java | 16 ++++--- .../briar/plugins/modem/CountryCodesTest.java | 43 +++++++++++++++---- 2 files changed, 44 insertions(+), 15 deletions(-) diff --git a/briar-core/src/net/sf/briar/plugins/modem/CountryCodes.java b/briar-core/src/net/sf/briar/plugins/modem/CountryCodes.java index c1de00b084..9aff2db950 100644 --- a/briar-core/src/net/sf/briar/plugins/modem/CountryCodes.java +++ b/briar-core/src/net/sf/briar/plugins/modem/CountryCodes.java @@ -19,7 +19,7 @@ class CountryCodes { new Country("AR", "Argentina", "54", "00", "0"), new Country("AS", "American Samoa", "1", "011", "1"), new Country("AT", "Austria", "43", "00", "0"), - new Country("AU", "Australia", "61", "00", ""), + new Country("AU", "Australia", "61", "0011", "0"), new Country("AW", "Aruba", "297", "00", ""), new Country("AZ", "Azerbaijan", "994", "00", "8"), new Country("BA", "Bosnia and Herzegovina", "387", "00", "0"), @@ -43,9 +43,9 @@ class CountryCodes { new Country("BZ", "Belize", "501", "00", "0"), new Country("CA", "Canada", "1", "011", "1"), new Country("CC", "Cocos (Keeling) Islands", "61", "0011", "0"), - new Country("CD", "Congo", "243", "00", ""), + new Country("CD", "Congo (Republic)", "243", "00", ""), new Country("CF", "Central African Republic", "236", "00", ""), - new Country("CG", "Congo", "242", "00", ""), + new Country("CG", "Congo (Democratic Republic)", "242", "00", "0"), new Country("CH", "Switzerland", "41", "00", "0"), new Country("CI", "Cote D'Ivoire", "225", "00", "0"), new Country("CK", "Cook Islands", "682", "00", "00"), @@ -257,12 +257,14 @@ class CountryCodes { static String translate(String number, String fromIso, String toIso) { Country from = COUNTRY_MAP.get(fromIso), to = COUNTRY_MAP.get(toIso); if(from == null || to == null) return null; - // Strip the IDD prefix and country code from the number if present + // Strip any prefixes and country codes from the number + String plusCountryCode = "+" + to.countryCode; String iddCountryCode = to.idd + to.countryCode; - if(number.startsWith(iddCountryCode)) + if(number.startsWith(plusCountryCode)) + number = number.substring(plusCountryCode.length()); + else if(number.startsWith(iddCountryCode)) number = number.substring(iddCountryCode.length()); - // Strip the NDD prefix from the number if present - if(number.startsWith(to.ndd)) + else if(number.startsWith(to.ndd)) number = number.substring(to.ndd.length()); if(from == to) return from.ndd + number; // National return from.idd + to.countryCode + number; // International diff --git a/briar-tests/src/net/sf/briar/plugins/modem/CountryCodesTest.java b/briar-tests/src/net/sf/briar/plugins/modem/CountryCodesTest.java index 8c87bb50ff..366e14253f 100644 --- a/briar-tests/src/net/sf/briar/plugins/modem/CountryCodesTest.java +++ b/briar-tests/src/net/sf/briar/plugins/modem/CountryCodesTest.java @@ -8,27 +8,54 @@ public class CountryCodesTest extends BriarTestCase { @Test public void testTranslation() { - // Unknown country for caller + // Unrecognised country for caller assertNull(CountryCodes.translate("02012345678", "ZZ", "GB")); - // Unknown country for callee + // Unrecognised country for callee assertNull(CountryCodes.translate("02012345678", "GB", "ZZ")); + + // GB to GB, callee has not included a prefix + assertEquals("02012345678", + CountryCodes.translate("2012345678", "GB", "GB")); // GB to GB, callee has included NDD prefix assertEquals("02012345678", CountryCodes.translate("02012345678", "GB", "GB")); + // GB to GB, callee has included plus sign and country code + assertEquals("02012345678", + CountryCodes.translate("+442012345678", "GB", "GB")); // GB to GB, callee has included IDD prefix and country code assertEquals("02012345678", CountryCodes.translate("00442012345678", "GB", "GB")); - // GB to GB, callee has not included a prefix - assertEquals("02012345678", - CountryCodes.translate("2012345678", "GB", "GB")); + + // Russia to GB, callee has not included a prefix + assertEquals("8**10442012345678", + CountryCodes.translate("2012345678", "RU", "GB")); // Russia to GB, callee has included NDD prefix assertEquals("8**10442012345678", CountryCodes.translate("02012345678", "RU", "GB")); + // Russia to GB, callee has included plus sign and country code + assertEquals("8**10442012345678", + CountryCodes.translate("+442012345678", "RU", "GB")); // Russia to GB, callee has included IDD prefix and country code assertEquals("8**10442012345678", CountryCodes.translate("00442012345678", "RU", "GB")); - // Russia to GB, callee has not included a prefix - assertEquals("8**10442012345678", - CountryCodes.translate("2012345678", "RU", "GB")); + + // Andorra to Andorra (no NDD), callee has not included a prefix + assertEquals("765432", CountryCodes.translate("765432", "AD", "AD")); + // Andorra to Andorra, callee has included plus sign and country code + assertEquals("765432", + CountryCodes.translate("+376765432", "AD", "AD")); + // Andorra to Andorra, callee has included IDD and country code + assertEquals("765432", + CountryCodes.translate("00376765432", "AD", "AD")); + + // GB to Andorra (no NDD), callee has not included a prefix + assertEquals("00376765432", + CountryCodes.translate("765432", "GB", "AD")); + // GB to Andorra, callee has included plus sign and country code + assertEquals("00376765432", + CountryCodes.translate("+376765432", "GB", "AD")); + // GB to Andorra, callee has included IDD and country code + assertEquals("00376765432", + CountryCodes.translate("00376765432", "GB", "AD")); } } -- GitLab