OSDN Git Service

Add a developer mode to wificond
authorChristopher Wiley <wiley@google.com>
Mon, 27 Jun 2016 20:43:03 +0000 (13:43 -0700)
committerChristopher Wiley <wiley@google.com>
Tue, 28 Jun 2016 15:29:48 +0000 (08:29 -0700)
When wificond starts up it will now check a system property
and claim a slightly different service name if the property
is set to true.

This allows us to restart wificond in a way which prevents the framework
from talking to wificond.

Bug: 29584274
Test: Modified integration tests confirm this is working.

Change-Id: I1aed389fbcb047f466f94ca568b66df4b7f9a419

Android.mk
ipc_constants.cpp [new file with mode: 0644]
ipc_constants.h [new file with mode: 0644]
main.cpp
tests/integration/life_cycle_test.cpp

index e799f69..c65d413 100644 (file)
@@ -28,6 +28,7 @@ LOCAL_SRC_FILES := \
 LOCAL_SHARED_LIBRARIES := \
     libbinder \
     libbase \
+    libcutils \
     libutils
 LOCAL_STATIC_LIBRARIES := \
     libwificond
@@ -60,6 +61,7 @@ LOCAL_MODULE := libwificond_ipc
 LOCAL_AIDL_INCLUDES += $(LOCAL_PATH)/aidl
 LOCAL_CPPFLAGS := $(wificond_cpp_flags)
 LOCAL_SRC_FILES := \
+    ipc_constants.cpp \
     aidl/android/net/wifi/IApInterface.aidl \
     aidl/android/net/wifi/IClientInterface.aidl \
     aidl/android/net/wifi/IWificond.aidl
@@ -110,6 +112,7 @@ LOCAL_SRC_FILES := \
 LOCAL_SHARED_LIBRARIES := \
     libbase \
     libbinder \
+    libcutils \
     libutils
 LOCAL_STATIC_LIBRARIES := \
     libwificond_ipc \
diff --git a/ipc_constants.cpp b/ipc_constants.cpp
new file mode 100644 (file)
index 0000000..b02a8d9
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "ipc_constants.h"
+
+namespace android {
+namespace wificond {
+namespace ipc_constants {
+
+const char kServiceName[] = "wificond";
+const char kDevModeServiceName[] = "wificond-dev";
+const char kDevModePropertyKey[] = "wificond.dev-mode-on";
+
+}  // namespace ipc_constants
+}  // namespace wificond
+}  // namespace android
+
diff --git a/ipc_constants.h b/ipc_constants.h
new file mode 100644 (file)
index 0000000..d967c2f
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef WIFICOND_IPC_CONSTANTS_H_
+#define WIFICOND_IPC_CONSTANTS_H_
+
+namespace android {
+namespace wificond {
+namespace ipc_constants {
+
+extern const char kServiceName[];
+extern const char kDevModeServiceName[];
+extern const char kDevModePropertyKey[];
+
+}  // namespace ipc_constants
+}  // namespace wificond
+}  // namespace android
+
+#endif  // WIFICOND_IPC_CONSTANTS_H_
index 9c8558a..33595aa 100644 (file)
--- a/main.cpp
+++ b/main.cpp
 #include <binder/IPCThreadState.h>
 #include <binder/IServiceManager.h>
 #include <binder/ProcessState.h>
+#include <cutils/properties.h>
 #include <utils/String16.h>
 
+#include "ipc_constants.h"
 #include "looper_backed_event_loop.h"
 #include "server.h"
 
 using android::net::wifi::IWificond;
+using android::wificond::ipc_constants::kDevModePropertyKey;
+using android::wificond::ipc_constants::kDevModeServiceName;
+using android::wificond::ipc_constants::kServiceName;
 
 namespace {
 
@@ -79,7 +84,10 @@ int SetupBinderOrCrash() {
 void RegisterServiceOrCrash(const android::sp<android::IBinder>& service) {
   android::sp<android::IServiceManager> sm = android::defaultServiceManager();
   CHECK_EQ(sm != NULL, true) << "Could not obtain IServiceManager";
-  CHECK_EQ(sm->addService(android::String16("wificond"), service),
+
+  const int8_t dev_mode_on = property_get_bool(kDevModePropertyKey, 0);
+  const char* service_name = (dev_mode_on) ? kDevModeServiceName : kServiceName;
+  CHECK_EQ(sm->addService(android::String16(service_name), service),
            android::NO_ERROR);
 }
 
index 095ee61..2164f32 100644 (file)
 
 #include <ctime>
 
+#include <android-base/logging.h>
 #include <android-base/strings.h>
 #include <binder/IServiceManager.h>
+#include <cutils/properties.h>
 #include <utils/Errors.h>
 #include <utils/String16.h>
 #include <utils/StrongPointer.h>
 
 #include "android/net/wifi/IApInterface.h"
 #include "android/net/wifi/IWificond.h"
+#include "ipc_constants.h"
+#include "server.h"
 #include "tests/shell_utils.h"
 
 using android::String16;
 using android::base::Trim;
 using android::net::wifi::IApInterface;
 using android::net::wifi::IWificond;
+using android::wificond::ipc_constants::kDevModePropertyKey;
+using android::wificond::ipc_constants::kDevModeServiceName;
+using android::wificond::ipc_constants::kServiceName;
 using android::wificond::tests::integration::RunShellCommand;
 
 namespace android {
@@ -41,8 +48,6 @@ namespace {
 const uint32_t kWificondDeathTimeoutSeconds = 10;
 const uint32_t kWificondStartTimeoutSeconds = 10;
 
-const char kWificondServiceName[] = "wificond";
-
 bool WificondIsRunning() {
   std::string output;
   RunShellCommand("pgrep -c ^wificond$", &output);
@@ -66,12 +71,33 @@ bool WaitForTrue(std::function<bool()> condition, time_t timeout_seconds) {
   return false;
 }
 
-bool IsRegistered() {
+bool IsRegisteredAs(const char* service_name) {
   sp<IBinder> service =
-      defaultServiceManager()->checkService(String16(kWificondServiceName));
+      defaultServiceManager()->checkService(String16(service_name));
   return service.get() != nullptr;
 }
 
+bool SetDevMode(bool is_on) {
+  return property_set(kDevModePropertyKey, (is_on) ? "1" : "0") == 0;
+}
+
+class SandboxedTest : public ::testing::Test {
+ protected:
+  void SetUp() override {
+    RunShellCommand("stop wificond");
+    EXPECT_TRUE(SetDevMode(true));
+    RunShellCommand("start wificond");
+    auto in_dev_mode = std::bind(IsRegisteredAs, kDevModeServiceName);
+    EXPECT_TRUE(WaitForTrue(in_dev_mode, kWificondStartTimeoutSeconds));
+  }
+
+  void TearDown() override {
+    RunShellCommand("stop wificond");
+    SetDevMode(false);
+    RunShellCommand("start wificond");
+  }
+};  // class SandboxedTest
+
 }  // namespace
 
 
@@ -81,24 +107,22 @@ TEST(LifeCycleTest, ProcessStartsUp) {
   EXPECT_TRUE(WaitForTrue(WificondIsDead, kWificondDeathTimeoutSeconds));
 
   // Confirm that the service manager has no binder for wificond.
-  EXPECT_FALSE(IsRegistered());
+  EXPECT_FALSE(IsRegisteredAs(kServiceName));
+  EXPECT_FALSE(IsRegisteredAs(kDevModeServiceName));
+  EXPECT_TRUE(SetDevMode(false));  // Clear dev mode
 
   // Start wificond.
   RunShellCommand("start wificond");
   EXPECT_TRUE(WaitForTrue(WificondIsRunning, kWificondStartTimeoutSeconds));
 
   // wificond should eventually register with the service manager.
-  EXPECT_TRUE(WaitForTrue(IsRegistered, kWificondStartTimeoutSeconds));
+  EXPECT_TRUE(WaitForTrue(std::bind(IsRegisteredAs, kServiceName),
+                          kWificondStartTimeoutSeconds));
 }
 
-TEST(LifeCycleTest, CanCreateApInterfaces) {
-  // Restart wificond and wait for it to come back.
-  RunShellCommand("stop wificond");
-  RunShellCommand("start wificond");
-  EXPECT_TRUE(WaitForTrue(IsRegistered, kWificondStartTimeoutSeconds));
-
+TEST_F(SandboxedTest, CanCreateApInterfaces) {
   sp<IWificond> service;
-  ASSERT_EQ(getService(String16(kWificondServiceName), &service), NO_ERROR);
+  ASSERT_EQ(NO_ERROR, getService(String16(kDevModeServiceName), &service));
 
   // We should be able to create an AP interface.
   sp<IApInterface> ap_interface;