public void Protect_InvalidUtf_Failure() { // Arrange Mock <IDataProtector> mockProtector = new Mock <IDataProtector>(); // Act & assert var ex = Assert.Throws <CryptographicException>(() => { DataProtectionExtensions.Protect(mockProtector.Object, "Hello\ud800"); }); Assert.IsAssignableFrom(typeof(EncoderFallbackException), ex.InnerException); }
public void Protect_Success() { // Arrange Mock <IDataProtector> mockProtector = new Mock <IDataProtector>(); mockProtector.Setup(p => p.Protect(new byte[] { 0x48, 0x65, 0x6c, 0x6c, 0x6f })).Returns(new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05 }); // Act string retVal = DataProtectionExtensions.Protect(mockProtector.Object, "Hello"); // Assert Assert.Equal("AQIDBAU", retVal); }
private async void btnOK_Click(object sender, EventArgs e) { try { this.UseWaitCursor = true; string strPassword = string.Empty; string strExpire = string.Empty; System.Net.ServicePointManager.ServerCertificateValidationCallback = (s, cert, chain, errors) => { return(true); }; char[] pChar = this.tbPassword.Text.ToCharArray(); SecureString pw = new SecureString(); foreach (char c in pChar) { pw.AppendChar(c); } char[] pwBuf = SecureStringUtils.SecureStringToCharArray(pw); pw.Dispose(); try { SeafileSession session = await SeafileSession.Establish(new Uri(this.tbURL.Text, UriKind.Absolute), this.tbAccount.Text, pwBuf); } catch (SeafException se) { log.Error(se.SeafError.GetErrorMessage()); //SeafileClient.Types.SeafErrorCode.InvalidCredentials if (se.SeafError.SeafErrorCode == SeafileClient.Types.SeafErrorCode.InvalidCredentials) { MessageBox.Show( Properties.Resources.InvalidCredentials, Properties.Resources.MessageBoxErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); } else { MessageBox.Show( se.SeafError.GetErrorMessage(), Properties.Resources.MessageBoxErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); } return; } string encryptedPassword = DataProtectionExtensions.Protect(this.tbPassword.Text); string decryptedPassword = DataProtectionExtensions.Unprotect(encryptedPassword); // Create a file that the application will store user specific data in. using (var file = File.CreateText(Application.UserAppDataPath + "\\seafileaddin.cfg")) { JObject objSetting = new JObject( new JProperty("accessurl", this.tbURL.Text), new JProperty("account", this.tbAccount.Text), new JProperty("poassword", encryptedPassword)); using (JsonTextWriter writer = new JsonTextWriter(file)) { objSetting.WriteTo(writer); } this.DialogResult = DialogResult.OK; } } catch (IOException ex) { log.Error(ex.Message); MessageBox.Show( ex.Message, Properties.Resources.MessageBoxErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } finally { this.UseWaitCursor = false; } }