SslStreamSecurityBindingElementTest.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // SslStreamSecurityBindingElementTest.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2006 Novell, Inc. http://www.novell.com
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections.ObjectModel;
  30. using System.IO;
  31. using System.Security.Cryptography.X509Certificates;
  32. using System.ServiceModel;
  33. using System.ServiceModel.Channels;
  34. using System.ServiceModel.Description;
  35. using System.ServiceModel.Security;
  36. using System.Text;
  37. using NUnit.Framework;
  38. namespace MonoTests.System.ServiceModel.Channels
  39. {
  40. [TestFixture]
  41. public class SslStreamSecurityBindingElementTest : BindingElementTest
  42. {
  43. [Test]
  44. [Category ("NotWorking")]
  45. public void DefaultValues ()
  46. {
  47. SslStreamSecurityBindingElement bel =
  48. new SslStreamSecurityBindingElement ();
  49. Assert.IsNotNull (bel.IdentityVerifier, "#1");
  50. Assert.AreEqual (false, bel.RequireClientCertificate, "#2");
  51. Assert.AreEqual ("<msf:SslTransportSecurity xmlns:msf=\"http://schemas.microsoft.com/ws/2006/05/framing/policy\" />", bel.GetTransportTokenAssertion ().OuterXml, "#3");
  52. }
  53. StreamSecurityUpgradeProvider CreateClientProvider (params object [] parameters)
  54. {
  55. SslStreamSecurityBindingElement bel =
  56. new SslStreamSecurityBindingElement ();
  57. BindingParameterCollection pl =
  58. new BindingParameterCollection ();
  59. foreach (object o in parameters)
  60. pl.Add (o);
  61. BindingContext ctx = new BindingContext (
  62. new CustomBinding (new HttpTransportBindingElement ()), pl);
  63. return bel.BuildClientStreamUpgradeProvider (ctx)
  64. as StreamSecurityUpgradeProvider;
  65. }
  66. [Test]
  67. [ExpectedException (typeof (InvalidOperationException))]
  68. [Category ("NotWorking")]
  69. public void ClientProviderCreateAcceptorBeforeOpen ()
  70. {
  71. StreamSecurityUpgradeProvider p = CreateClientProvider ();
  72. p.CreateUpgradeAcceptor ();
  73. }
  74. [Test]
  75. [Category ("NotWorking")]
  76. public void ClientAcceptUpgradeWithoutServiceCertificate ()
  77. {
  78. StreamSecurityUpgradeProvider p = CreateClientProvider ();
  79. Assert.IsNotNull (p, "#1");
  80. Assert.IsNull (p.Identity, "#2"); // not yet, before Open().
  81. p.Open ();
  82. StreamUpgradeAcceptor a = p.CreateUpgradeAcceptor ();
  83. try {
  84. Stream s = a.AcceptUpgrade (new MemoryStream (new byte [] {1, 2, 3, 4, 5}));
  85. Assert.Fail ("It should somehow raise an error."); // on Winfx it is unwise ArgumentNullException
  86. } catch (Exception) {
  87. } finally {
  88. p.Close ();
  89. }
  90. }
  91. [Test]
  92. [Ignore ("find out how to fill serverCertificate")]
  93. public void ClientAcceptUpgrade ()
  94. {
  95. ServiceCredentials cred = new ServiceCredentials ();
  96. X509Certificate2 cert =
  97. new X509Certificate2 ("Test/Resources/test.cer");
  98. cred.ServiceCertificate.Certificate = cert;
  99. X509CertificateEndpointIdentity ident =
  100. new X509CertificateEndpointIdentity (cert);
  101. StreamSecurityUpgradeProvider p = CreateClientProvider (cred, ident);
  102. p.Open ();
  103. try {
  104. StreamSecurityUpgradeAcceptor a =
  105. p.CreateUpgradeAcceptor ()
  106. as StreamSecurityUpgradeAcceptor;
  107. Assert.IsNotNull (a, "#1");
  108. SecurityMessageProperty prop =
  109. a.GetRemoteSecurity ();
  110. Assert.IsNull (prop, "#2"); // hmm
  111. Stream s = a.AcceptUpgrade (new MemoryStream (new byte [] {1, 2, 3, 4, 5}));
  112. } finally {
  113. p.Close ();
  114. }
  115. }
  116. }
  117. }