shill: cellular: Normalize MDN to remove non-digit characters.

BUG=chrome-os-partner:17513
TEST=Build and run unit tests.

Change-Id: I394fd206b8d8a13c388bc3013558b180489fcfb2
Reviewed-on: https://gerrit.chromium.org/gerrit/43232
Reviewed-by: Arman Uguray <armansito@chromium.org>
Commit-Queue: Ben Chan <benchan@chromium.org>
Tested-by: Ben Chan <benchan@chromium.org>
diff --git a/cellular_capability_universal.cc b/cellular_capability_universal.cc
index 4db03a0..ea75b26 100644
--- a/cellular_capability_universal.cc
+++ b/cellular_capability_universal.cc
@@ -853,10 +853,10 @@
   if (mdn_.empty())
     return false;
 
-  // If MDN contains only zeros ('+' and '-' characters are ignored),
-  // the service requires activation.
+  // If MDN contains only zeros, the service requires activation.
+  // Note that |mdn_| is normalized to contain only digits in OnMdnChanged().
   for (size_t i = 0; i < mdn_.size(); ++i) {
-    if (mdn_[i] != '0' && mdn_[i] != '-' && mdn_[i] != '+')
+    if (mdn_[i] != '0')
       return false;
   }
   return true;
@@ -1273,6 +1273,15 @@
   return !sim_path.empty() && sim_path != kRootPath;
 }
 
+string CellularCapabilityUniversal::NormalizeMdn(const string &mdn) const {
+  string normalized_mdn;
+  for (size_t i = 0; i < mdn.size(); ++i) {
+    if (IsAsciiDigit(mdn[i]))
+      normalized_mdn += mdn[i];
+  }
+  return normalized_mdn;
+}
+
 void CellularCapabilityUniversal::OnSimPathChanged(
     const string &sim_path) {
   if (sim_path == sim_path_)
@@ -1328,7 +1337,7 @@
 
 void CellularCapabilityUniversal::OnMdnChanged(
     const string &mdn) {
-  mdn_ = mdn;
+  mdn_ = NormalizeMdn(mdn);
 }
 
 void CellularCapabilityUniversal::OnModemManufacturerChanged(
diff --git a/cellular_capability_universal.h b/cellular_capability_universal.h
index 72c67c6..19d7667 100644
--- a/cellular_capability_universal.h
+++ b/cellular_capability_universal.h
@@ -147,6 +147,7 @@
   FRIEND_TEST(CellularCapabilityUniversalMainTest, GetTypeString);
   FRIEND_TEST(CellularCapabilityUniversalMainTest, IsServiceActivationRequired);
   FRIEND_TEST(CellularCapabilityUniversalMainTest, IsValidSimPath);
+  FRIEND_TEST(CellularCapabilityUniversalMainTest, NormalizeMdn);
   FRIEND_TEST(CellularCapabilityUniversalMainTest, OnListBearersReply);
   FRIEND_TEST(CellularCapabilityUniversalMainTest,
               OnModemCurrentCapabilitiesChanged);
@@ -309,6 +310,10 @@
   // and "/".
   bool IsValidSimPath(const std::string &sim_path) const;
 
+  // Returns the normalized version of |mdn| by keeping only digits in |mdn|
+  // and removing other non-digit characters.
+  std::string NormalizeMdn(const std::string &mdn) const;
+
   static std::string GenerateNewGenericServiceName();
 
   scoped_ptr<mm1::ModemModem3gppProxyInterface> modem_3gpp_proxy_;
diff --git a/cellular_capability_universal_unittest.cc b/cellular_capability_universal_unittest.cc
index f7ce850..05a9243 100644
--- a/cellular_capability_universal_unittest.cc
+++ b/cellular_capability_universal_unittest.cc
@@ -678,6 +678,16 @@
   EXPECT_TRUE(capability_->IsValidSimPath("path"));
 }
 
+TEST_F(CellularCapabilityUniversalMainTest, NormalizeMdn) {
+  EXPECT_EQ("", capability_->NormalizeMdn(""));
+  EXPECT_EQ("12345678901", capability_->NormalizeMdn("12345678901"));
+  EXPECT_EQ("12345678901", capability_->NormalizeMdn("+1 234 567 8901"));
+  EXPECT_EQ("12345678901", capability_->NormalizeMdn("+1-234-567-8901"));
+  EXPECT_EQ("12345678901", capability_->NormalizeMdn("+1 (234) 567-8901"));
+  EXPECT_EQ("12345678901", capability_->NormalizeMdn("1 234  567 8901 "));
+  EXPECT_EQ("2345678901", capability_->NormalizeMdn("(234) 567-8901"));
+}
+
 TEST_F(CellularCapabilityUniversalMainTest, SimPathChanged) {
   // Set up mock modem SIM properties
   const char kImsi[] = "310100000001";
@@ -1472,14 +1482,8 @@
   EXPECT_FALSE(capability_->IsServiceActivationRequired());
   capability_->mdn_ = "1234567890";
   EXPECT_FALSE(capability_->IsServiceActivationRequired());
-  capability_->mdn_ = "+1-234-567-890";
-  EXPECT_FALSE(capability_->IsServiceActivationRequired());
   capability_->mdn_ = "0000000000";
   EXPECT_TRUE(capability_->IsServiceActivationRequired());
-  capability_->mdn_ = "0-000-000-000";
-  EXPECT_TRUE(capability_->IsServiceActivationRequired());
-  capability_->mdn_ = "+0-000-000-000";
-  EXPECT_TRUE(capability_->IsServiceActivationRequired());
 }
 
 TEST_F(CellularCapabilityUniversalMainTest, OnModemCurrentCapabilitiesChanged) {