1 // Copyright 2014 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.
5 #include "base/files/file_path.h"
6 #include "base/strings/string_util.h"
7 #include "content/public/browser/browser_thread.h"
8 #include "content/public/test/test_browser_thread_bundle.h"
9 #include "extensions/browser/api/api_resource.h"
10 #include "extensions/browser/api/api_resource_manager.h"
11 #include "extensions/browser/extensions_test.h"
12 #include "extensions/common/extension.h"
13 #include "extensions/common/test_util.h"
14 #include "testing/gtest/include/gtest/gtest.h"
17 using content::BrowserThread
;
18 using content::NotificationService
;
20 namespace extensions
{
22 class ApiResourceManagerUnitTest
: public ExtensionsTest
{
24 ApiResourceManagerUnitTest()
25 : notification_service_(NotificationService::Create()) {}
28 content::TestBrowserThreadBundle thread_bundle_
;
29 scoped_ptr
<NotificationService
> notification_service_
;
32 class FakeApiResource
: public ApiResource
{
34 explicit FakeApiResource(const std::string
& owner_extension_id
)
35 : ApiResource(owner_extension_id
) {}
36 virtual ~FakeApiResource() {}
37 static const BrowserThread::ID kThreadId
= BrowserThread::UI
;
40 TEST_F(ApiResourceManagerUnitTest
, TwoAppsCannotShareResources
) {
41 scoped_ptr
<ApiResourceManager
<FakeApiResource
> > manager(
42 new ApiResourceManager
<FakeApiResource
>(NULL
));
43 scoped_refptr
<extensions::Extension
> extension_one
=
44 test_util::CreateExtensionWithID("one");
45 scoped_refptr
<extensions::Extension
> extension_two
=
46 test_util::CreateExtensionWithID("two");
48 const std::string
extension_one_id(extension_one
->id());
49 const std::string
extension_two_id(extension_two
->id());
51 int resource_one_id
= manager
->Add(new FakeApiResource(extension_one_id
));
52 int resource_two_id
= manager
->Add(new FakeApiResource(extension_two_id
));
53 CHECK(resource_one_id
);
54 CHECK(resource_two_id
);
56 // Confirm each extension can get its own resource.
57 ASSERT_TRUE(manager
->Get(extension_one_id
, resource_one_id
) != NULL
);
58 ASSERT_TRUE(manager
->Get(extension_two_id
, resource_two_id
) != NULL
);
60 // Confirm neither extension can get the other's resource.
61 ASSERT_TRUE(manager
->Get(extension_one_id
, resource_two_id
) == NULL
);
62 ASSERT_TRUE(manager
->Get(extension_two_id
, resource_one_id
) == NULL
);
64 // And make sure we're not susceptible to any Jedi mind tricks.
65 ASSERT_TRUE(manager
->Get(std::string(), resource_one_id
) == NULL
);
68 } // namespace extensions