New tests.
[mono.git] / mcs / class / corlib / System.Security.Policy / Evidence.cs
index 21b1151fc694c33eef7d6194bfed0714d169cd13..5d932f5259d0fec1b475152154acaa88b9129246 100644 (file)
@@ -9,7 +9,7 @@
 //
 // (C) 2001 Ximian, Inc.
 // Portions (C) 2003, 2004 Motus Technologies Inc. (http://www.motus.com)
-// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
@@ -39,15 +39,15 @@ using System.Runtime.InteropServices;
 using System.Security.Permissions;
 using System.Security.Cryptography.X509Certificates;
 
+#if !MOONLIGHT
 using Mono.Security.Authenticode;
+#endif
 
 namespace System.Security.Policy {
 
        [Serializable]
-       [MonoTODO ("Fix serialization compatibility with MS.NET")]
-#if NET_2_0
+       [MonoTODO ("Serialization format not compatible with .NET")]
        [ComVisible (true)]
-#endif
        public sealed class Evidence : ICollection, IEnumerable {
        
                private bool _locked;
@@ -57,31 +57,40 @@ namespace System.Security.Policy {
 
                public Evidence () 
                {
-                       hostEvidenceList = ArrayList.Synchronized (new ArrayList ());
-                       assemblyEvidenceList = ArrayList.Synchronized (new ArrayList ());
                }
 
-               public Evidence (Evidence evidence) : this ()
+               public Evidence (Evidence evidence)
                {
                        if (evidence != null)
                                Merge (evidence);       
                }
 
-               public Evidence (object[] hostEvidence, object[] assemblyEvidence) : this ()
+#if NET_4_0
+               [Obsolete]
+#endif
+               public Evidence (object[] hostEvidence, object[] assemblyEvidence)
                {
                        if (null != hostEvidence)
-                               hostEvidenceList.AddRange (hostEvidence);
+                               HostEvidenceList.AddRange (hostEvidence);
                        if (null != assemblyEvidence)
-                               assemblyEvidenceList.AddRange (assemblyEvidence);
+                               AssemblyEvidenceList.AddRange (assemblyEvidence);
                }
                
                //
                // Public Properties
                //
        
+#if NET_4_0
+               [Obsolete]
+#endif
                public int Count {
                        get {
-                               return (hostEvidenceList.Count + assemblyEvidenceList.Count);
+                               int count = 0;
+                               if (hostEvidenceList != null)
+                                       count += hostEvidenceList.Count;
+                               if (assemblyEvidenceList!= null)
+                                       count += assemblyEvidenceList.Count;
+                               return count;
                        }
                }
 
@@ -90,18 +99,13 @@ namespace System.Security.Policy {
                }
                
                public bool IsSynchronized {
-#if NET_2_0
                        get { return false; }
-#else
-                       // LAMESPEC: Always TRUE (not FALSE)
-                       get { return true; }
-#endif
                }
 
                public bool Locked {
                        get { return _locked; }
+                       [SecurityPermission (SecurityAction.Demand, ControlEvidence = true)]
                        set { 
-                               new SecurityPermission (SecurityPermissionFlag.ControlEvidence).Demand ();
                                _locked = value; 
                        }
                }       
@@ -110,44 +114,72 @@ namespace System.Security.Policy {
                        get { return this; }
                }
 
+               internal ArrayList HostEvidenceList {
+                       get {
+                               if (hostEvidenceList == null)
+                                       hostEvidenceList = ArrayList.Synchronized (new ArrayList ());
+                               return hostEvidenceList;
+                       }
+               }
+
+               internal ArrayList AssemblyEvidenceList {
+                       get {
+                               if (assemblyEvidenceList == null)
+                                       assemblyEvidenceList = ArrayList.Synchronized (new ArrayList ());
+                               return assemblyEvidenceList;
+                       }
+               }
+
                //
                // Public Methods
                //
 
+#if NET_4_0
+               [Obsolete]
+#endif
                public void AddAssembly (object id) 
                {
-                       assemblyEvidenceList.Add (id);
+                       AssemblyEvidenceList.Add (id);
                        _hashCode = 0;
                }
 
+#if NET_4_0
+               [Obsolete]
+#endif
                public void AddHost (object id) 
                {
-                       if (_locked) {
+                       if (_locked && SecurityManager.SecurityEnabled) {
                                new SecurityPermission (SecurityPermissionFlag.ControlEvidence).Demand ();
                        }
-                       hostEvidenceList.Add (id);
+                       HostEvidenceList.Add (id);
                        _hashCode = 0;
                }
 
-#if NET_2_0
                [ComVisible (false)]
                public void Clear ()
                {
-                       hostEvidenceList.Clear ();
-                       assemblyEvidenceList.Clear ();
+                       if (hostEvidenceList != null)
+                               hostEvidenceList.Clear ();
+                       if (assemblyEvidenceList != null)
+                               assemblyEvidenceList.Clear ();
                        _hashCode = 0;
                }
-#endif
 
+#if NET_4_0
+               [Obsolete]
+#endif
                public void CopyTo (Array array, int index) 
                {
-                       if (hostEvidenceList.Count > 0) 
-                               hostEvidenceList.CopyTo (array, index);
-                       if (assemblyEvidenceList.Count > 0) 
-                               assemblyEvidenceList.CopyTo (array, index + hostEvidenceList.Count);
+                       int hc = 0;
+                       if (hostEvidenceList != null) {
+                               hc = hostEvidenceList.Count;
+                               if (hc > 0)
+                                       hostEvidenceList.CopyTo (array, index);
+                       }
+                       if ((assemblyEvidenceList != null) && (assemblyEvidenceList.Count > 0))
+                               assemblyEvidenceList.CopyTo (array, index + hc);
                }
 
-#if NET_2_0
                [ComVisible (false)]
                public override bool Equals (object obj)
                {
@@ -157,9 +189,9 @@ namespace System.Security.Policy {
                        if (e == null)
                                return false;
 
-                       if (hostEvidenceList.Count != e.hostEvidenceList.Count)
+                       if (HostEvidenceList.Count != e.HostEvidenceList.Count)
                                return false;
-                       if (assemblyEvidenceList.Count != e.assemblyEvidenceList.Count)
+                       if (AssemblyEvidenceList.Count != e.AssemblyEvidenceList.Count)
                                return false;
 
                        for (int i = 0; i < hostEvidenceList.Count; i++) {
@@ -187,56 +219,63 @@ namespace System.Security.Policy {
                        
                        return true;
                }
-#endif
 
+#if NET_4_0
+               [Obsolete]
+#endif
                public IEnumerator GetEnumerator () 
                {
-                       return new EvidenceEnumerator (hostEvidenceList.GetEnumerator (), 
-                               assemblyEvidenceList.GetEnumerator ());
+                       IEnumerator he = null;
+                       if (hostEvidenceList != null)
+                               he = hostEvidenceList.GetEnumerator ();
+                       IEnumerator ae = null;
+                       if (assemblyEvidenceList != null)
+                               ae = assemblyEvidenceList.GetEnumerator ();
+                       return new EvidenceEnumerator (he, ae);
                }
 
                public IEnumerator GetAssemblyEnumerator () 
                {
-                       return assemblyEvidenceList.GetEnumerator ();
+                       return AssemblyEvidenceList.GetEnumerator ();
                }
 
-#if NET_2_0
                [ComVisible (false)]
                public override int GetHashCode ()
                {
                        // kind of long so we cache it
                        if (_hashCode == 0) {
-                               for (int i = 0; i < hostEvidenceList.Count; i++)
-                                       _hashCode ^= hostEvidenceList [i].GetHashCode ();
-                               for (int i = 0; i < assemblyEvidenceList.Count; i++)
-                                       _hashCode ^= assemblyEvidenceList [i].GetHashCode ();
+                               if (hostEvidenceList != null) {
+                                       for (int i = 0; i < hostEvidenceList.Count; i++)
+                                               _hashCode ^= hostEvidenceList [i].GetHashCode ();
+                               }
+                               if (assemblyEvidenceList != null) {
+                                       for (int i = 0; i < assemblyEvidenceList.Count; i++)
+                                               _hashCode ^= assemblyEvidenceList [i].GetHashCode ();
+                               }
                        }
                        return _hashCode;
                }
-#endif
 
                public IEnumerator GetHostEnumerator () 
                {
-                       return hostEvidenceList.GetEnumerator ();
+                       return HostEvidenceList.GetEnumerator ();
                }
 
                public void Merge (Evidence evidence) 
                {
                        if ((evidence != null) && (evidence.Count > 0)) {
-                               IEnumerator hostenum = evidence.GetHostEnumerator ();
-                               while (hostenum.MoveNext ()) {
-                                       AddHost (hostenum.Current);
+                               if (evidence.hostEvidenceList != null) {
+                                       foreach (object o in evidence.hostEvidenceList)
+                                               AddHost (o);
                                }
-
-                               IEnumerator assemblyenum = evidence.GetAssemblyEnumerator ();
-                               while (assemblyenum.MoveNext ()) {
-                                       AddAssembly (assemblyenum.Current);
+                               if (evidence.assemblyEvidenceList != null) {
+                                       foreach (object o in evidence.assemblyEvidenceList)
+                                               AddAssembly (o);
                                }
                                _hashCode = 0;
                        }
                }
 
-#if NET_2_0
                [ComVisible (false)]
                public void RemoveType (Type t)
                {
@@ -253,13 +292,17 @@ namespace System.Security.Policy {
                                }
                        }
                }
-#endif
 
                // Use an icall to avoid multiple file i/o to detect the 
                // "possible" presence of an Authenticode signature
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
                static extern bool IsAuthenticodePresent (Assembly a);
-
+#if NET_2_1
+               static internal Evidence GetDefaultHostEvidence (Assembly a)
+               {
+                       return new Evidence ();
+               }
+#else
                // this avoid us to build all evidences from the runtime
                // (i.e. multiple unmanaged->managed calls) and also allows
                // to delay their creation until (if) needed
@@ -267,7 +310,7 @@ namespace System.Security.Policy {
                static internal Evidence GetDefaultHostEvidence (Assembly a) 
                {
                        Evidence e = new Evidence ();
-                       string aname = a.CodeBase;
+                       string aname = a.EscapedCodeBase;
 
                        // by default all assembly have the Zone, Url and Hash evidences
                        e.AddHost (Zone.CreateFromUrl (aname));
@@ -299,7 +342,6 @@ namespace System.Security.Policy {
                                        }
                                }
                        }
-#if NET_2_0
                        // assemblies loaded from the GAC also get a Gac evidence (new in Fx 2.0)
                        if (a.GlobalAssemblyCache) {
                                e.AddHost (new GacInstalled ());
@@ -313,10 +355,12 @@ namespace System.Security.Policy {
                                        e = dommgr.HostSecurityManager.ProvideAssemblyEvidence (a, e);
                                }
                        }
-#endif
+
                        return e;
                }
-       
+
+#endif // NET_2_1
+
                private class EvidenceEnumerator : IEnumerator {
                        
                        private IEnumerator currentEnum, hostEnum, assemblyEnum;                
@@ -325,14 +369,17 @@ namespace System.Security.Policy {
                        {
                                this.hostEnum = hostenum;
                                this.assemblyEnum = assemblyenum;
-                               currentEnum = hostEnum;                 
+                               currentEnum = hostEnum;         
                        }
 
                        public bool MoveNext () 
                        {
+                               if (currentEnum == null)
+                                       return false;
+
                                bool ret = currentEnum.MoveNext ();
                                
-                               if ( !ret && hostEnum == currentEnum ) {
+                               if (!ret && (hostEnum == currentEnum) && (assemblyEnum != null)) {
                                        currentEnum = assemblyEnum;
                                        ret = assemblyEnum.MoveNext ();
                                }
@@ -342,9 +389,14 @@ namespace System.Security.Policy {
 
                        public void Reset () 
                        {
-                               hostEnum.Reset ();
-                               assemblyEnum.Reset ();
-                               currentEnum = hostEnum;
+                               if (hostEnum != null) {
+                                       hostEnum.Reset ();
+                                       currentEnum = hostEnum;
+                               } else {
+                                       currentEnum = assemblyEnum;
+                               }
+                               if (assemblyEnum != null)
+                                       assemblyEnum.Reset ();
                        }
 
                        public object Current {