1cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
2cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// found in the LICENSE file.
4cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
5cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include <cryptohi.h>
6cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
7cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/macros.h"
8cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/strings/stringprintf.h"
95f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "chrome/browser/chromeos/policy/device_policy_cros_browser_test.h"
10cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/browser/extensions/extension_apitest.h"
11116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "chrome/browser/extensions/extension_service.h"
12cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/browser/net/nss_context.h"
13cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/browser/net/url_request_mock_util.h"
145f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "chromeos/chromeos_switches.h"
155f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "chromeos/login/user_names.h"
16cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/policy/core/browser/browser_policy_connector.h"
17cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/policy/core/common/mock_configuration_policy_provider.h"
18cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/policy/core/common/policy_map.h"
19cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "content/public/browser/notification_service.h"
20cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "content/public/common/content_switches.h"
21cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "content/public/test/test_utils.h"
225f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "crypto/nss_util_internal.h"
235f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "crypto/scoped_test_system_nss_key_slot.h"
245f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "extensions/browser/notification_types.h"
25cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "net/base/net_errors.h"
26cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "net/cert/nss_cert_database.h"
271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "net/test/url_request/url_request_mock_http_job.h"
28cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "policy/policy_constants.h"
29cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "testing/gmock/include/gmock/gmock.h"
30cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
31cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)namespace {
32cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
335f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// The test extension has a certificate referencing this private key which will
345f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// be stored in the user's token in the test setup.
35cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)//
36cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// openssl genrsa > privkey.pem
37cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// openssl pkcs8 -inform pem -in privkey.pem -topk8
38cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)//   -outform der -out privkey8.der -nocrypt
39cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// xxd -i privkey8.der
405f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)const unsigned char privateKeyPkcs8User[] = {
41cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    0x30, 0x82, 0x01, 0x55, 0x02, 0x01, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a,
42cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82,
43cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    0x01, 0x3f, 0x30, 0x82, 0x01, 0x3b, 0x02, 0x01, 0x00, 0x02, 0x41, 0x00,
44cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    0xc7, 0xc1, 0x4d, 0xd5, 0xdc, 0x3a, 0x2e, 0x1f, 0x42, 0x30, 0x3d, 0x21,
45cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    0x1e, 0xa2, 0x1f, 0x60, 0xcb, 0x71, 0x11, 0x53, 0xb0, 0x75, 0xa0, 0x62,
46cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    0xfe, 0x5e, 0x0a, 0xde, 0xb0, 0x0f, 0x48, 0x97, 0x5e, 0x42, 0xa7, 0x3a,
47cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    0xd1, 0xca, 0x4c, 0xe3, 0xdb, 0x5f, 0x31, 0xc2, 0x99, 0x08, 0x89, 0xcd,
48cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    0x6d, 0x20, 0xaa, 0x75, 0xe6, 0x2b, 0x98, 0xd2, 0xf3, 0x7b, 0x4b, 0xe5,
49cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    0x9b, 0xfe, 0xe2, 0x6d, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x40, 0x4a,
50cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    0xf5, 0x76, 0x10, 0xe7, 0xb8, 0x89, 0x70, 0x3f, 0x75, 0x3c, 0xab, 0x3e,
51cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    0x04, 0x96, 0x83, 0xcb, 0x34, 0x1d, 0xcd, 0x6a, 0xed, 0x69, 0x07, 0x5c,
52cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    0xee, 0xcb, 0x63, 0x6f, 0x6b, 0xfc, 0xcf, 0xee, 0xa2, 0xc4, 0x67, 0x05,
53cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    0x68, 0x4d, 0x21, 0x7e, 0x3e, 0xde, 0x74, 0x72, 0xf8, 0x04, 0x35, 0x66,
54cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    0x1e, 0x6b, 0x1d, 0xef, 0x77, 0xf7, 0x33, 0xf0, 0x35, 0xcf, 0x35, 0x6e,
55cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    0x53, 0x3f, 0x9d, 0x02, 0x21, 0x00, 0xee, 0x48, 0x67, 0x1b, 0x24, 0x6e,
56cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    0x3d, 0x7b, 0xa0, 0xc3, 0xee, 0x8a, 0x2e, 0xc7, 0xd0, 0xa1, 0xdb, 0x25,
57cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    0x31, 0x12, 0x99, 0x43, 0x06, 0x3c, 0xb0, 0x80, 0x35, 0x2b, 0xf4, 0xc5,
58cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    0xa2, 0xd3, 0x02, 0x21, 0x00, 0xd6, 0x9b, 0x8b, 0x75, 0x91, 0x52, 0xd4,
59cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    0xf0, 0x76, 0xcf, 0xa2, 0xbe, 0xa6, 0xaf, 0x72, 0x6c, 0x52, 0xf9, 0xc9,
60cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    0x0e, 0xea, 0x4a, 0x4c, 0xd2, 0xdf, 0x25, 0x70, 0xc6, 0x66, 0x35, 0x9d,
61cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    0xbf, 0x02, 0x21, 0x00, 0xe8, 0x9e, 0x40, 0x21, 0xcc, 0x37, 0xde, 0xc7,
62cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    0xd1, 0x13, 0x55, 0xcd, 0x0a, 0x8c, 0x40, 0xcd, 0xb1, 0xed, 0xa5, 0xf1,
63cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    0x7d, 0x33, 0x64, 0x64, 0x5c, 0xfe, 0x5c, 0x6a, 0x34, 0x03, 0xb8, 0xc7,
64cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    0x02, 0x20, 0x17, 0xe1, 0xb5, 0x52, 0x3e, 0xfa, 0xc5, 0xc1, 0x80, 0xa7,
65cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    0x38, 0x88, 0x18, 0xca, 0x7b, 0x64, 0x3c, 0x93, 0x99, 0x61, 0x34, 0x87,
66cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    0x52, 0x27, 0x41, 0x37, 0xcc, 0x65, 0xf7, 0xa7, 0xcd, 0xc7, 0x02, 0x21,
67cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    0x00, 0x8a, 0x17, 0x7f, 0xf9, 0x45, 0xf3, 0xfd, 0xf7, 0x96, 0x62, 0xf3,
68cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    0x7a, 0x09, 0xfb, 0xe9, 0x9e, 0xc7, 0x7a, 0x1f, 0x53, 0x1a, 0xb8, 0xd5,
69cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    0x88, 0x9d, 0xd4, 0x79, 0x57, 0x88, 0x68, 0x72, 0x6f};
70cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
715f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// The test extension has a certificate referencing this private key which will
725f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// be stored in the system token in the test setup.
735f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)const unsigned char privateKeyPkcs8System[] = {
745f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    0x30, 0x82, 0x01, 0x54, 0x02, 0x01, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a,
755f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82,
765f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    0x01, 0x3e, 0x30, 0x82, 0x01, 0x3a, 0x02, 0x01, 0x00, 0x02, 0x41, 0x00,
775f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    0xe8, 0xb3, 0x04, 0xb1, 0xad, 0xef, 0x6b, 0xe5, 0xbe, 0xc9, 0x05, 0x75,
785f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    0x07, 0x41, 0xf5, 0x70, 0x50, 0xc2, 0xe8, 0xee, 0xeb, 0x09, 0x9d, 0x49,
795f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    0x64, 0x4c, 0x60, 0x61, 0x80, 0xbe, 0xc5, 0x41, 0xf3, 0x8c, 0x57, 0x90,
805f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    0x3a, 0x44, 0x62, 0x6d, 0x51, 0xb8, 0xbb, 0xc6, 0x9a, 0x16, 0xdf, 0xf9,
815f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    0xce, 0xe3, 0xb8, 0x8c, 0x2e, 0xa2, 0x16, 0xc8, 0xed, 0xc7, 0xf8, 0x4f,
825f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    0xbd, 0xd3, 0x6e, 0x63, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x40, 0x76,
835f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    0xc9, 0x83, 0xf8, 0xeb, 0xd0, 0x8f, 0xa4, 0xdd, 0x4a, 0xa2, 0xe5, 0x85,
845f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    0xc9, 0xee, 0xef, 0xe1, 0xda, 0x4d, 0xac, 0x41, 0x01, 0x4c, 0x70, 0x7d,
855f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    0xa9, 0xdb, 0x7d, 0x8a, 0x8a, 0x58, 0x09, 0x04, 0x45, 0x43, 0xa4, 0xf3,
865f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    0xb4, 0x98, 0xf6, 0x34, 0x68, 0x5f, 0xc1, 0xc2, 0xa7, 0x86, 0x3e, 0xec,
875f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    0x84, 0x0b, 0x18, 0xbc, 0xb1, 0xee, 0x6f, 0x3f, 0xb1, 0x6d, 0xbc, 0x3e,
885f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    0xbf, 0x6d, 0x31, 0x02, 0x21, 0x00, 0xff, 0x9d, 0x90, 0x4f, 0x0e, 0xe8,
895f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    0x7e, 0xf3, 0x38, 0xa7, 0xec, 0x73, 0x80, 0xf9, 0x39, 0x2c, 0xaa, 0x33,
905f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    0x91, 0x72, 0x10, 0x7c, 0x8b, 0xc3, 0x61, 0x6d, 0x40, 0x96, 0xac, 0xb3,
915f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    0x5e, 0xc9, 0x02, 0x21, 0x00, 0xe9, 0x0c, 0xa1, 0x34, 0xf2, 0x43, 0x3c,
925f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    0x74, 0xec, 0x1a, 0xf6, 0x80, 0x8e, 0x50, 0x10, 0x6d, 0x55, 0x64, 0xce,
935f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    0x47, 0x4a, 0x1e, 0x34, 0x27, 0x6c, 0x49, 0x79, 0x6a, 0x23, 0xc6, 0x9d,
945f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    0xcb, 0x02, 0x20, 0x48, 0xda, 0xa8, 0xc1, 0xcf, 0xb6, 0xf6, 0x4f, 0xee,
955f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    0x4a, 0xf6, 0x3a, 0xa9, 0x7c, 0xdf, 0x0d, 0xda, 0xe8, 0xdd, 0xc0, 0x8b,
965f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    0xf0, 0x63, 0x89, 0x69, 0x60, 0x51, 0x33, 0x60, 0xbf, 0xb2, 0xf9, 0x02,
975f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    0x21, 0x00, 0xb4, 0x77, 0x81, 0x46, 0x7c, 0xec, 0x30, 0x1e, 0xe2, 0xcf,
985f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    0x26, 0x5f, 0xfa, 0xd4, 0x69, 0x44, 0x21, 0x42, 0x84, 0xb2, 0x93, 0xe4,
995f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    0xbb, 0xc2, 0x63, 0x8a, 0xaa, 0x28, 0xd5, 0x37, 0x72, 0xed, 0x02, 0x20,
1005f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    0x16, 0xde, 0x3d, 0x57, 0xc5, 0xd5, 0x3d, 0x90, 0x8b, 0xfd, 0x90, 0x3b,
1015f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    0xd8, 0x71, 0x69, 0x5e, 0x8d, 0xb4, 0x48, 0x1c, 0xa4, 0x01, 0xce, 0xc1,
1025f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    0xb5, 0x6f, 0xe9, 0x1b, 0x32, 0x91, 0x34, 0x38
1035f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)};
1045f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
105cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)const base::FilePath::CharType kTestExtensionDir[] =
106cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    FILE_PATH_LITERAL("extensions/api_test/enterprise_platform_keys");
107cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)const base::FilePath::CharType kUpdateManifestFileName[] =
108cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    FILE_PATH_LITERAL("update_manifest.xml");
109cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
1105f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void ImportPrivateKeyPKCS8ToSlot(const unsigned char* pkcs8_der,
1115f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                                 size_t pkcs8_der_size,
1125f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                                 PK11SlotInfo* slot) {
1135f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  SECItem pki_der_user = {
114cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      siBuffer,
115cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      // NSS requires non-const data even though it is just for input.
1165f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      const_cast<unsigned char*>(pkcs8_der),
1175f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      pkcs8_der_size};
118cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
1195f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  SECKEYPrivateKey* seckey = NULL;
120cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ASSERT_EQ(SECSuccess,
1215f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)            PK11_ImportDERPrivateKeyInfoAndReturnKey(slot,
1225f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                                                     &pki_der_user,
123cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                                     NULL,    // nickname
124cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                                     NULL,    // publicValue
125cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                                     true,    // isPerm
126cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                                     true,    // isPrivate
127cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                                     KU_ALL,  // usage
1285f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                                                     &seckey,
129cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                                     NULL));
130cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
131cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
1325f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// The managed_storage extension has a key defined in its manifest, so that
1335f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// its extension ID is well-known and the policy system can push policies for
1345f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// the extension.
1355f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)const char kTestExtensionID[] = "aecpbnckhoppanpmefllkdkohionpmig";
1365f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
1371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccienum SystemToken {
1381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  SYSTEM_TOKEN_EXISTS,
1391320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  SYSTEM_TOKEN_NOT_EXISTS
1401320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci};
1411320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccienum DeviceStatus {
1431320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  DEVICE_STATUS_ENROLLED,
1441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  DEVICE_STATUS_NOT_ENROLLED
1451320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci};
1461320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1471320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccienum UserAffiliation {
1481320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  USER_AFFILIATION_ENROLLED_DOMAIN,
1491320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  USER_AFFILIATION_UNRELATED
1501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci};
1511320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1521320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccistruct Params {
1531320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  Params(SystemToken system_token,
1541320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci         DeviceStatus device_status,
1551320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci         UserAffiliation user_affiliation)
1561320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      : system_token_(system_token),
1571320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        device_status_(device_status),
1581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        user_affiliation_(user_affiliation) {}
1591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1601320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  SystemToken system_token_;
1611320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  DeviceStatus device_status_;
1621320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  UserAffiliation user_affiliation_;
1631320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci};
1641320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1651320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciclass EnterprisePlatformKeysTest
1661320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    : public ExtensionApiTest,
1671320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      public ::testing::WithParamInterface<Params> {
168cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) public:
1691320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  EnterprisePlatformKeysTest() {}
1705f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
171cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
172cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    ExtensionApiTest::SetUpCommandLine(command_line);
173cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
174cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    // Enable the WebCrypto API.
175cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    command_line->AppendSwitch(
176cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        switches::kEnableExperimentalWebPlatformFeatures);
1775f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
1781320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    std::string user_email = "someuser@anydomain.com";
1791320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1801320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    // The command line flag kLoginUser determines the user's email and thus
1811320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    // his affiliation to the domain that the device is enrolled to.
1821320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    if (GetParam().user_affiliation_ == USER_AFFILIATION_ENROLLED_DOMAIN)
1831320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      user_email = chromeos::login::kStubUser;
1841320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1851320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    command_line->AppendSwitchASCII(chromeos::switches::kLoginUser, user_email);
186cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
187cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
188cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
189cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    ExtensionApiTest::SetUpInProcessBrowserTestFixture();
190cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
1911320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    if (GetParam().device_status_ == DEVICE_STATUS_ENROLLED) {
1921320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      device_policy_test_helper_.device_policy()->policy_data().set_username(
1931320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          chromeos::login::kStubUser);
1945f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
1951320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      device_policy_test_helper_.device_policy()->Build();
1961320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      device_policy_test_helper_.MarkAsEnterpriseOwned();
1971320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    }
1985f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
199cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    EXPECT_CALL(policy_provider_, IsInitializationComplete(testing::_))
200cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        .WillRepeatedly(testing::Return(true));
201cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    policy_provider_.SetAutoRefresh();
202cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    policy::BrowserPolicyConnector::SetPolicyProviderForTesting(
203cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        &policy_provider_);
204cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
205cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
206cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual void SetUpOnMainThread() OVERRIDE {
2071320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    if (GetParam().system_token_ == SYSTEM_TOKEN_EXISTS) {
2081320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      base::RunLoop loop;
2091320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      content::BrowserThread::PostTask(
2101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          content::BrowserThread::IO,
2111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          FROM_HERE,
2121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          base::Bind(&EnterprisePlatformKeysTest::SetUpTestSystemSlotOnIO,
2131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                     base::Unretained(this),
2141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                     browser()->profile()->GetResourceContext(),
2151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                     loop.QuitClosure()));
2161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      loop.Run();
2171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    }
2181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
219cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    ExtensionApiTest::SetUpOnMainThread();
220cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
221cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    // Enable the URLRequestMock, which is required for force-installing the
222cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    // test extension through policy.
223cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    content::BrowserThread::PostTask(
224cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        content::BrowserThread::IO,
225cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        FROM_HERE,
226cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        base::Bind(chrome_browser_net::SetUrlRequestMocksEnabled, true));
227cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
228cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    {
229cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      base::RunLoop loop;
2305f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      GetNSSCertDatabaseForProfile(
2315f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)          browser()->profile(),
2325f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)          base::Bind(&EnterprisePlatformKeysTest::DidGetCertDatabase,
2335f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                     base::Unretained(this),
2345f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                     loop.QuitClosure()));
235cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      loop.Run();
236cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    }
237cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
238cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    SetPolicy();
239cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
240cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
2411320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  virtual void TearDownOnMainThread() OVERRIDE {
2421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    ExtensionApiTest::TearDownOnMainThread();
2435f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
2441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    if (GetParam().system_token_ == SYSTEM_TOKEN_EXISTS) {
2451320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      base::RunLoop loop;
2461320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      content::BrowserThread::PostTask(
2471320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          content::BrowserThread::IO,
2481320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          FROM_HERE,
2491320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          base::Bind(&EnterprisePlatformKeysTest::TearDownTestSystemSlotOnIO,
2501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                     base::Unretained(this),
2511320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                     loop.QuitClosure()));
2521320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      loop.Run();
2531320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    }
2545f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  }
2555f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
256cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) private:
2575f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  void DidGetCertDatabase(const base::Closure& done_callback,
2585f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                          net::NSSCertDatabase* cert_db) {
2595f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    // In order to use a prepared certificate, import a private key to the
2605f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    // user's token for which the Javscript test will import the certificate.
2615f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    ImportPrivateKeyPKCS8ToSlot(privateKeyPkcs8User,
2625f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                                arraysize(privateKeyPkcs8User),
2635f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                                cert_db->GetPrivateSlot().get());
2645f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    done_callback.Run();
2655f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  }
2665f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
2675f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  void SetUpTestSystemSlotOnIO(content::ResourceContext* context,
2681320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                               const base::Closure& done_callback) {
2695f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    test_system_slot_.reset(new crypto::ScopedTestSystemNSSKeySlot());
2705f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    ASSERT_TRUE(test_system_slot_->ConstructedSuccessfully());
2715f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
2725f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    // Import a private key to the system slot.  The Javascript part of this
2735f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    // test has a prepared certificate for this key.
2745f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    ImportPrivateKeyPKCS8ToSlot(privateKeyPkcs8System,
2755f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                                arraysize(privateKeyPkcs8System),
2765f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                                test_system_slot_->slot());
2775f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
2785f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    content::BrowserThread::PostTask(
2795f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        content::BrowserThread::UI, FROM_HERE, done_callback);
2805f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  }
2815f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
2825f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  void TearDownTestSystemSlotOnIO(const base::Closure& done_callback) {
2835f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    test_system_slot_.reset();
2845f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
2855f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    content::BrowserThread::PostTask(
2865f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        content::BrowserThread::UI, FROM_HERE, done_callback);
2875f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  }
2885f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
289cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  void SetPolicy() {
290cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    // Extensions that are force-installed come from an update URL, which
291cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    // defaults to the webstore. Use a mock URL for this test with an update
292cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    // manifest that includes the crx file of the test extension.
293cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    base::FilePath update_manifest_path =
294cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        base::FilePath(kTestExtensionDir).Append(kUpdateManifestFileName);
295cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    GURL update_manifest_url(
2961320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        net::URLRequestMockHTTPJob::GetMockUrl(update_manifest_path));
297cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
298cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    scoped_ptr<base::ListValue> forcelist(new base::ListValue);
299cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    forcelist->AppendString(base::StringPrintf(
300cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        "%s;%s", kTestExtensionID, update_manifest_url.spec().c_str()));
301cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
302cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    policy::PolicyMap policy;
303cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    policy.Set(policy::key::kExtensionInstallForcelist,
304cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)               policy::POLICY_LEVEL_MANDATORY,
305cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)               policy::POLICY_SCOPE_MACHINE,
306cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)               forcelist.release(),
307cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)               NULL);
308cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
309cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    // Set the policy and wait until the extension is installed.
310cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    content::WindowedNotificationObserver observer(
3115f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        extensions::NOTIFICATION_EXTENSION_WILL_BE_INSTALLED_DEPRECATED,
312cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        content::NotificationService::AllSources());
313cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    policy_provider_.UpdateChromePolicy(policy);
314cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    observer.Wait();
315cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
316cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
3175f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  policy::DevicePolicyCrosTestHelper device_policy_test_helper_;
3185f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  scoped_ptr<crypto::ScopedTestSystemNSSKeySlot> test_system_slot_;
319cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  policy::MockConfigurationPolicyProvider policy_provider_;
320cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)};
321cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
322cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}  // namespace
323cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
3241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciIN_PROC_BROWSER_TEST_P(EnterprisePlatformKeysTest, Basic) {
3251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // By default, the system token is disabled.
3261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  std::string system_token_availability = "";
3271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
3281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Only if the system token exists, and the current user is of the same domain
3291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // as the device is enrolled to, the system token is available to the
3301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // extension.
3311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (GetParam().system_token_ == SYSTEM_TOKEN_EXISTS &&
3321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      GetParam().device_status_ == DEVICE_STATUS_ENROLLED &&
3331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      GetParam().user_affiliation_ == USER_AFFILIATION_ENROLLED_DOMAIN) {
3341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    system_token_availability = "systemTokenEnabled";
3351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  }
3365f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
337cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ASSERT_TRUE(RunExtensionSubtest(
338cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      "",
3391320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      base::StringPrintf("chrome-extension://%s/basic.html?%s",
3401320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                         kTestExtensionID,
3411320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                         system_token_availability.c_str())))
342cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      << message_;
343cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
344116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
3451320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciINSTANTIATE_TEST_CASE_P(
3461320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    CheckSystemTokenAvailability,
3471320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    EnterprisePlatformKeysTest,
3481320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    ::testing::Values(Params(SYSTEM_TOKEN_EXISTS,
3491320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                             DEVICE_STATUS_ENROLLED,
3501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                             USER_AFFILIATION_ENROLLED_DOMAIN),
3511320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                      Params(SYSTEM_TOKEN_EXISTS,
3521320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                             DEVICE_STATUS_ENROLLED,
3531320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                             USER_AFFILIATION_UNRELATED),
3541320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                      Params(SYSTEM_TOKEN_EXISTS,
3551320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                             DEVICE_STATUS_NOT_ENROLLED,
3561320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                             USER_AFFILIATION_UNRELATED),
3571320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                      Params(SYSTEM_TOKEN_NOT_EXISTS,
3581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                             DEVICE_STATUS_ENROLLED,
3591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                             USER_AFFILIATION_ENROLLED_DOMAIN)));
3601320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
3611320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciclass EnterprisePlatformKeysTestNonPolicyInstalledExtension
3621320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    : public EnterprisePlatformKeysTest {};
3631320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
364116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Ensure that extensions that are not pre-installed by policy throw an install
365116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// warning if they request the enterprise.platformKeys permission in the
366116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// manifest and that such extensions don't see the
367116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// chrome.enterprise.platformKeys namespace.
368116680a4aac90f2aa7413d9095a592090648e557Ben MurdochIN_PROC_BROWSER_TEST_F(ExtensionApiTest,
369116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                       EnterprisePlatformKeysIsRestrictedToPolicyExtension) {
370116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  ASSERT_TRUE(RunExtensionSubtest("enterprise_platform_keys",
371116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                  "api_not_available.html",
372116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                  kFlagIgnoreManifestWarnings));
373116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
374116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  base::FilePath extension_path =
375116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      test_data_dir_.AppendASCII("enterprise_platform_keys");
3761320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  ExtensionService* service =
3771320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      extensions::ExtensionSystem::Get(profile())->extension_service();
378116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  const extensions::Extension* extension =
379116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      GetExtensionByPath(service->extensions(), extension_path);
380116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  ASSERT_FALSE(extension->install_warnings().empty());
381116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_EQ(
382116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      "'enterprise.platformKeys' is not allowed for specified install "
383116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      "location.",
384116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      extension->install_warnings()[0].message);
385116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
386