public void TransferCodeIsOfCorrectLength() { for (int length = 0; length < 999; length++) { string code = TransferCode.GenerateCode(length, CommonMocksAndStubs.CryptoRandomService()); Assert.AreEqual(length, code.Length); } }
public void TransferCodeIsOfCorrectAlphabet() { for (int length = 0; length < 999; length++) { string code = TransferCode.GenerateCode(length, CommonMocksAndStubs.CryptoRandomService()); Assert.IsTrue(IsOfCorrectAlphabet(code)); } }
/// <inheritdoc/> public override async Task Run() { try { SerializeableCloudStorageCredentials credentials = StoryBoard.LoadFromSession <SerializeableCloudStorageCredentials>(SynchronizationStorySessionKey.CloudStorageCredentials); _repositoryStorageService.LoadRepositoryOrDefault(out NoteRepositoryModel localRepository); SettingsModel settings = _settingsService.LoadSettingsOrDefault(); string transferCode = settings.TransferCode; bool needsNewTransferCode = !TransferCode.IsCodeSet(transferCode); if (needsNewTransferCode) { transferCode = TransferCode.GenerateCode(_cryptoRandomService); } byte[] encryptedRepository = EncryptRepository( localRepository, transferCode, _cryptoRandomService, settings.SelectedEncryptionAlgorithm); ICloudStorageClient cloudStorageClient = _cloudStorageClientFactory.GetOrCreate(credentials.CloudStorageId); await cloudStorageClient.UploadFileAsync(Config.RepositoryFileName, encryptedRepository, credentials); // All went well, time to save the transfer code, if a new one was created if (needsNewTransferCode) { settings.TransferCode = transferCode; _settingsService.TrySaveSettingsToLocalDevice(settings); string formattedTransferCode = TransferCode.FormatTransferCodeForDisplay(transferCode).Replace(' ', '-'); string messageNewCreated = _languageService.LoadTextFmt("transfer_code_created", formattedTransferCode); string messageWriteDown = _languageService.LoadText("transfer_code_writedown"); if (StoryBoard.Mode.ShouldUseGui()) { await _feedbackService.ShowMessageAsync(messageNewCreated + Environment.NewLine + messageWriteDown, null, MessageBoxButtons.Ok, false); } } await StoryBoard.ContinueWith(SynchronizationStoryStepId.StopAndShowRepository); _feedbackService.ShowToast(_languageService["sync_success"]); } catch (Exception ex) { // Keep the current page open and show the error message ShowExceptionMessage(ex, _feedbackService, _languageService); } }