OdbcPermissionTest.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // OdbcPermissionTest.cs - NUnit Test Cases for OdbcPermission
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // Copyright (C) 2004 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 NUnit.Framework;
  29. using System;
  30. using System.Data;
  31. using System.Data.Common;
  32. using System.Data.Odbc;
  33. using System.Security;
  34. using System.Security.Permissions;
  35. namespace MonoTests.System.Data.Odbc {
  36. // NOTE: Most tests are are located in the base class, DBDataPermission
  37. [TestFixture]
  38. public class OdbcPermissionTest {
  39. private void Check (string msg, DBDataPermission dbdp, bool blank, bool unrestricted, int count)
  40. {
  41. Assert.AreEqual (blank, dbdp.AllowBlankPassword, msg + ".AllowBlankPassword");
  42. Assert.AreEqual (unrestricted, dbdp.IsUnrestricted (), msg + ".IsUnrestricted");
  43. if (count == 0)
  44. Assert.IsNull (dbdp.ToXml ().Children, msg + ".Count != 0");
  45. else
  46. Assert.AreEqual (count, dbdp.ToXml ().Children.Count, msg + ".Count");
  47. }
  48. [Test]
  49. #if NET_2_0
  50. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  51. #else
  52. [ExpectedException (typeof (ArgumentException))]
  53. #endif
  54. public void PermissionState_Invalid ()
  55. {
  56. PermissionState ps = (PermissionState)Int32.MinValue;
  57. OdbcPermission perm = new OdbcPermission (ps);
  58. }
  59. [Test]
  60. public void None ()
  61. {
  62. OdbcPermission perm = new OdbcPermission (PermissionState.None);
  63. Check ("None-1", perm, false, false, 0);
  64. perm.AllowBlankPassword = true;
  65. Check ("None-2", perm, true, false, 0);
  66. OdbcPermission copy = (OdbcPermission)perm.Copy ();
  67. Check ("Copy_None-1", copy, true, false, 0);
  68. copy.AllowBlankPassword = false;
  69. Check ("Copy_None-2", copy, false, false, 0);
  70. }
  71. [Test]
  72. public void None_Childs ()
  73. {
  74. OdbcPermission perm = new OdbcPermission (PermissionState.None);
  75. perm.Add ("data source=localhost;", String.Empty, KeyRestrictionBehavior.AllowOnly);
  76. perm.Add ("data source=127.0.0.1;", "password=;", KeyRestrictionBehavior.PreventUsage);
  77. Check ("None-Childs-1", perm, false, false, 2);
  78. perm.AllowBlankPassword = true;
  79. Check ("None-Childs-2", perm, true, false, 2);
  80. OdbcPermission copy = (OdbcPermission)perm.Copy ();
  81. Check ("Copy_None-Childs-1", copy, true, false, 2);
  82. copy.AllowBlankPassword = false;
  83. Check ("Copy_None-Childs-2", copy, false, false, 2);
  84. }
  85. [Test]
  86. public void Unrestricted ()
  87. {
  88. OdbcPermission perm = new OdbcPermission (PermissionState.Unrestricted);
  89. Check ("Unrestricted-1", perm, false, true, 0);
  90. perm.AllowBlankPassword = true;
  91. Check ("Unrestricted-2", perm, true, true, 0);
  92. OdbcPermission copy = (OdbcPermission)perm.Copy ();
  93. // note: Unrestricted is always created with default values (so AllowBlankPassword is false)
  94. Check ("Copy_Unrestricted-1", copy, false, true, 0);
  95. copy.AllowBlankPassword = true;
  96. Check ("Copy_Unrestricted-2", copy, true, true, 0);
  97. }
  98. [Test]
  99. public void Unrestricted_Add ()
  100. {
  101. OdbcPermission perm = new OdbcPermission (PermissionState.Unrestricted);
  102. Check ("Unrestricted-NoChild", perm, false, true, 0);
  103. perm.Add ("data source=localhost;", String.Empty, KeyRestrictionBehavior.AllowOnly);
  104. // note: Lost unrestricted state when children was added
  105. Check ("Unrestricted-WithChild", perm, false, false, 1);
  106. }
  107. }
  108. }