1 /*
2  * This file is part of Licq, an instant messaging client for UNIX.
3  * Copyright (C) 2010-2014 Licq Developers <licq-dev@googlegroups.com>
4  *
5  * Licq is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * Licq is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with Licq; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19 
20 #include "../protocolplugin.h"
21 #include "../protocolplugininstance.h"
22 
23 #include <licq/plugin/protocolpluginfactory.h>
24 #include <licq/plugin/protocolplugininterface.h>
25 #include <licq/userid.h>
26 
27 #include <gtest/gtest.h>
28 #include <gmock/gmock.h>
29 
30 using LicqDaemon::ProtocolPlugin;
31 using LicqDaemon::ProtocolPluginInstance;
32 using LicqDaemon::DynamicLibrary;
33 using LicqDaemon::PluginThread;
34 
35 using ::testing::InSequence;
36 using ::testing::Return;
37 
38 namespace LicqTest
39 {
40 
41 class MockProtocolPluginFactory : public Licq::ProtocolPluginFactory
42 {
43 public:
44   MOCK_CONST_METHOD0(name, std::string());
45   MOCK_CONST_METHOD0(version, std::string());
46   MOCK_METHOD1(destroyPlugin, void(Licq::PluginInterface* plugin));
47 
48   MOCK_CONST_METHOD0(protocolId, unsigned long());
49   MOCK_CONST_METHOD0(capabilities, unsigned long());
50   MOCK_CONST_METHOD0(statuses, unsigned long());
51   MOCK_METHOD0(createPlugin, Licq::ProtocolPluginInterface*());
52   MOCK_METHOD2(createUser, Licq::User*(const Licq::UserId& id, bool temporary));
53   MOCK_METHOD1(createOwner, Licq::Owner*(const Licq::UserId& id));
54 };
55 
56 class MockProtocolPlugin : public Licq::ProtocolPluginInterface
57 {
58 public:
59   MOCK_METHOD2(init, bool(int argc, char** argv));
60   MOCK_METHOD0(run, int());
61   MOCK_METHOD0(shutdown, void());
62 
63   MOCK_METHOD1(pushSignal,
64                void(boost::shared_ptr<const Licq::ProtocolSignal> signal));
65 };
66 
nullDeleter(void *)67 static void nullDeleter(void*) { /* Empty */ }
68 
69 struct ProtocolPluginFixture : public ::testing::Test
70 {
71   DynamicLibrary::Ptr myLib;
72   PluginThread::Ptr myThread;
73   MockProtocolPluginFactory myMockFactory;
74   MockProtocolPlugin myMockInterface;
75   ProtocolPlugin plugin;
76   ProtocolPluginInstance instance;
77 
ProtocolPluginFixtureLicqTest::ProtocolPluginFixture78   ProtocolPluginFixture() :
79     myLib(new DynamicLibrary("")),
80     myThread(new PluginThread()),
81     plugin(myLib, boost::shared_ptr<MockProtocolPluginFactory>(
82                &myMockFactory, &nullDeleter), myThread),
83     instance(1, Licq::UserId(),
84              boost::shared_ptr<ProtocolPlugin>(&plugin, &nullDeleter),
85              myThread)
86   {
87     EXPECT_CALL(myMockFactory, createPlugin())
88         .WillOnce(Return(&myMockInterface));
89     EXPECT_CALL(myMockFactory, destroyPlugin(&myMockInterface));
90 
91     EXPECT_TRUE(instance.create(NULL));
92   }
93 
~ProtocolPluginFixtureLicqTest::ProtocolPluginFixture94   ~ProtocolPluginFixture()
95   {
96     myThread->cancel();
97   }
98 };
99 
100 struct RunnableProtocolPluginFixture
101   : public ProtocolPluginFixture,
102     public ::testing::WithParamInterface<bool>
103 {
104   // Empty
105 };
106 
TEST_P(RunnableProtocolPluginFixture,callApiFunctions)107 TEST_P(RunnableProtocolPluginFixture, callApiFunctions)
108 {
109   if (GetParam())
110     instance.setIsRunning(true);
111 
112   boost::shared_ptr<const Licq::ProtocolSignal> signal(
113       reinterpret_cast<Licq::ProtocolSignal*>(123), &nullDeleter);
114   Licq::UserId user;
115   Licq::UserId owner;
116 
117   InSequence dummy;
118   EXPECT_CALL(myMockFactory, protocolId());
119   EXPECT_CALL(myMockFactory, capabilities());
120   EXPECT_CALL(myMockFactory, statuses());
121   if (GetParam())
122     EXPECT_CALL(myMockInterface, pushSignal(signal));
123   EXPECT_CALL(myMockFactory, createUser(user, false));
124   EXPECT_CALL(myMockFactory, createOwner(owner));
125 
126   // Verify that the calls are forwarded to the interface
127   plugin.protocolId();
128   plugin.capabilities();
129   plugin.statuses();
130   instance.pushSignal(signal);
131   plugin.createUser(user, false);
132   plugin.createOwner(owner);
133 }
134 
135 INSTANTIATE_TEST_CASE_P(Running, RunnableProtocolPluginFixture,
136                         ::testing::Bool());
137 
138 } // namespace LicqTest
139