1 /* 2 * libjingle 3 * Copyright 2011, Google Inc. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #ifdef HAVE_DBUS_GLIB 29 30 #include "talk/base/dbus.h" 31 #include "talk/base/gunit.h" 32 #include "talk/base/thread.h" 33 34 namespace talk_base { 35 36 #define SIG_NAME "NameAcquired" 37 38 static const uint32 kTimeoutMs = 5000U; 39 40 class DBusSigFilterTest : public DBusSigFilter { 41 public: 42 // DBusSigFilterTest listens on DBus service itself for "NameAcquired" signal. 43 // This signal should be received when the application connects to DBus 44 // service and gains ownership of a name. 45 // http://dbus.freedesktop.org/doc/dbus-specification.html 46 DBusSigFilterTest() 47 : DBusSigFilter(GetFilter()), 48 message_received_(false) { 49 } 50 51 bool MessageReceived() { 52 return message_received_; 53 } 54 55 private: 56 static std::string GetFilter() { 57 return talk_base::DBusSigFilter::BuildFilterString("", "", SIG_NAME); 58 } 59 60 // Implement virtual method of DBusSigFilter. On caller thread. 61 virtual void ProcessSignal(DBusMessage *message) { 62 EXPECT_TRUE(message != NULL); 63 message_received_ = true; 64 } 65 66 bool message_received_; 67 }; 68 69 TEST(DBusMonitorTest, StartStopStartStop) { 70 DBusSigFilterTest filter; 71 talk_base::scoped_ptr<talk_base::DBusMonitor> monitor; 72 monitor.reset(talk_base::DBusMonitor::Create(DBUS_BUS_SYSTEM)); 73 if (monitor) { 74 EXPECT_TRUE(monitor->AddFilter(&filter)); 75 76 EXPECT_TRUE(monitor->StopMonitoring()); 77 EXPECT_EQ(monitor->GetStatus(), DBusMonitor::DMS_NOT_INITIALIZED); 78 79 EXPECT_TRUE(monitor->StartMonitoring()); 80 EXPECT_EQ_WAIT(DBusMonitor::DMS_RUNNING, monitor->GetStatus(), kTimeoutMs); 81 EXPECT_TRUE(monitor->StopMonitoring()); 82 EXPECT_EQ(monitor->GetStatus(), DBusMonitor::DMS_STOPPED); 83 EXPECT_TRUE(monitor->StopMonitoring()); 84 EXPECT_EQ(monitor->GetStatus(), DBusMonitor::DMS_STOPPED); 85 86 EXPECT_TRUE(monitor->StartMonitoring()); 87 EXPECT_EQ_WAIT(DBusMonitor::DMS_RUNNING, monitor->GetStatus(), kTimeoutMs); 88 EXPECT_TRUE(monitor->StartMonitoring()); 89 EXPECT_EQ(monitor->GetStatus(), DBusMonitor::DMS_RUNNING); 90 EXPECT_TRUE(monitor->StopMonitoring()); 91 EXPECT_EQ(monitor->GetStatus(), DBusMonitor::DMS_STOPPED); 92 } else { 93 LOG(LS_WARNING) << "DBus Monitor not started. Skipping test."; 94 } 95 } 96 97 // DBusMonitorTest listens on DBus service itself for "NameAcquired" signal. 98 // This signal should be received when the application connects to DBus 99 // service and gains ownership of a name. 100 // This test is to make sure that we capture the "NameAcquired" signal. 101 TEST(DBusMonitorTest, ReceivedNameAcquiredSignal) { 102 DBusSigFilterTest filter; 103 talk_base::scoped_ptr<talk_base::DBusMonitor> monitor; 104 monitor.reset(talk_base::DBusMonitor::Create(DBUS_BUS_SYSTEM)); 105 if (monitor) { 106 EXPECT_TRUE(monitor->AddFilter(&filter)); 107 108 EXPECT_TRUE(monitor->StartMonitoring()); 109 EXPECT_EQ_WAIT(DBusMonitor::DMS_RUNNING, monitor->GetStatus(), kTimeoutMs); 110 EXPECT_TRUE_WAIT(filter.MessageReceived(), kTimeoutMs); 111 EXPECT_TRUE(monitor->StopMonitoring()); 112 EXPECT_EQ(monitor->GetStatus(), DBusMonitor::DMS_STOPPED); 113 } else { 114 LOG(LS_WARNING) << "DBus Monitor not started. Skipping test."; 115 } 116 } 117 118 TEST(DBusMonitorTest, ConcurrentMonitors) { 119 DBusSigFilterTest filter1; 120 talk_base::scoped_ptr<talk_base::DBusMonitor> monitor1; 121 monitor1.reset(talk_base::DBusMonitor::Create(DBUS_BUS_SYSTEM)); 122 if (monitor1) { 123 EXPECT_TRUE(monitor1->AddFilter(&filter1)); 124 DBusSigFilterTest filter2; 125 talk_base::scoped_ptr<talk_base::DBusMonitor> monitor2; 126 monitor2.reset(talk_base::DBusMonitor::Create(DBUS_BUS_SYSTEM)); 127 EXPECT_TRUE(monitor2->AddFilter(&filter2)); 128 129 EXPECT_TRUE(monitor1->StartMonitoring()); 130 EXPECT_EQ_WAIT(DBusMonitor::DMS_RUNNING, monitor1->GetStatus(), kTimeoutMs); 131 EXPECT_TRUE(monitor2->StartMonitoring()); 132 EXPECT_EQ_WAIT(DBusMonitor::DMS_RUNNING, monitor2->GetStatus(), kTimeoutMs); 133 134 EXPECT_TRUE_WAIT(filter2.MessageReceived(), kTimeoutMs); 135 EXPECT_TRUE(monitor2->StopMonitoring()); 136 EXPECT_EQ(monitor2->GetStatus(), DBusMonitor::DMS_STOPPED); 137 138 EXPECT_TRUE_WAIT(filter1.MessageReceived(), kTimeoutMs); 139 EXPECT_TRUE(monitor1->StopMonitoring()); 140 EXPECT_EQ(monitor1->GetStatus(), DBusMonitor::DMS_STOPPED); 141 } else { 142 LOG(LS_WARNING) << "DBus Monitor not started. Skipping test."; 143 } 144 } 145 146 TEST(DBusMonitorTest, ConcurrentFilters) { 147 DBusSigFilterTest filter1; 148 DBusSigFilterTest filter2; 149 talk_base::scoped_ptr<talk_base::DBusMonitor> monitor; 150 monitor.reset(talk_base::DBusMonitor::Create(DBUS_BUS_SYSTEM)); 151 if (monitor) { 152 EXPECT_TRUE(monitor->AddFilter(&filter1)); 153 EXPECT_TRUE(monitor->AddFilter(&filter2)); 154 155 EXPECT_TRUE(monitor->StartMonitoring()); 156 EXPECT_EQ_WAIT(DBusMonitor::DMS_RUNNING, monitor->GetStatus(), kTimeoutMs); 157 158 EXPECT_TRUE_WAIT(filter1.MessageReceived(), kTimeoutMs); 159 EXPECT_TRUE_WAIT(filter2.MessageReceived(), kTimeoutMs); 160 161 EXPECT_TRUE(monitor->StopMonitoring()); 162 EXPECT_EQ(monitor->GetStatus(), DBusMonitor::DMS_STOPPED); 163 } else { 164 LOG(LS_WARNING) << "DBus Monitor not started. Skipping test."; 165 } 166 } 167 168 TEST(DBusMonitorTest, NoAddFilterIfRunning) { 169 DBusSigFilterTest filter1; 170 DBusSigFilterTest filter2; 171 talk_base::scoped_ptr<talk_base::DBusMonitor> monitor; 172 monitor.reset(talk_base::DBusMonitor::Create(DBUS_BUS_SYSTEM)); 173 if (monitor) { 174 EXPECT_TRUE(monitor->AddFilter(&filter1)); 175 176 EXPECT_TRUE(monitor->StartMonitoring()); 177 EXPECT_EQ_WAIT(DBusMonitor::DMS_RUNNING, monitor->GetStatus(), kTimeoutMs); 178 EXPECT_FALSE(monitor->AddFilter(&filter2)); 179 180 EXPECT_TRUE(monitor->StopMonitoring()); 181 EXPECT_EQ(monitor->GetStatus(), DBusMonitor::DMS_STOPPED); 182 } else { 183 LOG(LS_WARNING) << "DBus Monitor not started. Skipping test."; 184 } 185 } 186 187 TEST(DBusMonitorTest, AddFilterAfterStop) { 188 DBusSigFilterTest filter1; 189 DBusSigFilterTest filter2; 190 talk_base::scoped_ptr<talk_base::DBusMonitor> monitor; 191 monitor.reset(talk_base::DBusMonitor::Create(DBUS_BUS_SYSTEM)); 192 if (monitor) { 193 EXPECT_TRUE(monitor->AddFilter(&filter1)); 194 EXPECT_TRUE(monitor->StartMonitoring()); 195 EXPECT_EQ_WAIT(DBusMonitor::DMS_RUNNING, monitor->GetStatus(), kTimeoutMs); 196 EXPECT_TRUE_WAIT(filter1.MessageReceived(), kTimeoutMs); 197 EXPECT_TRUE(monitor->StopMonitoring()); 198 EXPECT_EQ(monitor->GetStatus(), DBusMonitor::DMS_STOPPED); 199 200 EXPECT_TRUE(monitor->AddFilter(&filter2)); 201 EXPECT_TRUE(monitor->StartMonitoring()); 202 EXPECT_EQ_WAIT(DBusMonitor::DMS_RUNNING, monitor->GetStatus(), kTimeoutMs); 203 EXPECT_TRUE_WAIT(filter1.MessageReceived(), kTimeoutMs); 204 EXPECT_TRUE_WAIT(filter2.MessageReceived(), kTimeoutMs); 205 EXPECT_TRUE(monitor->StopMonitoring()); 206 EXPECT_EQ(monitor->GetStatus(), DBusMonitor::DMS_STOPPED); 207 } else { 208 LOG(LS_WARNING) << "DBus Monitor not started. Skipping test."; 209 } 210 } 211 212 TEST(DBusMonitorTest, StopRightAfterStart) { 213 DBusSigFilterTest filter; 214 talk_base::scoped_ptr<talk_base::DBusMonitor> monitor; 215 monitor.reset(talk_base::DBusMonitor::Create(DBUS_BUS_SYSTEM)); 216 if (monitor) { 217 EXPECT_TRUE(monitor->AddFilter(&filter)); 218 219 EXPECT_TRUE(monitor->StartMonitoring()); 220 EXPECT_TRUE(monitor->StopMonitoring()); 221 222 // Stop the monitoring thread right after it had been started. 223 // If the monitoring thread got a chance to receive a DBus signal, it would 224 // post a message to the main thread and signal the main thread wakeup. 225 // This message will be cleaned out automatically when the filter get 226 // destructed. Here we also consume the wakeup signal (if there is one) so 227 // that the testing (main) thread is reset to a clean state. 228 talk_base::Thread::Current()->ProcessMessages(1); 229 } else { 230 LOG(LS_WARNING) << "DBus Monitor not started."; 231 } 232 } 233 234 TEST(DBusSigFilter, BuildFilterString) { 235 EXPECT_EQ(DBusSigFilter::BuildFilterString("", "", ""), 236 (DBUS_TYPE "='" DBUS_SIGNAL "'")); 237 EXPECT_EQ(DBusSigFilter::BuildFilterString("p", "", ""), 238 (DBUS_TYPE "='" DBUS_SIGNAL "'," DBUS_PATH "='p'")); 239 EXPECT_EQ(DBusSigFilter::BuildFilterString("p","i", ""), 240 (DBUS_TYPE "='" DBUS_SIGNAL "'," DBUS_PATH "='p'," 241 DBUS_INTERFACE "='i'")); 242 EXPECT_EQ(DBusSigFilter::BuildFilterString("p","i","m"), 243 (DBUS_TYPE "='" DBUS_SIGNAL "'," DBUS_PATH "='p'," 244 DBUS_INTERFACE "='i'," DBUS_MEMBER "='m'")); 245 } 246 247 } // namespace talk_base 248 249 #endif // HAVE_DBUS_GLIB 250