OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "chrome/browser/extensions/api/messaging/native_messaging_host_manifest
.h" |
| 6 |
| 7 #include "base/file_path.h" |
| 8 #include "base/file_util.h" |
| 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "extensions/common/url_pattern_set.h" |
| 11 #include "googleurl/src/gurl.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace extensions { |
| 15 |
| 16 class NativeMessagingHostManifestTest : public ::testing::Test { |
| 17 public: |
| 18 virtual void SetUp() OVERRIDE { |
| 19 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 20 manifest_path_ = temp_dir_.path().AppendASCII("test.json"); |
| 21 } |
| 22 |
| 23 protected: |
| 24 bool WriteManifest(const char* manifest_content) { |
| 25 return file_util::WriteFile( |
| 26 manifest_path_, manifest_content, strlen(manifest_content)); |
| 27 } |
| 28 |
| 29 base::ScopedTempDir temp_dir_; |
| 30 base::FilePath manifest_path_; |
| 31 }; |
| 32 |
| 33 TEST_F(NativeMessagingHostManifestTest, HostNameValidation) { |
| 34 EXPECT_TRUE(NativeMessagingHostManifest::IsValidHostName("a")); |
| 35 EXPECT_TRUE(NativeMessagingHostManifest::IsValidHostName("foo")); |
| 36 EXPECT_TRUE(NativeMessagingHostManifest::IsValidHostName("foo132")); |
| 37 EXPECT_TRUE(NativeMessagingHostManifest::IsValidHostName("foo.bar")); |
| 38 EXPECT_TRUE(NativeMessagingHostManifest::IsValidHostName("foo.bar2")); |
| 39 EXPECT_TRUE(NativeMessagingHostManifest::IsValidHostName("a._.c")); |
| 40 EXPECT_TRUE(NativeMessagingHostManifest::IsValidHostName("a._.c")); |
| 41 EXPECT_FALSE(NativeMessagingHostManifest::IsValidHostName("A.b")); |
| 42 EXPECT_FALSE(NativeMessagingHostManifest::IsValidHostName("a..b")); |
| 43 EXPECT_FALSE(NativeMessagingHostManifest::IsValidHostName(".a")); |
| 44 EXPECT_FALSE(NativeMessagingHostManifest::IsValidHostName("b.")); |
| 45 EXPECT_FALSE(NativeMessagingHostManifest::IsValidHostName("a*")); |
| 46 } |
| 47 |
| 48 TEST_F(NativeMessagingHostManifestTest, LoadValid) { |
| 49 ASSERT_TRUE(WriteManifest( |
| 50 "{" |
| 51 " \"name\": \"com.chrome.test.native_host\"," |
| 52 " \"description\": \"Native Messaging Test\"," |
| 53 " \"path\": \"/usr/bin/host\"," |
| 54 " \"type\": \"stdio\"," |
| 55 " \"allowed_origins\": [" |
| 56 " \"chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/\"" |
| 57 " ]" |
| 58 "}")); |
| 59 |
| 60 std::string error_message; |
| 61 scoped_ptr<NativeMessagingHostManifest> manifest = |
| 62 NativeMessagingHostManifest::Load(manifest_path_, &error_message); |
| 63 ASSERT_TRUE(manifest) << "Failed to load manifest: " << error_message; |
| 64 EXPECT_TRUE(error_message.empty()); |
| 65 |
| 66 EXPECT_EQ(manifest->name(), "com.chrome.test.native_host"); |
| 67 EXPECT_EQ(manifest->description(), "Native Messaging Test"); |
| 68 EXPECT_EQ(manifest->interface(), |
| 69 NativeMessagingHostManifest::HOST_INTERFACE_STDIO); |
| 70 EXPECT_EQ(manifest->path(), base::FilePath::FromUTF8Unsafe("/usr/bin/host")); |
| 71 EXPECT_TRUE(manifest->allowed_origins().MatchesSecurityOrigin( |
| 72 GURL("chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/"))); |
| 73 EXPECT_FALSE(manifest->allowed_origins().MatchesSecurityOrigin( |
| 74 GURL("chrome-extension://jnldjmfmopnpolahpmmgbagdohdnhkik/"))); |
| 75 } |
| 76 |
| 77 TEST_F(NativeMessagingHostManifestTest, InvalidName) { |
| 78 ASSERT_TRUE(WriteManifest( |
| 79 "{" |
| 80 " \"name\": \".com.chrome.test.native_host\"," |
| 81 " \"description\": \"Native Messaging Test\"," |
| 82 " \"path\": \"/usr/bin/host\"," |
| 83 " \"type\": \"stdio\"," |
| 84 " \"allowed_origins\": [" |
| 85 " \"chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/\"" |
| 86 " ]" |
| 87 "}")); |
| 88 |
| 89 std::string error_message; |
| 90 scoped_ptr<NativeMessagingHostManifest> manifest = |
| 91 NativeMessagingHostManifest::Load(manifest_path_, &error_message); |
| 92 ASSERT_FALSE(manifest); |
| 93 EXPECT_FALSE(error_message.empty()); |
| 94 } |
| 95 |
| 96 TEST_F(NativeMessagingHostManifestTest, MatchAllOrigin) { |
| 97 // Verify that match-all origins are rejected. |
| 98 ASSERT_TRUE(WriteManifest( |
| 99 "{" |
| 100 " \"name\": \"com.chrome.test.native_host\"," |
| 101 " \"description\": \"Native Messaging Test\"," |
| 102 " \"path\": \"/usr/bin/host\"," |
| 103 " \"type\": \"stdio\"," |
| 104 " \"allowed_origins\": [" |
| 105 " \"chrome-extension://*/\"" |
| 106 " ]" |
| 107 "}")); |
| 108 |
| 109 std::string error_message; |
| 110 scoped_ptr<NativeMessagingHostManifest> manifest = |
| 111 NativeMessagingHostManifest::Load(manifest_path_, &error_message); |
| 112 ASSERT_FALSE(manifest); |
| 113 EXPECT_FALSE(error_message.empty()); |
| 114 } |
| 115 |
| 116 } // namespace extensions |
OLD | NEW |