2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / System / Test / System.Net / DnsPermissionAttributeTest.cs
1 //
2 // DnsPermissionAttributeTest.cs - NUnit Test Cases for DnsPermissionAttribute
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
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
29 using NUnit.Framework;
30 using System;
31 using System.Net;
32 using System.Security;
33 using System.Security.Permissions;
34
35 namespace MonoTests.System.Net {
36
37         [TestFixture]
38         public class DnsPermissionAttributeTest {
39
40                 [Test]
41                 public void Default ()
42                 {
43                         DnsPermissionAttribute a = new DnsPermissionAttribute (SecurityAction.Assert);
44                         Assert.AreEqual (a.ToString (), a.TypeId.ToString (), "TypeId");
45                         Assert.IsFalse (a.Unrestricted, "Unrestricted");
46
47                         DnsPermission perm = (DnsPermission)a.CreatePermission ();
48                         Assert.IsFalse (a.Unrestricted, "Unrestricted");
49                 }
50
51                 [Test]
52                 public void Action ()
53                 {
54                         DnsPermissionAttribute a = new DnsPermissionAttribute (SecurityAction.Assert);
55                         Assert.AreEqual (SecurityAction.Assert, a.Action, "Action=Assert");
56                         a.Action = SecurityAction.Demand;
57                         Assert.AreEqual (SecurityAction.Demand, a.Action, "Action=Demand");
58                         a.Action = SecurityAction.Deny;
59                         Assert.AreEqual (SecurityAction.Deny, a.Action, "Action=Deny");
60                         a.Action = SecurityAction.InheritanceDemand;
61                         Assert.AreEqual (SecurityAction.InheritanceDemand, a.Action, "Action=InheritanceDemand");
62                         a.Action = SecurityAction.LinkDemand;
63                         Assert.AreEqual (SecurityAction.LinkDemand, a.Action, "Action=LinkDemand");
64                         a.Action = SecurityAction.PermitOnly;
65                         Assert.AreEqual (SecurityAction.PermitOnly, a.Action, "Action=PermitOnly");
66                         a.Action = SecurityAction.RequestMinimum;
67                         Assert.AreEqual (SecurityAction.RequestMinimum, a.Action, "Action=RequestMinimum");
68                         a.Action = SecurityAction.RequestOptional;
69                         Assert.AreEqual (SecurityAction.RequestOptional, a.Action, "Action=RequestOptional");
70                         a.Action = SecurityAction.RequestRefuse;
71                         Assert.AreEqual (SecurityAction.RequestRefuse, a.Action, "Action=RequestRefuse");
72 #if NET_2_0
73                         a.Action = SecurityAction.DemandChoice;
74                         Assert.AreEqual (SecurityAction.DemandChoice, a.Action, "Action=DemandChoice");
75                         a.Action = SecurityAction.InheritanceDemandChoice;
76                         Assert.AreEqual (SecurityAction.InheritanceDemandChoice, a.Action, "Action=InheritanceDemandChoice");
77                         a.Action = SecurityAction.LinkDemandChoice;
78                         Assert.AreEqual (SecurityAction.LinkDemandChoice, a.Action, "Action=LinkDemandChoice");
79 #endif
80                 }
81
82                 [Test]
83                 public void Action_Invalid ()
84                 {
85                         DnsPermissionAttribute a = new DnsPermissionAttribute ((SecurityAction)Int32.MinValue);
86                         // no validation in attribute
87                 }
88
89                 [Test]
90                 public void Unrestricted ()
91                 {
92                         DnsPermissionAttribute a = new DnsPermissionAttribute (SecurityAction.Assert);
93                         a.Unrestricted = true;
94                         DnsPermission dp = (DnsPermission) a.CreatePermission ();
95                         Assert.IsTrue (dp.IsUnrestricted (), "IsUnrestricted");
96
97                         a.Unrestricted = false;
98                         dp = (DnsPermission)a.CreatePermission ();
99                         Assert.IsFalse (dp.IsUnrestricted (), "!IsUnrestricted");
100                 }
101
102                 [Test]
103                 public void Attributes ()
104                 {
105                         Type t = typeof (DnsPermissionAttribute);
106                         Assert.IsTrue (t.IsSerializable, "IsSerializable");
107
108                         object [] attrs = t.GetCustomAttributes (typeof (AttributeUsageAttribute), false);
109                         Assert.AreEqual (1, attrs.Length, "AttributeUsage");
110                         AttributeUsageAttribute aua = (AttributeUsageAttribute)attrs [0];
111                         Assert.IsTrue (aua.AllowMultiple, "AllowMultiple");
112                         Assert.IsFalse (aua.Inherited, "Inherited");
113                         AttributeTargets at = (AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Constructor | AttributeTargets.Method);
114                         Assert.AreEqual (at, aua.ValidOn, "ValidOn");
115                 }
116         }
117 }