Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / extensions / api / messaging / native_messaging_host_manifest_unittest.cc
blob41551faf2ab9dfdafaa21d4d0a87fdfd22dd3255
1 // Copyright (c) 2013 The Chromium Authors. 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 "chrome/browser/extensions/api/messaging/native_messaging_host_manifest.h"
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/json/string_escape.h"
11 #include "extensions/common/url_pattern_set.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "url/gurl.h"
15 namespace extensions {
17 const char kTestHostName[] = "com.chrome.test.native_host";
18 #if defined(OS_WIN)
19 const char kTestHostPath[] = "C:\\ProgramFiles\\host.exe";
20 #else
21 const char kTestHostPath[] = "/usr/bin/host";
22 #endif
23 const char kTestOrigin[] =
24 "chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/";
26 class NativeMessagingHostManifestTest : public ::testing::Test {
27 public:
28 void SetUp() override {
29 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
30 manifest_path_ = temp_dir_.path().AppendASCII("test.json");
33 protected:
34 bool WriteManifest(const std::string& name,
35 const std::string& path,
36 const std::string& origin) {
37 return WriteManifest("{"
38 " \"name\": \"" + name + "\","
39 " \"description\": \"Native Messaging Test\","
40 " \"path\": " + base::GetQuotedJSONString(path) + ","
41 " \"type\": \"stdio\","
42 " \"allowed_origins\": ["
43 " \"" + origin + "\""
44 " ]"
45 "}");
48 bool WriteManifest(const std::string& manifest_content) {
49 return base::WriteFile(
50 manifest_path_, manifest_content.data(), manifest_content.size());
53 base::ScopedTempDir temp_dir_;
54 base::FilePath manifest_path_;
57 TEST_F(NativeMessagingHostManifestTest, HostNameValidation) {
58 EXPECT_TRUE(NativeMessagingHostManifest::IsValidName("a"));
59 EXPECT_TRUE(NativeMessagingHostManifest::IsValidName("foo"));
60 EXPECT_TRUE(NativeMessagingHostManifest::IsValidName("foo132"));
61 EXPECT_TRUE(NativeMessagingHostManifest::IsValidName("foo.bar"));
62 EXPECT_TRUE(NativeMessagingHostManifest::IsValidName("foo.bar2"));
63 EXPECT_TRUE(NativeMessagingHostManifest::IsValidName("a._.c"));
64 EXPECT_TRUE(NativeMessagingHostManifest::IsValidName("a._.c"));
65 EXPECT_FALSE(NativeMessagingHostManifest::IsValidName("A.b"));
66 EXPECT_FALSE(NativeMessagingHostManifest::IsValidName("a..b"));
67 EXPECT_FALSE(NativeMessagingHostManifest::IsValidName(".a"));
68 EXPECT_FALSE(NativeMessagingHostManifest::IsValidName("b."));
69 EXPECT_FALSE(NativeMessagingHostManifest::IsValidName("a*"));
72 TEST_F(NativeMessagingHostManifestTest, LoadValid) {
73 ASSERT_TRUE(WriteManifest(kTestHostName, kTestHostPath, kTestOrigin));
75 std::string error_message;
76 scoped_ptr<NativeMessagingHostManifest> manifest =
77 NativeMessagingHostManifest::Load(manifest_path_, &error_message);
78 ASSERT_TRUE(manifest) << "Failed to load manifest: " << error_message;
79 EXPECT_TRUE(error_message.empty());
81 EXPECT_EQ(manifest->name(), "com.chrome.test.native_host");
82 EXPECT_EQ(manifest->description(), "Native Messaging Test");
83 EXPECT_EQ(manifest->interface(),
84 NativeMessagingHostManifest::HOST_INTERFACE_STDIO);
85 EXPECT_EQ(manifest->path(), base::FilePath::FromUTF8Unsafe(kTestHostPath));
86 EXPECT_TRUE(manifest->allowed_origins().MatchesSecurityOrigin(
87 GURL("chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/")));
88 EXPECT_FALSE(manifest->allowed_origins().MatchesSecurityOrigin(
89 GURL("chrome-extension://jnldjmfmopnpolahpmmgbagdohdnhkik/")));
92 TEST_F(NativeMessagingHostManifestTest, InvalidName) {
93 ASSERT_TRUE(WriteManifest(".com.chrome.test.native_host",
94 kTestHostPath, kTestOrigin));
96 std::string error_message;
97 scoped_ptr<NativeMessagingHostManifest> manifest =
98 NativeMessagingHostManifest::Load(manifest_path_, &error_message);
99 ASSERT_FALSE(manifest);
100 EXPECT_FALSE(error_message.empty());
103 // Verify that match-all origins are rejected.
104 TEST_F(NativeMessagingHostManifestTest, MatchAllOrigin) {
105 ASSERT_TRUE(WriteManifest(kTestHostName, kTestHostPath,
106 "chrome-extension://*/"));
108 std::string error_message;
109 scoped_ptr<NativeMessagingHostManifest> manifest =
110 NativeMessagingHostManifest::Load(manifest_path_, &error_message);
111 ASSERT_FALSE(manifest);
112 EXPECT_FALSE(error_message.empty());
115 } // namespace extensions