Merge pull request #439 from mono-soc-2012/garyb/iconfix
[mono.git] / mcs / class / System / System.Security.Permissions / ResourcePermissionBase.cs
old mode 100755 (executable)
new mode 100644 (file)
index c579b46..88bdbf4
@@ -6,7 +6,7 @@
 //     Sebastien Pouliot  <sebastien@ximian.com>
 //
 // (C) 2002
-// 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
@@ -50,9 +50,8 @@ namespace System.Security.Permissions {
 
                protected ResourcePermissionBase (PermissionState state) : this ()
                {
-                       // there are no validation of the permission state
+                       PermissionHelper.CheckPermissionState (state, true);
                        _unrestricted = (state == PermissionState.Unrestricted);
-                       // but any invalid value results in a restricted set
                }
 
                public const string Any = "*";
@@ -110,15 +109,25 @@ namespace System.Security.Permissions {
                [MonoTODO ("incomplete - need more test")]
                public override void FromXml (SecurityElement securityElement)
                {
-// duplicate MS behaviour - reported as FDBK15052
                        if (securityElement == null)
-                               throw new NullReferenceException ("securityElement");
+                               throw new ArgumentNullException ("securityElement");
                        CheckSecurityElement (securityElement, "securityElement", version, version);
                        // Note: we do not (yet) care about the return value 
                        // as we only accept version 1 (min/max values)
 
+                       _list.Clear ();
                        _unrestricted = PermissionHelper.IsUnrestricted (securityElement);
-                       // TODO
+                       if ((securityElement.Children == null) || (securityElement.Children.Count < 1))
+                               return;
+
+                       string[] names = new string [1];
+                       foreach (SecurityElement child in securityElement.Children) {
+                               // TODO: handle multiple names
+                               names [0] = child.Attribute ("name");
+                               int access = (int) Enum.Parse (PermissionAccessType, child.Attribute ("access"));
+                               ResourcePermissionBaseEntry entry = new ResourcePermissionBaseEntry (access, names);
+                               AddPermissionAccess (entry);
+                       }
                }
 
                protected ResourcePermissionBaseEntry[] GetPermissionEntries ()
@@ -159,12 +168,8 @@ namespace System.Security.Permissions {
                public override bool IsSubsetOf (IPermission target)
                {
                        if (target == null) {
-#if NET_2_0
                                // do not use Cast - different permissions (and earlier Fx) return false :-/
                                return true;
-#else
-                               return false;
-#endif
                        }
 
                        ResourcePermissionBase rpb = (target as ResourcePermissionBase);
@@ -209,9 +214,15 @@ namespace System.Security.Permissions {
                        else {
                                foreach (ResourcePermissionBaseEntry entry in _list) {
                                        SecurityElement container = se;
+                                       string access = null;
+                                       if (PermissionAccessType != null)
+                                               access = Enum.Format (PermissionAccessType, entry.PermissionAccess, "g");
+
                                        for (int i=0; i < _tags.Length; i++) {
                                                SecurityElement child = new SecurityElement (_tags [i]);
                                                child.AddAttribute ("name", entry.PermissionAccessPath [i]);
+                                               if (access != null)
+                                                       child.AddAttribute ("access", access);
                                                container.AddChild (child);
                                                child = container;
                                        }
@@ -308,7 +319,11 @@ namespace System.Security.Permissions {
                {
                        if (se == null)
                                throw new ArgumentNullException (parameterName);
-
+                       // Tag is case-sensitive
+                       if (se.Tag != "IPermission") {
+                               string msg = String.Format (Locale.GetText ("Invalid tag {0}"), se.Tag);
+                               throw new ArgumentException (msg, parameterName);
+                       }
                        // Note: we do not care about the class attribute at 
                        // this stage (in fact we don't even if the class 
                        // attribute is present or not). Anyway the object has
@@ -327,7 +342,11 @@ namespace System.Security.Permissions {
                                        throw new ArgumentException (msg, parameterName, e);
                                }
                        }
-
+                       if ((version < minimumVersion) || (version > maximumVersion)) {
+                               string msg = Locale.GetText ("Unknown version '{0}', expected versions between ['{1}','{2}'].");
+                               msg = String.Format (msg, version, minimumVersion, maximumVersion);
+                               throw new ArgumentException (msg, parameterName);
+                       }
                        return version;
                }