all: notused.exe cas1a.exe cas2a.exe cas3a.exe \
cas1b.exe cas2b.exe cas3b.exe \
noncas1.exe noncas2.exe noncas3.exe noncas4.exe \
+ refload1.exe refload2.exe refload3.exe refload4.exe \
+ reftype1.exe reftype2.exe reftype3.exe reftype4.exe \
+ library1a.dll library1b.dll library2a.dll library2b.dll
aot: notused.exe.so cas1a.exe.so cas2a.exe.so cas3a.exe.so \
cas1b.exe.so cas2b.exe.so cas3b.exe.so \
noncas1.exe.so noncas2.exe.so noncas3.exe.so noncas4.exe.so \
+ refload1.exe.so refload2.exe.so refload3.exe.so refload4.exe.so \
+ reftype1.exe.so reftype2.exe.so reftype3.exe.so reftype4.exe.so \
+ library1a.dll.so library1b.dll.so library2a.dll.so library2b.dll.so
-FULLTRUST_TEST_FILES = notused cas1b cas2b cas3b noncas2 noncas4
+FULLTRUST_TEST_FILES = notused cas1b cas2b cas3b noncas2 noncas4 \
+ refload1 refload2 refload3 refload4 \
+ reftype1 reftype2 reftype3 reftype4
UNHANDLED_TEST_FILES = cas1a cas2a cas3a noncas1 noncas3
%.exe: %.cs
$(CSCOMPILE) $^ /out:$@
+%a.dll: %.cs
+ $(CSCOMPILE) /target:library $^ /out:$@
+
+%b.dll: %.cs
+ $(CSCOMPILE) /target:library $^ /out:$@ /keyfile:cas.snk
+
%a.exe: %.cs
$(CSCOMPILE) $^ /out:$@
noncas3.cs Non CAS (PrincipalPermission) on method (failure)
noncas4.cs Non CAS (PrincipalPermission) on method (success)
+refload1.cs Load assembly library1a (success)
+refload2.cs Load assembly library1b (success)
+refload3.cs Load assembly library2a (success)
+refload4.cs Load assembly library2b (success)
+reftype1.cs Load assembly library1a and access it's types
+reftype2.cs Load assembly library1b and access it's types
+reftype3.cs Load assembly library2a and access it's types
+reftype4.cs Load assembly library2b and access it's types
+library1.cs Inheritance (StrongName) on a class
+library2.cs Inheritance (StrongName) on a method
+
+The library#.cs assemblies are compiled two times. The first time (a) they
+aren't strongnamed (and not even delay-signed) using the cas.snk keypair. The
+second time (b) it is strongnamed using cas.snk.
+
Notes
* Inheritance checks are done at load time so SecurityException are mostly
- uncatchable (unhandled).
+ uncatchable (unhandled) - except when reflection is being used.
* Changing SecurityManager.SecurityEnabled has _NO_ effect on
InheritanceDemand, even for classes that aren't yet loaded.
--- /dev/null
+using System;
+using System.Security.Permissions;
+using System.Security.Principal;
+
+[StrongNameIdentityPermission (SecurityAction.InheritanceDemand, PublicKey="0024000004800000940000000602000000240000525341310004000011000000db294bcb78b7361ed6eb5656b612ce266fc81da8c8c6cb04116fc29b5e1d09a02f6c0f387f6d97a1ce9bdbbeb2d874832ae2d2971e70144ea039c710dccab5fb0a36cb14268a83c9b435c1e7318e7915518b68c8ed056b104e76166d6cabe9b77383f26bcf6a0a0b09d04f37b2a407b47d39421a34f2fbc6e6701a1d5c2e8cbb")]
+public abstract class BaseInheritanceDemand {
+
+ public virtual int Test ()
+ {
+ return 1;
+ }
+}
+
+public class InheritanceDemand : BaseInheritanceDemand {
+
+ public override int Test ()
+ {
+ Console.WriteLine ("*1* [this should NOT print]");
+ return base.Test ();
+ }
+}
+
+public class NoSecurity {
+
+ public int Test ()
+ {
+ Console.WriteLine ("*0* [this should print]");
+ return 0;
+ }
+}
--- /dev/null
+using System;
+using System.Security.Permissions;
+using System.Security.Principal;
+
+public abstract class BaseInheritanceDemand {
+
+ [StrongNameIdentityPermission (SecurityAction.InheritanceDemand, PublicKey="0024000004800000940000000602000000240000525341310004000011000000db294bcb78b7361ed6eb5656b612ce266fc81da8c8c6cb04116fc29b5e1d09a02f6c0f387f6d97a1ce9bdbbeb2d874832ae2d2971e70144ea039c710dccab5fb0a36cb14268a83c9b435c1e7318e7915518b68c8ed056b104e76166d6cabe9b77383f26bcf6a0a0b09d04f37b2a407b47d39421a34f2fbc6e6701a1d5c2e8cbb")]
+ public virtual int Test ()
+ {
+ return 1;
+ }
+}
+
+public class InheritanceDemand : BaseInheritanceDemand {
+
+ public override int Test ()
+ {
+ Console.WriteLine ("*1* [this should NOT print]");
+ return base.Test ();
+ }
+}
+
+public class NoSecurity {
+
+ public int Test ()
+ {
+ Console.WriteLine ("*0* [this should print]");
+ return 0;
+ }
+}
--- /dev/null
+using System;
+using System.Reflection;
+using System.Security;
+using System.Security.Permissions;
+
+[assembly: SecurityPermission (SecurityAction.RequestRefuse, ControlPrincipal=true)]
+
+public class Program {
+
+ static int Main (string[] args)
+ {
+ try {
+ string filename = "library1a";
+ Assembly a = Assembly.Load (filename);
+ if (a == null) {
+ Console.WriteLine ("*1* Couldn't load assembly '{0}'.", filename);
+ return 1;
+ } else {
+ Console.WriteLine ("*0* Assembly '{0}' loaded.", filename);
+ return 0;
+ }
+ }
+ catch (SecurityException se) {
+ Console.WriteLine ("*2* Unexpected SecurityException\n{0}", se);
+ return 2;
+ }
+ catch (Exception e) {
+ Console.WriteLine ("*3* Unexpected Exception\n{0}", e);
+ return 3;
+ }
+ }
+}
--- /dev/null
+using System;
+using System.Reflection;
+using System.Security;
+using System.Security.Permissions;
+
+[assembly: SecurityPermission (SecurityAction.RequestRefuse, ControlPrincipal=true)]
+
+public class Program {
+
+ static int Main (string[] args)
+ {
+ try {
+ string filename = "library1b";
+ Assembly a = Assembly.Load (filename);
+ if (a == null) {
+ Console.WriteLine ("*1* Couldn't load assembly '{0}'.", filename);
+ return 1;
+ } else {
+ Console.WriteLine ("*0* Assembly '{0}' loaded.", filename);
+ return 0;
+ }
+ }
+ catch (SecurityException se) {
+ Console.WriteLine ("*2* Unexpected SecurityException\n{0}", se);
+ return 2;
+ }
+ catch (Exception e) {
+ Console.WriteLine ("*3* Unexpected Exception\n{0}", e);
+ return 3;
+ }
+ }
+}
--- /dev/null
+using System;
+using System.Reflection;
+using System.Security;
+using System.Security.Permissions;
+
+[assembly: SecurityPermission (SecurityAction.RequestRefuse, ControlPrincipal=true)]
+
+public class Program {
+
+ static int Main (string[] args)
+ {
+ try {
+ string filename = "library2a";
+ Assembly a = Assembly.Load (filename);
+ if (a == null) {
+ Console.WriteLine ("*1* Couldn't load assembly '{0}'.", filename);
+ return 1;
+ } else {
+ Console.WriteLine ("*0* Assembly '{0}' loaded.", filename);
+ return 0;
+ }
+ }
+ catch (SecurityException se) {
+ Console.WriteLine ("*2* Unexpected SecurityException\n{0}", se);
+ return 2;
+ }
+ catch (Exception e) {
+ Console.WriteLine ("*3* Unexpected Exception\n{0}", e);
+ return 3;
+ }
+ }
+}
--- /dev/null
+using System;
+using System.Reflection;
+using System.Security;
+using System.Security.Permissions;
+
+[assembly: SecurityPermission (SecurityAction.RequestRefuse, ControlPrincipal=true)]
+
+public class Program {
+
+ static int Main (string[] args)
+ {
+ try {
+ string filename = "library2b";
+ Assembly a = Assembly.Load (filename);
+ if (a == null) {
+ Console.WriteLine ("*1* Couldn't load assembly '{0}'.", filename);
+ return 1;
+ } else {
+ Console.WriteLine ("*0* Assembly '{0}' loaded.", filename);
+ return 0;
+ }
+ }
+ catch (SecurityException se) {
+ Console.WriteLine ("*2* Unexpected SecurityException\n{0}", se);
+ return 2;
+ }
+ catch (Exception e) {
+ Console.WriteLine ("*3* Unexpected Exception\n{0}", e);
+ return 3;
+ }
+ }
+}
--- /dev/null
+using System;
+using System.Reflection;
+using System.Security;
+using System.Security.Permissions;
+
+public class Program {
+
+ private const string filename = "library1a";
+
+ static int GetTypeFalse (Assembly a)
+ {
+ string typename = "InheritanceDemand";
+ Type t = a.GetType (typename, false);
+ if (t == null) {
+ Console.WriteLine ("*0* Get null for type '{0}' with security.", typename);
+ return 0;
+ } else {
+ Console.WriteLine ("*1* Can get type '{0}' with security.", typename);
+ return 1;
+ }
+ }
+
+ static int GetTypeTrue (Assembly a)
+ {
+ try {
+ string typename = "InheritanceDemand";
+ Type t = a.GetType (typename, true);
+ Console.WriteLine ("*1* Can get type '{0}' with security.", t);
+ return 1;
+ }
+ catch (SecurityException se) {
+ Console.WriteLine ("*0* Expected SecurityException\n{0}", se);
+ return 0;
+ }
+ }
+
+ static int GetTypes (Assembly a)
+ {
+ try {
+ Type[] ts = a.GetTypes ();
+ Console.WriteLine ("*1* Can get all types from assembly '{0}' loaded. {1} types present.", filename, ts.Length);
+ return 1;
+ }
+ catch (ReflectionTypeLoadException rtle) {
+ Console.WriteLine ("*0* Expected ReflectionTypeLoadException\n{0}", rtle);
+ Console.WriteLine ("Types ({0}):", rtle.Types.Length);
+ for (int i=0; i < rtle.Types.Length; i++) {
+ Console.WriteLine ("\t{0}", rtle.Types [i]);
+ }
+ Console.WriteLine ("LoaderExceptions ({0}):", rtle.LoaderExceptions.Length);
+ for (int i=0; i < rtle.LoaderExceptions.Length; i++) {
+ Console.WriteLine ("\t{0}", rtle.LoaderExceptions [i]);
+ }
+ return 0;
+ }
+ }
+
+ static int Main ()
+ {
+ try {
+ Assembly a = Assembly.Load (filename);
+ if (a == null) {
+ Console.WriteLine ("*2* Couldn't load assembly '{0}'.", filename);
+ return 2;
+ }
+
+ string typename = "NoSecurity";
+ Type t = a.GetType (typename);
+ if (t == null) {
+ Console.WriteLine ("*3* Cannot get type '{0}' without security.", typename);
+ return 3;
+ }
+
+ int err = GetTypeFalse (a);
+ if (err != 0)
+ return err;
+
+ err = GetTypeTrue (a);
+ if (err != 0)
+ return err;
+
+ err = GetTypes (a);
+ return err;
+ }
+ catch (ReflectionTypeLoadException rtle) {
+ Console.WriteLine ("*4* Expected ReflectionTypeLoadException");
+ return 4;
+ }
+ catch (SecurityException se) {
+ Console.WriteLine ("*5* Unexpected SecurityException\n{0}", se);
+ return 5;
+ }
+ catch (Exception e) {
+ Console.WriteLine ("*6* Unexpected Exception\n{0}", e);
+ return 6;
+ }
+ }
+}
--- /dev/null
+using System;
+using System.Reflection;
+using System.Security;
+using System.Security.Permissions;
+
+public class Program {
+
+ private const string filename = "library1b";
+
+ static int GetTypeFalse (Assembly a)
+ {
+ string typename = "InheritanceDemand";
+ Type t = a.GetType (typename, false);
+ if (t == null) {
+ Console.WriteLine ("*1* Get null for type '{0}' with security.", typename);
+ return 1;
+ } else {
+ Console.WriteLine ("*0* Can get type '{0}' with security.", typename);
+ return 0;
+ }
+ }
+
+ static int GetTypeTrue (Assembly a)
+ {
+ string typename = "InheritanceDemand";
+ Type t = a.GetType (typename, true);
+ Console.WriteLine ("*0* Can get type '{0}' with security.", t);
+ return 0;
+ }
+
+ static int GetTypes (Assembly a)
+ {
+ Type[] ts = a.GetTypes ();
+ Console.WriteLine ("*0* Can get all types from assembly '{0}' loaded. {1} types present.", filename, ts.Length);
+ return 0;
+ }
+
+ static int Main ()
+ {
+ try {
+ Assembly a = Assembly.Load (filename);
+ if (a == null) {
+ Console.WriteLine ("*2* Couldn't load assembly '{0}'.", filename);
+ return 2;
+ }
+
+ string typename = "NoSecurity";
+ Type t = a.GetType (typename);
+ if (t == null) {
+ Console.WriteLine ("*3* Cannot get type '{0}' without security.", typename);
+ return 3;
+ }
+
+ int err = GetTypeFalse (a);
+ if (err != 0)
+ return err;
+
+ err = GetTypeTrue (a);
+ if (err != 0)
+ return err;
+
+ err = GetTypes (a);
+ return err;
+ }
+ catch (ReflectionTypeLoadException rtle) {
+ Console.WriteLine ("*4* Expected ReflectionTypeLoadException");
+ return 4;
+ }
+ catch (SecurityException se) {
+ Console.WriteLine ("*5* Unexpected SecurityException\n{0}", se);
+ return 5;
+ }
+ catch (Exception e) {
+ Console.WriteLine ("*6* Unexpected Exception\n{0}", e);
+ return 6;
+ }
+ }
+}
--- /dev/null
+using System;
+using System.Reflection;
+using System.Security;
+using System.Security.Permissions;
+
+public class Program {
+
+ private const string filename = "library2a";
+
+ static int GetTypeFalse (Assembly a)
+ {
+ string typename = "InheritanceDemand";
+ Type t = a.GetType (typename, false);
+ if (t == null) {
+ Console.WriteLine ("*0* Get null for type '{0}' with security.", typename);
+ return 0;
+ } else {
+ Console.WriteLine ("*1* Can get type '{0}' with security.", typename);
+ return 1;
+ }
+ }
+
+ static int GetTypeTrue (Assembly a)
+ {
+ try {
+ string typename = "InheritanceDemand";
+ Type t = a.GetType (typename, true);
+ Console.WriteLine ("*1* Can get type '{0}' with security.", t);
+ return 1;
+ }
+ catch (SecurityException se) {
+ Console.WriteLine ("*0* Expected SecurityException\n{0}", se);
+ return 0;
+ }
+ }
+
+ static int GetTypes (Assembly a)
+ {
+ try {
+ Type[] ts = a.GetTypes ();
+ Console.WriteLine ("*1* Can get all types from assembly '{0}' loaded. {1} types present.", filename, ts.Length);
+ return 1;
+ }
+ catch (ReflectionTypeLoadException rtle) {
+ Console.WriteLine ("*0* Expected ReflectionTypeLoadException\n{0}", rtle);
+ Console.WriteLine ("Types ({0}):", rtle.Types.Length);
+ for (int i=0; i < rtle.Types.Length; i++) {
+ Console.WriteLine ("\t{0}", rtle.Types [i]);
+ }
+ Console.WriteLine ("LoaderExceptions ({0}):", rtle.LoaderExceptions.Length);
+ for (int i=0; i < rtle.LoaderExceptions.Length; i++) {
+ Console.WriteLine ("\t{0}", rtle.LoaderExceptions [i]);
+ }
+ return 0;
+ }
+ }
+
+ static int Main ()
+ {
+ try {
+ Assembly a = Assembly.Load (filename);
+ if (a == null) {
+ Console.WriteLine ("*2* Couldn't load assembly '{0}'.", filename);
+ return 2;
+ }
+
+ string typename = "NoSecurity";
+ Type t = a.GetType (typename);
+ if (t == null) {
+ Console.WriteLine ("*3* Cannot get type '{0}' without security.", typename);
+ return 3;
+ }
+
+ int err = GetTypeFalse (a);
+ if (err != 0)
+ return err;
+
+ err = GetTypeTrue (a);
+ if (err != 0)
+ return err;
+
+ err = GetTypes (a);
+ return err;
+ }
+ catch (ReflectionTypeLoadException rtle) {
+ Console.WriteLine ("*4* Expected ReflectionTypeLoadException");
+ return 4;
+ }
+ catch (SecurityException se) {
+ Console.WriteLine ("*5* Unexpected SecurityException\n{0}", se);
+ return 5;
+ }
+ catch (Exception e) {
+ Console.WriteLine ("*6* Unexpected Exception\n{0}", e);
+ return 6;
+ }
+ }
+}
--- /dev/null
+using System;
+using System.Reflection;
+using System.Security;
+using System.Security.Permissions;
+
+public class Program {
+
+ private const string filename = "library2b";
+
+ static int GetTypeFalse (Assembly a)
+ {
+ string typename = "InheritanceDemand";
+ Type t = a.GetType (typename, false);
+ if (t == null) {
+ Console.WriteLine ("*1* Get null for type '{0}' with security.", typename);
+ return 1;
+ } else {
+ Console.WriteLine ("*0* Can get type '{0}' with security.", typename);
+ return 0;
+ }
+ }
+
+ static int GetTypeTrue (Assembly a)
+ {
+ string typename = "InheritanceDemand";
+ Type t = a.GetType (typename, true);
+ Console.WriteLine ("*0* Can get type '{0}' with security.", t);
+ return 0;
+ }
+
+ static int GetTypes (Assembly a)
+ {
+ Type[] ts = a.GetTypes ();
+ Console.WriteLine ("*0* Can get all types from assembly '{0}' loaded. {1} types present.", filename, ts.Length);
+ return 0;
+ }
+
+ static int Main ()
+ {
+ try {
+ Assembly a = Assembly.Load (filename);
+ if (a == null) {
+ Console.WriteLine ("*2* Couldn't load assembly '{0}'.", filename);
+ return 2;
+ }
+
+ string typename = "NoSecurity";
+ Type t = a.GetType (typename);
+ if (t == null) {
+ Console.WriteLine ("*3* Cannot get type '{0}' without security.", typename);
+ return 3;
+ }
+
+ int err = GetTypeFalse (a);
+ if (err != 0)
+ return err;
+
+ err = GetTypeTrue (a);
+ if (err != 0)
+ return err;
+
+ err = GetTypes (a);
+ return err;
+ }
+ catch (ReflectionTypeLoadException rtle) {
+ Console.WriteLine ("*4* Expected ReflectionTypeLoadException");
+ return 4;
+ }
+ catch (SecurityException se) {
+ Console.WriteLine ("*5* Unexpected SecurityException\n{0}", se);
+ return 5;
+ }
+ catch (Exception e) {
+ Console.WriteLine ("*6* Unexpected Exception\n{0}", e);
+ return 6;
+ }
+ }
+}