static XPathDocumentWriter() { var perm = new ReflectionPermission(PermissionState.Unrestricted) { Flags = ReflectionPermissionFlag.MemberAccess }; try { perm.Assert(); Type t = typeof(XPathDocument); defaultConstructor = t.GetConstructor( BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, new ParameterModifier[0]); Debug.Assert(defaultConstructor != null, ".NET Framework implementation changed"); loadWriterMethod = t.GetMethod("LoadFromWriter", BindingFlags.Instance | BindingFlags.NonPublic); Debug.Assert(loadWriterMethod != null, ".NET Framework implementation changed"); } finally { System.Security.CodeAccessPermission.RevertAssert(); } }
public bool makeTheRequestFormDataEditable() { try { object objectToReflect = HttpContext.Current.Request.Form; string stringObjectType = "MakeReadWrite"; // Assert the retrival of information from a Private Method ReflectionPermission reflectionPerm = new ReflectionPermission(ReflectionPermissionFlag.TypeInformation); reflectionPerm.Assert(); MethodInfo objTempMethodType = objectToReflect.GetType().GetMethod( stringObjectType, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, CallingConventions.Any, new Type[0] { }, null); // Revert the previous assert since it is only possible to have one active Assert ReflectionPermission.RevertAssert(); // Assert the execution of a Private Method reflectionPerm = new ReflectionPermission(ReflectionPermissionFlag.MemberAccess); reflectionPerm.Assert(); object invokeResult = objTempMethodType.Invoke( objectToReflect, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod, null, new object[0] { }, null); return(true); } catch (Exception ex) { HttpContext.Current.Response.Write(ex.Message); return(false); } }
protected override IStackWalk CreateStackWalk() { ReflectionPermission permission = new ReflectionPermission(PermissionState.Unrestricted); permission.Flags = attribute.Flags; return(permission); }
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 static PermissionToken FindToken(Type cls) { if (cls == null) { return(null); } #if FEATURE_CAS_POLICY if (cls.GetInterface("System.Security.Permissions.IBuiltInPermission") != null) { if (s_reflectPerm == null) { s_reflectPerm = new ReflectionPermission(PermissionState.Unrestricted); } s_reflectPerm.Assert(); MethodInfo method = cls.GetMethod("GetTokenIndex", BindingFlags.Static | BindingFlags.NonPublic); Contract.Assert(method != null, "IBuiltInPermission types should have a static method called 'GetTokenIndex'"); // GetTokenIndex needs to be invoked without any security checks, since doing a security check // will involve a ReflectionTargetDemand which creates a CompressedStack and attempts to get the // token. RuntimeMethodInfo getTokenIndex = method as RuntimeMethodInfo; Contract.Assert(getTokenIndex != null, "method is not a RuntimeMethodInfo"); int token = (int)getTokenIndex.UnsafeInvoke(null, BindingFlags.Default, null, null, null); return(s_theTokenFactory.BuiltInGetToken(token, null, cls)); } else #endif // FEATURE_CAS_POLICY { return(s_theTokenFactory.FindToken(cls)); } }
}// InsertPropSheetPageControls protected override void PutValuesinPage() { ReflectionPermission perm = (ReflectionPermission)m_perm; if (perm.IsUnrestricted()) { m_radUnrestricted.Checked = true; m_ucOptions.Enabled = false; } else { m_radGrantFollowingPermission.Checked = true; if ((perm.Flags & ReflectionPermissionFlag.MemberAccess) > 0) { m_chkMemberAccess.Checked = true; } if ((perm.Flags & ReflectionPermissionFlag.ReflectionEmit) > 0) { m_chkReflectionEmit.Checked = true; } if ((perm.Flags & ReflectionPermissionFlag.TypeInformation) > 0) { m_chkTypeInformation.Checked = true; } } }// PutValuesinPage
}// PutValuesinPage internal override IPermission GetCurrentPermission() { ReflectionPermission refperm; if (m_radUnrestricted.Checked == true) { refperm = new ReflectionPermission(PermissionState.Unrestricted); } else { ReflectionPermissionFlag rpf = ReflectionPermissionFlag.NoFlags; if (m_chkMemberAccess.Checked == true) { rpf |= ReflectionPermissionFlag.MemberAccess; } if (m_chkReflectionEmit.Checked == true) { rpf |= ReflectionPermissionFlag.ReflectionEmit; } if (m_chkTypeInformation.Checked == true) { rpf |= ReflectionPermissionFlag.TypeInformation; } refperm = new ReflectionPermission(rpf); } return(refperm); }// GetCurrentPermission
internal override PageFunctionBase ResumePageFunction() { PageFunctionBase pageFunction; Invariant.Assert(this._typeName.Value != null, "JournalEntry does not contain the Type for the PageFunction to be created"); //First try Type.GetType from the saved typename, then try Activator.CreateInstanceFrom //Type.GetType - Since the typename is fullyqualified //we will end up using the default binding mechanism to locate and bind to the assembly. //If the assembly was not a strongly named one nor is present in the APPBASE, this will //fail. Type pfType = Type.GetType(this._typeName.Value); new ReflectionPermission(ReflectionPermissionFlag.MemberAccess).Assert(); try { pageFunction = (PageFunctionBase)Activator.CreateInstance(pfType); } catch (Exception ex) { throw new Exception(SR.Get(SRID.FailedResumePageFunction, this._typeName.Value), ex); } finally { ReflectionPermission.RevertAssert(); } InitializeComponent(pageFunction); RestoreState(pageFunction); return(pageFunction); }
static XamlSchemaContext GetXamlSchemaContext(Assembly assembly, string helperClassName) { XamlSchemaContext typeSchemaContext = null; ReflectionPermission reflectionPerm = new ReflectionPermission(ReflectionPermissionFlag.MemberAccess); reflectionPerm.Assert(); try { Type schemaContextType = assembly.GetType(helperClassName); if (schemaContextType == null) { throw FxTrace.Exception.AsError(new InvalidOperationException(SR.SchemaContextFromBeforeInitializeComponentXBTExtensionNotFound(helperClassName))); } // The "official" BeforeInitializeComponent XBT Extension will not create a generic type for this helper class. // This check is here so that the assembly manifest can't lure us into creating a type with a generic argument from a different assembly. if (schemaContextType.IsGenericType || schemaContextType.IsGenericTypeDefinition) { throw FxTrace.Exception.AsError(new InvalidOperationException(SR.SchemaContextFromBeforeInitializeComponentXBTExtensionCannotBeGeneric(helperClassName))); } PropertyInfo schemaContextPropertyInfo = schemaContextType.GetProperty("SchemaContext", BindingFlags.NonPublic | BindingFlags.Static); typeSchemaContext = (XamlSchemaContext)schemaContextPropertyInfo.GetValue(null, BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.GetProperty, null, null, null); } finally { CodeAccessPermission.RevertAssert(); } return typeSchemaContext; }
public void IsSubsetOfBadPermission() { ReflectionPermission p1 = new ReflectionPermission(ReflectionPermissionFlag.NoFlags); EnvironmentPermission ep2 = new EnvironmentPermission(PermissionState.Unrestricted); Assert.IsTrue(p1.IsSubsetOf(ep2), "IsSubsetOf(EnvironmentPermission)"); }
//</Snippet4> //Copy creates and returns an identical copy of the current permission. //<Snippet5> private static void CopyDemo() { ReflectionPermission memberAccessPerm = new ReflectionPermission(ReflectionPermissionFlag.MemberAccess); ReflectionPermission restrictedMemberAccessPerm = (ReflectionPermission)memberAccessPerm.Copy(); Console.WriteLine("Result of copy = " + restrictedMemberAccessPerm.ToString()); }
public void Intersect() { // no intersection ReflectionPermission p1 = new ReflectionPermission(ReflectionPermissionFlag.MemberAccess); ReflectionPermission p2 = new ReflectionPermission(ReflectionPermissionFlag.TypeInformation); ReflectionPermission p3 = (ReflectionPermission)p1.Intersect(p2); Assert.IsNull(p3, "EP1 N EP2 == null"); // intersection in MemberAccess ReflectionPermission p4 = new ReflectionPermission(ReflectionPermissionFlag.MemberAccess | ReflectionPermissionFlag.ReflectionEmit); p3 = (ReflectionPermission)p4.Intersect(p1); Assert.AreEqual(ReflectionPermissionFlag.MemberAccess, p3.Flags, "Intersect-MemberAccess"); // intersection in TypeInformation ReflectionPermission p5 = new ReflectionPermission(ReflectionPermissionFlag.TypeInformation | ReflectionPermissionFlag.ReflectionEmit); p3 = (ReflectionPermission)p5.Intersect(p2); Assert.AreEqual(ReflectionPermissionFlag.TypeInformation, p3.Flags, "Intersect-TypeInformation"); // intersection in AllFlags ReflectionPermission p6 = new ReflectionPermission(ReflectionPermissionFlag.AllFlags); ReflectionPermission p7 = new ReflectionPermission(ReflectionPermissionFlag.AllFlags); p3 = (ReflectionPermission)p6.Intersect(p7); Assert.AreEqual(ReflectionPermissionFlag.AllFlags, p3.Flags, "Intersect-AllFlags"); Assert.IsTrue(p3.IsUnrestricted(), "Intersect-AllFlags-Unrestricted"); }
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 static IEnumerable <ReflectPropertyDescriptorInfo> Find() { ReflectionPermission perm = new ReflectionPermission(PermissionState.Unrestricted); if (perm.IsUnrestricted()) { Type reflectType = typeof(PropertyDescriptor).Module.GetType("System.ComponentModel.ReflectTypeDescriptionProvider"); FieldInfo propertyCacheFieldInfo = reflectType.GetField("_propertyCache", BindingFlags.Static | BindingFlags.NonPublic); Hashtable propertyCache = (Hashtable)propertyCacheFieldInfo.GetValue((object)null); if (propertyCache != null) { DictionaryEntry[] entries = new DictionaryEntry[propertyCache.Count]; propertyCache.CopyTo((Array)entries, 0); FieldInfo valueChangedHandlersFieldInfo = typeof(PropertyDescriptor).GetField("valueChangedHandlers", BindingFlags.Instance | BindingFlags.NonPublic); foreach (DictionaryEntry dictionaryEntry in entries) { PropertyDescriptor[] propertyDescriptors = (PropertyDescriptor[])dictionaryEntry.Value; if (propertyDescriptors != null) { foreach (PropertyDescriptor propertyDescriptor in propertyDescriptors) { Hashtable valueChangedHandlers = (Hashtable)valueChangedHandlersFieldInfo.GetValue((object)propertyDescriptor); if (valueChangedHandlers != null && valueChangedHandlers.Count != 0) { yield return(new ReflectPropertyDescriptorInfo(dictionaryEntry.Key.ToString(), propertyDescriptor.Name, valueChangedHandlers.Count)); } } } } } } }
private static void GetPermissions() { if (!m_Initialized) { // test RelectionPermission CodeAccessPermission securityTest; try { securityTest = new ReflectionPermission(PermissionState.Unrestricted); securityTest.Demand(); m_ReflectionPermission = true; } catch { // code access security error m_ReflectionPermission = false; } // test WebPermission try { securityTest = new WebPermission(PermissionState.Unrestricted); securityTest.Demand(); m_WebPermission = true; } catch { // code access security error m_WebPermission = false; } // test WebHosting Permission (Full Trust) try { securityTest = new AspNetHostingPermission(AspNetHostingPermissionLevel.Unrestricted); securityTest.Demand(); m_AspNetHostingPermission = true; } catch { // code access security error m_AspNetHostingPermission = false; } m_Initialized = true; // Test for Unmanaged Code permission try { securityTest = new SecurityPermission(SecurityPermissionFlag.UnmanagedCode); securityTest.Demand(); m_UnManagedCodePermission = true; } catch (Exception) { m_UnManagedCodePermission = false; } } }
public void Derestricted() { ReflectionPermission p = new ReflectionPermission(PermissionState.Unrestricted); Assert.IsTrue(p.IsUnrestricted(), "IsUnrestricted"); p.Flags = ReflectionPermissionFlag.NoFlags; Assert.IsTrue(!p.IsUnrestricted(), "IsUnrestricted"); }
public void IsSubsetOfNull() { ReflectionPermission p = new ReflectionPermission(ReflectionPermissionFlag.NoFlags); Assert.IsTrue(p.IsSubsetOf(null), "NoFlags.IsSubsetOf(null)"); p = new ReflectionPermission(ReflectionPermissionFlag.MemberAccess); Assert.IsTrue(!p.IsSubsetOf(null), "MemberAccess.IsSubsetOf(null)"); }
public void UnionWithNull() { ReflectionPermission p1 = new ReflectionPermission(ReflectionPermissionFlag.NoFlags); ReflectionPermission p2 = null; ReflectionPermission p3 = (ReflectionPermission)p1.Union(p2); Assert.AreEqual(p1.ToXml().ToString(), p3.ToXml().ToString(), "P1 U null == P1"); }
public void IntersectWithNull() { ReflectionPermission p1 = new ReflectionPermission(ReflectionPermissionFlag.NoFlags); ReflectionPermission p2 = null; ReflectionPermission p3 = (ReflectionPermission)p1.Intersect(p2); Assert.IsNull(p3, "P1 N null == null"); }
internal static void DemandMemberAccessPermission() { if (s_reflectionMemberAccess == null) { s_reflectionMemberAccess = new ReflectionPermission(ReflectionPermissionFlag.MemberAccess); } s_reflectionMemberAccess.Demand(); }
public void IndividualMethod() { ReflectionPermission rp = new ReflectionPermission(ReflectionPermissionFlag.AllFlags); rp.Demand(); IsolatedStorageFilePermission isp = new IsolatedStorageFilePermission(PermissionState.Unrestricted); isp.Demand(); }
public void MemberAccess() { ReflectionPermission p = new ReflectionPermission(ReflectionPermissionFlag.MemberAccess); Assert.IsTrue(!p.IsUnrestricted(), "IsUnrestricted"); SecurityElement se = p.ToXml(); Assert.AreEqual("MemberAccess", (se.Attributes ["Flags"] as string), "ToXml-Flags=MemberAccess"); }
public void AllFlags() { ReflectionPermission p = new ReflectionPermission(ReflectionPermissionFlag.AllFlags); Assert.IsTrue(p.IsUnrestricted(), "IsUnrestricted"); SecurityElement se = p.ToXml(); Assert.AreEqual("true", (se.Attributes ["Unrestricted"] as string), "ToXml-Unrestricted"); }
public PrivateObjectTester(object instance) { perm = new ReflectionPermission(PermissionState.Unrestricted); perm.Demand(); type = Type.GetTypeFromHandle(Type.GetTypeHandle(instance)); this.instance = instance; }
public void TypeInformation() { ReflectionPermission p = new ReflectionPermission(ReflectionPermissionFlag.TypeInformation); Assert.IsTrue(!p.IsUnrestricted(), "IsUnrestricted"); SecurityElement se = p.ToXml(); Assert.AreEqual("TypeInformation", (se.Attributes ["Flags"] as string), "ToXml-Flags=TypeInformation"); }
public void ReflectionEmit() { ReflectionPermission p = new ReflectionPermission(ReflectionPermissionFlag.ReflectionEmit); Assert.IsTrue(!p.IsUnrestricted(), "IsUnrestricted"); SecurityElement se = p.ToXml(); Assert.AreEqual("ReflectionEmit", (se.Attributes ["Flags"] as string), "ToXml-Flags=ReflectionEmit"); }
public void IsSubsetOfUnrestricted() { ReflectionPermission p1 = new ReflectionPermission(PermissionState.Unrestricted); ReflectionPermission p2 = new ReflectionPermission(ReflectionPermissionFlag.NoFlags); ReflectionPermission p3 = new ReflectionPermission(PermissionState.Unrestricted); Assert.IsTrue(!p1.IsSubsetOf(p2), "Unrestricted.IsSubsetOf()"); Assert.IsTrue(p2.IsSubsetOf(p1), "IsSubsetOf(Unrestricted)"); Assert.IsTrue(p1.IsSubsetOf(p3), "Unrestricted.IsSubsetOf(Unrestricted)"); }
internal CReflectPermControls(IPermission perm, Object oParent) : base(perm, oParent) { // If they don't have a permission for this permission set, we will // feed our property page a 'none' permission state. if (perm == null) { m_perm = new ReflectionPermission(PermissionState.None); } }// CReflectPermControls
public static void Init(TestContext ctx) { PlasmaContainer.DefaultReflectionPermission = ReflectionPermission = typeof(_AssemblyInit).Assembly.FullName.Contains("Precompiled") ? (Debugger.IsAttached ? ReflectionPermission.Throw : ReflectionPermission.Throw) : ReflectionPermission.Allow; Init(PlasmaContainer.Root); }
public void SetMethod() { ReflectionPermission rp = new ReflectionPermission(ReflectionPermissionFlag.AllFlags); IsolatedStorageFilePermission isp = new IsolatedStorageFilePermission(PermissionState.Unrestricted); System.Security.PermissionSet ps = new System.Security.PermissionSet(PermissionState.None); ps.AddPermission(rp); ps.AddPermission(isp); ps.Demand(); }
private Type type; // type of class to manage #endregion Fields #region Constructors public PrivateObjectTester(string qualifiedTypeName, params object[] args) { perm = new ReflectionPermission(PermissionState.Unrestricted); perm.Demand(); type = Type.GetType(qualifiedTypeName); Type[] types = new Type[args.Length]; for (int i=0; i < args.Length; i++) { types[i] = args[i].GetType(); } ConstructorInfo constructor = type.GetConstructor(bindingFlags,null,types,null); instance =constructor.Invoke(args); }
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"); }
public static void ReflectionPermissionCallMethods() { ReflectionPermission rp = new ReflectionPermission(new Permissions.PermissionState()); ReflectionPermission rp2 = new ReflectionPermission(new Permissions.ReflectionPermissionFlag()); IPermission ip = rp.Copy(); IPermission ip2 = rp.Intersect(ip); bool testbool = rp.IsSubsetOf(ip); testbool = rp.IsUnrestricted(); ip2 = rp.Union(ip); SecurityElement se = new SecurityElement(""); rp.FromXml(se); se = rp.ToXml(); }
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"); }