1 // Copyright (c) 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 "base/hash.h"
6 #include "ppapi/c/ppb_core.h"
7 #include "ppapi/proxy/interface_list.h"
8 #include "ppapi/proxy/ppapi_proxy_test.h"
9
10 namespace ppapi {
11 namespace proxy {
12
13 class InterfaceListTest : public PluginProxyTest {
14 public:
15 // Wrapper function so we can use the private InterfaceList::AddPPB.
AddPPB(InterfaceList * list,const char * iface_name,void * iface_addr,Permission perm)16 void AddPPB(InterfaceList* list,
17 const char* iface_name, void* iface_addr, Permission perm) {
18 list->AddPPB(iface_name, iface_addr, perm);
19 }
20
21 // Wrapper function so we can use the private
22 // InterfaceList::HashInterfaceName.
HashInterfaceName(const std::string & name)23 int HashInterfaceName(const std::string& name) {
24 return InterfaceList::HashInterfaceName(name);
25 }
26 };
27
28 // Tests looking up a stable interface.
TEST_F(InterfaceListTest,Stable)29 TEST_F(InterfaceListTest, Stable) {
30 InterfaceList list;
31 ASSERT_TRUE(list.GetInterfaceForPPB(PPB_CORE_INTERFACE_1_0) != NULL);
32 ASSERT_TRUE(list.GetInterfaceForPPB("FakeUnknownInterface") == NULL);
33 }
34
35 // Tests that dev channel restrictions work properly.
TEST_F(InterfaceListTest,DevChannel)36 TEST_F(InterfaceListTest, DevChannel) {
37 InterfaceList list;
38 // "Dev channel" interface.
39 static const char* dev_channel_iface_name = "TestDevChannelInterface";
40 void* dev_channel_iface_addr = (void*)0xdeadbeef;
41 // "Dev" interface
42 static const char* dev_iface_name = "TestDevInterface";
43 void* dev_iface_addr = (void*)0xcafefade;
44
45 AddPPB(&list, dev_channel_iface_name, dev_channel_iface_addr,
46 PERMISSION_DEV_CHANNEL);
47 AddPPB(&list, dev_iface_name, dev_iface_addr, PERMISSION_DEV);
48
49 InterfaceList::SetProcessGlobalPermissions(
50 PpapiPermissions(PERMISSION_NONE));
51 ASSERT_TRUE(list.GetInterfaceForPPB(dev_channel_iface_name) == NULL);
52 ASSERT_TRUE(list.GetInterfaceForPPB(dev_iface_name) == NULL);
53
54 InterfaceList::SetProcessGlobalPermissions(
55 PpapiPermissions(PERMISSION_DEV_CHANNEL));
56 ASSERT_TRUE(list.GetInterfaceForPPB(dev_channel_iface_name) ==
57 dev_channel_iface_addr);
58 ASSERT_TRUE(list.GetInterfaceForPPB(dev_iface_name) == NULL);
59
60 InterfaceList::SetProcessGlobalPermissions(
61 PpapiPermissions(PERMISSION_DEV));
62 ASSERT_TRUE(list.GetInterfaceForPPB(dev_channel_iface_name) == NULL);
63 ASSERT_TRUE(list.GetInterfaceForPPB(dev_iface_name) == dev_iface_addr);
64
65 InterfaceList::SetProcessGlobalPermissions(
66 PpapiPermissions(PERMISSION_DEV | PERMISSION_DEV_CHANNEL));
67 ASSERT_TRUE(list.GetInterfaceForPPB(dev_channel_iface_name) ==
68 dev_channel_iface_addr);
69 ASSERT_TRUE(list.GetInterfaceForPPB(dev_iface_name) == dev_iface_addr);
70 }
71
72 // Test that the hash function provided by base::Hash is unchanged. This is so
73 // that we will generate correct values when logging interface use to UMA.
TEST_F(InterfaceListTest,InterfaceHash)74 TEST_F(InterfaceListTest, InterfaceHash) {
75 EXPECT_EQ(612625164, HashInterfaceName("PPB_InputEvent;1.0"));
76 EXPECT_EQ(79708274, HashInterfaceName("PPB_TCPSocket;1.1"));
77 }
78
79 } // namespace proxy
80 } // namespace ppapi
81