Commit aeae18c6 authored by evan@chromium.org's avatar evan@chromium.org

linux: write a unit test for plugin mime parsing

No significant code change, just preparing to redo the parsing
code and thinking I needed test coverage.

(Since the parse can fail now, made the parse error out
rather than continuing when it's unable to parse some fields.
This will have more failure cases in the next version of this
code.)

Review URL: http://codereview.chromium.org/193046

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@25678 0039d316-1c4b-4281-b951-d872f2087c98
parent af4282a5
......@@ -12,6 +12,7 @@
#include "base/file_path.h"
#include "base/native_library.h"
#include "base/ref_counted.h"
#include "build/build_config.h"
#include "webkit/glue/plugins/plugin_list.h"
#include "webkit/glue/webplugin.h"
......@@ -34,6 +35,14 @@ class PluginLib : public base::RefCounted<PluginLib> {
// Returns false if the library couldn't be found, or if it's not a plugin.
static bool ReadWebPluginInfo(const FilePath& filename, WebPluginInfo* info);
#if defined(OS_LINUX)
// Parse the result of an NP_GetMIMEDescription() call, returning
// false on parse error. This API is only used on Linux, and is
// exposed here for testing.
static bool ParseMIMEDescription(const char* description,
std::vector<WebPluginMimeType>* mime_types);
#endif
// Unloads all the loaded plugin libraries and cleans up the plugin map.
static void UnloadAllPlugins();
......
......@@ -82,28 +82,9 @@ bool PluginLib::ReadWebPluginInfo(const FilePath& filename,
mime_description = NP_GetMIMEDescription();
if (mime_description) {
// We parse the description here into WebPluginMimeType structures.
// Description for Flash 10 looks like (all as one string):
// "application/x-shockwave-flash:swf:Shockwave Flash;"
// "application/futuresplash:spl:FutureSplash Player"
std::vector<std::string> descriptions;
SplitString(mime_description, ';', &descriptions);
for (size_t i = 0; i < descriptions.size(); ++i) {
if (descriptions[i].empty())
continue; // Don't warn if they have trailing semis.
std::vector<std::string> fields;
SplitString(descriptions[i], ':', &fields);
if (fields.size() != 3) {
LOG(WARNING) << "Couldn't parse plugin info: " << descriptions[i];
continue;
}
WebPluginMimeType mime_type;
mime_type.mime_type = fields[0];
SplitString(fields[1], ',', &mime_type.file_extensions);
mime_type.description = UTF8ToWide(fields[2]);
info->mime_types.push_back(mime_type);
if (!ParseMIMEDescription(mime_description, &info->mime_types)) {
base::UnloadNativeLibrary(dl);
return false;
}
}
......@@ -130,4 +111,40 @@ bool PluginLib::ReadWebPluginInfo(const FilePath& filename,
return true;
}
// static
bool PluginLib::ParseMIMEDescription(
const char* description,
std::vector<WebPluginMimeType>* mime_types) {
// TODO(evanm): rewrite this to better match Firefox; see
// ParsePluginMimeDescription near
// http://mxr.mozilla.org/firefox/source/modules/plugin/base/src/nsPluginsDirUtils.h#53
// We parse the description here into WebPluginMimeType structures.
// Description for Flash 10 looks like (all as one string):
// "application/x-shockwave-flash:swf:Shockwave Flash;"
// "application/futuresplash:spl:FutureSplash Player"
std::vector<std::string> descriptions;
SplitString(description, ';', &descriptions);
for (size_t i = 0; i < descriptions.size(); ++i) {
if (descriptions[i].empty())
continue; // Don't warn if they have trailing semis.
std::vector<std::string> fields;
SplitString(descriptions[i], ':', &fields);
if (fields.size() != 3) {
LOG(WARNING) << "Couldn't parse plugin info: " << description;
// This plugin's got something weird going on; abort.
return false;
}
WebPluginMimeType mime_type;
mime_type.mime_type = fields[0];
SplitString(fields[1], ',', &mime_type.file_extensions);
mime_type.description = UTF8ToWide(fields[2]);
mime_types->push_back(mime_type);
}
return true;
}
} // namespace NPAPI
// Copyright (c) 2009 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 "webkit/glue/plugins/plugin_lib.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
#if defined(OS_LINUX)
// Test parsing a simple description: Real Audio.
TEST(MIMEDescriptionParse, Simple) {
std::vector<WebPluginMimeType> types;
ASSERT_TRUE(NPAPI::PluginLib::ParseMIMEDescription(
"audio/x-pn-realaudio-plugin:rpm:RealAudio document;",
&types));
ASSERT_EQ(1U, types.size());
const WebPluginMimeType& type = types[0];
EXPECT_EQ("audio/x-pn-realaudio-plugin", type.mime_type);
ASSERT_EQ(1U, type.file_extensions.size());
EXPECT_EQ("rpm", type.file_extensions[0]);
EXPECT_EQ(L"RealAudio document", type.description);
}
// Test parsing a multi-entry description: QuickTime as provided by Totem.
TEST(MIMEDescriptionParse, Multi) {
std::vector<WebPluginMimeType> types;
ASSERT_TRUE(NPAPI::PluginLib::ParseMIMEDescription(
"video/quicktime:mov:QuickTime video;video/mp4:mp4:MPEG-4 "
"video;image/x-macpaint:pntg:MacPaint Bitmap image;image/x"
"-quicktime:pict, pict1, pict2:QuickTime image;video/x-m4v"
":m4v:MPEG-4 video;",
&types));
ASSERT_EQ(5U, types.size());
// Check the x-quicktime one, since it looks tricky with spaces in the
// extension list.
const WebPluginMimeType& type = types[3];
EXPECT_EQ("image/x-quicktime", type.mime_type);
ASSERT_EQ(3U, type.file_extensions.size());
EXPECT_EQ("pict2", type.file_extensions[2]);
EXPECT_EQ(L"QuickTime image", type.description);
}
// Test parsing a Japanese description, since we got this wrong in the past.
// This comes from loading Totem with LANG=ja_JP.UTF-8.
TEST(MIMEDescriptionParse, JapaneseUTF8) {
std::vector<WebPluginMimeType> types;
ASSERT_TRUE(NPAPI::PluginLib::ParseMIMEDescription(
"audio/x-ogg:ogg:Ogg \xe3\x82\xaa\xe3\x83\xbc\xe3\x83\x87"
"\xe3\x82\xa3\xe3\x83\xaa",
&types));
ASSERT_EQ(1U, types.size());
// Check we got the right number of Unicode characters out of the parse.
EXPECT_EQ(9U, types[0].description.size());
}
/*
TODO(evanm): write a test that covers the following, which does *not*
parse properly with the current code.
application/x-java-vm:class,jar:IcedTea;application/x-java-applet:class,jar:IcedTea;application/x-java-applet;version=1.1:class,jar:IcedTea;application/x-java-applet;version=1.1.1:class,jar:IcedTea;application/x-java-applet;version=1.1.2:class,jar:IcedTea;application/x-java-applet;version=1.1.3:class,jar:IcedTea;application/x-java-applet;version=1.2:class,jar:IcedTea;application/x-java-applet;version=1.2.1:class,jar:IcedTea;application/x-java-applet;version=1.2.2:class,jar:IcedTea;application/x-java-applet;version=1.3:class,jar:IcedTea;application/x-java-applet;version=1.3.1:class,jar:IcedTea;application/x-java-applet;version=1.4:class,jar:IcedTea
*/
#endif // defined(OS_LINUX)
......@@ -341,6 +341,7 @@
'../../glue/mock_resource_loader_bridge.h',
'../../glue/multipart_response_delegate_unittest.cc',
'../../glue/password_autocomplete_listener_unittest.cc',
'../../glue/plugins/plugin_lib_unittest.cc',
'../../glue/regular_expression_unittest.cc',
'../../glue/resource_fetcher_unittest.cc',
'../../glue/unittest_test_server.h',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment