BootstrapContextTest.cs 14 KB

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