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