Ejemplo n.º 1
0
        public void TestSymmetricEncryptionWithCompression()
        {
            ICryptoRandomService randomGenerator = CommonMocksAndStubs.CryptoRandomService(88);
            ICryptor             encryptor       = new Cryptor("sugus", randomGenerator);

            byte[]       binaryMessage = CommonMocksAndStubs.FilledByteArray(1024, 88);
            SecureString password      = CryptoUtils.StringToSecureString("Der schnelle Uhu fliegt über den faulen Hund.");

            byte[] cipher = encryptor.Encrypt(
                binaryMessage,
                password,
                KeyDerivationCostType.Low,
                BouncyCastleTwofishGcm.CryptoAlgorithmName,
                Pbkdf2.CryptoKdfName,
                Cryptor.CompressionGzip);
            byte[] decryptedMessage = encryptor.Decrypt(cipher, password);
            Assert.AreEqual(binaryMessage, decryptedMessage);
        }
Ejemplo n.º 2
0
        public void ObfuscationCanBeReversed()
        {
            SecureString         obfuscationKey  = CryptoUtils.StringToSecureString("A very strong passphrase...");
            ICryptoRandomService randomGenerator = CommonMocksAndStubs.CryptoRandomService();

            byte[] binaryMessage = randomGenerator.GetRandomBytes(100);

            var obfuscatedMessage   = CryptoUtils.Obfuscate(binaryMessage, obfuscationKey, randomGenerator);
            var deobfuscatedMessage = CryptoUtils.Deobfuscate(obfuscatedMessage, obfuscationKey);

            Assert.AreEqual(binaryMessage, deobfuscatedMessage);

            string plaintextMessage        = "welcome home";
            string obfuscatedMessageText   = CryptoUtils.Obfuscate(plaintextMessage, obfuscationKey, randomGenerator);
            string deobfuscatedMessageText = CryptoUtils.Deobfuscate(obfuscatedMessageText, obfuscationKey);

            Assert.AreEqual(plaintextMessage, deobfuscatedMessageText);
        }
Ejemplo n.º 3
0
        public void AccountIsStoredWhenDifferent()
        {
            SerializeableCloudStorageCredentials credentialsFromSession = new SerializeableCloudStorageCredentials {
                CloudStorageId = CloudStorageClientFactory.CloudStorageIdDropbox
            };
            SettingsModel settingsModel = new SettingsModel {
                Credentials = new SerializeableCloudStorageCredentials {
                    CloudStorageId = CloudStorageClientFactory.CloudStorageIdFtp
                }
            };

            Mock <IStoryBoard> storyBoard = new Mock <IStoryBoard>();

            storyBoard.
            Setup(m => m.LoadFromSession <SerializeableCloudStorageCredentials>(It.Is <SynchronizationStorySessionKey>(p => p == SynchronizationStorySessionKey.CloudStorageCredentials))).
            Returns(credentialsFromSession);
            Mock <ISettingsService> settingsService = new Mock <ISettingsService>();

            settingsService.
            Setup(m => m.LoadSettingsOrDefault()).Returns(settingsModel);
            Mock <ICloudStorageClient> cloudStorageClient = new Mock <ICloudStorageClient>();

            cloudStorageClient.
            Setup(m => m.ExistsFileAsync(It.IsAny <string>(), It.IsAny <CloudStorageCredentials>())).
            ReturnsAsync(true);

            // Run step
            var step = new ExistsCloudRepositoryStep(
                SynchronizationStoryStepId.ExistsCloudRepository,
                storyBoard.Object,
                CommonMocksAndStubs.LanguageService(),
                CommonMocksAndStubs.FeedbackService(),
                settingsService.Object,
                CommonMocksAndStubs.CloudStorageClientFactory(cloudStorageClient.Object));

            Assert.DoesNotThrowAsync(step.Run);

            // Settings are stored with account from session
            settingsService.Verify(m => m.TrySaveSettingsToLocalDevice(It.Is <SettingsModel>(s => s.Credentials == credentialsFromSession)), Times.Once);

            // Next step is called
            storyBoard.Verify(m => m.ContinueWith(It.Is <SynchronizationStoryStepId>(x => x == SynchronizationStoryStepId.DownloadCloudRepository)), Times.Once);
        }
Ejemplo n.º 4
0
        public void CryptoPbkdf2()
        {
            const int KeyLength        = 42;
            IKeyDerivationFunction kdf = new Pbkdf2();

            ICryptoRandomService randomGenerator = CommonMocksAndStubs.CryptoRandomService();

            byte[] salt = randomGenerator.GetRandomBytes(kdf.ExpectedSaltSizeBytes);
            int    cost = kdf.RecommendedCost(KeyDerivationCostType.Low);

            SecureString password = CryptoUtils.StringToSecureString("Das ist kein gutes Passwort");

            byte[] key = kdf.DeriveKeyFromPassword(password, KeyLength, salt, cost);
            Assert.AreEqual(KeyLength, key.Length);

            // Same parameters must result in the same output
            byte[] key2 = kdf.DeriveKeyFromPassword(password, KeyLength, salt, cost);
            Assert.AreEqual(key, key2);
        }
Ejemplo n.º 5
0
        public void QuitWhenMissingClientOrTransfercode()
        {
            SerializeableCloudStorageCredentials credentials = new SerializeableCloudStorageCredentials {
                CloudStorageId = CloudStorageClientFactory.CloudStorageIdDropbox
            };
            SettingsModel settingsModel = new SettingsModel {
                Credentials = credentials
            };

            Mock <IStoryBoard>      storyBoard      = new Mock <IStoryBoard>();
            Mock <ISettingsService> settingsService = new Mock <ISettingsService>();

            settingsService.
            Setup(m => m.LoadSettingsOrDefault()).Returns(settingsModel);
            Mock <ICloudStorageClient> cloudStorageClient = new Mock <ICloudStorageClient>();

            cloudStorageClient.
            Setup(m => m.ExistsFileAsync(It.IsAny <string>(), It.IsAny <CloudStorageCredentials>())).
            ReturnsAsync(true);

            // Run step with missing transfercode
            var step = new ExistsCloudRepositoryStep(
                PullPushStoryStepId.ExistsCloudRepository,
                storyBoard.Object,
                CommonMocksAndStubs.LanguageService(),
                CommonMocksAndStubs.FeedbackService(),
                settingsService.Object,
                CommonMocksAndStubs.CloudStorageClientFactory(cloudStorageClient.Object));

            Assert.DoesNotThrowAsync(step.Run);

            // Next step is called
            storyBoard.Verify(m => m.ContinueWith(It.Is <PullPushStoryStepId>(x => x == PullPushStoryStepId.DownloadCloudRepository)), Times.Never);

            // Run step with missing storage client
            settingsModel.TransferCode = "abc";
            settingsModel.Credentials.CloudStorageId = null;
            Assert.DoesNotThrowAsync(step.Run);

            // Next step is called
            storyBoard.Verify(m => m.ContinueWith(It.Is <PullPushStoryStepId>(x => x == PullPushStoryStepId.DownloadCloudRepository)), Times.Never);
        }
        public void RejectWhenNotesAreEqual()
        {
            NoteModel           noteModel       = new NoteModel();
            NoteRepositoryModel repositoryModel = new NoteRepositoryModel();

            repositoryModel.Notes.Add(noteModel);

            Mock <IStoryBoard> storyBoard = new Mock <IStoryBoard>();

            storyBoard.
            Setup(m => m.LoadFromSession <NoteRepositoryModel>(It.Is <PullPushStorySessionKey>(p => p == PullPushStorySessionKey.CloudRepository))).
            Returns(repositoryModel);     // same as from repositoryStorageService
            Mock <ISettingsService> settingsService = new Mock <ISettingsService>();

            settingsService.
            Setup(m => m.LoadSettingsOrDefault()).Returns(new SettingsModel());
            Mock <IRepositoryStorageService> repositoryStorageService = new Mock <IRepositoryStorageService>();

            repositoryStorageService.
            Setup(m => m.LoadRepositoryOrDefault(out repositoryModel));     // same as from storyBoard
            Mock <ICloudStorageClient> cloudStorageClient = new Mock <ICloudStorageClient>();

            // Run step
            var step = new StoreMergedRepositoryAndQuitStep(
                PullPushStoryStepId.StoreMergedRepositoryAndQuit,
                storyBoard.Object,
                noteModel.Id,
                PullPushDirection.PullFromServer,
                CommonMocksAndStubs.LanguageService(),
                CommonMocksAndStubs.FeedbackService(),
                settingsService.Object,
                CommonMocksAndStubs.CryptoRandomService(),
                repositoryStorageService.Object,
                CommonMocksAndStubs.CloudStorageClientFactory(cloudStorageClient.Object));;

            Assert.DoesNotThrowAsync(step.Run);

            // repository is not stored to the local device, nor to the cloud
            repositoryStorageService.Verify(m => m.TrySaveRepository(It.IsAny <NoteRepositoryModel>()), Times.Never);
            cloudStorageClient.Verify(m => m.UploadFileAsync(It.IsAny <string>(), It.IsAny <byte[]>(), It.IsAny <CloudStorageCredentials>()), Times.Never);
        }
Ejemplo n.º 7
0
        public void CorrectNextStepWhenNoCloudRepositoryExists()
        {
            SerializeableCloudStorageCredentials credentials = new SerializeableCloudStorageCredentials {
                CloudStorageId = CloudStorageClientFactory.CloudStorageIdDropbox
            };
            SettingsModel settingsModel = new SettingsModel {
                Credentials = credentials
            };

            Mock <IStoryBoard> storyBoard = new Mock <IStoryBoard>();

            storyBoard.
            Setup(m => m.LoadFromSession <SerializeableCloudStorageCredentials>(It.Is <SynchronizationStorySessionKey>(p => p == SynchronizationStorySessionKey.CloudStorageCredentials))).
            Returns(credentials);
            Mock <ISettingsService> settingsService = new Mock <ISettingsService>();

            settingsService.
            Setup(m => m.LoadSettingsOrDefault()).Returns(settingsModel);
            Mock <ICloudStorageClient> cloudStorageClient = new Mock <ICloudStorageClient>();

            cloudStorageClient.
            Setup(m => m.ExistsFileAsync(It.IsAny <string>(), It.IsAny <CloudStorageCredentials>())).
            ReturnsAsync(false);

            // Run step
            var step = new ExistsCloudRepositoryStep(
                SynchronizationStoryStepId.ExistsCloudRepository,
                storyBoard.Object,
                CommonMocksAndStubs.LanguageService(),
                CommonMocksAndStubs.FeedbackService(),
                settingsService.Object,
                CommonMocksAndStubs.CloudStorageClientFactory(cloudStorageClient.Object));

            Assert.DoesNotThrowAsync(step.Run);

            // Settings are not stored because they are equal
            settingsService.Verify(m => m.TrySaveSettingsToLocalDevice(It.IsAny <SettingsModel>()), Times.Never);

            // Next step is called
            storyBoard.Verify(m => m.ContinueWith(It.Is <SynchronizationStoryStepId>(x => x == SynchronizationStoryStepId.StoreLocalRepositoryToCloudAndQuit)), Times.Once);
        }
Ejemplo n.º 8
0
        public void RepositoryTooNewForApplicationLeadsToErrorMessage()
        {
            const string transferCode  = "abcdefgh";
            var          settingsModel = CreateSettingsModel(transferCode);

            byte[] encryptedRepository = CreateEncryptedRepository(transferCode);

            Mock <IStoryBoard> storyBoard = new Mock <IStoryBoard>();

            storyBoard.
            Setup(m => m.LoadFromSession <byte[]>(It.Is <SynchronizationStorySessionKey>(p => p == SynchronizationStorySessionKey.BinaryCloudRepository))).
            Returns(encryptedRepository);
            Mock <IFeedbackService> feedbackService = new Mock <IFeedbackService>();
            Mock <ISettingsService> settingsService = new Mock <ISettingsService>();

            settingsService.
            Setup(m => m.LoadSettingsOrDefault()).Returns(settingsModel);
            Mock <INoteRepositoryUpdater> updater = new Mock <INoteRepositoryUpdater>();

            updater.
            Setup(m => m.IsTooNewForThisApp(It.IsAny <XDocument>())).
            Returns(true);

            // Run step
            var step = new DecryptCloudRepositoryStep(
                SynchronizationStoryStepId.DecryptCloudRepository,
                storyBoard.Object,
                CommonMocksAndStubs.LanguageService(),
                feedbackService.Object,
                settingsService.Object,
                updater.Object);

            Assert.DoesNotThrowAsync(step.Run);

            // Error message shown
            feedbackService.Verify(m => m.ShowToast(It.IsAny <string>()), Times.Once);

            // no next step is called
            storyBoard.Verify(m => m.ContinueWith(It.IsAny <SynchronizationStoryStepId>()), Times.Never);
        }
        public void DoNotStoreAnythingWhenRepositoriesAreSame()
        {
            NoteRepositoryModel repositoryModel = new NoteRepositoryModel();

            repositoryModel.Revision = NoteRepositoryModel.NewestSupportedRevision;

            Mock <IStoryBoard> storyBoard = new Mock <IStoryBoard>();

            storyBoard.
            Setup(m => m.LoadFromSession <NoteRepositoryModel>(It.Is <int>(p => p == SynchronizationStorySessionKey.CloudRepository.ToInt()))).
            Returns(repositoryModel);     // same as from repositoryStorageService
            Mock <ISettingsService>          settingsService          = new Mock <ISettingsService>();
            Mock <IRepositoryStorageService> repositoryStorageService = new Mock <IRepositoryStorageService>();

            repositoryStorageService.
            Setup(m => m.LoadRepositoryOrDefault(out repositoryModel));     // same as from storyBoard
            Mock <ICloudStorageClient> cloudStorageClient = new Mock <ICloudStorageClient>();

            // Run step
            var step = new StoreMergedRepositoryAndQuitStep(
                SynchronizationStoryStepId.StoreLocalRepositoryToCloudAndQuit.ToInt(),
                storyBoard.Object,
                CommonMocksAndStubs.LanguageService(),
                CommonMocksAndStubs.FeedbackService(),
                settingsService.Object,
                CommonMocksAndStubs.CryptoRandomService(),
                repositoryStorageService.Object,
                CommonMocksAndStubs.CloudStorageClientFactory(cloudStorageClient.Object));

            Assert.DoesNotThrowAsync(step.Run);

            // repository is not stored to the local device
            repositoryStorageService.Verify(m => m.TrySaveRepository(It.IsAny <NoteRepositoryModel>()), Times.Never);

            // repository is not stored to the cloud
            cloudStorageClient.Verify(m => m.UploadFileAsync(It.IsAny <string>(), It.IsAny <byte[]>(), It.IsAny <CloudStorageCredentials>()), Times.Never);

            // Next step is called
            storyBoard.Verify(m => m.ContinueWith(It.Is <int>(x => x == SynchronizationStoryStepId.StopAndShowRepository.ToInt())), Times.Once);
        }
Ejemplo n.º 10
0
        public void MissingOrWrongTransfercodeLeadsToInputDialog()
        {
            const string transferCode  = "abcdefgh";
            var          settingsModel = CreateSettingsModel(null); // no transfer code at all

            byte[] encryptedRepository = CreateEncryptedRepository(transferCode);

            Mock <IStoryBoard> storyBoard = new Mock <IStoryBoard>();

            storyBoard.
            Setup(m => m.LoadFromSession <byte[]>(It.Is <SynchronizationStorySessionKey>(p => p == SynchronizationStorySessionKey.BinaryCloudRepository))).
            Returns(encryptedRepository);
            Mock <ISettingsService> settingsService = new Mock <ISettingsService>();

            settingsService.
            Setup(m => m.LoadSettingsOrDefault()).Returns(settingsModel);
            Mock <INoteRepositoryUpdater> updater = new Mock <INoteRepositoryUpdater>();

            // Run step
            var step = new DecryptCloudRepositoryStep(
                SynchronizationStoryStepId.DecryptCloudRepository,
                storyBoard.Object,
                CommonMocksAndStubs.LanguageService(),
                CommonMocksAndStubs.FeedbackService(),
                settingsService.Object,
                updater.Object);

            Assert.DoesNotThrowAsync(step.Run);

            // No changes should be done to the settings
            settingsService.Verify(m => m.TrySaveSettingsToLocalDevice(It.IsNotNull <SettingsModel>()), Times.Never);

            // Next step is called
            storyBoard.Verify(m => m.ContinueWith(It.Is <SynchronizationStoryStepId>(x => x == SynchronizationStoryStepId.ShowTransferCode)), Times.Once);

            // Run step with wrong transfer code
            settingsModel.TransferCode = "qqqqqqqq";
            Assert.DoesNotThrowAsync(step.Run);
            storyBoard.Verify(m => m.ContinueWith(It.Is <SynchronizationStoryStepId>(x => x == SynchronizationStoryStepId.ShowTransferCode)), Times.Exactly(2));
        }
        public void InvalidRepositoryLeadsToErrorMessage()
        {
            const string transferCode  = "abcdefgh";
            var          settingsModel = CreateSettingsModel(transferCode);

            byte[] encryptedRepository = CreateEncryptedRepository(transferCode);
            encryptedRepository[8]++; // make it invalid

            Mock <IStoryBoard> storyBoard = new Mock <IStoryBoard>();

            storyBoard.
            Setup(m => m.LoadFromSession <byte[]>(It.Is <int>(p => p == SynchronizationStorySessionKey.BinaryCloudRepository.ToInt()))).
            Returns(encryptedRepository);
            Mock <IFeedbackService> feedbackService = new Mock <IFeedbackService>();
            Mock <ISettingsService> settingsService = new Mock <ISettingsService>();

            settingsService.
            Setup(m => m.LoadSettingsOrDefault()).Returns(settingsModel);
            Mock <INoteRepositoryUpdater> updater = new Mock <INoteRepositoryUpdater>();

            // Run step
            var step = new DecryptCloudRepositoryStep(
                SynchronizationStoryStepId.DecryptCloudRepository.ToInt(),
                storyBoard.Object,
                CommonMocksAndStubs.LanguageService(),
                feedbackService.Object,
                settingsService.Object,
                updater.Object);

            Assert.DoesNotThrowAsync(step.Run);

            // Error message shown
            feedbackService.Verify(m => m.ShowToast(It.IsAny <string>()), Times.Once);

            // No changes should be done to the settings
            settingsService.Verify(m => m.TrySaveSettingsToLocalDevice(It.IsNotNull <SettingsModel>()), Times.Never);

            // no next step is called
            storyBoard.Verify(m => m.ContinueWith(It.IsAny <int>()), Times.Never);
        }
        public void KeepExistingTransfercode()
        {
            SerializeableCloudStorageCredentials credentialsFromSession = new SerializeableCloudStorageCredentials();
            var settingsModel = CreateSettingsModel("abcdefgh"); // Transfercode exists
            NoteRepositoryModel repositoryModel = new NoteRepositoryModel();

            Mock <IStoryBoard> storyBoard = new Mock <IStoryBoard>();

            storyBoard.
            Setup(m => m.LoadFromSession <SerializeableCloudStorageCredentials>(It.Is <SynchronizationStorySessionKey>(p => p == SynchronizationStorySessionKey.CloudStorageCredentials))).
            Returns(credentialsFromSession);
            Mock <ISettingsService> settingsService = new Mock <ISettingsService>();

            settingsService.
            Setup(m => m.LoadSettingsOrDefault()).Returns(settingsModel);
            Mock <IRepositoryStorageService> repositoryStorageService = new Mock <IRepositoryStorageService>();

            repositoryStorageService.
            Setup(m => m.LoadRepositoryOrDefault(out repositoryModel));

            // Run step
            var step = new StoreLocalRepositoryToCloudAndQuitStep(
                SynchronizationStoryStepId.StoreLocalRepositoryToCloudAndQuit,
                storyBoard.Object,
                CommonMocksAndStubs.LanguageService(),
                CommonMocksAndStubs.FeedbackService(),
                settingsService.Object,
                CommonMocksAndStubs.CryptoRandomService(),
                repositoryStorageService.Object,
                CommonMocksAndStubs.CloudStorageClientFactory());

            Assert.DoesNotThrowAsync(step.Run);

            // No settings are stored
            settingsService.Verify(m => m.TrySaveSettingsToLocalDevice(It.IsAny <SettingsModel>()), Times.Never);

            // Next step is called
            storyBoard.Verify(m => m.ContinueWith(It.Is <SynchronizationStoryStepId>(x => x == SynchronizationStoryStepId.StopAndShowRepository)), Times.Once);
        }
        public void GenerateAndStoreNewTransfercode()
        {
            SerializeableCloudStorageCredentials credentialsFromSession = new SerializeableCloudStorageCredentials();
            var settingsModel = CreateSettingsModel(null); // Transfercode does not yet exist
            NoteRepositoryModel repositoryModel = new NoteRepositoryModel();

            Mock <IStoryBoard> storyBoard = new Mock <IStoryBoard>();

            storyBoard.
            Setup(m => m.LoadFromSession <SerializeableCloudStorageCredentials>(It.Is <int>(p => p == SynchronizationStorySessionKey.CloudStorageCredentials.ToInt()))).
            Returns(credentialsFromSession);
            Mock <ISettingsService> settingsService = new Mock <ISettingsService>();

            settingsService.
            Setup(m => m.LoadSettingsOrDefault()).Returns(settingsModel);
            Mock <IRepositoryStorageService> repositoryStorageService = new Mock <IRepositoryStorageService>();

            repositoryStorageService.
            Setup(m => m.LoadRepositoryOrDefault(out repositoryModel));

            // Run step
            var step = new StoreLocalRepositoryToCloudAndQuitStep(
                SynchronizationStoryStepId.StoreLocalRepositoryToCloudAndQuit.ToInt(),
                storyBoard.Object,
                CommonMocksAndStubs.LanguageService(),
                CommonMocksAndStubs.FeedbackService(),
                settingsService.Object,
                CommonMocksAndStubs.CryptoRandomService(),
                repositoryStorageService.Object,
                CommonMocksAndStubs.CloudStorageClientFactory());

            Assert.DoesNotThrowAsync(step.Run);

            // Settings are stored with new transfer code
            settingsService.Verify(m => m.TrySaveSettingsToLocalDevice(It.Is <SettingsModel>(s => !string.IsNullOrEmpty(s.TransferCode))), Times.Once);

            // Next step is called
            storyBoard.Verify(m => m.ContinueWith(It.Is <int>(x => x == SynchronizationStoryStepId.StopAndShowRepository.ToInt())), Times.Once);
        }
Ejemplo n.º 14
0
        public void ErrorMessageIsShownInCaseOfException()
        {
            SerializeableCloudStorageCredentials credentialsFromSession = new SerializeableCloudStorageCredentials();

            byte[] repositoryFromSession = null;
            byte[] repository            = new byte[8];

            Mock <IStoryBoard> storyBoard = new Mock <IStoryBoard>();

            storyBoard.
            Setup(m => m.LoadFromSession <SerializeableCloudStorageCredentials>(It.Is <int>(p => p == SynchronizationStorySessionKey.CloudStorageCredentials.ToInt()))).
            Returns(credentialsFromSession);
            storyBoard.
            Setup(m => m.TryLoadFromSession(It.Is <int>(p => p == SynchronizationStorySessionKey.BinaryCloudRepository.ToInt()), out repositoryFromSession)).
            Returns(false);
            Mock <IFeedbackService>    feedbackService    = new Mock <IFeedbackService>();
            Mock <ICloudStorageClient> cloudStorageClient = new Mock <ICloudStorageClient>();

            cloudStorageClient.
            Setup(m => m.DownloadFileAsync(It.IsAny <string>(), It.IsAny <CloudStorageCredentials>())).
            Throws <ConnectionFailedException>();

            // Run step
            var step = new DownloadCloudRepositoryStep(
                SynchronizationStoryStepId.DownloadCloudRepository.ToInt(),
                storyBoard.Object,
                CommonMocksAndStubs.LanguageService(),
                feedbackService.Object,
                CommonMocksAndStubs.CloudStorageClientFactory(cloudStorageClient.Object));

            Assert.DoesNotThrowAsync(step.Run);

            // Error message was shown
            feedbackService.Verify(m => m.ShowToast(It.IsAny <string>()), Times.Once);

            // Next step is called
            storyBoard.Verify(m => m.ContinueWith(It.Is <int>(x => x == SynchronizationStoryStepId.ExistsTransferCode.ToInt())), Times.Never);
        }
Ejemplo n.º 15
0
        public void TransfercodeOfHistoryWillBeReused()
        {
            const string transferCode  = "abcdefgh";
            var          settingsModel = CreateSettingsModel("qqqqqqqq");

            settingsModel.TransferCodeHistory.Add(transferCode); // Good transfercode is in history
            byte[] encryptedRepository = CreateEncryptedRepository(transferCode);

            Mock <IStoryBoard> storyBoard = new Mock <IStoryBoard>();

            storyBoard.
            Setup(m => m.LoadFromSession <byte[]>(It.Is <SynchronizationStorySessionKey>(p => p == SynchronizationStorySessionKey.BinaryCloudRepository))).
            Returns(encryptedRepository);
            Mock <ISettingsService> settingsService = new Mock <ISettingsService>();

            settingsService.
            Setup(m => m.LoadSettingsOrDefault()).Returns(settingsModel);
            Mock <INoteRepositoryUpdater> updater = new Mock <INoteRepositoryUpdater>();

            // Run step
            var step = new DecryptCloudRepositoryStep(
                SynchronizationStoryStepId.DecryptCloudRepository,
                storyBoard.Object,
                CommonMocksAndStubs.LanguageService(),
                CommonMocksAndStubs.FeedbackService(),
                settingsService.Object,
                updater.Object);

            Assert.DoesNotThrowAsync(step.Run);

            // transfercode was moved from history to current and was stored
            Assert.AreEqual(transferCode, settingsModel.TransferCode);
            settingsService.Verify(m => m.TrySaveSettingsToLocalDevice(It.IsNotNull <SettingsModel>()), Times.Once);

            // Next step is called
            storyBoard.Verify(m => m.ContinueWith(It.Is <SynchronizationStoryStepId>(x => x == SynchronizationStoryStepId.IsSameRepository)), Times.Once);
        }
Ejemplo n.º 16
0
        public void SuccessfulFlowEndsInNextStep()
        {
            SerializeableCloudStorageCredentials credentialsFromSession = new SerializeableCloudStorageCredentials();

            byte[] repositoryFromSession = null;
            byte[] repository            = new byte[8];

            Mock <IStoryBoard> storyBoard = new Mock <IStoryBoard>();

            storyBoard.
            Setup(m => m.LoadFromSession <SerializeableCloudStorageCredentials>(It.Is <int>(p => p == SynchronizationStorySessionKey.CloudStorageCredentials.ToInt()))).
            Returns(credentialsFromSession);
            storyBoard.
            Setup(m => m.TryLoadFromSession(It.Is <int>(p => p == SynchronizationStorySessionKey.BinaryCloudRepository.ToInt()), out repositoryFromSession)).
            Returns(false);
            Mock <ICloudStorageClient> cloudStorageClient = new Mock <ICloudStorageClient>();

            cloudStorageClient.
            Setup(m => m.DownloadFileAsync(It.IsAny <string>(), It.IsAny <CloudStorageCredentials>())).
            ReturnsAsync(repository);

            // Run step
            var step = new DownloadCloudRepositoryStep(
                SynchronizationStoryStepId.DownloadCloudRepository.ToInt(),
                storyBoard.Object,
                CommonMocksAndStubs.LanguageService(),
                CommonMocksAndStubs.FeedbackService(),
                CommonMocksAndStubs.CloudStorageClientFactory(cloudStorageClient.Object));

            Assert.DoesNotThrowAsync(step.Run);

            // Repository was stored in session
            storyBoard.Verify(m => m.StoreToSession(It.Is <int>(p => p == SynchronizationStorySessionKey.BinaryCloudRepository.ToInt()), It.Is <object>(p => p == repository)), Times.Once);

            // Next step is called
            storyBoard.Verify(m => m.ContinueWith(It.Is <int>(x => x == SynchronizationStoryStepId.ExistsTransferCode.ToInt())), Times.Once);
        }
Ejemplo n.º 17
0
        public void CorrectNextStepWhenCloudRepositoryExists()
        {
            SerializeableCloudStorageCredentials credentials = new SerializeableCloudStorageCredentials {
                CloudStorageId = CloudStorageClientFactory.CloudStorageIdDropbox
            };
            SettingsModel settingsModel = new SettingsModel {
                Credentials = credentials, TransferCode = "abc"
            };

            Mock <IStoryBoard>      storyBoard      = new Mock <IStoryBoard>();
            Mock <ISettingsService> settingsService = new Mock <ISettingsService>();

            settingsService.
            Setup(m => m.LoadSettingsOrDefault()).Returns(settingsModel);
            Mock <ICloudStorageClient> cloudStorageClient = new Mock <ICloudStorageClient>();

            cloudStorageClient.
            Setup(m => m.ExistsFileAsync(It.IsAny <string>(), It.IsAny <CloudStorageCredentials>())).
            ReturnsAsync(true);

            // Run step
            var step = new ExistsCloudRepositoryStep(
                PullPushStoryStepId.ExistsCloudRepository,
                storyBoard.Object,
                CommonMocksAndStubs.LanguageService(),
                CommonMocksAndStubs.FeedbackService(),
                settingsService.Object,
                CommonMocksAndStubs.CloudStorageClientFactory(cloudStorageClient.Object));

            Assert.DoesNotThrowAsync(step.Run);

            // Settings are not stored because no token needs to be refreshed
            settingsService.Verify(m => m.TrySaveSettingsToLocalDevice(It.IsAny <SettingsModel>()), Times.Never);

            // Next step is called
            storyBoard.Verify(m => m.ContinueWith(It.Is <PullPushStoryStepId>(x => x == PullPushStoryStepId.DownloadCloudRepository)), Times.Once);
        }
Ejemplo n.º 18
0
        public void Version1ConfigWillBeUpdated()
        {
            XDocument xml = XDocument.Parse(Version1Settings);
            Mock <IXmlFileService> fileService = new Mock <IXmlFileService>();

            fileService.
            Setup(m => m.TryLoad(It.IsAny <string>(), out xml)).
            Returns(true);
            Mock <IDataProtectionService> dataProtectionService = new Mock <IDataProtectionService>();

            dataProtectionService.
            Setup(m => m.Protect(It.Is <byte[]>(p => p.SequenceEqual(Encoding.ASCII.GetBytes("abcdefgh"))))).
            Returns("protected_abcdefgh_v2");
            dataProtectionService.
            Setup(m => m.Unprotect(It.Is <string>(p => p == "protected_abcdefgh_v2"))).
            Returns(SecureStringExtensions.SecureStringToBytes(SecureStringExtensions.StringToSecureString("abcdefgh_v2"), Encoding.Unicode));
            dataProtectionService.
            Setup(m => m.Protect(It.Is <byte[]>(p => p.SequenceEqual(Encoding.UTF8.GetBytes("abcdefgh_v2"))))).
            Returns("protected_abcdefgh_v3");
            dataProtectionService.
            Setup(m => m.Unprotect(It.Is <string>(p => p == "protected_abcdefgh_v3"))).
            Returns(Encoding.UTF8.GetBytes("abcdefgh_v3"));

            dataProtectionService.
            Setup(m => m.Protect(It.Is <byte[]>(p => p.SequenceEqual(Encoding.UTF8.GetBytes("martinstoeckli"))))).
            Returns("protected_martinstoeckli_v3");
            dataProtectionService.
            Setup(m => m.Unprotect(It.Is <string>(p => p == "protected_martinstoeckli_v3"))).
            Returns(Encoding.UTF8.GetBytes("martinstoeckli_v3"));

            SettingsServiceBase service  = new TestableService(fileService.Object, dataProtectionService.Object, CommonMocksAndStubs.EnvironmentService());
            SettingsModel       settings = service.LoadSettingsOrDefault();

            Assert.AreEqual("twofish_gcm", settings.SelectedEncryptionAlgorithm);
            Assert.AreEqual("scuj2wfpdcodmgzm", settings.TransferCode);
            Assert.AreEqual(2, settings.TransferCodeHistory.Count);
            Assert.AreEqual("scuj2wfpdcodmgzm", settings.TransferCodeHistory[0]);
            Assert.AreEqual("b2bgiqeghfvn2ufx", settings.TransferCodeHistory[1]);

            Assert.AreEqual(SettingsModel.NewestSupportedRevision, settings.Revision);
            Assert.IsNotNull(settings.Credentials);
            Assert.AreEqual(CloudStorageClientFactory.CloudStorageIdWebdav, settings.Credentials.CloudStorageId);
            Assert.AreEqual("https://webdav.hidrive.strato.com/users/martinstoeckli/", settings.Credentials.Url);
            Assert.AreEqual("martinstoeckli_v3", settings.Credentials.Username);
            Assert.AreEqual("abcdefgh_v3", settings.Credentials.UnprotectedPassword);

            // Updated settings where saved
            fileService.Verify(m => m.TrySerializeAndSave(It.IsAny <string>(), It.IsAny <SettingsModel>()), Times.Once);
        }
Ejemplo n.º 19
0
        public void LoadSettingsCreatesDefaultIfNoFileFound()
        {
            XDocument xml = null;
            Mock <IXmlFileService> fileService = new Mock <IXmlFileService>();

            fileService.
            Setup(m => m.TryLoad(It.IsAny <string>(), out xml)).
            Returns(false);
            Mock <IDataProtectionService> dataProtectionService = new Mock <IDataProtectionService>();

            SettingsServiceBase service  = new TestableService(fileService.Object, dataProtectionService.Object, CommonMocksAndStubs.EnvironmentService());
            SettingsModel       settings = service.LoadSettingsOrDefault();

            // Created new settings and stored it
            Assert.IsNotNull(settings);
            fileService.Verify(m => m.TrySerializeAndSave(It.IsAny <string>(), It.IsAny <SettingsModel>()), Times.Once);
        }
Ejemplo n.º 20
0
        public void LoadsSettingsReturnsStoredSettings()
        {
            SettingsModel storedSettings = new SettingsModel {
                TransferCode = "abcdefgh"
            };
            XDocument xml = XmlUtils.SerializeToXmlDocument(storedSettings);
            Mock <IXmlFileService> fileService = new Mock <IXmlFileService>();

            fileService.
            Setup(m => m.TryLoad(It.IsAny <string>(), out xml)).
            Returns(true);
            Mock <IDataProtectionService> dataProtectionService = new Mock <IDataProtectionService>();

            SettingsServiceBase service  = new TestableService(fileService.Object, dataProtectionService.Object, CommonMocksAndStubs.EnvironmentService());
            SettingsModel       settings = service.LoadSettingsOrDefault();

            // Loaded existing settings and did not store it
            Assert.IsNotNull(settings);
            Assert.AreEqual("abcdefgh", settings.TransferCode);
            fileService.Verify(m => m.TrySerializeAndSave(It.IsAny <string>(), It.IsAny <SettingsModel>()), Times.Never);
        }
        private static byte[] CreateEncryptedRepository(string password, NoteRepositoryModel repository = null)
        {
            if (repository == null)
            {
                repository = new NoteRepositoryModel();
            }
            byte[]             serializedRepository = XmlUtils.SerializeToXmlBytes(repository);
            EncryptorDecryptor encryptor            = new EncryptorDecryptor("SilentNotes");

            return(encryptor.Encrypt(serializedRepository, password, SilentNotes.Crypto.KeyDerivation.KeyDerivationCostType.Low, CommonMocksAndStubs.CryptoRandomService(), BouncyCastleTwofishGcm.CryptoAlgorithmName));
        }
        public void PushStoresCorrectData()
        {
            NoteModel cloudNoteModel = new NoteModel
            {
                HtmlContent    = "cloudContent",
                InRecyclingBin = true,
                ModifiedAt     = new DateTime(2000, 10, 22)
            };
            NoteRepositoryModel cloudRepositoryModel = new NoteRepositoryModel();

            cloudRepositoryModel.Notes.Add(cloudNoteModel);

            NoteModel localNoteModel = new NoteModel
            {
                Id             = cloudNoteModel.Id,
                HtmlContent    = "localContent",
                InRecyclingBin = false,
                ModifiedAt     = new DateTime(1900, 10, 22)
            };
            NoteRepositoryModel localRepositoryModel = new NoteRepositoryModel {
                Id = cloudNoteModel.Id
            };

            localRepositoryModel.Notes.Add(localNoteModel);

            Mock <IStoryBoard> storyBoard = new Mock <IStoryBoard>();

            storyBoard.
            Setup(m => m.LoadFromSession <NoteRepositoryModel>(It.Is <PullPushStorySessionKey>(p => p == PullPushStorySessionKey.CloudRepository))).
            Returns(cloudRepositoryModel);
            Mock <ISettingsService> settingsService = new Mock <ISettingsService>();

            settingsService.
            Setup(m => m.LoadSettingsOrDefault()).Returns(new SettingsModel {
                TransferCode = "AAAAAAAAAAAA", Credentials = new SerializeableCloudStorageCredentials()
            });
            Mock <IRepositoryStorageService> repositoryStorageService = new Mock <IRepositoryStorageService>();

            repositoryStorageService.
            Setup(m => m.LoadRepositoryOrDefault(out localRepositoryModel));
            Mock <ICloudStorageClient> cloudStorageClient = new Mock <ICloudStorageClient>();

            // Run step
            var step = new StoreMergedRepositoryAndQuitStep(
                PullPushStoryStepId.StoreMergedRepositoryAndQuit,
                storyBoard.Object,
                cloudNoteModel.Id,
                PullPushDirection.PushToServer,
                CommonMocksAndStubs.LanguageService(),
                CommonMocksAndStubs.FeedbackService(),
                settingsService.Object,
                CommonMocksAndStubs.CryptoRandomService(),
                repositoryStorageService.Object,
                CommonMocksAndStubs.CloudStorageClientFactory(cloudStorageClient.Object));;

            Assert.DoesNotThrowAsync(step.Run);

            // Cloud note object still exists and contains the content of the cloud plus a new modification date
            NoteModel newCloudNote = cloudRepositoryModel.Notes.FindById(localNoteModel.Id);

            Assert.AreSame(cloudNoteModel, newCloudNote);
            Assert.AreEqual("localContent", newCloudNote.HtmlContent);
            Assert.AreEqual(false, newCloudNote.InRecyclingBin);
            Assert.AreEqual(DateTime.UtcNow.Day, newCloudNote.ModifiedAt.Day);

            // Local note object still exists and has new modification date
            NoteModel newLocalNote = localRepositoryModel.Notes.FindById(cloudNoteModel.Id);

            Assert.AreEqual(DateTime.UtcNow.Day, newLocalNote.ModifiedAt.Day);

            // repository was stored to the local device and to the cloud
            repositoryStorageService.Verify(m => m.TrySaveRepository(It.IsAny <NoteRepositoryModel>()), Times.Once);
            cloudStorageClient.Verify(m => m.UploadFileAsync(It.IsAny <string>(), It.IsAny <byte[]>(), It.IsAny <CloudStorageCredentials>()), Times.Once);
        }
Ejemplo n.º 23
0
 public void CompressReturnsData()
 {
     byte[] data           = CommonMocksAndStubs.FilledByteArray(64, 88);
     byte[] compressedData = CompressUtils.Compress(data);
     Assert.IsTrue(compressedData.Length > 3);
 }