update_engine: Use p2p when enterprise-enrolled and policy is unset.

If au_p2p_enabled is unset in the CPanel and the device is
enterprise-enrolled, use p2p. Non-enterprise-enrolled devices will
continue to not use p2p (unless specifically requested by the chrosh
flag).

In effect, this CL enables p2p for enterprises but allows them to opt
out.

BUG=chromium:415563
TEST=New unit tests + unit tests passes.

Change-Id: I8125dfe82c46026e1c97e5dee8abd1e3c4abe12b
Reviewed-on: https://chromium-review.googlesource.com/219332
Reviewed-by: Julian Pastarmov <pastarmovj@chromium.org>
Reviewed-by: Alex Deymo <deymo@chromium.org>
Commit-Queue: David Zeuthen <zeuthen@chromium.org>
Tested-by: David Zeuthen <zeuthen@chromium.org>
diff --git a/p2p_manager.cc b/p2p_manager.cc
index 8c48601..2ec827a 100644
--- a/p2p_manager.cc
+++ b/p2p_manager.cc
@@ -190,19 +190,14 @@
   // either the crosh flag OR by Enterprise Policy, e.g. the following
   // truth table:
   //
+  //  crosh_flag == FALSE  &&  enterprise_policy == unset  -> use_p2p == *
+  //  crosh_flag == TRUE   &&  enterprise_policy == unset  -> use_p2p == TRUE
   //  crosh_flag == FALSE  &&  enterprise_policy == FALSE  -> use_p2p == FALSE
   //  crosh_flag == FALSE  &&  enterprise_policy == TRUE   -> use_p2p == TRUE
   //  crosh_flag == TRUE   &&  enterprise_policy == FALSE  -> use_p2p == TRUE
   //  crosh_flag == TRUE   &&  enterprise_policy == TRUE   -> use_p2p == TRUE
-
-  if (device_policy_ != nullptr) {
-    if (device_policy_->GetAuP2PEnabled(&p2p_enabled)) {
-      if (p2p_enabled) {
-        LOG(INFO) << "Enterprise Policy indicates that p2p is enabled.";
-        return true;
-      }
-    }
-  }
+  //
+  // *: TRUE if Enterprise Enrolled, FALSE otherwise.
 
   if (prefs_ != nullptr &&
       prefs_->Exists(kPrefsP2PEnabled) &&
@@ -212,6 +207,23 @@
     return true;
   }
 
+  if (device_policy_ != nullptr) {
+    if (device_policy_->GetAuP2PEnabled(&p2p_enabled)) {
+      if (p2p_enabled) {
+        LOG(INFO) << "Enterprise Policy indicates that p2p is enabled.";
+        return true;
+      }
+    } else {
+      // Enterprise-enrolled devices have an empty owner in their device policy.
+      string owner;
+      if (!device_policy_->GetOwner(&owner) || owner.empty()) {
+        LOG(INFO) << "No p2p_enabled setting in Enterprise Policy but device "
+                  << "is Enterprise Enrolled so allowing p2p.";
+        return true;
+      }
+    }
+  }
+
   LOG(INFO) << "Neither Enterprise Policy nor crosh flag indicates that p2p "
             << "is enabled.";
   return false;
diff --git a/p2p_manager_unittest.cc b/p2p_manager_unittest.cc
index 456c8ad..59ab152 100644
--- a/p2p_manager_unittest.cc
+++ b/p2p_manager_unittest.cc
@@ -139,6 +139,60 @@
   EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
 }
 
+// Check that IsP2PEnabled() returns TRUE if
+// - The crosh flag is not set.
+// - Enterprise Policy is not set.
+// - Device is Enterprise Enrolled.
+TEST_F(P2PManagerTest, P2PEnabledEnterpriseEnrolledDevicesDefaultToEnabled) {
+  string temp_dir;
+  Prefs prefs;
+  EXPECT_TRUE(utils::MakeTempDirectory("PayloadStateP2PTests.XXXXXX",
+                                       &temp_dir));
+  prefs.Init(base::FilePath(temp_dir));
+
+  scoped_ptr<P2PManager> manager(P2PManager::Construct(test_conf_,
+                                                       &prefs, "cros_au", 3));
+  scoped_ptr<policy::MockDevicePolicy> device_policy(
+      new policy::MockDevicePolicy());
+  // We return an empty owner as this is an enterprise.
+  EXPECT_CALL(*device_policy, GetOwner(testing::_)).WillRepeatedly(
+      DoAll(testing::SetArgumentPointee<0>(std::string("")),
+            testing::Return(true)));
+  manager->SetDevicePolicy(device_policy.get());
+  EXPECT_TRUE(manager->IsP2PEnabled());
+
+  EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
+}
+
+// Check that IsP2PEnabled() returns FALSE if
+// - The crosh flag is not set.
+// - Enterprise Policy is set to FALSE.
+// - Device is Enterprise Enrolled.
+TEST_F(P2PManagerTest, P2PEnabledEnterpriseEnrolledDevicesOverrideDefault) {
+  string temp_dir;
+  Prefs prefs;
+  EXPECT_TRUE(utils::MakeTempDirectory("PayloadStateP2PTests.XXXXXX",
+                                       &temp_dir));
+  prefs.Init(base::FilePath(temp_dir));
+
+  scoped_ptr<P2PManager> manager(P2PManager::Construct(test_conf_,
+                                                       &prefs, "cros_au", 3));
+  scoped_ptr<policy::MockDevicePolicy> device_policy(
+      new policy::MockDevicePolicy());
+  // We return an empty owner as this is an enterprise.
+  EXPECT_CALL(*device_policy, GetOwner(testing::_)).WillRepeatedly(
+      DoAll(testing::SetArgumentPointee<0>(std::string("")),
+            testing::Return(true)));
+  // Make Enterprise Policy indicate p2p is not enabled.
+  EXPECT_CALL(*device_policy, GetAuP2PEnabled(testing::_)).WillRepeatedly(
+      DoAll(testing::SetArgumentPointee<0>(false),
+            testing::Return(true)));
+  manager->SetDevicePolicy(device_policy.get());
+  EXPECT_FALSE(manager->IsP2PEnabled());
+
+  EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
+}
+
 // Check that we keep the $N newest files with the .$EXT.p2p extension.
 TEST_F(P2PManagerTest, Housekeeping) {
   scoped_ptr<P2PManager> manager(P2PManager::Construct(test_conf_,