diff options
author | Wei Li <weili@chromium.org> | 2015-12-17 17:46:58 -0800 |
---|---|---|
committer | Wei Li <weili@chromium.org> | 2015-12-17 17:46:58 -0800 |
commit | cc70b7b55c9edcd0ff038f59080699060fbbede1 (patch) | |
tree | 15e4137ce96d2a74450a8a84d2c4c3b31741f2c4 /core/src/fpdftext/fpdf_text_int_unittest.cpp | |
parent | 391783579c5a8aad346b56903b51e6551600550b (diff) | |
download | pdfium-cc70b7b55c9edcd0ff038f59080699060fbbede1.tar.xz |
Correctly extracting email addresses
An email address contains user name part and host name part.
User name allows dash or underscore, but not leading/ending/double
period. Host name doesn't allow leading/ending/double
period either.
BUG=489107
R=jun_fang@foxitsoftware.com, thestig@chromium.org
Review URL: https://codereview.chromium.org/1530763005 .
Diffstat (limited to 'core/src/fpdftext/fpdf_text_int_unittest.cpp')
-rw-r--r-- | core/src/fpdftext/fpdf_text_int_unittest.cpp | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/core/src/fpdftext/fpdf_text_int_unittest.cpp b/core/src/fpdftext/fpdf_text_int_unittest.cpp new file mode 100644 index 0000000000..b482f02be4 --- /dev/null +++ b/core/src/fpdftext/fpdf_text_int_unittest.cpp @@ -0,0 +1,56 @@ +// Copyright 2015 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "testing/gtest/include/gtest/gtest.h" + +#include "core/src/fpdftext/text_int.h" + +// Class to help test functions in CPDF_LinkExtract class. +class CPDF_TestLinkExtract : public CPDF_LinkExtract { + private: + // Add test cases as friends to access protected member functions. + // Access CheckMailLink. + FRIEND_TEST(fpdf_text_int, CheckMailLink); +}; + +TEST(fpdf_text_int, CheckMailLink) { + CPDF_TestLinkExtract extractor; + // Check cases that fail to extract valid mail link. + const wchar_t* invalid_strs[] = { + L"", + L"peter.pan" // '@' is required. + L"abc@server", // Domain name needs at least one '.'. + L"abc.@gmail.com", // '.' can not immediately precede '@'. + L"abc@xyz&q.org", // Domain name should not contain '&'. + L"abc@.xyz.org", // Domain name should not start with '.'. + L"fan@g..com" // Domain name should not have consecutive '.' + }; + for (int i = 0; i < FX_ArraySize(invalid_strs); ++i) { + CFX_WideString text_str(invalid_strs[i]); + EXPECT_FALSE(extractor.CheckMailLink(text_str)); + } + + // Check cases that can extract valid mail link. + // An array of {input_string, expected_extracted_email_address}. + const wchar_t* valid_strs[][2] = { + {L"peter@abc.d", L"peter@abc.d"}, + {L"red.teddy.b@abc.com", L"red.teddy.b@abc.com"}, + {L"abc_@gmail.com", L"abc_@gmail.com"}, // '_' is ok before '@'. + {L"dummy-hi@gmail.com", + L"dummy-hi@gmail.com"}, // '-' is ok in user name. + {L"a..df@gmail.com", L"df@gmail.com"}, // Stop at consecutive '.'. + {L".john@yahoo.com", L"john@yahoo.com"}, // Remove heading '.'. + {L"abc@xyz.org?/", L"abc@xyz.org"}, // Trim ending invalid chars. + {L"fan{abc@xyz.org", L"abc@xyz.org"}, // Trim beginning invalid chars. + {L"fan@g.com..", L"fan@g.com"}, // Trim the ending periods. + {L"CAP.cap@Gmail.Com", L"CAP.cap@Gmail.Com"}, // Keep the original case. + }; + for (int i = 0; i < FX_ArraySize(valid_strs); ++i) { + CFX_WideString text_str(valid_strs[i][0]); + CFX_WideString expected_str(L"mailto:"); + expected_str += valid_strs[i][1]; + EXPECT_TRUE(extractor.CheckMailLink(text_str)); + EXPECT_STREQ(text_str.c_str(), expected_str.c_str()); + } +} |