private void TestReflectionEmitIsNotPropagated() { ReflectionPermission perm = new ReflectionPermission(ReflectionPermissionFlag.ReflectionEmit); perm.Deny(); RegexCompilationInfo[] compiles; AssemblyName asmName; compiles = new RegexCompilationInfo[1]; compiles[0] = new RegexCompilationInfo(@"[a-z]+\d+", RegexOptions.None, "RegexPatOne", "RegexPatterns", true); asmName = new AssemblyName(); asmName.Name = "RegexPatAsm2"; Regex.CompileToAssembly(compiles, asmName); if (File.Exists("RegexPatAsm2.dll")) { File.Delete("RegexPatAsm2.dll"); } Debug.WriteLine(String.Format("Finished with regex")); String g = "tempfile" + Guid.NewGuid().ToString(); AssemblyName asmname = new AssemblyName(); asmname.Name = g; AssemblyBuilder asmbuild = System.Threading.Thread.GetDomain(). DefineDynamicAssembly(asmname, AssemblyBuilderAccess.RunAndSave); Debug.WriteLine(String.Format("AssemblyBuilder created, {0}", asmbuild)); ModuleBuilder mod = asmbuild.DefineDynamicModule("Mod1", asmname.Name + ".exe"); Debug.WriteLine(String.Format("Module created, {0}", mod)); throw new Exception("We shouldn't have gotten this far"); }
private static void Main(string[] args) { // verify that things work OK without reflection access var permission = new ReflectionPermission(ReflectionPermissionFlag.AllFlags); permission.Deny(); Console.WriteLine("Direct access"); TestDirect(1, false); // for JIT etc TestDirect(Cycles, true); Console.WriteLine(); string typeName = typeof(HyperTypeDescriptionProvider).Name; Console.WriteLine("Without " + typeName); RunTests(1, 1, false); // for JIT etc RunTests(MetaCycles, Cycles, true); HyperTypeDescriptionProvider.Add(typeof(MyEntity)); Console.WriteLine(); Console.WriteLine("With " + typeName); RunTests(1, 1, false); // for Emit, JIT etc RunTests(MetaCycles, Cycles, true); Console.ReadLine(); }
public void CannotInvokeNonPublicMethodWhenCallsMadeDirectly() { ReflectionPermission permission = new ReflectionPermission(ReflectionPermissionFlag.TypeInformation); permission.Deny(); XmlIncludeTypeDataCollection xmlIncludeTypes = new XmlIncludeTypeDataCollection(); xmlIncludeTypes.Add(new XmlIncludeTypeData("shouldNotBeAccessible", typeof(TestingInternal.ShouldNotBeAccessible).AssemblyQualifiedName)); RuntimeConfigurationView view = new RuntimeConfigurationView(new ConfigurationContext(new ConfigurationDictionary())); Type [] collectedTypes = view.CollectTypes(xmlIncludeTypes); Assert.AreEqual(1, collectedTypes.Length); }
private void TestReflectionEmit() { ReflectionPermission perm = new ReflectionPermission(ReflectionPermissionFlag.ReflectionEmit); perm.Deny(); RegexCompilationInfo[] compiles; AssemblyName asmName; compiles = new RegexCompilationInfo[1]; compiles[0] = new RegexCompilationInfo(@"[a-z]+\d+", RegexOptions.None, "RegexPatOne", "RegexPatterns", true); asmName = new AssemblyName(); asmName.Name = "RegexPatAsm1"; Regex.CompileToAssembly(compiles, asmName); if (File.Exists("RegexPatAsm1.dll")) { File.Delete("RegexPatAsm1.dll"); } }
public void CannotInvokeNonPublicMethodWhenCallsMadeThroughReflection() { ReflectionPermission permission = new ReflectionPermission(ReflectionPermissionFlag.TypeInformation); permission.Deny(); XmlIncludeTypeDataCollection xmlIncludeTypes = new XmlIncludeTypeDataCollection(); xmlIncludeTypes.Add(new XmlIncludeTypeData("shouldNotBeAccessible", typeof(TestingInternal.ShouldNotBeAccessible).AssemblyQualifiedName)); RuntimeConfigurationView view = new RuntimeConfigurationView(new ConfigurationContext(new ConfigurationDictionary())); MethodInfo collectTypes = view.GetType().GetMethod("CollectTypes", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[] { typeof(XmlIncludeTypeDataCollection) }, null); object returnedTypes = collectTypes.Invoke(view, new object[] { xmlIncludeTypes }); Type [] collectedTypes = returnedTypes as Type[]; Assert.AreEqual(1, collectedTypes.Length); }
public bool runTest() { Console.WriteLine(s_strTFPath + "\\" + s_strTFName + " , for " + s_strClassMethod + " , Source ver " + s_strDtTmVer); String strLoc = "Loc_000oo"; String strValue = String.Empty; int iCountErrors = 0; int iCountTestcases = 0; Int32 iValue; A a1; V v1; S s1; V_I vi1; Surrogate_1 surrogate; SurrogateSelector selector; SoapFormatter formatter; MemoryStream stream; StreamingContext context = new StreamingContext(StreamingContextStates.All); Object objRet; ReflectionPermission perm; try { strLoc = "Loc_9347sg"; iCountTestcases++; iValue = 5; formatter = new SoapFormatter(); stream = new MemoryStream(); try{ Serialize(formatter, stream, iValue); iCountErrors++; Console.WriteLine("Err_7349sg! No exception thrown, "); }catch(SecurityException){ }catch(Exception ex){ iCountErrors++; Console.WriteLine("Err_348tdg! Wrong exception thrown, " + ex.GetType().Name); } strLoc = "Loc_2407sdg"; iCountTestcases++; stream.Position = 0; formatter.Serialize(stream, iValue); stream.Position = 0; try{ objRet = Deserialize(formatter, stream); }catch(Exception ex){ iCountErrors++; Console.WriteLine("Err_207tdsg! Wrong exception thrown, " + ex.GetType().Name); } strLoc = "Loc_3947tfdg"; iCountTestcases++; a1 = new A(5); formatter = new SoapFormatter(); stream = new MemoryStream(); try{ Serialize(formatter, stream, a1); iCountErrors++; Console.WriteLine("Err_39475sdg! No exception thrown, "); }catch(SecurityException){ }catch(Exception ex){ iCountErrors++; Console.WriteLine("Err_2047tsfgb! Wrong exception thrown, " + ex.GetType().Name); } stream.Position = 0; formatter.Serialize(stream, a1); stream.Position = 0; try{ objRet = Deserialize(formatter, stream); iCountErrors++; Console.WriteLine("Err_9347tsfg! No exception thrown, "); }catch(SecurityException){ }catch(Exception ex){ iCountErrors++; Console.WriteLine("Err_0347tsfg! Wrong exception thrown, " + ex.GetType().Name); } strLoc = "Loc_34975sg"; iCountTestcases++; v1 = new V(5); formatter = new SoapFormatter(); stream = new MemoryStream(); try{ Serialize(formatter, stream, v1); iCountErrors++; Console.WriteLine("Err_98324sdg! No exception thrown, "); }catch(SecurityException){ }catch(Exception ex){ iCountErrors++; Console.WriteLine("Err_3047tfxg! Wrong exception thrown, " + ex.GetType().Name); } stream.Position = 0; formatter.Serialize(stream, v1); stream.Position = 0; try{ objRet = Deserialize(formatter, stream); iCountErrors++; Console.WriteLine("Err_7349sg! No exception thrown, "); } catch(SecurityException) { } catch(Exception ex) { iCountErrors++; Console.WriteLine("Err_29437tsg! Wrong exception thrown, " + ex.GetType().Name); } strLoc = "Loc_34975sg"; iCountTestcases++; s1 = new S(5); formatter = new SoapFormatter(); stream = new MemoryStream(); try{ Serialize(formatter, stream, s1); iCountErrors++; Console.WriteLine("Err_3946tsdg! No exception thrown, "); }catch(SecurityException){ }catch(Exception ex){ iCountErrors++; Console.WriteLine("Err_9347tsdg! Wrong exception thrown, " + ex.GetType().Name); } stream.Position = 0; formatter.Serialize(stream, s1); stream.Position = 0; try{ objRet = Deserialize(formatter, stream); iCountErrors++; Console.WriteLine("Err_98324sdg! No exception thrown, "); } catch(SecurityException) { } catch(Exception ex) { iCountErrors++; Console.WriteLine("Err_93745sdg! Exception thrown, " + ex.GetType().Name); } strLoc = "Loc_34975sg"; iCountTestcases++; vi1 = new V_I(6); formatter = new SoapFormatter(); stream = new MemoryStream(); try{ Serialize(formatter, stream, vi1); iCountErrors++; Console.WriteLine("Err_2075sdg! No exception thrown, "); }catch(SecurityException){ }catch(Exception ex){ iCountErrors++; Console.WriteLine("Err_294375sdg! Wrong exception thrown, " + ex.GetType().Name); } stream.Position = 0; formatter.Serialize(stream, vi1); stream.Position = 0; try{ objRet = Deserialize(formatter, stream); iCountErrors++; Console.WriteLine("Err_98324sdg! No exception thrown, "); } catch(SecurityException) { } catch(Exception ex) { iCountErrors++; Console.WriteLine("Err_24907tsdfg! Exception thrown, " + ex.GetType().Name); } strLoc = "Loc_34975sg"; iCountTestcases++; selector = new SurrogateSelector(); surrogate = new Surrogate_1(); selector.AddSurrogate(typeof(A), context, surrogate); selector.AddSurrogate(typeof(V), context, surrogate); formatter = new SoapFormatter(); formatter.SurrogateSelector = selector; stream = new MemoryStream(); a1 = new A(5); try{ Serialize(formatter, stream, a1); iCountErrors++; Console.WriteLine("Err_93475sdg! No exception thrown, "); }catch(SecurityException){ }catch(Exception ex){ iCountErrors++; Console.WriteLine("Err_3947tsg! Wrong exception thrown, " + ex.GetType().Name); } stream.Position = 0; formatter.Serialize(stream, a1); stream.Position = 0; try{ objRet = Deserialize(formatter, stream); iCountErrors++; Console.WriteLine("Err_98324sdg! No exception thrown, "); } catch(SecurityException) { } catch(Exception ex) { iCountErrors++; Console.WriteLine("Err_39047tsg! Exception thrown, " + ex.GetType().Name); } strLoc = "Loc_3048sdg"; iCountTestcases++; v1 = new V(7); formatter = new SoapFormatter(); formatter.SurrogateSelector = selector; stream = new MemoryStream(); try{ Serialize(formatter, stream, v1); iCountErrors++; Console.WriteLine("Err_9347tsgf! No exception thrown, "); }catch(SecurityException){ }catch(Exception ex){ iCountErrors++; Console.WriteLine("Err_2947tsgd! Wrong exception thrown, " + ex.GetType().Name); } stream.Position = 0; formatter.Serialize(stream, v1); stream.Position = 0; try{ objRet = Deserialize(formatter, stream); iCountErrors++; Console.WriteLine("Err_98324sdg! No exception thrown, "); } catch(SecurityException) { } catch(Exception ex) { iCountErrors++; Console.WriteLine("Err_89734trfsg! Exception thrown, " + ex.GetType().Name); } perm = new ReflectionPermission(PermissionState.Unrestricted); perm.Deny(); strLoc = "Loc_83245tsg"; iCountTestcases++; a1 = new A(5); formatter = new SoapFormatter(); stream = new MemoryStream(); try{ formatter.Serialize(stream, a1); stream.Position = 0; objRet = formatter.Deserialize(stream); if(((A)objRet).I!=5){ iCountErrors++; Console.WriteLine("Err_3947tsdg! wrong value returned, " + (Int32)objRet); } }catch(Exception ex){ iCountErrors++; Console.WriteLine("Err_29746tsfdg! Exception thrown, " + ex.GetType().Name); } } catch (Exception exc_general ) { ++iCountErrors; Console.WriteLine (s_strTFAbbrev + " : Error Err_8888yyy! strLoc=="+ strLoc +", exc_general=="+exc_general.ToString()); } if ( iCountErrors == 0 ) { Console.WriteLine( "paSs. "+s_strTFName+" ,iCountTestcases=="+iCountTestcases.ToString()); return true; } else { Console.WriteLine("FAiL! "+s_strTFName+" ,inCountErrors=="+iCountErrors.ToString()+" , BugNums?: "+s_strActiveBugNums ); return false; } }
private void TestReflectionEmit() { ReflectionPermission perm = new ReflectionPermission(ReflectionPermissionFlag.ReflectionEmit); perm.Deny(); RegexCompilationInfo[] compiles; AssemblyName asmName; compiles = new RegexCompilationInfo[1]; compiles[0] = new RegexCompilationInfo(@"[a-z]+\d+", RegexOptions.None, "RegexPatOne", "RegexPatterns", true); asmName = new AssemblyName(); asmName.Name = "RegexPatAsm1"; Regex.CompileToAssembly(compiles, asmName); if(File.Exists("RegexPatAsm1.dll")) File.Delete("RegexPatAsm1.dll"); }
private void TestReflectionEmitIsNotPropagated() { ReflectionPermission perm = new ReflectionPermission(ReflectionPermissionFlag.ReflectionEmit); perm.Deny(); RegexCompilationInfo[] compiles; AssemblyName asmName; compiles = new RegexCompilationInfo[1]; compiles[0] = new RegexCompilationInfo(@"[a-z]+\d+", RegexOptions.None, "RegexPatOne", "RegexPatterns", true); asmName = new AssemblyName(); asmName.Name = "RegexPatAsm2"; Regex.CompileToAssembly(compiles, asmName); if(File.Exists("RegexPatAsm2.dll")) File.Delete("RegexPatAsm2.dll"); Debug.WriteLine(String.Format("Finished with regex")); String g = "tempfile" + Guid.NewGuid().ToString(); AssemblyName asmname = new AssemblyName(); asmname.Name = g; AssemblyBuilder asmbuild = System.Threading.Thread.GetDomain(). DefineDynamicAssembly(asmname, AssemblyBuilderAccess.RunAndSave); Debug.WriteLine(String.Format("AssemblyBuilder created, {0}", asmbuild)); ModuleBuilder mod = asmbuild.DefineDynamicModule("Mod1", asmname.Name + ".exe" ); Debug.WriteLine(String.Format("Module created, {0}", mod)); throw new Exception("We shouldn't have gotten this far"); }
public bool runTest() { Console.WriteLine(s_strTFPath + "\\" + s_strTFName + " , for " + s_strClassMethod + " , Source ver " + s_strDtTmVer); String strLoc = "Loc_000oo"; String strValue = String.Empty; int iCountErrors = 0; int iCountTestcases = 0; Int32 iValue; A a1; V v1; S s1; V_I vi1; Surrogate_1 surrogate; SurrogateSelector selector; SoapFormatter formatter; MemoryStream stream; StreamingContext context = new StreamingContext(StreamingContextStates.All); Object objRet; ReflectionPermission perm; try { strLoc = "Loc_9347sg"; iCountTestcases++; iValue = 5; formatter = new SoapFormatter(); stream = new MemoryStream(); try{ Serialize(formatter, stream, iValue); iCountErrors++; Console.WriteLine("Err_7349sg! No exception thrown, "); }catch (SecurityException) { }catch (Exception ex) { iCountErrors++; Console.WriteLine("Err_348tdg! Wrong exception thrown, " + ex.GetType().Name); } strLoc = "Loc_2407sdg"; iCountTestcases++; stream.Position = 0; formatter.Serialize(stream, iValue); stream.Position = 0; try{ objRet = Deserialize(formatter, stream); }catch (Exception ex) { iCountErrors++; Console.WriteLine("Err_207tdsg! Wrong exception thrown, " + ex.GetType().Name); } strLoc = "Loc_3947tfdg"; iCountTestcases++; a1 = new A(5); formatter = new SoapFormatter(); stream = new MemoryStream(); try{ Serialize(formatter, stream, a1); iCountErrors++; Console.WriteLine("Err_39475sdg! No exception thrown, "); }catch (SecurityException) { }catch (Exception ex) { iCountErrors++; Console.WriteLine("Err_2047tsfgb! Wrong exception thrown, " + ex.GetType().Name); } stream.Position = 0; formatter.Serialize(stream, a1); stream.Position = 0; try{ objRet = Deserialize(formatter, stream); iCountErrors++; Console.WriteLine("Err_9347tsfg! No exception thrown, "); }catch (SecurityException) { }catch (Exception ex) { iCountErrors++; Console.WriteLine("Err_0347tsfg! Wrong exception thrown, " + ex.GetType().Name); } strLoc = "Loc_34975sg"; iCountTestcases++; v1 = new V(5); formatter = new SoapFormatter(); stream = new MemoryStream(); try{ Serialize(formatter, stream, v1); iCountErrors++; Console.WriteLine("Err_98324sdg! No exception thrown, "); }catch (SecurityException) { }catch (Exception ex) { iCountErrors++; Console.WriteLine("Err_3047tfxg! Wrong exception thrown, " + ex.GetType().Name); } stream.Position = 0; formatter.Serialize(stream, v1); stream.Position = 0; try{ objRet = Deserialize(formatter, stream); iCountErrors++; Console.WriteLine("Err_7349sg! No exception thrown, "); } catch (SecurityException) { } catch (Exception ex) { iCountErrors++; Console.WriteLine("Err_29437tsg! Wrong exception thrown, " + ex.GetType().Name); } strLoc = "Loc_34975sg"; iCountTestcases++; s1 = new S(5); formatter = new SoapFormatter(); stream = new MemoryStream(); try{ Serialize(formatter, stream, s1); iCountErrors++; Console.WriteLine("Err_3946tsdg! No exception thrown, "); }catch (SecurityException) { }catch (Exception ex) { iCountErrors++; Console.WriteLine("Err_9347tsdg! Wrong exception thrown, " + ex.GetType().Name); } stream.Position = 0; formatter.Serialize(stream, s1); stream.Position = 0; try{ objRet = Deserialize(formatter, stream); iCountErrors++; Console.WriteLine("Err_98324sdg! No exception thrown, "); } catch (SecurityException) { } catch (Exception ex) { iCountErrors++; Console.WriteLine("Err_93745sdg! Exception thrown, " + ex.GetType().Name); } strLoc = "Loc_34975sg"; iCountTestcases++; vi1 = new V_I(6); formatter = new SoapFormatter(); stream = new MemoryStream(); try{ Serialize(formatter, stream, vi1); iCountErrors++; Console.WriteLine("Err_2075sdg! No exception thrown, "); }catch (SecurityException) { }catch (Exception ex) { iCountErrors++; Console.WriteLine("Err_294375sdg! Wrong exception thrown, " + ex.GetType().Name); } stream.Position = 0; formatter.Serialize(stream, vi1); stream.Position = 0; try{ objRet = Deserialize(formatter, stream); iCountErrors++; Console.WriteLine("Err_98324sdg! No exception thrown, "); } catch (SecurityException) { } catch (Exception ex) { iCountErrors++; Console.WriteLine("Err_24907tsdfg! Exception thrown, " + ex.GetType().Name); } strLoc = "Loc_34975sg"; iCountTestcases++; selector = new SurrogateSelector(); surrogate = new Surrogate_1(); selector.AddSurrogate(typeof(A), context, surrogate); selector.AddSurrogate(typeof(V), context, surrogate); formatter = new SoapFormatter(); formatter.SurrogateSelector = selector; stream = new MemoryStream(); a1 = new A(5); try{ Serialize(formatter, stream, a1); iCountErrors++; Console.WriteLine("Err_93475sdg! No exception thrown, "); }catch (SecurityException) { }catch (Exception ex) { iCountErrors++; Console.WriteLine("Err_3947tsg! Wrong exception thrown, " + ex.GetType().Name); } stream.Position = 0; formatter.Serialize(stream, a1); stream.Position = 0; try{ objRet = Deserialize(formatter, stream); iCountErrors++; Console.WriteLine("Err_98324sdg! No exception thrown, "); } catch (SecurityException) { } catch (Exception ex) { iCountErrors++; Console.WriteLine("Err_39047tsg! Exception thrown, " + ex.GetType().Name); } strLoc = "Loc_3048sdg"; iCountTestcases++; v1 = new V(7); formatter = new SoapFormatter(); formatter.SurrogateSelector = selector; stream = new MemoryStream(); try{ Serialize(formatter, stream, v1); iCountErrors++; Console.WriteLine("Err_9347tsgf! No exception thrown, "); }catch (SecurityException) { }catch (Exception ex) { iCountErrors++; Console.WriteLine("Err_2947tsgd! Wrong exception thrown, " + ex.GetType().Name); } stream.Position = 0; formatter.Serialize(stream, v1); stream.Position = 0; try{ objRet = Deserialize(formatter, stream); iCountErrors++; Console.WriteLine("Err_98324sdg! No exception thrown, "); } catch (SecurityException) { } catch (Exception ex) { iCountErrors++; Console.WriteLine("Err_89734trfsg! Exception thrown, " + ex.GetType().Name); } perm = new ReflectionPermission(PermissionState.Unrestricted); perm.Deny(); strLoc = "Loc_83245tsg"; iCountTestcases++; a1 = new A(5); formatter = new SoapFormatter(); stream = new MemoryStream(); try{ formatter.Serialize(stream, a1); stream.Position = 0; objRet = formatter.Deserialize(stream); if (((A)objRet).I != 5) { iCountErrors++; Console.WriteLine("Err_3947tsdg! wrong value returned, " + (Int32)objRet); } }catch (Exception ex) { iCountErrors++; Console.WriteLine("Err_29746tsfdg! Exception thrown, " + ex.GetType().Name); } } catch (Exception exc_general) { ++iCountErrors; Console.WriteLine(s_strTFAbbrev + " : Error Err_8888yyy! strLoc==" + strLoc + ", exc_general==" + exc_general.ToString()); } if (iCountErrors == 0) { Console.WriteLine("paSs. " + s_strTFName + " ,iCountTestcases==" + iCountTestcases.ToString()); return(true); } else { Console.WriteLine("FAiL! " + s_strTFName + " ,inCountErrors==" + iCountErrors.ToString() + " , BugNums?: " + s_strActiveBugNums); return(false); } }