15f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
25f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// found in the LICENSE file.
45f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
55f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "crypto/scoped_test_nss_chromeos_user.h"
65f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
75f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "base/logging.h"
85f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "crypto/nss_util.h"
95f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "crypto/nss_util_internal.h"
105f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
115f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)namespace crypto {
125f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
135f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)ScopedTestNSSChromeOSUser::ScopedTestNSSChromeOSUser(
145f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    const std::string& username_hash)
155f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    : username_hash_(username_hash), constructed_successfully_(false) {
165f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  if (!temp_dir_.CreateUniqueTempDir())
175f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    return;
185f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // This opens a software DB in the given folder. In production code that is in
195f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // the home folder, but for testing the temp folder is used.
201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  constructed_successfully_ =
211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      InitializeNSSForChromeOSUser(username_hash, temp_dir_.path());
225f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
235f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
245f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)ScopedTestNSSChromeOSUser::~ScopedTestNSSChromeOSUser() {
255f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  if (constructed_successfully_)
265f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    CloseChromeOSUserForTesting(username_hash_);
275f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
285f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
295f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void ScopedTestNSSChromeOSUser::FinishInit() {
305f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  DCHECK(constructed_successfully_);
315f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  if (!ShouldInitializeTPMForChromeOSUser(username_hash_))
325f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    return;
335f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  WillInitializeTPMForChromeOSUser(username_hash_);
345f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  InitializePrivateSoftwareSlotForChromeOSUser(username_hash_);
355f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
365f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
375f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}  // namespace crypto
38