Example #1
0
	// Union creates a new permission that is the union of the current permission and the specified permission. 
	private bool UnionDemo()
	{

		bool returnValue = true;

		StrongNameIdentityPermission snIdPerm1, snIdPerm2;
		IPermission snIdPerm3;

		snIdPerm1 = new StrongNameIdentityPermission(blob, "MyCompany.MyDepartment.*", new Version("1.0.0.0"));
		snIdPerm2 = new StrongNameIdentityPermission(blob, "MyCompany.MyDepartment.MyFile", new Version("1.0.0.0"));
		Console.WriteLine("About to unionise");
		snIdPerm3 = (StrongNameIdentityPermission)snIdPerm1.Union(snIdPerm2);
		Console.WriteLine("We unionised but the union leaders are in league with the company management!");
		try
		{
			Console.WriteLine("The union of MyCompany.MyDepartment.*" +
			"and MyCompany.MyDepartment.MyFile is " +
			((StrongNameIdentityPermission)snIdPerm3).Name.ToString());
		}
		catch (Exception e)
		{
			Console.WriteLine("An expected exception was thrown: " + e.Message);
		}


		return returnValue;

	}
Example #2
0
 private void Compare(StrongNameIdentityPermission p1, StrongNameIdentityPermission p2, string prefix)
 {
     Assert.AreEqual(p1.Name, p2.Name, prefix + ".Name");
     Assert.AreEqual(p1.PublicKey, p2.PublicKey, prefix + ".PublicKey");
     Assert.AreEqual(p1.Version, p2.Version, prefix + ".Version");
     Assert.IsFalse(Object.ReferenceEquals(p1, p2), "ReferenceEquals");
 }
Example #3
0
        public void Intersect()
        {
            StrongNamePublicKeyBlob      blob = new StrongNamePublicKeyBlob(ecma);
            StrongNameIdentityPermission snip = new StrongNameIdentityPermission(blob, "mono", new Version(1, 2, 3, 4));

            StrongNameIdentityPermission intersect = (StrongNameIdentityPermission)snip.Intersect(null);

            Assert.IsNull(intersect, "snip N null");

            StrongNameIdentityPermission empty = new StrongNameIdentityPermission(PermissionState.None);

            intersect = (StrongNameIdentityPermission)snip.Intersect(empty);
#if NET_2_0
            Assert.IsNull(intersect, "snip N empty");
#else
            Compare(empty, intersect, "snip U empty");
#endif
            intersect = (StrongNameIdentityPermission)snip.Intersect(snip);
            Compare(snip, intersect, "snip U snip");

            StrongNameIdentityPermission samePk = new StrongNameIdentityPermission(blob, "novell", new Version(1, 2));
            intersect = (StrongNameIdentityPermission)snip.Intersect(samePk);
            Assert.IsNull(intersect, "(snip N samePk)");
            // strange, I would have expected a SNIP with the same public key...
        }
Example #4
0
    //</Snippet3>
    //<Snippet4>
    // Intersect creates and returns a new permission that is the intersection of the current
    // permission and the permission specified.
    private bool IntersectDemo()
    {
        bool returnValue = true;

        StrongNameIdentityPermission snIdPerm1, snIdPerm2, snIdPerm3;

        snIdPerm1 = new StrongNameIdentityPermission(blob, "MyCompany.MyDepartment.*", new Version("1.0.0.0"));
        snIdPerm2 = new StrongNameIdentityPermission(blob, "MyCompany.MyDepartment.MyFile", new Version("1.0.0.0"));

        try
        {
            snIdPerm3 = (StrongNameIdentityPermission)snIdPerm1.Intersect(snIdPerm2);

            Console.WriteLine("The intersection of MyCompany.MyDepartment.*"
                              + "MyCompany.MyDepartment.MyFile is "
                              + ((StrongNameIdentityPermission)snIdPerm3).Name.ToString());
        }
        catch (Exception e)
        {
            Console.WriteLine("An exception was thrown: " + e);
            returnValue = false;
        }

        return(returnValue);
    }
Example #5
0
        public void Name_Empty()
        {
            StrongNamePublicKeyBlob      blob = new StrongNamePublicKeyBlob(ecma);
            StrongNameIdentityPermission snip = new StrongNameIdentityPermission(blob, "mono", new Version(1, 2, 3, 4));

            snip.Name = String.Empty;
        }
Example #6
0
    //</Snippet2>
    //<Snippet3>
    // Union creates a new permission that is the union of the current permission and the specified permission.
    private bool UnionDemo()
    {
        bool returnValue = true;

        StrongNameIdentityPermission snIdPerm1, snIdPerm2;
        IPermission snIdPerm3;

        snIdPerm1 = new StrongNameIdentityPermission(blob, "MyCompany.MyDepartment.*", new Version("1.0.0.0"));
        snIdPerm2 = new StrongNameIdentityPermission(blob, "MyCompany.MyDepartment.MyFile", new Version("1.0.0.0"));

        snIdPerm3 = (StrongNameIdentityPermission)snIdPerm1.Union(snIdPerm2);

        try
        {
            Console.WriteLine("The union of MyCompany.MyDepartment.*" +
                              "and MyCompany.MyDepartment.MyFile is " +
                              ((StrongNameIdentityPermission)snIdPerm3).Name.ToString());
        }
        catch (Exception e)
        {
            Console.WriteLine("An expected exception was thrown: " + e.Message);
        }

        return(returnValue);
    }
Example #7
0
        public void PermissionStateNone()
        {
            StrongNameIdentityPermission snip = new StrongNameIdentityPermission(PermissionState.None);

            Assert.AreEqual(String.Empty, snip.Name, "Name");
            Assert.IsNull(snip.PublicKey, "PublicKey");
            Assert.AreEqual("0.0", snip.Version.ToString(), "Version");

            SecurityElement se = snip.ToXml();

#if NET_2_0
            Assert.IsNull(se.Attribute("Name"), "Xml-Name");
            Assert.IsNull(se.Attribute("AssemblyVersion"), "Xml-AssemblyVersion");
#else
            Assert.AreEqual(String.Empty, se.Attribute("Name"), "Xml-Name");
            Assert.AreEqual("0.0", se.Attribute("AssemblyVersion"), "Xml-AssemblyVersion");
#endif
            Assert.IsNull(se.Attribute("PublicKeyBlob"), "Xml-PublicKeyBlob");

            // because Name == String.Empty, which is illegal using the other constructor
            StrongNameIdentityPermission copy = (StrongNameIdentityPermission)snip.Copy();
            Assert.AreEqual(String.Empty, copy.Name, "Copy-Name");
#if NET_2_0
            // Strangely once copied the Name becomes equals to String.Empty in 2.0 [FDBK19351]
            Assert.IsNull(se.Attribute("AssemblyVersion"), "Copy-Version");
#else
            Assert.AreEqual("0.0", copy.Version.ToString(), "Copy-Version");
#endif
            Assert.IsNull(copy.PublicKey, "Copy-PublicKey");
        }
Example #8
0
        public void Intersect_DifferentPermissions()
        {
            StrongNameIdentityPermission a = new StrongNameIdentityPermission(PermissionState.None);
            SecurityPermission           b = new SecurityPermission(PermissionState.None);

            Assert.IsNull(a.Intersect(b));
        }
Example #9
0
        public void IsSubsetOf_DifferentPermissions()
        {
            StrongNameIdentityPermission a = new StrongNameIdentityPermission(PermissionState.None);
            SecurityPermission           b = new SecurityPermission(PermissionState.None);

            a.IsSubsetOf(b);
        }
Example #10
0
        public void Union()
        {
            StrongNamePublicKeyBlob      blob = new StrongNamePublicKeyBlob(ecma);
            StrongNameIdentityPermission snip = new StrongNameIdentityPermission(blob, "mono", new Version(1, 2, 3, 4));

            StrongNameIdentityPermission union = (StrongNameIdentityPermission)snip.Union(null);

            Compare(snip, union, "snip U null");

            StrongNameIdentityPermission empty = new StrongNameIdentityPermission(PermissionState.None);

            union = (StrongNameIdentityPermission)snip.Union(empty);
            Compare(snip, union, "snip U empty");

            union = (StrongNameIdentityPermission)snip.Union(snip);
            Compare(snip, union, "snip U snip");

            // note: can't be tested with PermissionState.Unrestricted

            StrongNameIdentityPermission samePk = new StrongNameIdentityPermission(blob, null, null);

            union = (StrongNameIdentityPermission)snip.Union(samePk);
#if !NET_2_0
            // can't compare the properties with multiple entries
            Compare(snip, union, "snip U samePk");
#endif
            Assert.IsTrue(snip.IsSubsetOf(union), "snip.IsSubsetOf (union)");

            union = (StrongNameIdentityPermission)samePk.Union(snip);
#if !NET_2_0
            // can't compare the properties with multiple entries
            Compare(snip, union, "samePk U snip");
#endif
            Assert.IsTrue(samePk.IsSubsetOf(union), "snip.IsSubsetOf (union)");
        }
Example #11
0
        public void Union_DifferentPermissions()
        {
            StrongNameIdentityPermission a = new StrongNameIdentityPermission(PermissionState.None);
            SecurityPermission           b = new SecurityPermission(PermissionState.None);

            a.Union(b);
        }
Example #12
0
        public void PublicKey_Null()
        {
            StrongNamePublicKeyBlob      blob = new StrongNamePublicKeyBlob(ecma);
            StrongNameIdentityPermission snip = new StrongNameIdentityPermission(blob, "mono", new Version(1, 2, 3, 4));

            snip.PublicKey = null;
        }
Example #13
0
        public void FromXml_WrongTag()
        {
            StrongNameIdentityPermission snip = new StrongNameIdentityPermission(PermissionState.None);
            SecurityElement se = snip.ToXml();

            se.Tag = "IMono";
            snip.FromXml(se);
        }
Example #14
0
        public void FromXml_WrongTagCase()
        {
            StrongNameIdentityPermission snip = new StrongNameIdentityPermission(PermissionState.None);
            SecurityElement se = snip.ToXml();

            se.Tag = "IPERMISSION";             // instead of IPermission
            snip.FromXml(se);
        }
Example #15
0
        public void PublicKey_Multiple_Set()
        {
            StrongNameIdentityPermission union = GetUnion();

            union.PublicKey = new StrongNamePublicKeyBlob(ecma);
            Assert.AreEqual(String.Empty, union.Name, "Name");
            Assert.IsNotNull(union.PublicKey, "PublicKey");
            Assert.AreEqual("0.0", union.Version.ToString(), "Version");
        }
Example #16
0
        private StrongNameIdentityPermission GetUnion()
        {
            StrongNamePublicKeyBlob      blob   = new StrongNamePublicKeyBlob(ecma);
            StrongNameIdentityPermission snip   = new StrongNameIdentityPermission(blob, "mono", new Version(1, 2, 3, 4));
            StrongNamePublicKeyBlob      blob2  = new StrongNamePublicKeyBlob(new byte[16]);
            StrongNameIdentityPermission diffPk = new StrongNameIdentityPermission(blob2, "mono", new Version(1, 2, 3, 4));

            return((StrongNameIdentityPermission)snip.Union(diffPk));
        }
Example #17
0
        public void FromXml_WrongVersion()
        {
            StrongNameIdentityPermission snip = new StrongNameIdentityPermission(PermissionState.None);
            SecurityElement se = snip.ToXml();

            se.Attributes.Remove("version");
            se.Attributes.Add("version", "2");
            snip.FromXml(se);
        }
Example #18
0
        public void Version_Multiple_Set()
        {
            StrongNameIdentityPermission union = GetUnion();

            union.Version = new Version("1.2.3.4");
            Assert.AreEqual(String.Empty, union.Name, "Name");
            Assert.IsNull(union.PublicKey, "PublicKey");
            Assert.AreEqual("1.2.3.4", union.Version.ToString(), "Version");
        }
Example #19
0
        public void Name_Multiple_Set()
        {
            StrongNameIdentityPermission union = GetUnion();

            union.Name = "oops";
            Assert.AreEqual("oops", union.Name, "Name");
            Assert.IsNull(union.PublicKey, "PublicKey");
            Assert.AreEqual("0.0", union.Version.ToString(), "Version");
        }
Example #20
0
        public void Name_Empty()
        {
            StrongNamePublicKeyBlob      blob = new StrongNamePublicKeyBlob(ecma);
            StrongNameIdentityPermission snip = new StrongNameIdentityPermission(blob, "mono", new Version(1, 2, 3, 4));

            snip.Name = String.Empty;
#if !NET_2_0
            Assert.AreEqual(String.Empty, snip.Name, "Name");
#endif
        }
Example #21
0
        public void FromXml_NoVersion()
        {
            StrongNameIdentityPermission snip = new StrongNameIdentityPermission(PermissionState.None);
            SecurityElement se = snip.ToXml();

            SecurityElement w = new SecurityElement(se.Tag);

            w.AddAttribute("class", se.Attribute("class"));
            snip.FromXml(w);
        }
Example #22
0
        public void IsSubsetOf()
        {
            StrongNamePublicKeyBlob      blob = new StrongNamePublicKeyBlob(ecma);
            StrongNameIdentityPermission snip = new StrongNameIdentityPermission(blob, "mono", new Version(1, 2, 3, 4));

            Assert.IsFalse(snip.IsSubsetOf(null), "snip.IsSubsetOf (null)");

            StrongNameIdentityPermission empty = new StrongNameIdentityPermission(PermissionState.None);

            Assert.IsTrue(empty.IsSubsetOf(null), "empty.IsSubsetOf (null)");
        }
Example #23
0
        public void Version()
        {
            StrongNamePublicKeyBlob      blob = new StrongNamePublicKeyBlob(ecma);
            StrongNameIdentityPermission snip = new StrongNameIdentityPermission(blob, "mono", new Version(1, 2, 3, 4));

            Assert.AreEqual("1.2.3.4", snip.Version.ToString(), "Version-1");
            snip.Version = null;
            Assert.IsNull(snip.Version, "Version-2");
            snip.Version = new Version(1, 2, 3);
            Assert.AreEqual("1.2.3", snip.Version.ToString(), "Version-3");
        }
Example #24
0
        public void Name()
        {
            StrongNamePublicKeyBlob      blob = new StrongNamePublicKeyBlob(ecma);
            StrongNameIdentityPermission snip = new StrongNameIdentityPermission(blob, "mono", new Version(1, 2, 3, 4));

            Assert.AreEqual("mono", snip.Name, "Name-1");
            snip.Name = null;
            Assert.IsNull(snip.Name, "Name-2");
            snip.Name = "mono";
            Assert.AreEqual("mono", snip.Name, "Name-3");
        }
        public void CreatePermission_OnlyPublicKey()
        {
            StrongNameIdentityPermissionAttribute a = new StrongNameIdentityPermissionAttribute(SecurityAction.Assert);

            a.PublicKey = pk;
            StrongNameIdentityPermission p = (StrongNameIdentityPermission)a.CreatePermission();

            Assert.IsNull(p.Name, "Name");
            Assert.AreEqual(pk, p.PublicKey.ToString(), "PublicKey");
            Assert.IsNull(p.Version, "Version");
        }
Example #26
0
        public void FromXml_NoClass()
        {
            StrongNameIdentityPermission snip = new StrongNameIdentityPermission(PermissionState.None);
            SecurityElement se = snip.ToXml();

            SecurityElement w = new SecurityElement(se.Tag);

            w.AddAttribute("version", se.Attribute("version"));
            snip.FromXml(w);
            // doesn't even care of the class attribute presence
        }
Example #27
0
        public void FromXml_NameEmpty()
        {
            StrongNameIdentityPermission snip = new StrongNameIdentityPermission(PermissionState.None);
            SecurityElement se = snip.ToXml();

            snip.FromXml(se);

            snip.PublicKey = new StrongNamePublicKeyBlob(ecma);
            snip.Version   = new Version("1.2.3.4");
            se             = snip.ToXml();
            snip.FromXml(se);
        }
Example #28
0
        public void Copy_NameEmpty()
        {
            StrongNameIdentityPermission snip = new StrongNameIdentityPermission(PermissionState.None);

            snip.PublicKey = new StrongNamePublicKeyBlob(ecma);
            snip.Version   = new Version("1.2.3.4");

            // because Name == String.Empty, which is illegal using the other constructor
            // but (somewhat) required to copy the teo other informations
            StrongNameIdentityPermission copy = (StrongNameIdentityPermission)snip.Copy();

            Assert.IsTrue(copy.Equals(snip), "Equals");
        }
        public void CreatePermission()
        {
            StrongNameIdentityPermissionAttribute a = new StrongNameIdentityPermissionAttribute(SecurityAction.Assert);

            a.Name      = "mono";
            a.PublicKey = pk;
            a.Version   = "1.2.3.4";
            StrongNameIdentityPermission p = (StrongNameIdentityPermission)a.CreatePermission();

            Assert.AreEqual("mono", p.Name, "Name");
            Assert.AreEqual(pk, p.PublicKey.ToString(), "PublicKey");
            Assert.AreEqual("1.2.3.4", p.Version.ToString(), "Version");
        }
Example #30
0
        public void FromXml_WrongClass()
        {
            StrongNameIdentityPermission snip = new StrongNameIdentityPermission(PermissionState.None);
            SecurityElement se = snip.ToXml();

            SecurityElement w = new SecurityElement(se.Tag);

            w.AddAttribute("class", "Wrong" + se.Attribute("class"));
            w.AddAttribute("version", se.Attribute("version"));
            snip.FromXml(w);
            // doesn't care of the class name at that stage
            // anyway the class has already be created so...
        }
Example #31
0
    //</Snippet5>
    //<Snippet6>
    // ToXml creates an XML encoding of the permission and its current state;
    //FromXml reconstructs a permission with the specified state from the XML encoding.
    private bool ToFromXmlDemo()
    {
        bool returnValue = true;

        StrongNameIdentityPermission snIdPerm1, snIdPerm2;

        snIdPerm1 = new StrongNameIdentityPermission(blob, "MyCompany.MyDepartment.*", new Version("1.0.0.0"));
        snIdPerm2 = new StrongNameIdentityPermission(PermissionState.None);
        snIdPerm2.FromXml(snIdPerm1.ToXml());
        Console.WriteLine("Result of ToFromXml = " + snIdPerm2.ToString() + "\n");

        return(returnValue);
    }
Example #32
0
	// IsSubsetOf determines whether the current permission is a subset of the specified permission. 
	private bool IsSubsetOfDemo()
	{

		bool returnValue = true;

		StrongNameIdentityPermission snIdPerm1, snIdPerm2;

		snIdPerm1 = new StrongNameIdentityPermission(blob, "MyCompany.MyDepartment.*", new Version("1.0.0.0"));
		snIdPerm2 = new StrongNameIdentityPermission(blob, "MyCompany.MyDepartment.MyFile", new Version("1.0.0.0"));

		if (snIdPerm1.IsSubsetOf(snIdPerm2))
		{

			Console.WriteLine("MyCompany.MyDepartment.* is a subset " +
			"of MyCompany.MyDepartment.MyFile \n");
		}
		else
		{
			Console.WriteLine("MyCompany.MyDepartment.*" +
			" is not a subset of MyCompany.MyDepartment.MyFile \n");
		}

		return returnValue;
	}
Example #33
0
	// Intersect creates and returns a new permission that is the intersection of the current 
	// permission and the permission specified. 
	private bool IntersectDemo()
	{

		bool returnValue = true;

		StrongNameIdentityPermission snIdPerm1, snIdPerm2, snIdPerm3;

		snIdPerm1 = new StrongNameIdentityPermission(blob, "MyCompany.MyDepartment.*", new Version("1.0.0.0"));
		snIdPerm2 = new StrongNameIdentityPermission(blob, "MyCompany.MyDepartment.MyFile", new Version("1.0.0.0"));

		try
		{

			snIdPerm3 = (StrongNameIdentityPermission)snIdPerm1.Intersect(snIdPerm2);

			Console.WriteLine("The intersection of MyCompany.MyDepartment.*"
			+ "MyCompany.MyDepartment.MyFile is "
			+ ((StrongNameIdentityPermission)snIdPerm3).Name.ToString());

		}
		catch (Exception e)
		{
			Console.WriteLine("An exception was thrown: " + e);
			returnValue = false;
		}

		return returnValue;

	}
Example #34
0
 public static void StrongNameIdentityPermissionCallMethods()
 {
     StrongNameIdentityPermission snip = new StrongNameIdentityPermission(new Permissions.PermissionState());
     StrongNameIdentityPermission snip2 = new StrongNameIdentityPermission(new Permissions.StrongNamePublicKeyBlob(new byte[1]), "test", new System.Version(1, 2));
     IPermission ip = snip.Copy();
     IPermission ip2 = snip.Intersect(ip);
     bool testbool = snip.IsSubsetOf(ip);
     ip2 = snip.Union(ip);
     SecurityElement se = new SecurityElement("");
     snip.FromXml(se);
     se = snip.ToXml();
 }
Example #35
0
	// ToXml creates an XML encoding of the permission and its current state; 
	//FromXml reconstructs a permission with the specified state from the XML encoding. 
	private bool ToFromXmlDemo()
	{

		bool returnValue = true;

		StrongNameIdentityPermission snIdPerm1, snIdPerm2;

		snIdPerm1 = new StrongNameIdentityPermission(blob, "MyCompany.MyDepartment.*", new Version("1.0.0.0"));
		snIdPerm2 = new StrongNameIdentityPermission(PermissionState.None);
		snIdPerm2.FromXml(snIdPerm1.ToXml());
		Console.WriteLine("Result of ToFromXml = " + snIdPerm2.ToString() + "\n");

		return returnValue;

	}