shill: clear more state on calls to Device::Stop and WiFi::Stop

BUG=chromium-os:20333
TEST=unittests, WiFiManager/000_SSID_Length_Limit

in addition to the main change, this CL
- moves some common code out of derived classes into Device
- adds some debugging messages (at VLOG(3)) to track down
  unexpected live references
- eliminates TestEventDispatcher (in cellular_unittest.cc),
  in favor of using the MockRTNLHandler

note that the 000_SSID_Length_Limit test was run with the
"Test MAX+1 ssid length" step disabled, because that requires
support for Manager.GetService.

Change-Id: I852d984a3af92e2c18e4a6e8d1dd5e4714069fdc
Reviewed-on: http://gerrit.chromium.org/gerrit/7734
Reviewed-by: mukesh agrawal <quiche@chromium.org>
Tested-by: mukesh agrawal <quiche@chromium.org>
diff --git a/device_unittest.cc b/device_unittest.cc
index 896c3f0..1270a49 100644
--- a/device_unittest.cc
+++ b/device_unittest.cc
@@ -5,6 +5,8 @@
 #include "shill/device.h"
 
 #include <ctype.h>
+#include <sys/socket.h>
+#include <linux/if.h>  // Needs typedefs from sys/socket.h.
 
 #include <map>
 #include <string>
@@ -23,6 +25,7 @@
 #include "shill/mock_device.h"
 #include "shill/mock_glib.h"
 #include "shill/mock_ipconfig.h"
+#include "shill/mock_rtnl_handler.h"
 #include "shill/mock_service.h"
 #include "shill/mock_store.h"
 #include "shill/property_store_unittest.h"
@@ -55,12 +58,17 @@
   }
   virtual ~DeviceTest() {}
 
+  virtual void SetUp() {
+    device_->rtnl_handler_ = &rtnl_handler_;
+  }
+
  protected:
   static const char kDeviceName[];
   static const char kDeviceAddress[];
 
   MockControl control_interface_;
   DeviceRefPtr device_;
+  StrictMock<MockRTNLHandler> rtnl_handler_;
 };
 
 const char DeviceTest::kDeviceName[] = "testdevice";
@@ -198,4 +206,23 @@
   device_->SelectService(NULL);
 }
 
+TEST_F(DeviceTest, Stop) {
+  device_->ipconfig_ = new IPConfig(&control_interface_, kDeviceName);
+  scoped_refptr<MockService> service(
+      new NiceMock<MockService>(&control_interface_,
+                                dispatcher(),
+                                manager()));
+  device_->SelectService(service);
+
+  EXPECT_CALL(*service.get(), state()).
+      WillRepeatedly(Return(Service::kStateConnected));
+  EXPECT_CALL(*dynamic_cast<DeviceMockAdaptor *>(device_->adaptor_.get()),
+              UpdateEnabled());
+  EXPECT_CALL(rtnl_handler_, SetInterfaceFlags(_, 0, IFF_UP));
+  device_->Stop();
+
+  EXPECT_FALSE(device_->ipconfig_.get());
+  EXPECT_FALSE(device_->selected_service_.get());
+}
+
 }  // namespace shill