2
0

BootstrapContextTest.cs 14 KB

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