1 // Copyright 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_test_util.h" 6 7 #include "base/files/file_path.h" 8 #include "base/json/json_file_value_serializer.h" 9 #include "base/memory/scoped_ptr.h" 10 #include "base/path_service.h" 11 #include "base/strings/stringprintf.h" 12 #include "base/strings/utf_string_conversions.h" 13 #include "base/values.h" 14 #include "chrome/common/chrome_paths.h" 15 #include "testing/gtest/include/gtest/gtest.h" 16 17 #if defined(OS_WIN) 18 #include "base/win/registry.h" 19 #endif 20 21 namespace extensions { 22 23 namespace { 24 25 void WriteTestNativeHostManifest(const base::FilePath& target_dir, 26 const std::string& host_name, 27 const base::FilePath& host_path, 28 bool user_level) { 29 scoped_ptr<base::DictionaryValue> manifest(new base::DictionaryValue()); 30 manifest->SetString("name", host_name); 31 manifest->SetString("description", "Native Messaging Echo Test"); 32 manifest->SetString("type", "stdio"); 33 manifest->SetString("path", host_path.AsUTF8Unsafe()); 34 35 scoped_ptr<base::ListValue> origins(new base::ListValue()); 36 origins->AppendString(base::StringPrintf( 37 "chrome-extension://%s/", ScopedTestNativeMessagingHost::kExtensionId)); 38 manifest->Set("allowed_origins", origins.release()); 39 40 base::FilePath manifest_path = target_dir.AppendASCII(host_name + ".json"); 41 JSONFileValueSerializer serializer(manifest_path); 42 ASSERT_TRUE(serializer.Serialize(*manifest)); 43 44 #if defined(OS_WIN) 45 HKEY root_key = user_level ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE; 46 base::string16 key = L"SOFTWARE\\Google\\Chrome\\NativeMessagingHosts\\" + 47 base::UTF8ToUTF16(host_name); 48 base::win::RegKey manifest_key( 49 root_key, key.c_str(), 50 KEY_SET_VALUE | KEY_CREATE_SUB_KEY | KEY_CREATE_LINK); 51 ASSERT_EQ(ERROR_SUCCESS, 52 manifest_key.WriteValue(NULL, manifest_path.value().c_str())); 53 #endif 54 } 55 56 } // namespace 57 58 const char ScopedTestNativeMessagingHost::kHostName[] = 59 "com.google.chrome.test.echo"; 60 const char ScopedTestNativeMessagingHost::kBinaryMissingHostName[] = 61 "com.google.chrome.test.host_binary_missing"; 62 const char ScopedTestNativeMessagingHost::kExtensionId[] = 63 "knldjmfmopnpolahpmmgbagdohdnhkik"; 64 65 ScopedTestNativeMessagingHost::ScopedTestNativeMessagingHost() {} 66 67 void ScopedTestNativeMessagingHost::RegisterTestHost(bool user_level) { 68 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 69 ScopedTestNativeMessagingHost test_host; 70 71 base::FilePath test_user_data_dir; 72 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_user_data_dir)); 73 test_user_data_dir = test_user_data_dir.AppendASCII("native_messaging") 74 .AppendASCII("native_hosts"); 75 76 #if defined(OS_WIN) 77 HKEY root_key = user_level ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE; 78 registry_override_.OverrideRegistry(root_key, L"native_messaging"); 79 #else 80 path_override_.reset(new base::ScopedPathOverride( 81 user_level ? chrome::DIR_USER_NATIVE_MESSAGING 82 : chrome::DIR_NATIVE_MESSAGING, 83 temp_dir_.path())); 84 #endif 85 86 #if defined(OS_POSIX) 87 base::FilePath host_path = test_user_data_dir.AppendASCII("echo.py"); 88 #else 89 base::FilePath host_path = test_user_data_dir.AppendASCII("echo.bat"); 90 #endif 91 ASSERT_NO_FATAL_FAILURE(WriteTestNativeHostManifest( 92 temp_dir_.path(), kHostName, host_path, user_level)); 93 94 ASSERT_NO_FATAL_FAILURE(WriteTestNativeHostManifest( 95 temp_dir_.path(), kBinaryMissingHostName, 96 test_user_data_dir.AppendASCII("missing_nm_binary.exe"), user_level)); 97 } 98 99 ScopedTestNativeMessagingHost::~ScopedTestNativeMessagingHost() {} 100 101 } // namespace extensions 102