native_messaging_test_util.cc revision 868fa2fe829687343ffae624259930155e16dbd8
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/values.h"
13#include "chrome/common/chrome_paths.h"
14#include "testing/gtest/include/gtest/gtest.h"
15
16namespace extensions {
17
18const char kTestNativeMessagingHostName[] = "com.google.chrome.test.echo";
19const char kTestNativeMessagingExtensionId[] =
20    "knldjmfmopnpolahpmmgbagdohdnhkik";
21
22void CreateTestNativeHostManifest(base::FilePath manifest_path) {
23  scoped_ptr<base::DictionaryValue> manifest(new base::DictionaryValue());
24  manifest->SetString("name", kTestNativeMessagingHostName);
25  manifest->SetString("description", "Native Messaging Echo Test");
26  manifest->SetString("type", "stdio");
27
28  base::FilePath test_user_data_dir;
29  ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_user_data_dir));
30  test_user_data_dir = test_user_data_dir.AppendASCII("native_messaging");
31  test_user_data_dir = test_user_data_dir.AppendASCII("native_hosts");
32#if defined(OS_POSIX)
33  base::FilePath host_path = test_user_data_dir.AppendASCII("echo.py");
34#else
35  base::FilePath host_path = test_user_data_dir.AppendASCII("echo.bat");
36#endif
37  manifest->SetString("path", host_path.AsUTF8Unsafe());
38
39  scoped_ptr<base::ListValue> origins(new base::ListValue());
40  origins->AppendString(base::StringPrintf("chrome-extension://%s/",
41                                           kTestNativeMessagingExtensionId));
42  manifest->Set("allowed_origins", origins.release());
43
44  JSONFileValueSerializer serializer(manifest_path);
45  ASSERT_TRUE(serializer.Serialize(*manifest));
46}
47
48}  // namespace extensions
49