PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System.IdentityModel/Test/System.IdentityModel.Tokens/BootstrapContextTest.cs

https://github.com/pruiz/mono
C# | 249 lines | 214 code | 26 blank | 9 comment | 3 complexity | afedb96e5a56caf829606080a44e6470 MD5 | raw file
Possible License(s): LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. //
  2. // BootstrapContextTest.cs - NUnit Test Cases for System.IdentityModel.Tokens.BootstrapContext
  3. //
  4. using System;
  5. using System.IO;
  6. using System.IdentityModel.Tokens;
  7. using System.Runtime.Serialization;
  8. using System.Runtime.Serialization.Formatters.Binary;
  9. using System.Text;
  10. using System.Xml;
  11. using NUnit.Framework;
  12. namespace MonoTests.System.IdentityModel.Tokens.net_4_5 {
  13. [TestFixture]
  14. public class BootstrapContextTest {
  15. // The following byte arrays are the serialized bytes as emitted on Microsoft .Net 4.5.
  16. private static readonly byte [] SerializedBootstrapContextByteArray = new byte [] { 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x02, 0x00, 0x00, 0x00, 0x57, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6D, 0x2E, 0x49, 0x64, 0x65, 0x6E, 0x74, 0x69, 0x74, 0x79, 0x4D, 0x6F, 0x64, 0x65, 0x6C, 0x2C, 0x20, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3D, 0x34, 0x2E, 0x30, 0x2E, 0x30, 0x2E, 0x30, 0x2C, 0x20, 0x43, 0x75, 0x6C, 0x74, 0x75, 0x72, 0x65, 0x3D, 0x6E, 0x65, 0x75, 0x74, 0x72, 0x61, 0x6C, 0x2C, 0x20, 0x50, 0x75, 0x62, 0x6C, 0x69, 0x63, 0x4B, 0x65, 0x79, 0x54, 0x6F, 0x6B, 0x65, 0x6E, 0x3D, 0x62, 0x37, 0x37, 0x61, 0x35, 0x63, 0x35, 0x36, 0x31, 0x39, 0x33, 0x34, 0x65, 0x30, 0x38, 0x39, 0x05, 0x01, 0x00, 0x00, 0x00, 0x2C, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6D, 0x2E, 0x49, 0x64, 0x65, 0x6E, 0x74, 0x69, 0x74, 0x79, 0x4D, 0x6F, 0x64, 0x65, 0x6C, 0x2E, 0x54, 0x6F, 0x6B, 0x65, 0x6E, 0x73, 0x2E, 0x42, 0x6F, 0x6F, 0x74, 0x73, 0x74, 0x72, 0x61, 0x70, 0x43, 0x6F, 0x6E, 0x74, 0x65, 0x78, 0x74, 0x02, 0x00, 0x00, 0x00, 0x01, 0x4B, 0x01, 0x54, 0x00, 0x07, 0x03, 0x02, 0x02, 0x00, 0x00, 0x00, 0x42, 0x09, 0x03, 0x00, 0x00, 0x00, 0x0F, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x01, 0x0B };
  17. private static readonly byte [] SerializedBootstrapContextString = new byte [] { 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x02, 0x00, 0x00, 0x00, 0x57, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6D, 0x2E, 0x49, 0x64, 0x65, 0x6E, 0x74, 0x69, 0x74, 0x79, 0x4D, 0x6F, 0x64, 0x65, 0x6C, 0x2C, 0x20, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3D, 0x34, 0x2E, 0x30, 0x2E, 0x30, 0x2E, 0x30, 0x2C, 0x20, 0x43, 0x75, 0x6C, 0x74, 0x75, 0x72, 0x65, 0x3D, 0x6E, 0x65, 0x75, 0x74, 0x72, 0x61, 0x6C, 0x2C, 0x20, 0x50, 0x75, 0x62, 0x6C, 0x69, 0x63, 0x4B, 0x65, 0x79, 0x54, 0x6F, 0x6B, 0x65, 0x6E, 0x3D, 0x62, 0x37, 0x37, 0x61, 0x35, 0x63, 0x35, 0x36, 0x31, 0x39, 0x33, 0x34, 0x65, 0x30, 0x38, 0x39, 0x05, 0x01, 0x00, 0x00, 0x00, 0x2C, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6D, 0x2E, 0x49, 0x64, 0x65, 0x6E, 0x74, 0x69, 0x74, 0x79, 0x4D, 0x6F, 0x64, 0x65, 0x6C, 0x2E, 0x54, 0x6F, 0x6B, 0x65, 0x6E, 0x73, 0x2E, 0x42, 0x6F, 0x6F, 0x74, 0x73, 0x74, 0x72, 0x61, 0x70, 0x43, 0x6F, 0x6E, 0x74, 0x65, 0x78, 0x74, 0x02, 0x00, 0x00, 0x00, 0x01, 0x4B, 0x01, 0x54, 0x00, 0x01, 0x03, 0x02, 0x00, 0x00, 0x00, 0x53, 0x06, 0x03, 0x00, 0x00, 0x00, 0x05, 0x74, 0x6F, 0x6B, 0x65, 0x6E, 0x0B };
  18. // Put in some non-ascii/latin1 characters to test the encoding scheme
  19. // \u018E == Latin capital letter Reversed E
  20. private const string user = "us\u018Er";
  21. // \u00BD == Vulgar Fraction one half
  22. private const string password = "pass\u00BDword";
  23. private static readonly string SerializedBootstrapContextSecurityTokenString = "<UserNameSecurityToken Id=\"uuid-927c0b98-ba18-49d2-a653-306d60f85751-3\" Username=\"" + user + "\" Password=\"" + password + "\"/>";
  24. private static readonly byte [] SerializedBootstrapContextSecurityToken = new byte [] { 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x02, 0x00, 0x00, 0x00, 0x57, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6D, 0x2E, 0x49, 0x64, 0x65, 0x6E, 0x74, 0x69, 0x74, 0x79, 0x4D, 0x6F, 0x64, 0x65, 0x6C, 0x2C, 0x20, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3D, 0x34, 0x2E, 0x30, 0x2E, 0x30, 0x2E, 0x30, 0x2C, 0x20, 0x43, 0x75, 0x6C, 0x74, 0x75, 0x72, 0x65, 0x3D, 0x6E, 0x65, 0x75, 0x74, 0x72, 0x61, 0x6C, 0x2C, 0x20, 0x50, 0x75, 0x62, 0x6C, 0x69, 0x63, 0x4B, 0x65, 0x79, 0x54, 0x6F, 0x6B, 0x65, 0x6E, 0x3D, 0x62, 0x37, 0x37, 0x61, 0x35, 0x63, 0x35, 0x36, 0x31, 0x39, 0x33, 0x34, 0x65, 0x30, 0x38, 0x39, 0x05, 0x01, 0x00, 0x00, 0x00, 0x2C, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6D, 0x2E, 0x49, 0x64, 0x65, 0x6E, 0x74, 0x69, 0x74, 0x79, 0x4D, 0x6F, 0x64, 0x65, 0x6C, 0x2E, 0x54, 0x6F, 0x6B, 0x65, 0x6E, 0x73, 0x2E, 0x42, 0x6F, 0x6F, 0x74, 0x73, 0x74, 0x72, 0x61, 0x70, 0x43, 0x6F, 0x6E, 0x74, 0x65, 0x78, 0x74, 0x02, 0x00, 0x00, 0x00, 0x01, 0x4B, 0x01, 0x54, 0x00, 0x01, 0x03, 0x02, 0x00, 0x00, 0x00, 0x54, 0x06, 0x03, 0x00, 0x00, 0x00, 0x98, 0x01, 0x50, 0x46, 0x56, 0x7A, 0x5A, 0x58, 0x4A, 0x4F, 0x59, 0x57, 0x31, 0x6C, 0x55, 0x32, 0x56, 0x6A, 0x64, 0x58, 0x4A, 0x70, 0x64, 0x48, 0x6C, 0x55, 0x62, 0x32, 0x74, 0x6C, 0x62, 0x69, 0x42, 0x4A, 0x5A, 0x44, 0x30, 0x69, 0x64, 0x58, 0x56, 0x70, 0x5A, 0x43, 0x30, 0x35, 0x4D, 0x6A, 0x64, 0x6A, 0x4D, 0x47, 0x49, 0x35, 0x4F, 0x43, 0x31, 0x69, 0x59, 0x54, 0x45, 0x34, 0x4C, 0x54, 0x51, 0x35, 0x5A, 0x44, 0x49, 0x74, 0x59, 0x54, 0x59, 0x31, 0x4D, 0x79, 0x30, 0x7A, 0x4D, 0x44, 0x5A, 0x6B, 0x4E, 0x6A, 0x42, 0x6D, 0x4F, 0x44, 0x55, 0x33, 0x4E, 0x54, 0x45, 0x74, 0x4D, 0x79, 0x49, 0x67, 0x56, 0x58, 0x4E, 0x6C, 0x63, 0x6D, 0x35, 0x68, 0x62, 0x57, 0x55, 0x39, 0x49, 0x6E, 0x56, 0x7A, 0x78, 0x6F, 0x35, 0x79, 0x49, 0x69, 0x42, 0x51, 0x59, 0x58, 0x4E, 0x7A, 0x64, 0x32, 0x39, 0x79, 0x5A, 0x44, 0x30, 0x69, 0x63, 0x47, 0x46, 0x7A, 0x63, 0x38, 0x4B, 0x39, 0x64, 0x32, 0x39, 0x79, 0x5A, 0x43, 0x49, 0x76, 0x50, 0x67, 0x3D, 0x3D, 0x0B };
  25. [Test]
  26. public void Ctor_StringToken_Works ()
  27. {
  28. BootstrapContext bootstrapContext = new BootstrapContext ("token");
  29. Assert.AreEqual ("token", bootstrapContext.Token, "#1");
  30. Assert.IsNull (bootstrapContext.TokenBytes, "#2");
  31. Assert.IsNull (bootstrapContext.SecurityToken, "#3");
  32. Assert.IsNull (bootstrapContext.SecurityTokenHandler, "#4");
  33. }
  34. [Test]
  35. [ExpectedException (typeof (ArgumentNullException))]
  36. public void Ctor_StringToken_NullToken_Throws ()
  37. {
  38. BootstrapContext bootstrapContext = new BootstrapContext ((string)null);
  39. Assert.Fail ("Should have thrown");
  40. }
  41. [Test]
  42. public void Serialize_StringToken_Works ()
  43. {
  44. BootstrapContext bootstrapContext = new BootstrapContext ("token");
  45. BinaryFormatter binaryFormatter = new BinaryFormatter ();
  46. using (var s = new MemoryStream ()) {
  47. binaryFormatter.Serialize (s, bootstrapContext);
  48. s.Position = 0;
  49. BootstrapContext bootstrapContext2 = binaryFormatter.Deserialize (s) as BootstrapContext;
  50. Assert.IsNotNull (bootstrapContext2, "#1");
  51. Assert.AreEqual (bootstrapContext.Token, bootstrapContext2.Token, "#2");
  52. Assert.AreEqual (bootstrapContext.TokenBytes, bootstrapContext2.TokenBytes, "#3");
  53. Assert.AreEqual (bootstrapContext.SecurityToken, bootstrapContext2.SecurityToken, "#4");
  54. Assert.AreEqual (bootstrapContext.SecurityTokenHandler, bootstrapContext2.SecurityTokenHandler, "#5");
  55. }
  56. }
  57. [Test]
  58. public void Deserialize_StringToken_Works ()
  59. {
  60. BinaryFormatter binaryFormatter = new BinaryFormatter ();
  61. using (var s = new MemoryStream (SerializedBootstrapContextString)) {
  62. BootstrapContext bootstrapContext = binaryFormatter.Deserialize (s) as BootstrapContext;
  63. Assert.IsNotNull (bootstrapContext, "#1");
  64. Assert.AreEqual ("token", bootstrapContext.Token, "#2");
  65. Assert.IsNull (bootstrapContext.TokenBytes, "#3");
  66. Assert.IsNull (bootstrapContext.SecurityToken, "#4");
  67. Assert.IsNull (bootstrapContext.SecurityTokenHandler, "#5");
  68. }
  69. }
  70. [Test]
  71. public void Ctor_ByteArrayToken_Works ()
  72. {
  73. BootstrapContext bootstrapContext = new BootstrapContext (new byte [] { 0x01 });
  74. Assert.IsNotNull (bootstrapContext.TokenBytes, "#1");
  75. Assert.AreEqual (1, bootstrapContext.TokenBytes.Length, "#2");
  76. Assert.AreEqual (1, bootstrapContext.TokenBytes [0], "#3");
  77. Assert.IsNull (bootstrapContext.Token, "#4");
  78. Assert.IsNull (bootstrapContext.SecurityToken, "#5");
  79. Assert.IsNull (bootstrapContext.SecurityTokenHandler, "#6");
  80. }
  81. [Test]
  82. [ExpectedException (typeof (ArgumentNullException))]
  83. public void Ctor_ByteArrayToken_NullToken_Throws ()
  84. {
  85. BootstrapContext bootstrapContext = new BootstrapContext ((byte [])null);
  86. Assert.Fail ("Should have thrown");
  87. }
  88. [Test]
  89. public void Serialize_ByteArrayToken_Works ()
  90. {
  91. BootstrapContext bootstrapContext = new BootstrapContext (new byte [] { 0x1 });
  92. BinaryFormatter binaryFormatter = new BinaryFormatter ();
  93. using (var s = new MemoryStream ()) {
  94. binaryFormatter.Serialize (s, bootstrapContext);
  95. s.Position = 0;
  96. BootstrapContext bootstrapContext2 = binaryFormatter.Deserialize (s) as BootstrapContext;
  97. Assert.IsNotNull (bootstrapContext2, "#1");
  98. Assert.AreEqual (bootstrapContext.Token, bootstrapContext2.Token, "#2");
  99. Assert.AreEqual (bootstrapContext.TokenBytes, bootstrapContext2.TokenBytes, "#3");
  100. Assert.AreEqual (bootstrapContext.SecurityToken, bootstrapContext2.SecurityToken, "#4");
  101. Assert.AreEqual (bootstrapContext.SecurityTokenHandler, bootstrapContext2.SecurityTokenHandler, "#5");
  102. }
  103. }
  104. [Test]
  105. public void Deserialize_ByteArrayToken_Works ()
  106. {
  107. BinaryFormatter binaryFormatter = new BinaryFormatter ();
  108. using (var s = new MemoryStream (SerializedBootstrapContextByteArray)) {
  109. BootstrapContext bootstrapContext = binaryFormatter.Deserialize (s) as BootstrapContext;
  110. Assert.IsNotNull (bootstrapContext, "#1");
  111. Assert.IsNotNull (bootstrapContext.TokenBytes, "#2");
  112. Assert.AreEqual (1, bootstrapContext.TokenBytes.Length, "#3");
  113. Assert.AreEqual (1, bootstrapContext.TokenBytes [0], "#4");
  114. Assert.IsNull (bootstrapContext.Token, "#5");
  115. Assert.IsNull (bootstrapContext.SecurityToken, "#6");
  116. Assert.IsNull (bootstrapContext.SecurityTokenHandler, "#7");
  117. }
  118. }
  119. [Test]
  120. public void Ctor_SecurityToken_Works ()
  121. {
  122. var securityToken = new UserNameSecurityToken (user, password);
  123. var securityTokenHandler = new SimpleSecurityTokenHandler ();
  124. BootstrapContext bootstrapContext = new BootstrapContext (securityToken, securityTokenHandler);
  125. Assert.IsNotNull (bootstrapContext.SecurityToken, "#1");
  126. Assert.AreEqual (user, securityToken.UserName, "#2");
  127. Assert.AreEqual (password, securityToken.Password, "#3");
  128. Assert.AreEqual (securityTokenHandler, bootstrapContext.SecurityTokenHandler, "#4");
  129. Assert.IsNull (bootstrapContext.Token, "#5");
  130. Assert.IsNull (bootstrapContext.TokenBytes, "#6");
  131. }
  132. [Test]
  133. [ExpectedException (typeof (ArgumentNullException))]
  134. public void Ctor_SecurityToken_NullToken_Throws ()
  135. {
  136. BootstrapContext bootstrapContext = new BootstrapContext (null, new SimpleSecurityTokenHandler ());
  137. Assert.Fail ("Should have thrown");
  138. }
  139. [Test]
  140. [ExpectedException (typeof (ArgumentNullException))]
  141. public void Ctor_SecurityToken_NullHandler_Throws ()
  142. {
  143. BootstrapContext bootstrapContext = new BootstrapContext (new UserNameSecurityToken ("user", "password"), null);
  144. Assert.Fail ("Should have thrown");
  145. }
  146. [Test]
  147. public void Serialize_SecurityTokenAndHandler_Works ()
  148. {
  149. var securityToken = new UserNameSecurityToken (user, password, "uuid-927c0b98-ba18-49d2-a653-306d60f85751-3");
  150. var securityTokenHandler = new SimpleSecurityTokenHandler ();
  151. BootstrapContext bootstrapContext = new BootstrapContext (securityToken, securityTokenHandler);
  152. BinaryFormatter binaryFormatter = new BinaryFormatter ();
  153. using (var s = new MemoryStream ()) {
  154. binaryFormatter.Serialize (s, bootstrapContext);
  155. s.Position = 0;
  156. BootstrapContext bootstrapContext2 = binaryFormatter.Deserialize (s) as BootstrapContext;
  157. Assert.IsNotNull (bootstrapContext2, "#1");
  158. // Deserialize does not restore the SecurityToken, but restores into the Token.
  159. Assert.IsNotNull (bootstrapContext2.Token, "#3");
  160. // We replace ' /' by '/' to accomodate the xml writer differences between mono and .net
  161. Assert.AreEqual (SerializedBootstrapContextSecurityTokenString.Replace (" /", "/"), bootstrapContext2.Token.Replace (" /", "/"), "#2");
  162. Assert.AreEqual (bootstrapContext.TokenBytes, bootstrapContext2.TokenBytes, "#3");
  163. Assert.IsNull (bootstrapContext2.SecurityToken, "#4");
  164. Assert.IsNull (bootstrapContext2.SecurityTokenHandler, "#5");
  165. }
  166. }
  167. [Test]
  168. public void Deserialize_SecurityTokenAndHandler_Works ()
  169. {
  170. BinaryFormatter binaryFormatter = new BinaryFormatter ();
  171. using (var s = new MemoryStream (SerializedBootstrapContextSecurityToken)) {
  172. BootstrapContext bootstrapContext = binaryFormatter.Deserialize (s) as BootstrapContext;
  173. Assert.IsNotNull (bootstrapContext, "#1");
  174. Assert.AreEqual (SerializedBootstrapContextSecurityTokenString, bootstrapContext.Token, "#2");
  175. Assert.IsNull (bootstrapContext.SecurityToken, "#3");
  176. Assert.IsNull (bootstrapContext.SecurityTokenHandler, "#4");
  177. Assert.IsNull (bootstrapContext.TokenBytes, "#5");
  178. }
  179. }
  180. private static void DumpAsText (byte [] data)
  181. {
  182. Console.WriteLine ("{0}", Encoding.ASCII.GetString (data));
  183. }
  184. private static void Dump (byte [] data)
  185. {
  186. var sb = new StringBuilder ();
  187. sb.Append ("new byte[] { ");
  188. bool first = true;
  189. foreach (byte b in data) {
  190. if (!first)
  191. sb.Append (", ");
  192. else
  193. first = false;
  194. sb.AppendFormat ("0x{0:X2}", b);
  195. }
  196. sb.Append (" };");
  197. Console.WriteLine (sb.ToString ());
  198. }
  199. private class SimpleSecurityTokenHandler : SecurityTokenHandler {
  200. public override string [] GetTokenTypeIdentifiers ()
  201. {
  202. throw new NotImplementedException ();
  203. }
  204. public override Type TokenType {
  205. get { return typeof (UserNameSecurityToken); }
  206. }
  207. public override bool CanWriteToken {
  208. get { return true; }
  209. }
  210. public override void WriteToken (XmlWriter writer, SecurityToken token)
  211. {
  212. UserNameSecurityToken unst = token as UserNameSecurityToken;
  213. if (unst == null)
  214. throw new ArgumentException ("Token must be of type UserNameSecurityToken", "token");
  215. writer.WriteStartElement ("UserNameSecurityToken");
  216. writer.WriteAttributeString ("Id", unst.Id);
  217. writer.WriteAttributeString ("Username", unst.UserName);
  218. writer.WriteAttributeString ("Password", unst.Password);
  219. writer.WriteEndElement ();
  220. }
  221. }
  222. }
  223. }