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

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 iclelland'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 private
51 // key (use this to generate new samples for this test file) is:
52 //
53 // 0x83, 0x67, 0xf4, 0xcd, 0x2a, 0x1f, 0x0e, 0x04, 0x0d, 0x43, 0x13,
54 // 0x4c, 0x67, 0xc4, 0xf4, 0x28, 0xc9, 0x90, 0x15, 0x02, 0xe2, 0xba,
55 // 0xfd, 0xbb, 0xfa, 0xbc, 0x92, 0x76, 0x8a, 0x2c, 0x4b, 0xc7, 0x75,
56 // 0x10, 0xac, 0xf9, 0x3a, 0x1c, 0xb8, 0xa9, 0x28, 0x70, 0xd2, 0x9a,
57 // 0xd0, 0x0b, 0x59, 0xe1, 0xac, 0x2b, 0xb7, 0xd5, 0xca, 0x1f, 0x64,
58 // 0x90, 0x08, 0x8e, 0xa8, 0xe0, 0x56, 0x3a, 0x04, 0xd0
59 const uint8_t kTestPublicKey[] = {
60 0x75, 0x10, 0xac, 0xf9, 0x3a, 0x1c, 0xb8, 0xa9, 0x28, 0x70, 0xd2,
61 0x9a, 0xd0, 0x0b, 0x59, 0xe1, 0xac, 0x2b, 0xb7, 0xd5, 0xca, 0x1f,
62 0x64, 0x90, 0x08, 0x8e, 0xa8, 0xe0, 0x56, 0x3a, 0x04, 0xd0,
63 };
64
45 void StatusAndQuitCallback(ServiceWorkerStatusCode* result, 65 void StatusAndQuitCallback(ServiceWorkerStatusCode* result,
46 const base::Closure& quit_closure, 66 const base::Closure& quit_closure,
47 ServiceWorkerStatusCode status) { 67 ServiceWorkerStatusCode status) {
48 *result = status; 68 *result = status;
49 quit_closure.Run(); 69 quit_closure.Run();
50 } 70 }
51 71
52 void StatusCallback(bool* was_called, 72 void StatusCallback(bool* was_called,
53 ServiceWorkerStatusCode* result, 73 ServiceWorkerStatusCode* result,
54 ServiceWorkerStatusCode status) { 74 ServiceWorkerStatusCode status) {
(...skipping 1700 matching lines...) Expand 10 before | Expand all | Expand 10 after
1755 1775
1756 // Remove other registration at first origin. 1776 // Remove other registration at first origin.
1757 EXPECT_EQ(SERVICE_WORKER_OK, 1777 EXPECT_EQ(SERVICE_WORKER_OK,
1758 DeleteRegistration(kRegistrationId2, kScope2.GetOrigin())); 1778 DeleteRegistration(kRegistrationId2, kScope2.GetOrigin()));
1759 1779
1760 // No foreign fetch registrations remain. 1780 // No foreign fetch registrations remain.
1761 EXPECT_FALSE(storage()->OriginHasForeignFetchRegistrations(kOrigin1)); 1781 EXPECT_FALSE(storage()->OriginHasForeignFetchRegistrations(kOrigin1));
1762 EXPECT_FALSE(storage()->OriginHasForeignFetchRegistrations(kOrigin2)); 1782 EXPECT_FALSE(storage()->OriginHasForeignFetchRegistrations(kOrigin2));
1763 } 1783 }
1764 1784
1785 class ServiceWorkerStorageOriginTrialsTest : public ServiceWorkerStorageTest {
1786 public:
1787 ServiceWorkerStorageOriginTrialsTest() {}
1788 ~ServiceWorkerStorageOriginTrialsTest() override {}
1789
1790 protected:
1791 void WriteRegistration(const RegistrationData& registration,
1792 const std::vector<ResourceRecord>& resources) {
1793 ServiceWorkerDatabase::RegistrationData deleted_version;
1794 std::vector<int64_t> newly_purgeable_resources;
1795
1796 ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK,
1797 storage()->database_->WriteRegistration(
1798 registration, resources, &deleted_version,
1799 &newly_purgeable_resources));
1800 }
1801 };
1802
1803 TEST_F(ServiceWorkerStorageOriginTrialsTest, AbsentEntryAndEmptyEntry) {
1804 const GURL origin1("http://www1.example.com");
1805 const GURL scope1("http://www1.example.com/foo/");
1806 RegistrationData data1;
1807 data1.registration_id = 100;
1808 data1.scope = scope1;
1809 data1.script = GURL(origin1.spec() + "/script.js");
1810 data1.version_id = 1000;
1811 data1.is_active = true;
1812 data1.resources_total_size_bytes = 100;
1813 // Don't set origin_trial_tokens to simulate old database entry.
1814 std::vector<ServiceWorkerDatabase::ResourceRecord> resources1;
1815 resources1.push_back(
1816 ServiceWorkerDatabase::ResourceRecord(1, data1.script, 100));
1817 WriteRegistration(data1, resources1);
1818
1819 const GURL origin2("http://www2.example.com");
1820 const GURL scope2("http://www2.example.com/foo/");
1821 RegistrationData data2;
1822 data2.registration_id = 200;
1823 data2.scope = scope2;
1824 data2.script = GURL(origin2.spec() + "/script.js");
1825 data2.version_id = 2000;
1826 data2.is_active = true;
1827 data2.resources_total_size_bytes = 200;
1828 // Set empty origin_trial_tokens.
1829 data2.origin_trial_tokens = TrialTokenValidator::FeatureToTokensMap();
1830 std::vector<ServiceWorkerDatabase::ResourceRecord> resources2;
1831 resources2.push_back(
1832 ServiceWorkerDatabase::ResourceRecord(2, data2.script, 200));
1833 WriteRegistration(data2, resources2);
1834
1835 scoped_refptr<ServiceWorkerRegistration> found_registration;
1836
1837 EXPECT_EQ(SERVICE_WORKER_OK,
1838 FindRegistrationForDocument(scope1, &found_registration));
1839 ASSERT_TRUE(found_registration->active_version());
1840 // origin_trial_tokens must be unset.
1841 EXPECT_FALSE(found_registration->active_version()->origin_trial_tokens());
1842
1843 EXPECT_EQ(SERVICE_WORKER_OK,
1844 FindRegistrationForDocument(scope2, &found_registration));
1845 ASSERT_TRUE(found_registration->active_version());
1846 // Empty origin_trial_tokens must exist.
1847 ASSERT_TRUE(found_registration->active_version()->origin_trial_tokens());
1848 EXPECT_TRUE(
1849 found_registration->active_version()->origin_trial_tokens()->empty());
1850 }
1851
1852 class ServiceWorkerStorageOriginTrialsDiskTest
1853 : public ServiceWorkerStorageTest {
1854 public:
1855 ServiceWorkerStorageOriginTrialsDiskTest() {
1856 SetContentClient(&test_content_client_);
1857 }
1858 ~ServiceWorkerStorageOriginTrialsDiskTest() override {
1859 SetContentClient(nullptr);
1860 }
1861 void SetUp() override {
1862 ASSERT_TRUE(InitUserDataDirectory());
1863 ServiceWorkerStorageTest::SetUp();
1864 }
1865
1866 private:
1867 class TestOriginTrialPolicy : public OriginTrialPolicy {
1868 public:
1869 base::StringPiece GetPublicKey() const override {
1870 return base::StringPiece(reinterpret_cast<const char*>(kTestPublicKey),
1871 arraysize(kTestPublicKey));
1872 }
1873 bool IsFeatureDisabled(base::StringPiece feature) const override {
1874 return false;
1875 }
1876 };
1877 class TestContentClient : public ContentClient {
1878 public:
1879 // ContentRendererClient methods
1880 OriginTrialPolicy* GetOriginTrialPolicy() override {
1881 return &origin_trial_policy_;
1882 }
1883
1884 private:
1885 TestOriginTrialPolicy origin_trial_policy_;
1886 };
1887 TestContentClient test_content_client_;
1888 };
1889
1890 TEST_F(ServiceWorkerStorageOriginTrialsDiskTest, FromMainScript) {
1891 LazyInitialize();
1892 const GURL kScope("https://valid.example.com/scope");
1893 const GURL kScript("https://valid.example.com/script.js");
1894 const int64_t kRegistrationId = 1;
1895 const int64_t kVersionId = 1;
1896 scoped_refptr<ServiceWorkerRegistration> registration =
1897 new ServiceWorkerRegistration(kScope, kRegistrationId,
1898 context()->AsWeakPtr());
1899 scoped_refptr<ServiceWorkerVersion> version = new ServiceWorkerVersion(
1900 registration.get(), kScript, kVersionId, context()->AsWeakPtr());
1901
1902 net::HttpResponseInfo http_info;
1903 http_info.ssl_info.cert =
1904 net::ImportCertFromFile(net::GetTestCertsDirectory(), "ok_cert.pem");
1905 EXPECT_TRUE(http_info.ssl_info.is_valid());
1906 http_info.ssl_info.security_bits = 0x100;
1907 // SSL3 TLS_DHE_RSA_WITH_AES_256_CBC_SHA
1908 http_info.ssl_info.connection_status = 0x300039;
1909
1910 const std::string kHTTPHeaderLine("HTTP/1.1 200 OK\n\n");
1911 const std::string kOriginTrial("Origin-Trial: ");
1912 // Token for Feature1 which expires 2033-05-18.
1913 // generate_token.py valid.example.com Feature1 --expire-timestamp=2000000000
1914 // TODO(horo): Generate this sample token during the build.
1915 const std::string kFeature1Token(
1916 "AtiUXksymWhTv5ipBE7853JytiYb0RMj3wtEBjqu3PeufQPwV1oEaNjHt4R/oEBfcK0UiWlA"
1917 "P2b9BE2/eThqcAYAAABYeyJvcmlnaW4iOiAiaHR0cHM6Ly92YWxpZC5leGFtcGxlLmNvbTo0"
1918 "NDMiLCAiZmVhdHVyZSI6ICJGZWF0dXJlMSIsICJleHBpcnkiOiAyMDAwMDAwMDAwfQ==");
1919 // Token for Feature2 which expires 2033-05-18.
1920 // generate_token.py valid.example.com Feature2 --expire-timestamp=2000000000
1921 // TODO(horo): Generate this sample token during the build.
1922 const std::string kFeature2Token1(
1923 "ApmHVC6Dpez0KQNBy13o6cGuoB5AgzOLN0keQMyAN5mjebCwR0MA8/IyjKQIlyom2RuJVg/u"
1924 "LmnqEpldfewkbA8AAABYeyJvcmlnaW4iOiAiaHR0cHM6Ly92YWxpZC5leGFtcGxlLmNvbTo0"
1925 "NDMiLCAiZmVhdHVyZSI6ICJGZWF0dXJlMiIsICJleHBpcnkiOiAyMDAwMDAwMDAwfQ==");
1926 // Token for Feature2 which expires 2036-07-18.
1927 // generate_token.py valid.example.com Feature2 --expire-timestamp=2100000000
1928 // TODO(horo): Generate this sample token during the build.
1929 const std::string kFeature2Token2(
1930 "AmV2SSxrYstE2zSwZToy7brAbIJakd146apC/6+VDflLmc5yDfJlHGILe5+ZynlcliG7clOR"
1931 "fHhXCzS5Lh1v4AAAAABYeyJvcmlnaW4iOiAiaHR0cHM6Ly92YWxpZC5leGFtcGxlLmNvbTo0"
1932 "NDMiLCAiZmVhdHVyZSI6ICJGZWF0dXJlMiIsICJleHBpcnkiOiAyMTAwMDAwMDAwfQ==");
1933 // Token for Feature3 which expired 2001-09-09.
1934 // generate_token.py valid.example.com Feature3 --expire-timestamp=1000000000
1935 const std::string kFeature3ExpiredToken(
1936 "AtSAc03z4qvid34W4MHMxyRFUJKlubZ+P5cs5yg6EiBWcagVbnm5uBgJMJN34pag7D5RywGV"
1937 "ol2RFf+4Sdm1hQ4AAABYeyJvcmlnaW4iOiAiaHR0cHM6Ly92YWxpZC5leGFtcGxlLmNvbTo0"
1938 "NDMiLCAiZmVhdHVyZSI6ICJGZWF0dXJlMyIsICJleHBpcnkiOiAxMDAwMDAwMDAwfQ==");
1939 http_info.headers = make_scoped_refptr(new net::HttpResponseHeaders(""));
1940 http_info.headers->AddHeader(kOriginTrial + kFeature1Token);
1941 http_info.headers->AddHeader(kOriginTrial + kFeature2Token1);
1942 http_info.headers->AddHeader(kOriginTrial + kFeature2Token2);
1943 http_info.headers->AddHeader(kOriginTrial + kFeature3ExpiredToken);
1944 version->SetMainScriptHttpResponseInfo(http_info);
1945 ASSERT_TRUE(version->origin_trial_tokens());
1946 const TrialTokenValidator::FeatureToTokensMap& tokens =
1947 *version->origin_trial_tokens();
1948 ASSERT_EQ(2UL, tokens.size());
1949 ASSERT_EQ(1UL, tokens.at("Feature1").size());
1950 EXPECT_EQ(kFeature1Token, tokens.at("Feature1")[0]);
1951 ASSERT_EQ(2UL, tokens.at("Feature2").size());
1952 EXPECT_EQ(kFeature2Token1, tokens.at("Feature2")[0]);
1953 EXPECT_EQ(kFeature2Token2, tokens.at("Feature2")[1]);
1954
1955 std::vector<ServiceWorkerDatabase::ResourceRecord> record;
1956 record.push_back(ServiceWorkerDatabase::ResourceRecord(1, kScript, 100));
1957 version->script_cache_map()->SetResources(record);
1958 version->set_fetch_handler_existence(
1959 ServiceWorkerVersion::FetchHandlerExistence::EXISTS);
1960 version->SetStatus(ServiceWorkerVersion::INSTALLED);
1961 registration->SetActiveVersion(version);
1962
1963 EXPECT_EQ(SERVICE_WORKER_OK, StoreRegistration(registration, version));
1964
1965 // Simulate browser shutdown and restart.
1966 registration = nullptr;
1967 version = nullptr;
1968 InitializeTestHelper();
1969 LazyInitialize();
1970
1971 scoped_refptr<ServiceWorkerRegistration> found_registration;
1972 EXPECT_EQ(SERVICE_WORKER_OK,
1973 FindRegistrationForDocument(kScope, &found_registration));
1974 ASSERT_TRUE(found_registration->active_version());
1975 const TrialTokenValidator::FeatureToTokensMap& found_tokens =
1976 *found_registration->active_version()->origin_trial_tokens();
1977 ASSERT_EQ(2UL, found_tokens.size());
1978 ASSERT_EQ(1UL, found_tokens.at("Feature1").size());
1979 EXPECT_EQ(kFeature1Token, found_tokens.at("Feature1")[0]);
1980 ASSERT_EQ(2UL, found_tokens.at("Feature2").size());
1981 EXPECT_EQ(kFeature2Token1, found_tokens.at("Feature2")[0]);
1982 EXPECT_EQ(kFeature2Token2, found_tokens.at("Feature2")[1]);
1983 }
1984
1765 } // namespace content 1985 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698