Browse Source

In the short number library, check that the number's country calling code matches the region it's being dialed from, when checking whether it's possible or valid, or getting its cost.

pull/799/head
David Yonge-Mallo 10 years ago
parent
commit
994c913514
6 changed files with 72 additions and 0 deletions
  1. +22
    -0
      cpp/src/phonenumbers/shortnumberinfo.cc
  2. +3
    -0
      cpp/src/phonenumbers/shortnumberinfo.h
  3. +13
    -0
      cpp/test/phonenumbers/shortnumberinfo_test.cc
  4. +19
    -0
      java/libphonenumber/src/com/google/i18n/phonenumbers/ShortNumberInfo.java
  5. +11
    -0
      java/libphonenumber/test/com/google/i18n/phonenumbers/ShortNumberInfoTest.java
  6. +4
    -0
      resources/PhoneNumberMetadataForTesting.xml

+ 22
- 0
cpp/src/phonenumbers/shortnumberinfo.cc View File

@ -16,6 +16,7 @@
#include "phonenumbers/shortnumberinfo.h"
#include <algorithm>
#include <string.h>
#include <iterator>
#include <map>
@ -93,6 +94,18 @@ bool MatchesPossibleNumberAndNationalNumber(
}
} // namespace
// Helper method to check that the country calling code of the number matches
// the region it's being dialed from.
bool ShortNumberInfo::RegionDialingFromMatchesNumber(const PhoneNumber& number,
const string& region_dialing_from) const {
list<string> region_codes;
phone_util_.GetRegionCodesForCountryCallingCode(number.country_code(),
&region_codes);
return std::find(region_codes.begin(),
region_codes.end(),
region_dialing_from) != region_codes.end();
}
bool ShortNumberInfo::IsPossibleShortNumberForRegion(
const string& short_number, const string& region_dialing_from) const {
const PhoneMetadata* phone_metadata =
@ -106,6 +119,9 @@ bool ShortNumberInfo::IsPossibleShortNumberForRegion(
bool ShortNumberInfo::IsPossibleShortNumberForRegion(const PhoneNumber& number,
const string& region_dialing_from) const {
if (!RegionDialingFromMatchesNumber(number, region_dialing_from)) {
return false;
}
const PhoneMetadata* phone_metadata =
GetMetadataForRegion(region_dialing_from);
if (!phone_metadata) {
@ -156,6 +172,9 @@ bool ShortNumberInfo::IsValidShortNumberForRegion(
bool ShortNumberInfo::IsValidShortNumberForRegion(
const PhoneNumber& number, const string& region_dialing_from) const {
if (!RegionDialingFromMatchesNumber(number, region_dialing_from)) {
return false;
}
const PhoneMetadata* phone_metadata =
GetMetadataForRegion(region_dialing_from);
if (!phone_metadata) {
@ -217,6 +236,9 @@ ShortNumberInfo::ShortNumberCost ShortNumberInfo::GetExpectedCostForRegion(
ShortNumberInfo::ShortNumberCost ShortNumberInfo::GetExpectedCostForRegion(
const PhoneNumber& number, const string& region_dialing_from) const {
if (!RegionDialingFromMatchesNumber(number, region_dialing_from)) {
return ShortNumberInfo::UNKNOWN_COST;
}
const PhoneMetadata* phone_metadata =
GetMetadataForRegion(region_dialing_from);
if (!phone_metadata) {


+ 3
- 0
cpp/src/phonenumbers/shortnumberinfo.h View File

@ -216,6 +216,9 @@ class ShortNumberInfo {
const i18n::phonenumbers::PhoneMetadata* GetMetadataForRegion(
const string& region_code) const;
bool RegionDialingFromMatchesNumber(const PhoneNumber& number,
const string& region_dialing_from) const;
// Helper method to get the region code for a given phone number, from a list
// of possible region codes. If the list contains more than one region, the
// first region for which the number is valid is returned.


+ 13
- 0
cpp/test/phonenumbers/shortnumberinfo_test.cc View File

@ -466,5 +466,18 @@ TEST_F(ShortNumberInfoTest, OverlappingNANPANumber) {
ParseNumberForTesting("211", RegionCode::CA()), RegionCode::CA()));
}
TEST_F(ShortNumberInfoTest, CountryCallingCodeIsNotIgnored) {
// +46 is the country calling code for Sweden (SE), and 40404 is a valid short
// number in the US.
EXPECT_FALSE(short_info_.IsPossibleShortNumberForRegion(
ParseNumberForTesting("+4640404", RegionCode::SE()), RegionCode::US()));
EXPECT_FALSE(short_info_.IsValidShortNumberForRegion(
ParseNumberForTesting("+4640404", RegionCode::SE()), RegionCode::US()));
EXPECT_EQ(ShortNumberInfo::UNKNOWN_COST,
short_info_.GetExpectedCostForRegion(
ParseNumberForTesting("+4640404", RegionCode::SE()),
RegionCode::US()));
}
} // namespace phonenumbers
} // namespace i18n

+ 19
- 0
java/libphonenumber/src/com/google/i18n/phonenumbers/ShortNumberInfo.java View File

@ -100,6 +100,16 @@ public class ShortNumberInfo {
: regionCodes);
}
/**
* Helper method to check that the country calling code of the number matches the region it's
* being dialed from.
*/
private boolean regionDialingFromMatchesNumber(PhoneNumber number,
String regionDialingFrom) {
List<String> regionCodes = getRegionCodesForCountryCode(number.getCountryCode());
return regionCodes.contains(regionDialingFrom);
}
/**
* Check whether a short number is a possible number when dialled from a region, given the number
* in the form of a string, and the region where the number is dialed from. This provides a more
@ -133,6 +143,9 @@ public class ShortNumberInfo {
* @return whether the number is a possible short number
*/
public boolean isPossibleShortNumberForRegion(PhoneNumber number, String regionDialingFrom) {
if (!regionDialingFromMatchesNumber(number, regionDialingFrom)) {
return false;
}
PhoneMetadata phoneMetadata =
MetadataManager.getShortNumberMetadataForRegion(regionDialingFrom);
if (phoneMetadata == null) {
@ -205,6 +218,9 @@ public class ShortNumberInfo {
* @return whether the short number matches a valid pattern
*/
public boolean isValidShortNumberForRegion(PhoneNumber number, String regionDialingFrom) {
if (!regionDialingFromMatchesNumber(number, regionDialingFrom)) {
return false;
}
PhoneMetadata phoneMetadata =
MetadataManager.getShortNumberMetadataForRegion(regionDialingFrom);
if (phoneMetadata == null) {
@ -317,6 +333,9 @@ public class ShortNumberInfo {
* category.
*/
public ShortNumberCost getExpectedCostForRegion(PhoneNumber number, String regionDialingFrom) {
if (!regionDialingFromMatchesNumber(number, regionDialingFrom)) {
return ShortNumberCost.UNKNOWN_COST;
}
// Note that regionDialingFrom may be null, in which case phoneMetadata will also be null.
PhoneMetadata phoneMetadata = MetadataManager.getShortNumberMetadataForRegion(
regionDialingFrom);


+ 11
- 0
java/libphonenumber/test/com/google/i18n/phonenumbers/ShortNumberInfoTest.java View File

@ -333,6 +333,17 @@ public class ShortNumberInfoTest extends TestMetadataTestCase {
shortInfo.getExpectedCostForRegion(parse("211", RegionCode.CA), RegionCode.CA));
}
public void testCountryCallingCodeIsNotIgnored() {
// +46 is the country calling code for Sweden (SE), and 40404 is a valid short number in the US.
assertFalse(shortInfo.isPossibleShortNumberForRegion(
parse("+4640404", RegionCode.SE), RegionCode.US));
assertFalse(shortInfo.isValidShortNumberForRegion(
parse("+4640404", RegionCode.SE), RegionCode.US));
assertEquals(ShortNumberInfo.ShortNumberCost.UNKNOWN_COST,
shortInfo.getExpectedCostForRegion(
parse("+4640404", RegionCode.SE), RegionCode.US));
}
private PhoneNumber parse(String number, String regionCode) {
try {
return phoneUtil.parse(number, regionCode);


+ 4
- 0
resources/PhoneNumberMetadataForTesting.xml View File

@ -780,6 +780,10 @@
</premiumRate>
</territory>
<!-- Sweden -->
<territory id="SE" countryCode="46" internationalPrefix="00">
</territory>
<!-- Singapore -->
<!-- http://www.ida.gov.sg/policies%20and%20regulation/20060508120124.aspx -->
<!-- For testing the AYTF with long NDDs, "777777" is added as a possible national prefix. -->


Loading…
Cancel
Save