New test.
[mono.git] / mcs / class / corlib / System.Security / PermissionSetCollection.cs
1 //
2 // System.Security.PermissionSetCollection class
3 //
4 // Authors
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2004-2005 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 #if NET_2_0
30
31 using System.Collections;
32 using System.Runtime.InteropServices;
33 using System.Security.Permissions;
34
35 namespace System.Security {
36
37         [Serializable]
38         [ComVisible (true)]
39         [Obsolete ("seems the *Choice actions won't survive")]
40         public sealed class PermissionSetCollection : ICollection, IEnumerable {
41
42                 private static string tagName = "PermissionSetCollection";
43                 private IList _list;
44
45                 public PermissionSetCollection ()
46                 {
47                         _list = (IList) new ArrayList ();
48                 }
49
50                 // properties
51
52                 public int Count {
53                         get { return _list.Count; }
54                 }
55
56                 public bool IsSynchronized {
57                         get { return false; }
58                 }
59
60                 public IList PermissionSets {
61                         get { return _list; }
62                 }
63
64                 public object SyncRoot {
65                         // this is the "real thing"
66                         get { throw new NotSupportedException (); }
67                 }
68
69                 // methods
70
71                 public void Add (PermissionSet permSet)
72                 {
73                         if (permSet == null)
74                                 throw new ArgumentNullException ("permSet");
75                         _list.Add (permSet);
76                 }
77
78                 public PermissionSetCollection Copy ()
79                 {
80                         PermissionSetCollection psc = new PermissionSetCollection ();
81                         foreach (PermissionSet ps in _list) {
82                                 psc._list.Add (ps.Copy ());
83                         }
84                         return psc;
85                 }
86
87                 public void CopyTo (PermissionSet[] array, int index)
88                 {
89                         // this is the "real thing"
90                         throw new NotSupportedException ();
91                 }
92
93                 void ICollection.CopyTo (Array array, int index)
94                 {
95                         // this is the "real thing"
96                         throw new NotSupportedException ();
97                 }
98
99                 public void Demand ()
100                 {
101                         // check all collection in a single stack walk
102                         PermissionSet superset = new PermissionSet (PermissionState.None);
103                         foreach (PermissionSet ps in _list) {
104                                 foreach (IPermission p in ps) {
105                                         superset.AddPermission (p);
106                                 }
107                         }
108                         superset.Demand ();
109                 }
110
111                 public void FromXml (SecurityElement el) 
112                 {
113                         if (el == null)
114                                 throw new ArgumentNullException ("el");
115                         if (el.Tag != tagName) {
116                                 string msg = String.Format ("Invalid tag {0} expected {1}", el.Tag, tagName);
117                                 throw new ArgumentException (msg, "el");
118                         }
119                         _list.Clear ();
120                         if (el.Children != null) {
121                                 foreach (SecurityElement child in el.Children) {
122                                         PermissionSet ps = new PermissionSet (PermissionState.None);
123                                         ps.FromXml (child);
124                                         _list.Add (ps);
125                                 }
126                         }
127                 }
128
129                 public IEnumerator GetEnumerator ()
130                 {
131                         return _list.GetEnumerator ();
132                 }
133
134                 public PermissionSet GetSet (int index) 
135                 {
136                         if ((index < 0) || (index >= _list.Count))
137                                 throw new ArgumentOutOfRangeException ("index");
138
139                         return (PermissionSet) _list [index];
140                 }
141
142                 public void RemoveSet (int index) 
143                 {
144                         if ((index < 0) || (index >= _list.Count))
145                                 throw new ArgumentOutOfRangeException ("index");
146
147                         _list.RemoveAt (index);
148                 }
149
150                 public override string ToString ()
151                 {
152                         return ToXml ().ToString ();
153                 }
154
155                 public SecurityElement ToXml ()
156                 {
157                         SecurityElement se = new SecurityElement (tagName);
158                         foreach (PermissionSet ps in _list) {
159                                 se.AddChild (ps.ToXml ());
160                         }
161                         return se;
162                 }
163
164                 // internal stuff
165
166                 internal void DemandChoice ()
167                 {
168                         SecurityException exception = null;
169                         bool result = false;
170                         foreach (PermissionSet pset in _list) {
171                                 try {
172                                         pset.Demand ();
173                                         result = true;
174                                         break;
175                                 }
176                                 catch (SecurityException se) {
177                                         // keep the first failure, we may throw it if we not succeed
178                                         if (exception == null)
179                                                 exception = se;
180                                 }
181                         }
182
183                         if (!result) {
184                                 if (exception != null)
185                                         throw exception;
186                                 else
187                                         throw new SecurityException ("DemandChoice failed.");
188                         }
189                 }
190
191                 // 2.0 metadata format
192
193                 internal static PermissionSetCollection CreateFromBinaryFormat (byte[] data)
194                 {
195                         if ((data == null) || (data [0] != 0x2E) || (data.Length < 2)) {
196                                 string msg = Locale.GetText ("Invalid data in 2.0 metadata format.");
197                                 throw new SecurityException (msg);
198                         }
199
200                         int pos = 1;
201                         int numattr = PermissionSet.ReadEncodedInt (data, ref pos);
202                         PermissionSetCollection psc = new PermissionSetCollection ();
203                         for (int i = 0; i < numattr; i++) {
204                                 IPermission p = PermissionSet.ProcessAttribute (data, ref pos);
205                                 if (p == null) {
206                                         string msg = Locale.GetText ("Unsupported data found in 2.0 metadata format.");
207                                         throw new SecurityException (msg);
208                                 }
209
210                                 PermissionSet ps = new PermissionSet (PermissionState.None);
211                                 ps.DeclarativeSecurity = true;
212                                 ps.AddPermission (p); 
213                                 psc.Add (ps);
214                         }
215                         return psc;
216                 }
217         }
218 }
219
220 #endif