public void FormatNotChunkedToken() { // Arrange var tokenMock = new Mock <IToken>(); var nonceMock = new Mock <Nonce>(); nonceMock.Setup(n => n.ToString()).Returns("123:654"); var samlToken = "<xml>saml</xml>"; tokenMock.Setup <string>(t => t.SamlToken).Returns(samlToken); var nonce = nonceMock.Object; tokenMock.Setup <Nonce>(t => t.Nonce).Returns(nonce); byte[] bodyHash = { 1, 2, 3 }; tokenMock.Setup <byte[]>(t => t.BodyHash).Returns(bodyHash); SigningAlgorithm signAlg = SigningAlgorithm.RSA_SHA384; tokenMock.Setup <SigningAlgorithm>(t => t.SignatureAlgorithm).Returns(signAlg); byte[] signature = { 4, 5, 6 }; tokenMock.Setup <byte[]>(t => t.Signature).Returns(signature); var expected = GetExpected(samlToken, nonce, bodyHash, signAlg, signature); // Act var actual = new TokenFormatter().Format(tokenMock.Object, 4086); // Assert Assert.AreEqual(1, actual.Length); Assert.AreEqual(expected, actual[0]); }
public void ParseValidToken() { // Arrange var expectedSamlToken = "<xml>samltoken</xml>"; var expectedNonce = Nonce.FromNow(); var expectedBodyHash = new byte[] { 1, 2, 3 }; var expectedSignature = new byte[] { 1, 4, 1 }; var expectedSignatureAlg = SigningAlgorithm.RSA_SHA384; var token = GetExpected( expectedSamlToken, expectedNonce, expectedBodyHash, expectedSignatureAlg, expectedSignature); // Act var actual = new TokenFormatter().Parse(new [] { token }); // Assert Assert.AreEqual(expectedSamlToken, actual.SamlToken); Assert.AreEqual(expectedNonce, actual.Nonce); Assert.That(expectedBodyHash, Is.EqualTo(actual.BodyHash)); Assert.AreEqual(expectedSignature, actual.Signature); Assert.That(expectedSignatureAlg, Is.EqualTo(actual.SignatureAlgorithm)); }
private static void TestFormat(string expected, string format, TokenOptions options, params object[] tokenValueObjects) { foreach (object tokenValues in tokenValueObjects) { string result = TokenFormatter.Format(format, tokenValues, options); Assert.AreEqual(expected, result); } }
public void PropertyAccessModifier() { var obj = new PropertyAccess(); Assert.AreEqual("Value", TokenFormatter.Format("{PublicValue}", obj)); Assert.AreEqual("Value", TokenFormatter.Format("{InternalValue}", obj, TokenOptions.NonPublicAccess)); Assert.ThrowsException <KeyNotFoundException>(() => TokenFormatter.Format("{InternalValue}", obj)); }
public void EscapedTokenMarkers() { Assert.AreEqual("{", TokenFormatter.Format("{{", new object())); Assert.AreEqual("}{", TokenFormatter.Format("}}{{", new object())); Assert.AreEqual("{0}", TokenFormatter.Format("{{{Length}}}", string.Empty)); Assert.AreEqual("{Length}", TokenFormatter.Format("{{Length}}", string.Empty)); Assert.ThrowsException <FormatException>(() => TokenFormatter.Format("Unescaped { brace", string.Empty)); Assert.ThrowsException <FormatException>(() => TokenFormatter.Format("Unescaped } brace", string.Empty)); }
public void TokenFormatter_Converts_Tokens_Into_Proper_Signs() { var input = "(2+3)*4/5-6.7+cos(60deg)+sin(30deg)+tan(45deg)+2a^2+sqrt(16)"; var tokenizer = new Tokenizer(); tokenizer.Tokenize(input, null); var underTest = new TokenFormatter(); var result = underTest.Format(tokenizer.Tokens); result.Should().Be("(2+3)*4/5-6.7+0.5+0.5+1.0+2*a^2+16^0.5"); }
public void ParseJavaLibraryGeneratedToken() { // Arrange var token = "SIGN token=\"H4sIAAAAAAAAALOpyM2xS8zNUSjJz07Ns9EHcQHi3QwdFAAAAA==\", nonce=\"1579529430238:737412601\", bodyhash=\"8ZBm4RjyoqMQhJPl5Iur3OfYmwchbLLSV5vAz/Om+Ak=\", signature_alg=\"RSA-SHA256\", signature=\"TIUhHrX85O8mDdE6ZWq6AYSQkguHA9h7w1eRH8Lalq9aejaTscAR8cVOEy7f8CfgoQGgJet6H7V59TIpYQUfXZ8xguMzbHARugQv53jgdWinZMfnCchQZl6EAIPZrFBn0F6Pxp9R2tPSNFBQbNh8obPXVt5Lx8kiSQUVydFWKHzcMzul7rXtO4nb5dUKP99oyb93wfqxftU4IhWOVTI12iAL3qdcbXQUigQw9utpJPXAtFwsLCXAayLwaCSR2wHmoIM85tsUAdXrtuL9fy4BKmBYkx5Wv7pdBIQGqTd8WjzJAjRbqRmNZxygyn4QYCIyHwP9hYiXCzG8Tg==\""; // Act var actual = new TokenFormatter().Parse(new [] { token }); // Assert Assert.NotNull(actual); }
public void TokenValueNotFound() { string format = "Name: {x?[missing]}"; var obj = new Dictionary <string, object>(); var ex = Assert.ThrowsException <KeyNotFoundException>(() => TokenFormatter.Format(format, obj)); Assert.IsTrue(ex.Message.Contains("'x'", StringComparison.Ordinal)); ex = Assert.ThrowsException <KeyNotFoundException>(() => TokenFormatter.Format(format, obj)); Assert.IsTrue(ex.Message.Contains("'x'", StringComparison.Ordinal)); Assert.AreEqual("Name: [missing]", TokenFormatter.Format(format, obj, TokenOptions.AllowMissingKeys)); }
public void GeneralTokenizerTest() { var formatString = "{P},{P}, {F},{L}, {P}\n{P}\n{P}\n{M} {Suffix} .... {asdfasdfasdf}"; var foo = new Foo() { First = "Joshua", Last = "Miller", Middle = "Boop", Suffix = "Dr" }; var tokenFormatter = new TokenFormatter <Foo>(new FooExtractor(), new Tokenizer(), new DataApplier(), new RuleApplier()); var output = tokenFormatter.Format(foo, formatString); }
public void ParseChunkedToken() { // Arrange var tokenMock = new Mock <IToken>(); var nonceMock = new Mock <Nonce>(); var nonceString = "123:654"; nonceMock.Setup(n => n.ToString()).Returns(nonceString); var samlToken = Resources.HoKSamlToken; var tokenCompressedValue = Bas64CompressedUtf8(samlToken); var maxChunkSize = (tokenCompressedValue.Length + 3 + ParamValue.Param.token.ToString().Length) / 2 + 1; tokenMock.Setup <string>(t => t.SamlToken).Returns(samlToken); var nonce = nonceMock.Object; tokenMock.Setup <Nonce>(t => t.Nonce).Returns(nonce); byte[] bodyHash = { 1, 2, 3 }; tokenMock.Setup <byte[]>(t => t.BodyHash).Returns(bodyHash); SigningAlgorithm signAlg = SigningAlgorithm.RSA_SHA384; tokenMock.Setup <SigningAlgorithm>(t => t.SignatureAlgorithm).Returns(signAlg); byte[] signature = { 4, 5, 6 }; tokenMock.Setup <byte[]>(t => t.Signature).Returns(signature); var formattedToken = new TokenFormatter().Format(tokenMock.Object, maxChunkSize); // Act var actual = new TokenFormatter().Parse(formattedToken); // Assert Assert.AreEqual(samlToken, actual.SamlToken); Assert.AreEqual(nonceString, actual.Nonce.ToString()); Assert.That(bodyHash, Is.EqualTo(actual.BodyHash)); Assert.AreEqual(signAlg, actual.SignatureAlgorithm); Assert.That(signature, Is.EqualTo(actual.Signature)); }
public void FormatChunkedToken() { // Arrange var tokenMock = new Mock <IToken>(); var nonceMock = new Mock <Nonce>(); nonceMock.Setup(n => n.ToString()).Returns("123:654"); var samlToken = Resources.HoKSamlToken; var tokenCompressedValue = Bas64CompressedUtf8(samlToken); var maxChunkSize = (tokenCompressedValue.Length + 3 + ParamValue.Param.token.ToString().Length) / 2 + 1; tokenMock.Setup <string>(t => t.SamlToken).Returns(samlToken); var nonce = nonceMock.Object; tokenMock.Setup <Nonce>(t => t.Nonce).Returns(nonce); byte[] bodyHash = { 1, 2, 3 }; tokenMock.Setup <byte[]>(t => t.BodyHash).Returns(bodyHash); SigningAlgorithm signAlg = SigningAlgorithm.RSA_SHA384; tokenMock.Setup <SigningAlgorithm>(t => t.SignatureAlgorithm).Returns(signAlg); byte[] signature = { 4, 5, 6 }; tokenMock.Setup <byte[]>(t => t.Signature).Returns(signature); // Act var actual = new TokenFormatter().Format(tokenMock.Object, maxChunkSize); // Assert Assert.GreaterOrEqual(actual.Length, 2); Assert.IsTrue(actual[0].StartsWith("SIGN token=\"")); Assert.IsTrue(actual[0].EndsWith("\"")); Assert.IsTrue(actual[1].StartsWith("token=\"")); }
public void TokenValueFormat() { Assert.AreEqual("0005", TokenFormatter.Format("{Value:D4}", new { Value = 5 })); Assert.AreEqual("0005", TokenFormatter.Format("{Object.Value:D4}", new { Object = new { Value = 5 } })); }