1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/sync/engine_impl/js_sync_encryption_handler_observer.h"
#include "base/location.h"
#include "base/run_loop.h"
#include "base/values.h"
#include "components/sync/base/cryptographer.h"
#include "components/sync/base/fake_encryptor.h"
#include "components/sync/base/model_type.h"
#include "components/sync/base/passphrase_type.h"
#include "components/sync/base/time.h"
#include "components/sync/engine/sync_string_conversions.h"
#include "components/sync/js/js_event_details.h"
#include "components/sync/js/js_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace syncer {
namespace {
using ::testing::InSequence;
using ::testing::StrictMock;
class JsSyncEncryptionHandlerObserverTest : public testing::Test {
protected:
JsSyncEncryptionHandlerObserverTest() {
js_sync_encryption_handler_observer_.SetJsEventHandler(
mock_js_event_handler_.AsWeakHandle());
}
private:
// This must be destroyed after the member variables below in order
// for WeakHandles to be destroyed properly.
base::MessageLoop message_loop_;
protected:
StrictMock<MockJsEventHandler> mock_js_event_handler_;
JsSyncEncryptionHandlerObserver js_sync_encryption_handler_observer_;
void PumpLoop() { base::RunLoop().RunUntilIdle(); }
};
TEST_F(JsSyncEncryptionHandlerObserverTest, NoArgNotifiations) {
InSequence dummy;
EXPECT_CALL(
mock_js_event_handler_,
HandleJsEvent("onEncryptionComplete", HasDetails(JsEventDetails())));
js_sync_encryption_handler_observer_.OnEncryptionComplete();
PumpLoop();
}
TEST_F(JsSyncEncryptionHandlerObserverTest, OnPassphraseRequired) {
InSequence dummy;
base::DictionaryValue reason_passphrase_not_required_details;
base::DictionaryValue reason_encryption_details;
base::DictionaryValue reason_decryption_details;
reason_passphrase_not_required_details.SetString(
"reason",
PassphraseRequiredReasonToString(REASON_PASSPHRASE_NOT_REQUIRED));
reason_encryption_details.SetString(
"reason", PassphraseRequiredReasonToString(REASON_ENCRYPTION));
reason_decryption_details.SetString(
"reason", PassphraseRequiredReasonToString(REASON_DECRYPTION));
EXPECT_CALL(mock_js_event_handler_,
HandleJsEvent("onPassphraseRequired",
HasDetailsAsDictionary(
reason_passphrase_not_required_details)));
EXPECT_CALL(mock_js_event_handler_,
HandleJsEvent("onPassphraseRequired",
HasDetailsAsDictionary(reason_encryption_details)));
EXPECT_CALL(mock_js_event_handler_,
HandleJsEvent("onPassphraseRequired",
HasDetailsAsDictionary(reason_decryption_details)));
js_sync_encryption_handler_observer_.OnPassphraseRequired(
REASON_PASSPHRASE_NOT_REQUIRED, sync_pb::EncryptedData());
js_sync_encryption_handler_observer_.OnPassphraseRequired(
REASON_ENCRYPTION, sync_pb::EncryptedData());
js_sync_encryption_handler_observer_.OnPassphraseRequired(
REASON_DECRYPTION, sync_pb::EncryptedData());
PumpLoop();
}
TEST_F(JsSyncEncryptionHandlerObserverTest, OnBootstrapTokenUpdated) {
base::DictionaryValue bootstrap_token_details;
bootstrap_token_details.SetString("bootstrapToken", "<redacted>");
bootstrap_token_details.SetString("type", "PASSPHRASE_BOOTSTRAP_TOKEN");
EXPECT_CALL(mock_js_event_handler_,
HandleJsEvent("onBootstrapTokenUpdated",
HasDetailsAsDictionary(bootstrap_token_details)));
js_sync_encryption_handler_observer_.OnBootstrapTokenUpdated(
"sensitive_token", PASSPHRASE_BOOTSTRAP_TOKEN);
PumpLoop();
}
TEST_F(JsSyncEncryptionHandlerObserverTest, OnEncryptedTypesChanged) {
base::DictionaryValue expected_details;
base::ListValue* encrypted_type_values = new base::ListValue();
const bool encrypt_everything = false;
expected_details.Set("encryptedTypes", encrypted_type_values);
expected_details.SetBoolean("encryptEverything", encrypt_everything);
ModelTypeSet encrypted_types;
for (int i = FIRST_REAL_MODEL_TYPE; i < MODEL_TYPE_COUNT; ++i) {
ModelType type = ModelTypeFromInt(i);
encrypted_types.Put(type);
encrypted_type_values->AppendString(ModelTypeToString(type));
}
EXPECT_CALL(mock_js_event_handler_,
HandleJsEvent("onEncryptedTypesChanged",
HasDetailsAsDictionary(expected_details)));
js_sync_encryption_handler_observer_.OnEncryptedTypesChanged(
encrypted_types, encrypt_everything);
PumpLoop();
}
TEST_F(JsSyncEncryptionHandlerObserverTest, OnCryptographerStateChanged) {
base::DictionaryValue expected_details;
bool expected_ready = false;
bool expected_pending = false;
expected_details.SetBoolean("ready", expected_ready);
expected_details.SetBoolean("hasPendingKeys", expected_pending);
ModelTypeSet encrypted_types;
EXPECT_CALL(mock_js_event_handler_,
HandleJsEvent("onCryptographerStateChanged",
HasDetailsAsDictionary(expected_details)));
FakeEncryptor encryptor;
Cryptographer cryptographer(&encryptor);
js_sync_encryption_handler_observer_.OnCryptographerStateChanged(
&cryptographer);
PumpLoop();
}
TEST_F(JsSyncEncryptionHandlerObserverTest, OnPassphraseTypeChanged) {
InSequence dummy;
base::DictionaryValue passphrase_type_details;
passphrase_type_details.SetString("passphraseType",
"PassphraseType::IMPLICIT_PASSPHRASE");
passphrase_type_details.SetInteger("explicitPassphraseTime", 10);
EXPECT_CALL(mock_js_event_handler_,
HandleJsEvent("onPassphraseTypeChanged",
HasDetailsAsDictionary(passphrase_type_details)));
js_sync_encryption_handler_observer_.OnPassphraseTypeChanged(
PassphraseType::IMPLICIT_PASSPHRASE, ProtoTimeToTime(10));
PumpLoop();
}
} // namespace
} // namespace syncer
|