Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 #include "chrome/browser/chromeos/policy/affiliation_test_helper.h" | |
| 5 | |
| 6 #include <string> | |
| 7 #include <vector> | |
| 8 | |
| 9 #include "base/command_line.h" | |
| 10 #include "base/files/file_path.h" | |
| 11 #include "base/path_service.h" | |
| 12 #include "base/prefs/pref_service.h" | |
| 13 #include "base/prefs/scoped_user_pref_update.h" | |
| 14 #include "chrome/browser/browser_process.h" | |
| 15 #include "chrome/browser/chrome_notification_types.h" | |
| 16 #include "chrome/browser/chromeos/login/existing_user_controller.h" | |
| 17 #include "chrome/browser/chromeos/login/session/user_session_manager_test_api.h" | |
| 18 #include "chrome/browser/chromeos/login/startup_utils.h" | |
| 19 #include "chrome/browser/chromeos/policy/device_cloud_policy_store_chromeos.h" | |
| 20 #include "chrome/browser/chromeos/policy/device_policy_builder.h" | |
| 21 #include "chrome/browser/chromeos/policy/device_policy_cros_browser_test.h" | |
| 22 #include "chromeos/chromeos_paths.h" | |
| 23 #include "chromeos/chromeos_switches.h" | |
| 24 #include "chromeos/dbus/cryptohome_client.h" | |
| 25 #include "chromeos/dbus/fake_session_manager_client.h" | |
| 26 #include "chromeos/dbus/session_manager_client.h" | |
| 27 #include "chromeos/login/auth/key.h" | |
| 28 #include "chromeos/login/auth/user_context.h" | |
| 29 #include "components/policy/core/common/cloud/cloud_policy_core.h" | |
| 30 #include "components/policy/core/common/cloud/cloud_policy_store.h" | |
| 31 #include "components/policy/core/common/cloud/policy_builder.h" | |
| 32 #include "content/public/browser/notification_service.h" | |
| 33 #include "content/public/test/test_utils.h" | |
| 34 #include "crypto/rsa_private_key.h" | |
| 35 #include "testing/gtest/include/gtest/gtest.h" | |
| 36 | |
| 37 namespace chromeos { | |
| 38 | |
| 39 namespace affiliation_test_helper { | |
| 40 | |
| 41 const char kFakeRefreshToken[] = "fake-refresh-token"; | |
| 42 const char kEnterpriseUser[] = "testuser@example.com"; | |
| 43 | |
| 44 void SetUserKeys(policy::UserPolicyBuilder* user_policy) { | |
| 45 std::string username = user_policy->policy_data().username(); | |
| 46 base::FilePath user_keys_dir; | |
| 47 ASSERT_TRUE(PathService::Get(chromeos::DIR_USER_POLICY_KEYS, &user_keys_dir)); | |
| 48 const std::string sanitized_username = | |
| 49 chromeos::CryptohomeClient::GetStubSanitizedUsername(username); | |
| 50 const base::FilePath user_key_file = | |
| 51 user_keys_dir.AppendASCII(sanitized_username).AppendASCII("policy.pub"); | |
| 52 std::vector<uint8> user_key_bits; | |
| 53 ASSERT_TRUE(user_policy->GetSigningKey()->ExportPublicKey(&user_key_bits)); | |
| 54 ASSERT_TRUE(base::CreateDirectory(user_key_file.DirName())); | |
| 55 ASSERT_EQ(base::WriteFile(user_key_file, | |
| 56 reinterpret_cast<const char*>(user_key_bits.data()), | |
| 57 user_key_bits.size()), | |
| 58 static_cast<int>(user_key_bits.size())); | |
| 59 } | |
| 60 | |
| 61 void SetDeviceAffiliationID( | |
| 62 policy::DevicePolicyCrosTestHelper* test_helper, | |
| 63 FakeSessionManagerClient* fake_session_manager_client, | |
| 64 const std::set<std::string>& device_affiliation_ids) { | |
| 65 test_helper->InstallOwnerKey(); | |
| 66 test_helper->MarkAsEnterpriseOwned(); | |
| 67 | |
| 68 policy::DevicePolicyBuilder* device_policy = test_helper->device_policy(); | |
| 69 for (const auto& device_affiliation_id : device_affiliation_ids) { | |
| 70 device_policy->policy_data().add_device_affiliation_ids( | |
| 71 device_affiliation_id); | |
| 72 } | |
| 73 device_policy->SetDefaultSigningKey(); | |
| 74 device_policy->Build(); | |
| 75 | |
| 76 fake_session_manager_client->set_device_policy(device_policy->GetBlob()); | |
| 77 fake_session_manager_client->OnPropertyChangeComplete(true); | |
| 78 } | |
| 79 | |
| 80 void SetUserAffiliationIDs( | |
| 81 policy::UserPolicyBuilder* user_policy, | |
| 82 FakeSessionManagerClient* fake_session_manager_client, | |
| 83 const std::string& user_email, | |
| 84 const std::set<std::string>& user_affiliation_ids) { | |
| 85 user_policy->policy_data().set_username(user_email); | |
| 86 SetUserKeys(user_policy); | |
| 87 for (const auto& user_affiliation_id : user_affiliation_ids) { | |
| 88 user_policy->policy_data().add_user_affiliation_ids(user_affiliation_id); | |
| 89 } | |
| 90 user_policy->Build(); | |
| 91 fake_session_manager_client->set_user_policy(user_email, | |
| 92 user_policy->GetBlob()); | |
| 93 } | |
| 94 | |
| 95 void PreLoginUser(const std::string& user_id) { | |
| 96 ListPrefUpdate users_pref(g_browser_process->local_state(), "LoggedInUsers"); | |
| 97 users_pref->AppendIfNotPresent(new base::StringValue(user_id)); | |
| 98 chromeos::StartupUtils::MarkOobeCompleted(); | |
| 99 } | |
| 100 | |
| 101 void LoginUser(const std::string& user_id) { | |
| 102 test::UserSessionManagerTestApi session_manager_test_api( | |
| 103 UserSessionManager::GetInstance()); | |
| 104 session_manager_test_api.SetShouldObtainTokenHandleInTests(false); | |
| 105 | |
| 106 chromeos::UserContext user_context(user_id); | |
| 107 user_context.SetGaiaID("gaia-id-" + user_id); | |
| 108 user_context.SetKey(chromeos::Key("password")); | |
| 109 if (user_id == kEnterpriseUser) { | |
| 110 user_context.SetRefreshToken(kFakeRefreshToken); | |
| 111 } | |
| 112 chromeos::ExistingUserController* controller = | |
| 113 chromeos::ExistingUserController::current_controller(); | |
| 114 CHECK(controller); | |
| 115 content::WindowedNotificationObserver observer( | |
| 116 chrome::NOTIFICATION_SESSION_STARTED, | |
| 117 content::NotificationService::AllSources()); | |
| 118 controller->Login(user_context, chromeos::SigninSpecifics()); | |
| 119 observer.Wait(); | |
| 120 | |
| 121 const user_manager::UserList& logged_users = | |
| 122 user_manager::UserManager::Get()->GetLoggedInUsers(); | |
| 123 for (user_manager::UserList::const_iterator it = logged_users.begin(); | |
| 124 it != logged_users.end(); ++it) { | |
| 125 if ((*it)->email() == user_context.GetUserID()) | |
| 126 return; | |
| 127 } | |
| 128 ADD_FAILURE(); | |
|
Andrew T Wilson (Slow)
2015/09/16 15:02:05
Should you add some information to this failure, l
peletskyi
2015/09/21 14:17:24
Done.
| |
| 129 } | |
| 130 | |
| 131 void AppendCommandLineSwitches(base::CommandLine* command_line) { | |
| 132 command_line->AppendSwitch(chromeos::switches::kLoginManager); | |
| 133 command_line->AppendSwitch(chromeos::switches::kForceLoginManagerInTests); | |
| 134 } | |
| 135 | |
| 136 } // namespace affiliation_test_helper | |
| 137 | |
| 138 } // namespace chromeos | |
| OLD | NEW |