15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright (c) 2013 The Chromium Authors. All rights reserved.
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/hash.h"
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ppapi/c/ppb_core.h"
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ppapi/proxy/interface_list.h"
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ppapi/proxy/ppapi_proxy_test.h"
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace ppapi {
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace proxy {
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class InterfaceListTest : public PluginProxyTest {
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public:
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Wrapper function so we can use the private InterfaceList::AddPPB.
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void AddPPB(InterfaceList* list,
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)              const char* iface_name, void* iface_addr, Permission perm) {
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    list->AddPPB(iface_name, iface_addr, perm);
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Wrapper function so we can use the private
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // InterfaceList::HashInterfaceName.
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  int HashInterfaceName(const std::string& name) {
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return InterfaceList::HashInterfaceName(name);
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Tests looking up a stable interface.
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(InterfaceListTest, Stable) {
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  InterfaceList list;
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(list.GetInterfaceForPPB(PPB_CORE_INTERFACE_1_0) != NULL);
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(list.GetInterfaceForPPB("FakeUnknownInterface") == NULL);
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Tests that dev channel restrictions work properly.
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(InterfaceListTest, DevChannel) {
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  InterfaceList list;
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // "Dev channel" interface.
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  static const char* dev_channel_iface_name = "TestDevChannelInterface";
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void* dev_channel_iface_addr = (void*)0xdeadbeef;
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // "Dev" interface
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  static const char* dev_iface_name = "TestDevInterface";
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void* dev_iface_addr = (void*)0xcafefade;
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AddPPB(&list, dev_channel_iface_name, dev_channel_iface_addr,
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)         PERMISSION_DEV_CHANNEL);
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AddPPB(&list, dev_iface_name, dev_iface_addr, PERMISSION_DEV);
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  InterfaceList::SetProcessGlobalPermissions(
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      PpapiPermissions(PERMISSION_NONE));
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(list.GetInterfaceForPPB(dev_channel_iface_name) == NULL);
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(list.GetInterfaceForPPB(dev_iface_name) == NULL);
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  InterfaceList::SetProcessGlobalPermissions(
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      PpapiPermissions(PERMISSION_DEV_CHANNEL));
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(list.GetInterfaceForPPB(dev_channel_iface_name) ==
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)              dev_channel_iface_addr);
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(list.GetInterfaceForPPB(dev_iface_name) == NULL);
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  InterfaceList::SetProcessGlobalPermissions(
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      PpapiPermissions(PERMISSION_DEV));
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(list.GetInterfaceForPPB(dev_channel_iface_name) == NULL);
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(list.GetInterfaceForPPB(dev_iface_name) == dev_iface_addr);
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  InterfaceList::SetProcessGlobalPermissions(
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      PpapiPermissions(PERMISSION_DEV | PERMISSION_DEV_CHANNEL));
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(list.GetInterfaceForPPB(dev_channel_iface_name) ==
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)              dev_channel_iface_addr);
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(list.GetInterfaceForPPB(dev_iface_name) == dev_iface_addr);
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Test that the hash function provided by base::Hash is unchanged. This is so
735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// that we will generate correct values when logging interface use to UMA.
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(InterfaceListTest, InterfaceHash) {
755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(612625164, HashInterfaceName("PPB_InputEvent;1.0"));
765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(79708274, HashInterfaceName("PPB_TCPSocket;1.1"));
775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace proxy
805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace ppapi
81