Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(221)

Side by Side Diff: content/browser/service_worker/service_worker_storage_unittest.cc

Issue 2376403004: Store Origin-Trial tokens to ServiceWorkerDataBase (Closed)
Patch Set: incorporated chasej's comment Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/service_worker/service_worker_storage.h" 5 #include "content/browser/service_worker/service_worker_storage.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 #include <string> 8 #include <string>
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/files/file_util.h" 11 #include "base/files/file_util.h"
12 #include "base/files/scoped_temp_dir.h" 12 #include "base/files/scoped_temp_dir.h"
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/macros.h" 14 #include "base/macros.h"
15 #include "base/run_loop.h" 15 #include "base/run_loop.h"
16 #include "base/threading/thread_task_runner_handle.h" 16 #include "base/threading/thread_task_runner_handle.h"
17 #include "build/build_config.h" 17 #include "build/build_config.h"
18 #include "content/browser/browser_thread_impl.h" 18 #include "content/browser/browser_thread_impl.h"
19 #include "content/browser/service_worker/embedded_worker_test_helper.h" 19 #include "content/browser/service_worker/embedded_worker_test_helper.h"
20 #include "content/browser/service_worker/service_worker_context_core.h" 20 #include "content/browser/service_worker/service_worker_context_core.h"
21 #include "content/browser/service_worker/service_worker_disk_cache.h" 21 #include "content/browser/service_worker/service_worker_disk_cache.h"
22 #include "content/browser/service_worker/service_worker_registration.h" 22 #include "content/browser/service_worker/service_worker_registration.h"
23 #include "content/browser/service_worker/service_worker_version.h" 23 #include "content/browser/service_worker/service_worker_version.h"
24 #include "content/common/service_worker/service_worker_status_code.h" 24 #include "content/common/service_worker/service_worker_status_code.h"
25 #include "content/common/service_worker/service_worker_utils.h" 25 #include "content/common/service_worker/service_worker_utils.h"
26 #include "content/public/common/content_client.h"
27 #include "content/public/common/origin_trial_policy.h"
26 #include "content/public/test/test_browser_thread_bundle.h" 28 #include "content/public/test/test_browser_thread_bundle.h"
27 #include "ipc/ipc_message.h" 29 #include "ipc/ipc_message.h"
28 #include "net/base/io_buffer.h" 30 #include "net/base/io_buffer.h"
29 #include "net/base/net_errors.h" 31 #include "net/base/net_errors.h"
30 #include "net/base/test_completion_callback.h" 32 #include "net/base/test_completion_callback.h"
31 #include "net/http/http_response_headers.h" 33 #include "net/http/http_response_headers.h"
34 #include "net/http/http_util.h"
35 #include "net/test/cert_test_util.h"
36 #include "net/test/test_data_directory.h"
32 #include "testing/gtest/include/gtest/gtest.h" 37 #include "testing/gtest/include/gtest/gtest.h"
33 38
34 using net::IOBuffer; 39 using net::IOBuffer;
35 using net::TestCompletionCallback; 40 using net::TestCompletionCallback;
36 using net::WrappedIOBuffer; 41 using net::WrappedIOBuffer;
37 42
38 namespace content { 43 namespace content {
39 44
40 namespace { 45 namespace {
41 46
42 typedef ServiceWorkerDatabase::RegistrationData RegistrationData; 47 typedef ServiceWorkerDatabase::RegistrationData RegistrationData;
43 typedef ServiceWorkerDatabase::ResourceRecord ResourceRecord; 48 typedef ServiceWorkerDatabase::ResourceRecord ResourceRecord;
44 49
50 // This is a sample public key for testing the API. The corresponding
51 // private
falken 2016/10/07 03:35:22 nit: line wrapping
horo 2016/10/07 05:33:32 Done.
52 // key (use this to generate new samples for this test file) is:
53 //
54 // 0x83, 0x67, 0xf4, 0xcd, 0x2a, 0x1f, 0x0e, 0x04, 0x0d, 0x43, 0x13,
55 // 0x4c, 0x67, 0xc4, 0xf4, 0x28, 0xc9, 0x90, 0x15, 0x02, 0xe2, 0xba,
56 // 0xfd, 0xbb, 0xfa, 0xbc, 0x92, 0x76, 0x8a, 0x2c, 0x4b, 0xc7, 0x75,
57 // 0x10, 0xac, 0xf9, 0x3a, 0x1c, 0xb8, 0xa9, 0x28, 0x70, 0xd2, 0x9a,
58 // 0xd0, 0x0b, 0x59, 0xe1, 0xac, 0x2b, 0xb7, 0xd5, 0xca, 0x1f, 0x64,
59 // 0x90, 0x08, 0x8e, 0xa8, 0xe0, 0x56, 0x3a, 0x04, 0xd0
60 const uint8_t kTestPublicKey[] = {
61 0x75, 0x10, 0xac, 0xf9, 0x3a, 0x1c, 0xb8, 0xa9, 0x28, 0x70, 0xd2,
62 0x9a, 0xd0, 0x0b, 0x59, 0xe1, 0xac, 0x2b, 0xb7, 0xd5, 0xca, 0x1f,
63 0x64, 0x90, 0x08, 0x8e, 0xa8, 0xe0, 0x56, 0x3a, 0x04, 0xd0,
64 };
65
45 void StatusAndQuitCallback(ServiceWorkerStatusCode* result, 66 void StatusAndQuitCallback(ServiceWorkerStatusCode* result,
46 const base::Closure& quit_closure, 67 const base::Closure& quit_closure,
47 ServiceWorkerStatusCode status) { 68 ServiceWorkerStatusCode status) {
48 *result = status; 69 *result = status;
49 quit_closure.Run(); 70 quit_closure.Run();
50 } 71 }
51 72
52 void StatusCallback(bool* was_called, 73 void StatusCallback(bool* was_called,
53 ServiceWorkerStatusCode* result, 74 ServiceWorkerStatusCode* result,
54 ServiceWorkerStatusCode status) { 75 ServiceWorkerStatusCode status) {
(...skipping 1700 matching lines...) Expand 10 before | Expand all | Expand 10 after
1755 1776
1756 // Remove other registration at first origin. 1777 // Remove other registration at first origin.
1757 EXPECT_EQ(SERVICE_WORKER_OK, 1778 EXPECT_EQ(SERVICE_WORKER_OK,
1758 DeleteRegistration(kRegistrationId2, kScope2.GetOrigin())); 1779 DeleteRegistration(kRegistrationId2, kScope2.GetOrigin()));
1759 1780
1760 // No foreign fetch registrations remain. 1781 // No foreign fetch registrations remain.
1761 EXPECT_FALSE(storage()->OriginHasForeignFetchRegistrations(kOrigin1)); 1782 EXPECT_FALSE(storage()->OriginHasForeignFetchRegistrations(kOrigin1));
1762 EXPECT_FALSE(storage()->OriginHasForeignFetchRegistrations(kOrigin2)); 1783 EXPECT_FALSE(storage()->OriginHasForeignFetchRegistrations(kOrigin2));
1763 } 1784 }
1764 1785
1786 class ServiceWorkerStorageOriginTrialsTest : public ServiceWorkerStorageTest {
1787 public:
1788 ServiceWorkerStorageOriginTrialsTest() {}
1789 ~ServiceWorkerStorageOriginTrialsTest() override {}
1790
1791 protected:
1792 void WriteRegistration(const RegistrationData& registration,
1793 const std::vector<ResourceRecord>& resources) {
1794 ServiceWorkerDatabase::RegistrationData deleted_version;
1795 std::vector<int64_t> newly_purgeable_resources;
1796
1797 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK,
1798 storage()->database_->WriteRegistration(
1799 registration, resources, &deleted_version,
1800 &newly_purgeable_resources));
1801 }
1802 };
1803
1804 TEST_F(ServiceWorkerStorageOriginTrialsTest, AbsentEntryAndEmptyEntry) {
1805 const GURL origin1("http://www1.example.com");
1806 const GURL scope1("http://www1.example.com/foo/");
1807 RegistrationData data1;
1808 data1.registration_id = 100;
1809 data1.scope = scope1;
1810 data1.script = GURL(origin1.spec() + "/script.js");
1811 data1.version_id = 1000;
1812 data1.is_active = true;
1813 data1.resources_total_size_bytes = 100;
1814 // Don't set origin_trial_tokens to simulate old database entry.
1815 std::vector<ServiceWorkerDatabase::ResourceRecord> resources1;
1816 resources1.push_back(
1817 ServiceWorkerDatabase::ResourceRecord(1, data1.script, 100));
1818 WriteRegistration(data1, resources1);
1819
1820 const GURL origin2("http://www2.example.com");
1821 const GURL scope2("http://www2.example.com/foo/");
1822 RegistrationData data2;
1823 data2.registration_id = 200;
1824 data2.scope = scope2;
1825 data2.script = GURL(origin2.spec() + "/script.js");
1826 data2.version_id = 2000;
1827 data2.is_active = true;
1828 data2.resources_total_size_bytes = 200;
1829 // Set empty origin_trial_tokens.
1830 data2.origin_trial_tokens = TrialTokenValidator::FeatureToTokensMap();
1831 std::vector<ServiceWorkerDatabase::ResourceRecord> resources2;
1832 resources2.push_back(
1833 ServiceWorkerDatabase::ResourceRecord(2, data2.script, 200));
1834 WriteRegistration(data2, resources2);
1835
1836 scoped_refptr<ServiceWorkerRegistration> found_registration;
1837
1838 EXPECT_EQ(SERVICE_WORKER_OK,
1839 FindRegistrationForDocument(scope1, &found_registration));
1840 ASSERT_TRUE(found_registration->active_version());
1841 // origin_trial_tokens must be unset.
1842 EXPECT_FALSE(found_registration->active_version()->origin_trial_tokens());
1843
1844 EXPECT_EQ(SERVICE_WORKER_OK,
1845 FindRegistrationForDocument(scope2, &found_registration));
1846 ASSERT_TRUE(found_registration->active_version());
1847 // Empty origin_trial_tokens must exist.
1848 ASSERT_TRUE(found_registration->active_version()->origin_trial_tokens());
1849 EXPECT_TRUE(
1850 found_registration->active_version()->origin_trial_tokens()->empty());
1851 }
1852
1853 class ServiceWorkerStorageOriginTrialsDiskTest
1854 : public ServiceWorkerStorageTest {
1855 public:
1856 ServiceWorkerStorageOriginTrialsDiskTest() {
1857 SetContentClient(&test_content_client_);
1858 }
1859 ~ServiceWorkerStorageOriginTrialsDiskTest() override {
1860 SetContentClient(nullptr);
1861 }
1862 void SetUp() override {
1863 ASSERT_TRUE(InitUserDataDirectory());
1864 ServiceWorkerStorageTest::SetUp();
1865 }
1866
1867 private:
1868 class TestOriginTrialPolicy : public OriginTrialPolicy {
1869 public:
1870 base::StringPiece GetPublicKey() const override {
1871 return base::StringPiece(reinterpret_cast<const char*>(kTestPublicKey),
1872 arraysize(kTestPublicKey));
1873 }
1874 bool IsFeatureDisabled(base::StringPiece feature) const override {
1875 return false;
1876 }
1877 };
1878 class TestContentClient : public ContentClient {
1879 public:
1880 // ContentRendererClient methods
1881 OriginTrialPolicy* GetOriginTrialPolicy() override {
1882 return &origin_trial_policy_;
1883 }
1884
1885 private:
1886 TestOriginTrialPolicy origin_trial_policy_;
1887 };
1888 TestContentClient test_content_client_;
1889 };
1890
1891 TEST_F(ServiceWorkerStorageOriginTrialsDiskTest, FromMainScript) {
1892 LazyInitialize();
1893 const GURL kScope("https://valid.example.com/scope");
1894 const GURL kScript("https://valid.example.com/script.js");
1895 const int64_t kRegistrationId = 1;
1896 const int64_t kVersionId = 1;
1897 scoped_refptr<ServiceWorkerRegistration> registration =
1898 new ServiceWorkerRegistration(kScope, kRegistrationId,
1899 context()->AsWeakPtr());
1900 scoped_refptr<ServiceWorkerVersion> version = new ServiceWorkerVersion(
1901 registration.get(), kScript, kVersionId, context()->AsWeakPtr());
1902
1903 net::HttpResponseInfo http_info;
1904 http_info.ssl_info.cert =
1905 net::ImportCertFromFile(net::GetTestCertsDirectory(), "ok_cert.pem");
1906 EXPECT_TRUE(http_info.ssl_info.is_valid());
1907 http_info.ssl_info.security_bits = 0x100;
1908 // SSL3 TLS_DHE_RSA_WITH_AES_256_CBC_SHA
1909 http_info.ssl_info.connection_status = 0x300039;
1910
1911 const std::string kHTTPHeaderLine("HTTP/1.1 200 OK\n\n");
1912 const std::string kOriginTrial("Origin-Trial: ");
1913 // Token for Feature1 which expires 2033-05-18.
1914 // generate_token.py valid.example.com Feature1 --expire-timestamp=2000000000
1915 // TODO(horo): Generate this sample token during the build.
1916 const std::string kFeature1Token(
1917 "AtiUXksymWhTv5ipBE7853JytiYb0RMj3wtEBjqu3PeufQPwV1oEaNjHt4R/oEBfcK0UiWlA"
1918 "P2b9BE2/eThqcAYAAABYeyJvcmlnaW4iOiAiaHR0cHM6Ly92YWxpZC5leGFtcGxlLmNvbTo0"
1919 "NDMiLCAiZmVhdHVyZSI6ICJGZWF0dXJlMSIsICJleHBpcnkiOiAyMDAwMDAwMDAwfQ==");
1920 // Token for Feature2 which expires 2033-05-18.
1921 // generate_token.py valid.example.com Feature2 --expire-timestamp=2000000000
1922 // TODO(horo): Generate this sample token during the build.
1923 const std::string kFeature2Token1(
1924 "ApmHVC6Dpez0KQNBy13o6cGuoB5AgzOLN0keQMyAN5mjebCwR0MA8/IyjKQIlyom2RuJVg/u"
1925 "LmnqEpldfewkbA8AAABYeyJvcmlnaW4iOiAiaHR0cHM6Ly92YWxpZC5leGFtcGxlLmNvbTo0"
1926 "NDMiLCAiZmVhdHVyZSI6ICJGZWF0dXJlMiIsICJleHBpcnkiOiAyMDAwMDAwMDAwfQ==");
1927 // Token for Feature2 which expires 2036-07-18.
1928 // generate_token.py valid.example.com Feature2 --expire-timestamp=2100000000
1929 // TODO(horo): Generate this sample token during the build.
1930 const std::string kFeature2Token2(
1931 "AmV2SSxrYstE2zSwZToy7brAbIJakd146apC/6+VDflLmc5yDfJlHGILe5+ZynlcliG7clOR"
1932 "fHhXCzS5Lh1v4AAAAABYeyJvcmlnaW4iOiAiaHR0cHM6Ly92YWxpZC5leGFtcGxlLmNvbTo0"
1933 "NDMiLCAiZmVhdHVyZSI6ICJGZWF0dXJlMiIsICJleHBpcnkiOiAyMTAwMDAwMDAwfQ==");
1934 // Token for Feature3 which expired 2001-09-09.
1935 // generate_token.py valid.example.com Feature3 --expire-timestamp=1000000000
1936 const std::string kFeature3ExpiredToken(
1937 "AtSAc03z4qvid34W4MHMxyRFUJKlubZ+P5cs5yg6EiBWcagVbnm5uBgJMJN34pag7D5RywGV"
1938 "ol2RFf+4Sdm1hQ4AAABYeyJvcmlnaW4iOiAiaHR0cHM6Ly92YWxpZC5leGFtcGxlLmNvbTo0"
1939 "NDMiLCAiZmVhdHVyZSI6ICJGZWF0dXJlMyIsICJleHBpcnkiOiAxMDAwMDAwMDAwfQ==");
1940 http_info.headers = make_scoped_refptr(new net::HttpResponseHeaders(""));
1941 http_info.headers->AddHeader(kOriginTrial + kFeature1Token);
1942 http_info.headers->AddHeader(kOriginTrial + kFeature2Token1);
1943 http_info.headers->AddHeader(kOriginTrial + kFeature2Token2);
1944 http_info.headers->AddHeader(kOriginTrial + kFeature3ExpiredToken);
1945 version->SetMainScriptHttpResponseInfo(http_info);
1946 ASSERT_TRUE(version->origin_trial_tokens());
1947 const TrialTokenValidator::FeatureToTokensMap& tokens =
1948 *version->origin_trial_tokens();
1949 ASSERT_EQ(2UL, tokens.size());
1950 ASSERT_EQ(1UL, tokens.at("Feature1").size());
1951 EXPECT_EQ(kFeature1Token, tokens.at("Feature1")[0]);
1952 ASSERT_EQ(2UL, tokens.at("Feature2").size());
1953 EXPECT_EQ(kFeature2Token1, tokens.at("Feature2")[0]);
1954 EXPECT_EQ(kFeature2Token2, tokens.at("Feature2")[1]);
1955
1956 std::vector<ServiceWorkerDatabase::ResourceRecord> record;
1957 record.push_back(ServiceWorkerDatabase::ResourceRecord(1, kScript, 100));
1958 version->script_cache_map()->SetResources(record);
1959 version->set_fetch_handler_existence(
1960 ServiceWorkerVersion::FetchHandlerExistence::EXISTS);
1961 version->SetStatus(ServiceWorkerVersion::INSTALLED);
1962 registration->SetActiveVersion(version);
1963
1964 EXPECT_EQ(SERVICE_WORKER_OK, StoreRegistration(registration, version));
1965
1966 // Simulate browser shutdown and restart.
1967 registration = nullptr;
1968 version = nullptr;
1969 InitializeTestHelper();
1970 LazyInitialize();
1971
1972 scoped_refptr<ServiceWorkerRegistration> found_registration;
1973 EXPECT_EQ(SERVICE_WORKER_OK,
1974 FindRegistrationForDocument(kScope, &found_registration));
1975 ASSERT_TRUE(found_registration->active_version());
1976 const TrialTokenValidator::FeatureToTokensMap& found_tokens =
1977 *found_registration->active_version()->origin_trial_tokens();
1978 ASSERT_EQ(2UL, found_tokens.size());
1979 ASSERT_EQ(1UL, found_tokens.at("Feature1").size());
1980 EXPECT_EQ(kFeature1Token, found_tokens.at("Feature1")[0]);
1981 ASSERT_EQ(2UL, found_tokens.at("Feature2").size());
1982 EXPECT_EQ(kFeature2Token1, found_tokens.at("Feature2")[0]);
1983 EXPECT_EQ(kFeature2Token2, found_tokens.at("Feature2")[1]);
1984 }
1985
1765 } // namespace content 1986 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698