OleDbPermissionTest.cs 4.8 KB

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