1 // Copyright (c) 2013 Intel Corporation. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "xwalk/application/common/manifest_handlers/csp_handler.h"
7 #include "xwalk/application/common/application_manifest_constants.h"
8 #include "testing/gtest/include/gtest/gtest.h"
12 namespace keys = application_manifest_keys;
13 namespace widget_keys = application_widget_keys;
15 namespace application {
17 class CSPHandlerTest: public testing::Test {
19 scoped_refptr<ApplicationData> CreateApplication() {
21 scoped_refptr<ApplicationData> app_data = ApplicationData::Create(
22 base::FilePath(), std::string(), ApplicationData::LOCAL_DIRECTORY,
23 make_scoped_ptr(new Manifest(make_scoped_ptr(manifest.DeepCopy()))),
28 const CSPInfo* GetCSPInfo(
29 scoped_refptr<ApplicationData> application) {
30 const CSPInfo* info = static_cast<CSPInfo*>(
31 application->GetManifestData(GetCSPKey(application->manifest_type())));
35 base::DictionaryValue manifest;
38 // FIXME: the default CSP policy settings in CSP manifest handler
39 // are temporally removed, since they had affected some tests and legacy apps.
40 TEST_F(CSPHandlerTest, DISABLED_NoCSP) {
41 manifest.SetString(keys::kNameKey, "no name");
42 manifest.SetString(keys::kXWalkVersionKey, "0");
43 scoped_refptr<ApplicationData> application = CreateApplication();
44 EXPECT_TRUE(application.get());
45 EXPECT_EQ(GetCSPInfo(application)->GetDirectives().size(), 2);
48 TEST_F(CSPHandlerTest, EmptyCSP) {
49 manifest.SetString(keys::kNameKey, "no name");
50 manifest.SetString(keys::kXWalkVersionKey, "0");
51 manifest.SetString(keys::kCSPKey, "");
52 scoped_refptr<ApplicationData> application = CreateApplication();
53 EXPECT_TRUE(application.get());
54 EXPECT_EQ(GetCSPInfo(application)->GetDirectives().size(), 0);
57 TEST_F(CSPHandlerTest, CSP) {
58 manifest.SetString(keys::kNameKey, "no name");
59 manifest.SetString(keys::kXWalkVersionKey, "0");
60 manifest.SetString(keys::kCSPKey, "default-src 'self' ");
61 scoped_refptr<ApplicationData> application = CreateApplication();
62 EXPECT_TRUE(application.get());
63 const std::map<std::string, std::vector<std::string> >& policies =
64 GetCSPInfo(application)->GetDirectives();
65 EXPECT_EQ(policies.size(), 1);
66 std::map<std::string, std::vector<std::string> >::const_iterator it =
67 policies.find("default-src");
68 ASSERT_NE(it, policies.end());
69 EXPECT_EQ(it->second.size(), 1);
70 EXPECT_STREQ((it->second)[0].c_str(), "'self'");
74 TEST_F(CSPHandlerTest, WGTEmptyCSP) {
75 manifest.SetString(widget_keys::kNameKey, "no name");
76 manifest.SetString(widget_keys::kVersionKey, "0");
77 manifest.SetString(widget_keys::kCSPKey, "");
78 scoped_refptr<ApplicationData> application = CreateApplication();
79 EXPECT_TRUE(application.get());
80 EXPECT_TRUE(GetCSPInfo(application));
81 EXPECT_EQ(GetCSPInfo(application)->GetDirectives().size(), 0);
84 TEST_F(CSPHandlerTest, WGTCSP) {
85 manifest.SetString(widget_keys::kNameKey, "no name");
86 manifest.SetString(widget_keys::kVersionKey, "0");
87 manifest.SetString(widget_keys::kCSPKey, "default-src 'self' ");
88 scoped_refptr<ApplicationData> application = CreateApplication();
89 EXPECT_TRUE(application.get());
90 EXPECT_TRUE(GetCSPInfo(application));
91 const std::map<std::string, std::vector<std::string> >& policies =
92 GetCSPInfo(application)->GetDirectives();
93 EXPECT_EQ(policies.size(), 1);
94 std::map<std::string, std::vector<std::string> >::const_iterator it =
95 policies.find("default-src");
96 ASSERT_NE(it, policies.end());
97 EXPECT_EQ(it->second.size(), 1);
98 EXPECT_STREQ((it->second)[0].c_str(), "'self'");
102 } // namespace application