1 // Copyright (c) 2011 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 <string> 6 #include <vector> 7 8 #include "base/file_path.h" 9 #include "base/file_util.h" 10 #include "base/memory/scoped_ptr.h" 11 #include "base/path_service.h" 12 #include "chrome/browser/extensions/convert_user_script.h" 13 #include "chrome/common/chrome_paths.h" 14 #include "chrome/common/extensions/extension.h" 15 #include "testing/gtest/include/gtest/gtest.h" 16 17 TEST(ExtensionFromUserScript, Basic) { 18 FilePath test_file; 19 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file)); 20 test_file = test_file.AppendASCII("extensions") 21 .AppendASCII("user_script_basic.user.js"); 22 23 std::string error; 24 scoped_refptr<Extension> extension(ConvertUserScriptToExtension( 25 test_file, GURL("http://www.google.com/foo"), &error)); 26 27 ASSERT_TRUE(extension.get()); 28 EXPECT_EQ("", error); 29 30 // Validate generated extension metadata. 31 EXPECT_EQ("My user script", extension->name()); 32 EXPECT_EQ("2.2.2", extension->VersionString()); 33 EXPECT_EQ("Does totally awesome stuff.", extension->description()); 34 EXPECT_EQ("IhCFCg9PMQTAcJdc9ytUP99WME+4yh6aMnM1uupkovo=", 35 extension->public_key()); 36 37 ASSERT_EQ(1u, extension->content_scripts().size()); 38 const UserScript& script = extension->content_scripts()[0]; 39 ASSERT_EQ(2u, script.globs().size()); 40 EXPECT_EQ("http://www.google.com/*", script.globs().at(0)); 41 EXPECT_EQ("http://www.yahoo.com/*", script.globs().at(1)); 42 ASSERT_EQ(1u, script.exclude_globs().size()); 43 EXPECT_EQ("*foo*", script.exclude_globs().at(0)); 44 ASSERT_EQ(1u, script.url_patterns().size()); 45 EXPECT_EQ("http://www.google.com/*", script.url_patterns()[0].GetAsString()); 46 47 // Make sure the files actually exist on disk. 48 EXPECT_TRUE(file_util::PathExists( 49 extension->path().Append(script.js_scripts()[0].relative_path()))); 50 EXPECT_TRUE(file_util::PathExists( 51 extension->path().Append(Extension::kManifestFilename))); 52 53 // Cleanup 54 file_util::Delete(extension->path(), true); 55 } 56 57 TEST(ExtensionFromUserScript, NoMetdata) { 58 FilePath test_file; 59 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file)); 60 test_file = test_file.AppendASCII("extensions") 61 .AppendASCII("user_script_no_metadata.user.js"); 62 63 std::string error; 64 scoped_refptr<Extension> extension(ConvertUserScriptToExtension( 65 test_file, GURL("http://www.google.com/foo/bar.user.js?monkey"), &error)); 66 67 ASSERT_TRUE(extension.get()); 68 EXPECT_EQ("", error); 69 70 // Validate generated extension metadata. 71 EXPECT_EQ("bar.user.js", extension->name()); 72 EXPECT_EQ("1.0", extension->VersionString()); 73 EXPECT_EQ("", extension->description()); 74 EXPECT_EQ("k1WxKx54hX6tfl5gQaXD/m4d9QUMwRdXWM4RW+QkWcY=", 75 extension->public_key()); 76 77 ASSERT_EQ(1u, extension->content_scripts().size()); 78 const UserScript& script = extension->content_scripts()[0]; 79 ASSERT_EQ(1u, script.globs().size()); 80 EXPECT_EQ("*", script.globs()[0]); 81 EXPECT_EQ(0u, script.exclude_globs().size()); 82 ASSERT_EQ(2u, script.url_patterns().size()); 83 EXPECT_EQ("http://*/*", script.url_patterns()[0].GetAsString()); 84 EXPECT_EQ("https://*/*", script.url_patterns()[1].GetAsString()); 85 86 // Make sure the files actually exist on disk. 87 EXPECT_TRUE(file_util::PathExists( 88 extension->path().Append(script.js_scripts()[0].relative_path()))); 89 EXPECT_TRUE(file_util::PathExists( 90 extension->path().Append(Extension::kManifestFilename))); 91 92 // Cleanup 93 file_util::Delete(extension->path(), true); 94 } 95 96 TEST(ExtensionFromUserScript, NotUTF8) { 97 FilePath test_file; 98 99 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file)); 100 test_file = test_file.AppendASCII("extensions") 101 .AppendASCII("user_script_not_utf8.user.js"); 102 103 std::string error; 104 scoped_refptr<Extension> extension(ConvertUserScriptToExtension( 105 test_file, GURL("http://www.google.com/foo/bar.user.js?monkey"), &error)); 106 107 ASSERT_FALSE(extension.get()); 108 EXPECT_EQ("User script must be UTF8 encoded.", error); 109 } 110