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_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_,