Index: chrome/browser/extensions/api/messaging/native_messaging_host_manifest_unittest.cc |
diff --git a/chrome/browser/extensions/api/messaging/native_messaging_host_manifest_unittest.cc b/chrome/browser/extensions/api/messaging/native_messaging_host_manifest_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..40ecd717573f13525c10ada7e29c53c3028a9829 |
--- /dev/null |
+++ b/chrome/browser/extensions/api/messaging/native_messaging_host_manifest_unittest.cc |
@@ -0,0 +1,116 @@ |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/extensions/api/messaging/native_messaging_host_manifest.h" |
+ |
+#include "base/file_path.h" |
+#include "base/file_util.h" |
+#include "base/files/scoped_temp_dir.h" |
+#include "extensions/common/url_pattern_set.h" |
+#include "googleurl/src/gurl.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace extensions { |
+ |
+class NativeMessagingHostManifestTest : public ::testing::Test { |
+ public: |
+ virtual void SetUp() OVERRIDE { |
+ ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
+ manifest_path_ = temp_dir_.path().AppendASCII("test.json"); |
+ } |
+ |
+ protected: |
+ bool WriteManifest(const char* manifest_content) { |
+ return file_util::WriteFile( |
+ manifest_path_, manifest_content, strlen(manifest_content)); |
+ } |
+ |
+ base::ScopedTempDir temp_dir_; |
+ base::FilePath manifest_path_; |
+}; |
+ |
+TEST_F(NativeMessagingHostManifestTest, HostNameValidation) { |
+ EXPECT_TRUE(NativeMessagingHostManifest::IsValidHostName("a")); |
+ EXPECT_TRUE(NativeMessagingHostManifest::IsValidHostName("foo")); |
+ EXPECT_TRUE(NativeMessagingHostManifest::IsValidHostName("foo132")); |
+ EXPECT_TRUE(NativeMessagingHostManifest::IsValidHostName("foo.bar")); |
+ EXPECT_TRUE(NativeMessagingHostManifest::IsValidHostName("foo.bar2")); |
+ EXPECT_TRUE(NativeMessagingHostManifest::IsValidHostName("a._.c")); |
+ EXPECT_TRUE(NativeMessagingHostManifest::IsValidHostName("a._.c")); |
+ EXPECT_FALSE(NativeMessagingHostManifest::IsValidHostName("A.b")); |
+ EXPECT_FALSE(NativeMessagingHostManifest::IsValidHostName("a..b")); |
+ EXPECT_FALSE(NativeMessagingHostManifest::IsValidHostName(".a")); |
+ EXPECT_FALSE(NativeMessagingHostManifest::IsValidHostName("b.")); |
+ EXPECT_FALSE(NativeMessagingHostManifest::IsValidHostName("a*")); |
+} |
+ |
+TEST_F(NativeMessagingHostManifestTest, LoadValid) { |
+ ASSERT_TRUE(WriteManifest( |
+ "{" |
+ " \"name\": \"com.chrome.test.native_host\"," |
+ " \"description\": \"Native Messaging Test\"," |
+ " \"path\": \"/usr/bin/host\"," |
+ " \"type\": \"stdio\"," |
+ " \"allowed_origins\": [" |
+ " \"chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/\"" |
+ " ]" |
+ "}")); |
+ |
+ std::string error_message; |
+ scoped_ptr<NativeMessagingHostManifest> manifest = |
+ NativeMessagingHostManifest::Load(manifest_path_, &error_message); |
+ ASSERT_TRUE(manifest) << "Failed to load manifest: " << error_message; |
+ EXPECT_TRUE(error_message.empty()); |
+ |
+ EXPECT_EQ(manifest->name(), "com.chrome.test.native_host"); |
+ EXPECT_EQ(manifest->description(), "Native Messaging Test"); |
+ EXPECT_EQ(manifest->interface(), |
+ NativeMessagingHostManifest::HOST_INTERFACE_STDIO); |
+ EXPECT_EQ(manifest->path(), base::FilePath::FromUTF8Unsafe("/usr/bin/host")); |
+ EXPECT_TRUE(manifest->allowed_origins().MatchesSecurityOrigin( |
+ GURL("chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/"))); |
+ EXPECT_FALSE(manifest->allowed_origins().MatchesSecurityOrigin( |
+ GURL("chrome-extension://jnldjmfmopnpolahpmmgbagdohdnhkik/"))); |
+} |
+ |
+TEST_F(NativeMessagingHostManifestTest, InvalidName) { |
+ ASSERT_TRUE(WriteManifest( |
+ "{" |
+ " \"name\": \".com.chrome.test.native_host\"," |
+ " \"description\": \"Native Messaging Test\"," |
+ " \"path\": \"/usr/bin/host\"," |
+ " \"type\": \"stdio\"," |
+ " \"allowed_origins\": [" |
+ " \"chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/\"" |
+ " ]" |
+ "}")); |
+ |
+ std::string error_message; |
+ scoped_ptr<NativeMessagingHostManifest> manifest = |
+ NativeMessagingHostManifest::Load(manifest_path_, &error_message); |
+ ASSERT_FALSE(manifest); |
+ EXPECT_FALSE(error_message.empty()); |
+} |
+ |
+TEST_F(NativeMessagingHostManifestTest, MatchAllOrigin) { |
+ // Verify that match-all origins are rejected. |
+ ASSERT_TRUE(WriteManifest( |
+ "{" |
+ " \"name\": \"com.chrome.test.native_host\"," |
+ " \"description\": \"Native Messaging Test\"," |
+ " \"path\": \"/usr/bin/host\"," |
+ " \"type\": \"stdio\"," |
+ " \"allowed_origins\": [" |
+ " \"chrome-extension://*/\"" |
+ " ]" |
+ "}")); |
+ |
+ std::string error_message; |
+ scoped_ptr<NativeMessagingHostManifest> manifest = |
+ NativeMessagingHostManifest::Load(manifest_path_, &error_message); |
+ ASSERT_FALSE(manifest); |
+ EXPECT_FALSE(error_message.empty()); |
+} |
+ |
+} // namespace extensions |