This source file includes following definitions.
- GetBadSSLInfo
- GetGoodSSLInfo
- IsGoodSSLInfo
- passed_
- SendReport
- SendReport
- CreateURLRequest
- SendReport
- DoReportIsSent
- DoReportIsNotSent
- DoMockReportIsSent
- TEST
- TEST
- TEST
- TEST
#include "chrome/browser/net/chrome_fraudulent_certificate_reporter.h"
#include <string>
#include "base/bind.h"
#include "base/files/file_path.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/synchronization/waitable_event.h"
#include "base/threading/thread.h"
#include "chrome/browser/net/chrome_url_request_context.h"
#include "content/public/test/test_browser_thread.h"
#include "net/base/request_priority.h"
#include "net/base/test_data_directory.h"
#include "net/cert/x509_certificate.h"
#include "net/http/transport_security_state.h"
#include "net/ssl/ssl_info.h"
#include "net/test/cert_test_util.h"
#include "net/url_request/fraudulent_certificate_reporter.h"
#include "net/url_request/url_request.h"
#include "testing/gtest/include/gtest/gtest.h"
using content::BrowserThread;
using net::SSLInfo;
namespace chrome_browser_net {
static SSLInfo GetBadSSLInfo() {
SSLInfo info;
info.cert = net::ImportCertFromFile(net::GetTestCertsDirectory(),
"expired_cert.pem");
info.cert_status = net::CERT_STATUS_DATE_INVALID;
info.is_issued_by_known_root = false;
return info;
}
static SSLInfo GetGoodSSLInfo() {
SSLInfo info;
info.cert = net::ImportCertFromFile(net::GetTestCertsDirectory(),
"test_mail_google_com.pem");
info.is_issued_by_known_root = true;
return info;
}
static bool IsGoodSSLInfo(const SSLInfo& info) {
return info.is_valid() && info.is_issued_by_known_root;
}
class TestReporter : public ChromeFraudulentCertificateReporter {
public:
explicit TestReporter(net::URLRequestContext* request_context)
: ChromeFraudulentCertificateReporter(request_context) {}
};
class SendingTestReporter : public TestReporter {
public:
explicit SendingTestReporter(net::URLRequestContext* request_context)
: TestReporter(request_context), passed_(false) {}
virtual void SendReport(const std::string& hostname,
const SSLInfo& ssl_info,
bool sni_available) OVERRIDE {
EXPECT_TRUE(IsGoodSSLInfo(ssl_info));
EXPECT_TRUE(net::TransportSecurityState::IsGooglePinnedProperty(
hostname, sni_available));
passed_ = true;
}
virtual ~SendingTestReporter() {
EXPECT_TRUE(passed_);
}
bool passed_;
};
class NotSendingTestReporter : public TestReporter {
public:
explicit NotSendingTestReporter(net::URLRequestContext* request_context)
: TestReporter(request_context) {}
virtual void SendReport(const std::string& hostname,
const SSLInfo& ssl_info,
bool sni_available) OVERRIDE {
EXPECT_FALSE(IsGoodSSLInfo(ssl_info));
EXPECT_FALSE(net::TransportSecurityState::IsGooglePinnedProperty(
hostname, sni_available));
}
};
class MockURLRequest : public net::URLRequest {
public:
explicit MockURLRequest(net::URLRequestContext* context)
: net::URLRequest(GURL(std::string()),
net::DEFAULT_PRIORITY,
NULL,
context) {}
private:
};
class MockReporter : public ChromeFraudulentCertificateReporter {
public:
explicit MockReporter(net::URLRequestContext* request_context)
: ChromeFraudulentCertificateReporter(request_context) {}
virtual scoped_ptr<net::URLRequest> CreateURLRequest(
net::URLRequestContext* context) OVERRIDE {
return scoped_ptr<net::URLRequest>(new MockURLRequest(context));
}
virtual void SendReport(
const std::string& hostname,
const net::SSLInfo& ssl_info,
bool sni_available) OVERRIDE {
DCHECK(!hostname.empty());
DCHECK(ssl_info.is_valid());
ChromeFraudulentCertificateReporter::SendReport(hostname, ssl_info,
sni_available);
}
};
static void DoReportIsSent() {
ChromeURLRequestContext context;
SendingTestReporter reporter(&context);
SSLInfo info = GetGoodSSLInfo();
reporter.SendReport("mail.google.com", info, true);
}
static void DoReportIsNotSent() {
ChromeURLRequestContext context;
NotSendingTestReporter reporter(&context);
SSLInfo info = GetBadSSLInfo();
reporter.SendReport("www.example.com", info, true);
}
static void DoMockReportIsSent() {
ChromeURLRequestContext context;
MockReporter reporter(&context);
SSLInfo info = GetGoodSSLInfo();
reporter.SendReport("mail.google.com", info, true);
}
TEST(ChromeFraudulentCertificateReporterTest, GoodBadInfo) {
SSLInfo good = GetGoodSSLInfo();
EXPECT_TRUE(IsGoodSSLInfo(good));
SSLInfo bad = GetBadSSLInfo();
EXPECT_FALSE(IsGoodSSLInfo(bad));
}
TEST(ChromeFraudulentCertificateReporterTest, ReportIsSent) {
base::MessageLoopForIO loop;
content::TestBrowserThread io_thread(BrowserThread::IO, &loop);
loop.PostTask(FROM_HERE, base::Bind(&DoReportIsSent));
loop.RunUntilIdle();
}
TEST(ChromeFraudulentCertificateReporterTest, MockReportIsSent) {
base::MessageLoopForIO loop;
content::TestBrowserThread io_thread(BrowserThread::IO, &loop);
loop.PostTask(FROM_HERE, base::Bind(&DoMockReportIsSent));
loop.RunUntilIdle();
}
TEST(ChromeFraudulentCertificateReporterTest, ReportIsNotSent) {
base::MessageLoopForIO loop;
content::TestBrowserThread io_thread(BrowserThread::IO, &loop);
loop.PostTask(FROM_HERE, base::Bind(&DoReportIsNotSent));
loop.RunUntilIdle();
}
}