Imported Upstream version 3.6.0

Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
This commit is contained in:
Jo Shields
2014-08-13 10:39:27 +01:00
commit a575963da9
50588 changed files with 8155799 additions and 0 deletions

View File

@@ -0,0 +1,90 @@
// AssemblyAlgorithmIdAttributeTest.cs
//
// Author: Vineeth N <nvineeth@yahoo.com>
//
// (C) 2004 Ximian, Inc. http://www.ximian.com
//
#if !MONOTOUCH
using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
using System.Configuration.Assemblies;
using NUnit.Framework;
namespace MonoTests.System.Reflection {
/// <summary>
/// Test Fixture for AssemblyAlgorithmIdAttribute class
/// </summary>
[TestFixture]
public class AssemblyAlgorithmIdAttributeTest
{
private AssemblyBuilder dynAssembly;
AssemblyName dynAsmName = new AssemblyName ();
AssemblyAlgorithmIdAttribute attr;
public AssemblyAlgorithmIdAttributeTest ()
{
//create a dynamic assembly with the required attribute
//and check for the validity
dynAsmName.Name = "TestAssembly";
dynAssembly = Thread.GetDomain ().DefineDynamicAssembly (
dynAsmName,AssemblyBuilderAccess.Run
);
// Set the required Attribute of the assembly.
Type attribute = typeof (AssemblyAlgorithmIdAttribute);
ConstructorInfo ctrInfo = attribute.GetConstructor (
new Type [] { typeof (AssemblyHashAlgorithm) }
);
CustomAttributeBuilder attrBuilder =
new CustomAttributeBuilder (
ctrInfo,
new object [1] { AssemblyHashAlgorithm.MD5 }
);
dynAssembly.SetCustomAttribute (attrBuilder);
object [] attributes = dynAssembly.GetCustomAttributes (true);
attr = attributes [0] as AssemblyAlgorithmIdAttribute;
}
[Test]
public void AlgorithmIdTest()
{
Assert.AreEqual (
attr.AlgorithmId,
(uint) AssemblyHashAlgorithm.MD5, "#1");
}
[Test]
public void TypeIdTest ()
{
Assert.AreEqual (
attr.TypeId,
typeof (AssemblyAlgorithmIdAttribute), "#1"
);
}
[Test]
public void MatchTestForTrue ()
{
Assert.AreEqual (
attr.Match (attr),
true, "#1");
}
[Test]
public void MatchTestForFalse ()
{
Assert.AreEqual (
attr.Match (new AssemblyAlgorithmIdAttribute (AssemblyHashAlgorithm.SHA1)),
false, "#1");
}
}
}
#endif

View File

@@ -0,0 +1,290 @@
//
// AssemblyCas.cs - CAS unit tests for System.Reflection.Assembly
//
// Author:
// Sebastien Pouliot <sebastien@ximian.com>
//
// Copyright (C) 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
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using NUnit.Framework;
using System;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization;
using System.Security;
using System.Security.Permissions;
namespace MonoCasTests.System.Reflection {
[TestFixture]
[Category ("CAS")]
public class AssemblyCas {
private MonoTests.System.Reflection.AssemblyTest at;
private Assembly corlib;
private Assembly corlib_test;
[TestFixtureSetUp]
public void FixtureSetUp ()
{
at = new MonoTests.System.Reflection.AssemblyTest ();
corlib = typeof (int).Assembly;
corlib_test = Assembly.GetExecutingAssembly ();
}
[SetUp]
public void SetUp ()
{
if (!SecurityManager.SecurityEnabled)
Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
}
// Partial Trust Tests - i.e. call "normal" unit with reduced privileges
[Test]
[PermissionSet (SecurityAction.Deny, Unrestricted = true)]
public void PartialTrust_Deny_Unrestricted ()
{
at.CreateInstance ();
at.CreateInvalidInstance ();
at.GetAssembly ();
at.GetReferencedAssemblies ();
Assert.IsNotNull (Assembly.GetCallingAssembly (), "GetCallingAssembly");
Assembly.GetEntryAssembly (); // null for MS, non-null with Mono
Assert.IsTrue (corlib.GetCustomAttributes (true).Length > 0, "GetCustomAttribute");
Assert.IsTrue (corlib.GetExportedTypes ().Length > 0, "GetExportedTypes");
Assert.IsTrue (corlib.GetLoadedModules (true).Length > 0, "GetLoadedModules(true)");
Assert.IsNotNull (corlib.ToString (), "ToString");
Module[] ms = corlib.GetModules (true);
Assert.IsTrue (ms.Length > 0, "GetModules(true)");
// can't use ms [0].Name as this requires PathDiscovery
// but ToString return the same value without the check
Assert.IsNotNull (corlib.GetModule (ms [0].ToString ()), "GetModule");
corlib.GetManifestResourceNames ();
Assembly corlib_test = Assembly.GetExecutingAssembly ();
Assert.AreEqual (corlib_test.GetCustomAttributes (true).Length,
corlib_test.GetCustomAttributes (false).Length, "GetCustomAttribute true==false");
Assert.AreEqual (corlib_test.GetLoadedModules ().Length,
corlib_test.GetLoadedModules (false).Length, "GetLoadedModules()==(false)");
Assert.AreEqual (corlib_test.GetModules ().Length,
corlib_test.GetModules (false).Length, "GetModules()==(false)");
Assert.IsTrue (corlib_test.GetReferencedAssemblies ().Length > 0, "GetReferencedAssemblies");
}
[Test]
[SecurityPermission (SecurityAction.PermitOnly, ControlEvidence = true)]
[FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
public void PartialTrust_PermitOnly_ControlEvidenceFileIOPermission ()
{
at.Corlib_test ();
}
[Test]
[SecurityPermission (SecurityAction.Deny, ControlEvidence = true)]
[ExpectedException (typeof (SecurityException))]
public void PartialTrust_Deny_ControlEvidence ()
{
Assert.IsNotNull (corlib_test.Evidence, "Evidence");
}
[Test]
[FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
[ExpectedException (typeof (SecurityException))]
public void CodeBase_Deny_FileIOPermission ()
{
Assert.IsNotNull (corlib_test.CodeBase, "CodeBase");
}
[Test]
[FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
[ExpectedException (typeof (SecurityException))]
public void EscapedCodeBase_Deny_FileIOPermission ()
{
Assert.IsNotNull (corlib_test.EscapedCodeBase, "EscapedCodeBase");
}
[Test]
[FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
[ExpectedException (typeof (SecurityException))]
public void Location_Deny_FileIOPermission ()
{
Assert.IsNotNull (corlib_test.Location, "Location");
}
[Test]
public void GetFile_PermitOnly_FileIOPermission ()
{
FileStream[] fss = corlib.GetFiles (false);
if (fss.Length > 0) {
foreach (FileStream fs in fss) {
GetFile_PermitOnly (fs.Name);
}
}
}
[FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
private void GetFile_PermitOnly (string filename)
{
corlib.GetFile (filename);
}
[Test]
public void GetFile_Deny_FileIOPermission ()
{
FileStream[] fss = corlib.GetFiles (false);
if (fss.Length > 0) {
foreach (FileStream fs in fss) {
GetFile_Deny (fs.Name);
}
}
// note: we already know the name
}
[FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
private void GetFile_Deny (string filename)
{
corlib.GetFile (filename);
}
[Test]
[FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
public void GetFile_Unexisting_Deny ()
{
corlib.GetFile ("TOTO");
}
[Test]
[FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
public void GetFiles_PermitOnly_FileIOPermission ()
{
at.GetFiles_False ();
at.GetFiles_True ();
}
[Test]
[FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
public void GetFilesFalse_Deny_FileIOPermission ()
{
try {
FileStream[] fss = corlib.GetFiles (false);
if (fss.Length != 0)
Assert.Fail ("Expected SecurityException");
}
catch (SecurityException) {
// so there was at least one (like on MS runtime)
}
}
[Test]
[FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
public void GetFilesTrue_Deny_FileIOPermission ()
{
try {
FileStream[] fss = corlib.GetFiles (true);
if (fss.Length != 0)
Assert.Fail ("Expected SecurityException");
}
catch (SecurityException) {
// so there was at least one (like on MS runtime)
}
}
[Test]
[FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
[ExpectedException (typeof (SecurityException))]
public void GetName_Deny_FileIOPermission ()
{
corlib.GetName ();
}
[Test]
[FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
public void GetName_PermitOnly_FileIOPermission ()
{
corlib.GetName ();
}
[Test]
[FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
public void LoadWithPartialName_Deny_FileIOPermission ()
{
// FileIOPermission isn't (always) required for LoadWithPartialName
// e.g. in this case both assemblies are already loaded in memory
at.LoadWithPartialName ();
}
#if !NET_2_0
// that one is unclear (undocumented) and doesn't happen in 2.0
// will not be implemented in Mono unless if find out why...
[Category ("NotWorking")]
[Test]
[SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
[ExpectedException (typeof (SecurityException))]
public void LoadWithPartialName_Deny_SecurityPermission ()
{
at.LoadWithPartialName ();
}
#endif
// we use reflection to call Assembly as some methods and events are protected
// by LinkDemand (which will be converted into full demand, i.e. a stack walk)
// when reflection is used (i.e. it gets testable).
[Test]
[SecurityPermission (SecurityAction.Deny, SerializationFormatter = true)]
[ExpectedException (typeof (SecurityException))]
public void GetObjectData ()
{
SerializationInfo info = null;
StreamingContext context = new StreamingContext (StreamingContextStates.All);
Assembly a = Assembly.GetExecutingAssembly ();
MethodInfo mi = typeof (Assembly).GetMethod ("GetObjectData");
mi.Invoke (a, new object [2] { info, context });
}
[Test]
[SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
[ExpectedException (typeof (SecurityException))]
public void AddModuleResolve ()
{
Assembly a = Assembly.GetExecutingAssembly ();
MethodInfo mi = typeof (Assembly).GetMethod ("add_ModuleResolve");
mi.Invoke (a, new object [1] { null });
}
[Test]
[SecurityPermission (SecurityAction.Deny, ControlAppDomain = true)]
[ExpectedException (typeof (SecurityException))]
public void RemoveModuleResolve ()
{
Assembly a = Assembly.GetExecutingAssembly ();
MethodInfo mi = typeof (Assembly).GetMethod ("remove_ModuleResolve");
mi.Invoke (a, new object [1] { null });
}
}
}

View File

@@ -0,0 +1,86 @@
// AssemblyConfigurationAttributeTest.cs
//
// Author: Vineeth N <nvineeth@yahoo.com>
//
// (C) 2004 Ximian, Inc. http://www.ximian.com
//
#if !MOBILE
using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
using NUnit.Framework;
namespace MonoTests.System.Reflection {
/// <summary>
/// Test Fixture for AssemblyConfigurationAttribute class
/// </summary>
[TestFixture]
public class AssemblyConfigurationAttributeTest
{
private AssemblyBuilder dynAssembly;
AssemblyName dynAsmName = new AssemblyName ();
AssemblyConfigurationAttribute attr;
public AssemblyConfigurationAttributeTest ()
{
//create a dynamic assembly with the required attribute
//and check for the validity
dynAsmName.Name = "TestAssembly";
dynAssembly = Thread.GetDomain ().DefineDynamicAssembly (
dynAsmName,AssemblyBuilderAccess.Run
);
// Set the required Attribute of the assembly.
Type attribute = typeof (AssemblyConfigurationAttribute);
ConstructorInfo ctrInfo = attribute.GetConstructor (
new Type [] { typeof (string) }
);
CustomAttributeBuilder attrBuilder =
new CustomAttributeBuilder (ctrInfo, new object [1] { "Config" } );
dynAssembly.SetCustomAttribute (attrBuilder);
object [] attributes = dynAssembly.GetCustomAttributes (true);
attr = attributes [0] as AssemblyConfigurationAttribute;
}
[Test]
public void ConfigurationTest ()
{
Assert.AreEqual (
attr.Configuration,
"Config", "#1");
}
[Test]
public void TypeIdTest ()
{
Assert.AreEqual (
attr.TypeId,
typeof (AssemblyConfigurationAttribute)
, "#1");
}
[Test]
public void MatchTestForTrue ()
{
Assert.AreEqual (
attr.Match (attr),
true, "#1");
}
[Test]
public void MatchTestForFalse ()
{
Assert.AreEqual (
attr.Match (new AssemblyConfigurationAttribute ("abcd")),
false, "#1");
}
}
}
#endif

View File

@@ -0,0 +1,85 @@
// AssemblyCopyrightAttributeTest.cs
//
// Author: Vineeth N <nvineeth@yahoo.com>
//
// (C) 2004 Ximian, Inc. http://www.ximian.com
//
#if !MOBILE
using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
using NUnit.Framework;
namespace MonoTests.System.Reflection {
/// <summary>
/// Test Fixture for AssemblyCopyrightAttribute
/// </summary>
[TestFixture]
public class AssemblyCopyrightAttributeTest
{
private AssemblyBuilder dynAssembly;
AssemblyName dynAsmName = new AssemblyName ();
AssemblyCopyrightAttribute attr;
public AssemblyCopyrightAttributeTest ()
{
//create a dynamic assembly with the required attribute
//and check for the validity
dynAsmName.Name = "TestAssembly";
dynAssembly = Thread.GetDomain().DefineDynamicAssembly (
dynAsmName,AssemblyBuilderAccess.Run
);
// Set the required Attribute of the assembly.
Type attribute = typeof (AssemblyCopyrightAttribute);
ConstructorInfo ctrInfo = attribute.GetConstructor (
new Type []{ typeof (string) }
);
CustomAttributeBuilder attrBuilder =
new CustomAttributeBuilder (ctrInfo, new object [1] {"Ximian"} );
dynAssembly.SetCustomAttribute(attrBuilder);
object [] attributes = dynAssembly.GetCustomAttributes (true);
attr = attributes [0] as AssemblyCopyrightAttribute;
}
[Test]
public void CopyrightTest ()
{
Assert.AreEqual (
attr.Copyright,
"Ximian", "#1");
}
[Test]
public void TypeIdTest ()
{
Assert.AreEqual (
attr.TypeId,
typeof (AssemblyCopyrightAttribute)
, "#1");
}
[Test]
public void MatchTestForTrue ()
{
Assert.AreEqual (
attr.Match (attr),
true, "#1");
}
[Test]
public void MatchTestForFalse ()
{
Assert.AreEqual (
attr.Match (new AssemblyCopyrightAttribute ("imian")),
false, "#1");
}
}
}
#endif

View File

@@ -0,0 +1,86 @@
// AssemblyCultureAttributeTest.cs
//
// Author: Vineeth N <nvineeth@yahoo.com>
//
// (C) 2004 Ximian, Inc. http://www.ximian.com
//
#if !MOBILE
using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
using NUnit.Framework;
namespace MonoTests.System.Reflection {
/// <summary>
/// Test Fixture for AssemblyCultureAttribute
/// </summary>
[TestFixture]
public class AssemblyCultureAttributeTest
{
private AssemblyBuilder dynAssembly;
AssemblyName dynAsmName = new AssemblyName ();
AssemblyCultureAttribute attr;
public AssemblyCultureAttributeTest ()
{
//create a dynamic assembly with the required attribute
//and check for the validity
dynAsmName.Name = "TestAssembly";
dynAssembly = Thread.GetDomain ().DefineDynamicAssembly (
dynAsmName,AssemblyBuilderAccess.Run
);
// Set the required Attribute of the assembly.
Type attribute = typeof (AssemblyCultureAttribute);
ConstructorInfo ctrInfo = attribute.GetConstructor (
new Type [] { typeof (string) }
);
CustomAttributeBuilder attrBuilder =
new CustomAttributeBuilder (ctrInfo, new object [1] { "India" });
dynAssembly.SetCustomAttribute (attrBuilder);
object [] attributes = dynAssembly.GetCustomAttributes(true);
attr = attributes [0] as AssemblyCultureAttribute;
}
[Test]
public void CultureTest ()
{
Assert.AreEqual (
attr.Culture,
"India", "#1");
}
[Test]
public void TypeIdTest ()
{
Assert.AreEqual (
attr.TypeId,
typeof (AssemblyCultureAttribute), "#1"
);
}
[Test]
public void MatchTestForTrue ()
{
Assert.AreEqual (
attr.Match (attr),
true, "#1");
}
[Test]
public void MatchTestForFalse ()
{
Assert.AreEqual (
attr.Match (new AssemblyCultureAttribute ("Spanish")),
false, "#1");
}
}
}
#endif

View File

@@ -0,0 +1,84 @@
// AssemblyDelaySignAttributeTest.cs
//
// Author: Vineeth N <nvineeth@yahoo.com>
//
// (C) 2004 Ximian, Inc. http://www.ximian.com
//
#if !MOBILE
using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
using NUnit.Framework;
namespace MonoTests.System.Reflection {
/// <summary>
/// Summary description for AssemblyDelaySignAttributeTest.
/// </summary>
[TestFixture]
public class AssemblyDelaySignAttributeTest
{
private AssemblyBuilder dynAssembly;
AssemblyName dynAsmName = new AssemblyName ();
AssemblyDelaySignAttribute attr;
public AssemblyDelaySignAttributeTest ()
{
//create a dynamic assembly with the required attribute
//and check for the validity
dynAsmName.Name = "TestAssembly";
dynAssembly = Thread.GetDomain ().DefineDynamicAssembly (
dynAsmName,AssemblyBuilderAccess.Run
);
// Set the required Attribute of the assembly.
Type attribute = typeof (AssemblyDelaySignAttribute);
ConstructorInfo ctrInfo = attribute.GetConstructor (
new Type [] { typeof (bool) }
);
CustomAttributeBuilder attrBuilder =
new CustomAttributeBuilder (ctrInfo, new object [1] { false });
dynAssembly.SetCustomAttribute (attrBuilder);
object [] attributes = dynAssembly.GetCustomAttributes (true);
attr = attributes [0] as AssemblyDelaySignAttribute;
}
[Test]
public void DelaySignTest ()
{
Assert.AreEqual (
attr.DelaySign,
false, "#1");
}
[Test]
public void TypeIdTest ()
{
Assert.AreEqual (
attr.TypeId,
typeof (AssemblyDelaySignAttribute)
, "#1");
}
[Test]
public void MatchTestForTrue ()
{
Assert.AreEqual (
attr.Match (attr),
true, "#1");
}
[Test]
public void MatchTestForFalse ()
{
Assert.AreEqual (
attr.Match (new AssemblyDelaySignAttribute (true)),
false, "#1");
}
}
}
#endif

View File

@@ -0,0 +1,85 @@
// AssemblyDescriptionAttributeTest.cs
//
// Author: Vineeth N <nvineeth@yahoo.com>
//
// (C) 2004 Ximian, Inc. http://www.ximian.com
//
#if !MOBILE
using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
using NUnit.Framework;
namespace MonoTests.System.Reflection {
/// <summary>
/// Summary description for AssemblyDescriptionAttributeTest.
/// </summary>
[TestFixture]
public class AssemblyDescriptionAttributeTest
{
private AssemblyBuilder dynAssembly;
AssemblyName dynAsmName = new AssemblyName ();
AssemblyDescriptionAttribute attr;
public AssemblyDescriptionAttributeTest ()
{
//create a dynamic assembly with the required attribute
//and check for the validity
dynAsmName.Name = "TestAssembly";
dynAssembly = Thread.GetDomain ().DefineDynamicAssembly (
dynAsmName,AssemblyBuilderAccess.Run
);
// Set the required Attribute of the assembly.
Type attribute = typeof (AssemblyDescriptionAttribute);
ConstructorInfo ctrInfo = attribute.GetConstructor (
new Type [] { typeof (string) }
);
CustomAttributeBuilder attrBuilder =
new CustomAttributeBuilder (ctrInfo, new object [1] { "Emitted Assembly" });
dynAssembly.SetCustomAttribute (attrBuilder);
object [] attributes = dynAssembly.GetCustomAttributes (true);
attr = attributes [0] as AssemblyDescriptionAttribute;
}
[Test]
public void DescriptionTest ()
{
Assert.AreEqual (
attr.Description,
"Emitted Assembly", "#1");
}
[Test]
public void TypeIdTest ()
{
Assert.AreEqual (
attr.TypeId,
typeof (AssemblyDescriptionAttribute), "#1"
);
}
[Test]
public void MatchTestForTrue ()
{
Assert.AreEqual (
attr.Match (attr),
true, "#1");
}
[Test]
public void MatchTestForFalse ()
{
Assert.AreEqual (
attr.Match (new AssemblyDescriptionAttribute ("Descrptn")),
false, "#1");
}
}
}
#endif

View File

@@ -0,0 +1,93 @@
// AssemblyFileVersionAttributeTest.cs
//
// Author: Vineeth N <nvineeth@yahoo.com>
//
// (C) 2004 Ximian, Inc. http://www.ximian.com
//
#if !MOBILE
using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
using NUnit.Framework;
namespace MonoTests.System.Reflection {
/// <summary>
/// Summary description for AssemblyFileVersionAttributeTest.
/// </summary>
[TestFixture]
public class AssemblyFileVersionAttributeTest {
private AssemblyBuilder dynAssembly;
AssemblyName dynAsmName = new AssemblyName ();
AssemblyFileVersionAttribute attr;
public AssemblyFileVersionAttributeTest ()
{
//create a dynamic assembly with the required attribute
//and check for the validity
dynAsmName.Name = "TestAssembly";
dynAssembly = Thread.GetDomain ().DefineDynamicAssembly (
dynAsmName,AssemblyBuilderAccess.Run
);
// Set the required Attribute of the assembly.
Type attribute = typeof (AssemblyFileVersionAttribute);
ConstructorInfo ctrInfo = attribute.GetConstructor (
new Type [] { typeof(string) }
);
CustomAttributeBuilder attrBuilder =
new CustomAttributeBuilder(ctrInfo, new object [1] { "1.0.0.0" });
dynAssembly.SetCustomAttribute (attrBuilder);
object [] attributes = dynAssembly.GetCustomAttributes (true);
attr = attributes [0] as AssemblyFileVersionAttribute;
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void ArgumentNullExceptionTest()
{
string version = null;
new AssemblyFileVersionAttribute (version);
}
[Test]
public void FileVersionTest ()
{
Assert.AreEqual (attr.Version,
"1.0.0.0", "#1");
}
[Test]
public void TypeIdTest ()
{
Assert.AreEqual (
attr.TypeId,
typeof (AssemblyFileVersionAttribute), "#1"
);
}
[Test]
public void MatchTestForTrue ()
{
Assert.AreEqual (
attr.Match (attr),
true, "#1");
}
[Test]
public void MatchTestForFalse ()
{
Assert.AreEqual (
attr.Match (new AssemblyFileVersionAttribute ("Descrptn")),
false, "#1");
}
}
}
#endif

View File

@@ -0,0 +1,85 @@
// AssemblyInformationalVersionAttributeTest.cs
//
// Author: Vineeth N <nvineeth@yahoo.com>
//
// (C) 2004 Ximian, Inc. http://www.ximian.com
//
#if !MOBILE
using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
using NUnit.Framework;
namespace MonoTests.System.Reflection {
/// <summary>
/// Test Fixture for AssemblyInformationalVersionAttribute.
/// </summary>
[TestFixture]
public class AssemblyInformationalVersionAttributeTest {
private AssemblyBuilder dynAssembly;
AssemblyName dynAsmName = new AssemblyName ();
AssemblyInformationalVersionAttribute attr;
public AssemblyInformationalVersionAttributeTest ()
{
//create a dynamic assembly with the required attribute
//and check for the validity
dynAsmName.Name = "TestAssembly";
dynAssembly = Thread.GetDomain ().DefineDynamicAssembly (
dynAsmName,AssemblyBuilderAccess.Run
);
// Set the required Attribute of the assembly.
Type attribute = typeof (AssemblyInformationalVersionAttribute);
ConstructorInfo ctrInfo = attribute.GetConstructor (
new Type [] { typeof (string) }
);
CustomAttributeBuilder attrBuilder =
new CustomAttributeBuilder (ctrInfo, new object [1] { "2.0.0.0" });
dynAssembly.SetCustomAttribute (attrBuilder);
object [] attributes = dynAssembly.GetCustomAttributes (true);
attr = attributes [0] as AssemblyInformationalVersionAttribute;
}
[Test]
public void InformationalVersionTest ()
{
Assert.AreEqual (attr.InformationalVersion,
"2.0.0.0", "#1");
}
[Test]
public void TypeIdTest ()
{
Assert.AreEqual (
attr.TypeId,
typeof (AssemblyInformationalVersionAttribute), "#1"
);
}
[Test]
public void MatchTestForTrue ()
{
Assert.AreEqual (
attr.Match (attr),
true, "#1");
}
[Test]
public void MatchTestForFalse ()
{
Assert.AreEqual (
attr.Match (new AssemblyInformationalVersionAttribute ("Descrptn")),
false, "#1");
}
}
}
#endif

View File

@@ -0,0 +1,137 @@
//
// AssemblyNameCas.cs - CAS unit tests for System.Reflection.AssemblyName
//
// Author:
// Sebastien Pouliot <sebastien@ximian.com>
//
// Copyright (C) 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
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using NUnit.Framework;
using System;
using System.Configuration.Assemblies;
using System.Reflection;
using System.Runtime.Serialization;
using System.Security;
using System.Security.Permissions;
namespace MonoCasTests.System.Reflection {
[TestFixture]
[Category ("CAS")]
public class AssemblyNameCas {
private MonoTests.System.Reflection.AssemblyNameTest ant;
private AssemblyName main;
[TestFixtureSetUp]
public void FixtureSetUp ()
{
ant = new MonoTests.System.Reflection.AssemblyNameTest ();
}
[SetUp]
public void SetUp ()
{
if (!SecurityManager.SecurityEnabled)
Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
ant.SetUp ();
}
[TearDown]
public void TearDown ()
{
ant.TearDown ();
}
// Partial Trust Tests
[Test]
[PermissionSet (SecurityAction.Deny, Unrestricted = true)]
public void PartialTrust_Deny_Unrestricted ()
{
// call "normal" unit with reduced privileges
ant.Constructor0 ();
ant.SetPublicKey ();
ant.SetPublicKeyToken ();
ant.Clone_Empty ();
}
[Test]
[SecurityPermission (SecurityAction.PermitOnly, SerializationFormatter = true)]
public void PartialTrust_PermitOnly_SerializationFormatter ()
{
ant.Serialization_WithoutStrongName ();
}
[Test]
[SecurityPermission (SecurityAction.PermitOnly, UnmanagedCode = true)]
public void PartialTrust_PermitOnly_UnmanagedCode ()
{
ant.KeyPair ();
}
[Test]
[SecurityPermission (SecurityAction.PermitOnly, UnmanagedCode = true, SerializationFormatter = true)]
public void PartialTrust_PermitOnly_UnmanagedCodeSerializationFormatter ()
{
// UnmanagedCode is required to create a StrongNameKeyPair instance
ant.Serialization ();
}
[Test]
[SecurityPermission (SecurityAction.Deny, SerializationFormatter = true)]
[ExpectedException (typeof (SecurityException))]
public void PartialTrust_Deny_SerializationFormatter ()
{
ant.Serialization ();
}
[Test]
[SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
[ExpectedException (typeof (SecurityException))]
public void PartialTrust_Deny_UnmanagedCode ()
{
ant.KeyPair ();
}
[Test]
[FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
public void PartialTrust_PermitOnly_FileIOPermission ()
{
// call "normal" unit with reduced privileges
ant.FullName_Name ();
ant.FullName_Version ();
ant.FullName_Culture ();
ant.FullName_PublicKey ();
ant.FullName_PublicKeyToken ();
ant.FullName_VersionCulture ();
ant.FullName_VersionPublicKey ();
ant.FullName_CulturePublicKey ();
ant.HashAlgorithm ();
ant.Clone_Empty ();
// mostly because they call Assembly.GetName
}
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,137 @@
//
// System.Reflection.ConstructorInfo Test Cases
//
// Authors:
// Gert Driesen (drieseng@users.sourceforge.net)
//
// (c) 2007 Gert Driesen
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Reflection;
using System.Runtime.Serialization;
using NUnit.Framework;
namespace MonoTests.System.Reflection
{
[TestFixture]
public class ConstructorInfoTest
{
[Test]
public void Invoke ()
{
ConstructorInfo ctor = typeof (Foo).GetConstructor (
BindingFlags.Public | BindingFlags.Instance, null,
Type.EmptyTypes, null);
Foo foo = ctor.Invoke (new object [0]) as Foo;
Assert.IsNotNull (foo, "#1");
}
[Test]
public void InvokeAbstract ()
{
object obj = FormatterServices.GetUninitializedObject (typeof (Foo));
ConstructorInfo ctor = typeof (AbstractFoo).GetConstructor (
BindingFlags.Instance | BindingFlags.NonPublic, null,
Type.EmptyTypes, null);
Assert.IsNotNull (ctor, "#A1");
object ret = ctor.Invoke (obj, new object [0]);
Assert.IsNull (ret, "#A2");
try {
ctor.Invoke (null, new object [0]);
Assert.Fail ("#B1");
} catch (TargetException ex) {
// Non-static method requires a target
Assert.AreEqual (typeof (TargetException), ex.GetType (), "#B2");
Assert.IsNull (ex.InnerException, "#B3");
Assert.IsNotNull (ex.Message, "#B4");
}
try {
ctor.Invoke (new object [0]);
Assert.Fail ("#C1");
} catch (MemberAccessException ex) {
// Cannot create an abstract class
Assert.AreEqual (typeof (MemberAccessException), ex.GetType (), "#C2");
Assert.IsNull (ex.InnerException, "#C3");
Assert.IsNotNull (ex.Message, "#C4");
}
}
[Test]
public void InvokeOptionalArguments ()
{
var constructor = typeof (Optional).GetConstructors () [0];
try {
constructor.Invoke (BindingFlags.Default, null, null, null);
Assert.Fail ("#1");
} catch (TargetParameterCountException) {
}
object[] parameters = new [] { Type.Missing, Type.Missing, Type.Missing };
var instance = constructor.Invoke (BindingFlags.InvokeMethod | BindingFlags.CreateInstance, null, parameters, null);
Assert.IsNotNull (instance, "#2a");
}
abstract class AbstractFoo
{
}
class Foo : AbstractFoo
{
}
[Test]
[ExpectedException (typeof (MemberAccessException))]
public void InvokeOpenGenericType () {
typeof (Gen<>).GetConstructor (Type.EmptyTypes).Invoke (null);
}
public class Gen<T> {
public Gen() {
}
}
class Optional
{
public Optional (string caption = null, string value = null, string layoutName = null)
{
}
}
[Test]
[ExpectedException (typeof (TargetException))]
public void InvokeWithNullTarget ()
{
typeof (Foo).GetConstructors ()[0].Invoke (null, BindingFlags.Default, null, null, null);
}
[Test]
[ExpectedException (typeof (TargetException))]
public void InvokeWithWrongTarget ()
{
typeof (Foo).GetConstructors ()[0].Invoke (new object (), BindingFlags.Default, null, null, null);
}
}
}

View File

@@ -0,0 +1,64 @@
//
// System.Reflection.CustomAttributeData Test Cases
//
// Authors:
// Zoltan Varga (vargaz@gmail.com)
//
// (c) 2003 Ximian, Inc. (http://www.ximian.com)
// Copyright (C) 2004-2008 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
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using NUnit.Framework;
using System;
using System.Reflection;
using System.Collections.Generic;
namespace MonoTests.System.Reflection
{
class Attr : Attribute {
public Attr (byte[] arr) {
}
}
[TestFixture]
public class CustomAttributeDataTest
{
[Attr (new byte [] { 1, 2 })]
public void MethodWithAttr () {
}
[Test]
[Category ("MobileNotWorking")] // #10263
public void Arrays () {
IList<CustomAttributeData> cdata = CustomAttributeData.GetCustomAttributes (typeof (CustomAttributeDataTest).GetMethod ("MethodWithAttr"));
Assert.AreEqual (1, cdata.Count);
CustomAttributeTypedArgument arg = cdata [0].ConstructorArguments [0];
Assert.IsTrue (typeof (IList<CustomAttributeTypedArgument>).IsAssignableFrom (arg.Value.GetType ()));
IList<CustomAttributeTypedArgument> arr = (IList<CustomAttributeTypedArgument>)arg.Value;
Assert.AreEqual (2, arr.Count);
Assert.AreEqual (typeof (byte), arr [0].ArgumentType);
Assert.AreEqual (1, arr [0].Value);
Assert.AreEqual (typeof (byte), arr [1].ArgumentType);
Assert.AreEqual (2, arr [1].Value);
}
}
}

View File

@@ -0,0 +1,138 @@
//
// EventInfoTest
//
// Ben Maurer (bmaurer@ximian.com)
//
// Copyright (C) 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
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Threading;
using System.Reflection;
using System.Runtime.InteropServices;
using NUnit.Framework;
namespace MonoTests.System.Reflection
{
[TestFixture]
public class EventInfoTest
{
[Test]
public void IsDefined_AttributeType_Null ()
{
EventInfo priv = typeof (PrivateEvent).GetEvents (
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Static) [0];
try {
priv.IsDefined ((Type) null, false);
Assert.Fail ("#1");
} catch (ArgumentNullException ex) {
Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
Assert.IsNotNull (ex.ParamName, "#5");
Assert.AreEqual ("attributeType", ex.ParamName, "#6");
}
}
[Test]
public void TestGetXXXMethod ()
{
EventInfo priv = typeof (PrivateEvent).GetEvents (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static) [0];
Assert.IsNull (priv.GetAddMethod (), "#A1");
Assert.IsNull (priv.GetRaiseMethod (), "#A2");
Assert.IsNull (priv.GetRemoveMethod (), "#A3");
EventInfo pub = typeof (PublicEvent).GetEvents (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static) [0];
Assert.IsNotNull (pub.GetAddMethod (), "#B1");
Assert.IsNull (pub.GetRaiseMethod (), "#B2");
Assert.IsNotNull (pub.GetRemoveMethod (), "#B3");
}
#if NET_2_0
[Test]
public void AddHandlerToNullInstanceEventRaisesTargetException ()
{
EventInfo ev = typeof (TestClass).GetEvent ("pub");
EventHandler dele = (a,b) => {};
try {
ev.AddEventHandler (null, dele);
Assert.Fail ("#1");
} catch (TargetException) {}
}
[Test]
public void AddHandleToPrivateEventRaisesInvalidOperationException ()
{
EventInfo ev = typeof (TestClass).GetEvent ("priv", BindingFlags.NonPublic| BindingFlags.Instance);
EventHandler dele = (a,b) => {};
try {
ev.AddEventHandler (new PrivateEvent (), dele);
Assert.Fail ("#1");
} catch (InvalidOperationException) {}
}
[Test]
public void AddHandlerWithIncompatibleTargetShouldRaiseTargetException ()
{
EventInfo ev = typeof (TestClass).GetEvent ("pub");
EventHandler dele = (a,b) => {};
try {
ev.AddEventHandler (new PublicEvent (), dele);
Assert.Fail ("#1");
} catch (TargetException) {}
}
[Test]
public void RemoveHandleToPrivateEventRaisesInvalidOperationException ()
{
EventInfo ev = typeof (TestClass).GetEvent ("priv", BindingFlags.NonPublic| BindingFlags.Instance);
EventHandler dele = (a,b) => {};
try {
ev.RemoveEventHandler (new PrivateEvent (), dele);
Assert.Fail ("#1");
} catch (InvalidOperationException) {}
}
#endif
#pragma warning disable 67
public class PrivateEvent
{
private static event EventHandler x;
}
public class PublicEvent
{
public static event EventHandler x;
}
public class TestClass
{
public event EventHandler pub;
private event EventHandler priv;
}
#pragma warning restore 67
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,57 @@
//
// IntrospectionExtensionsTest.cs:
//
// Authors:
// Marek Safar (marek.safar@gmail.com)
//
// (C) 2013
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#if NET_4_5
using System;
using System.Reflection;
using NUnit.Framework;
namespace MonoTests.System.Reflection
{
[TestFixture]
public class IntrospectionExtensionsTest
{
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void GetTypeInfo_NullArgument ()
{
IntrospectionExtensions.GetTypeInfo (null);
}
[Test]
public void GetTypeInfo_SystemType ()
{
var t = typeof (double);
Assert.AreSame (t, IntrospectionExtensions.GetTypeInfo (t));
}
}
}
#endif

View File

@@ -0,0 +1,418 @@
//
// System.Reflection.MethodBase Test Cases
//
// Authors:
// Gert Driesen (drieseng@users.sourceforge.net)
//
// Copyright (C) 2008 Gert Driesen
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Reflection;
using NUnit.Framework;
namespace MonoTests.System.Reflection
{
public class Generic<T> {
public void Foo () {
}
public void GenericFoo<K> () {
}
}
public class AnotherGeneric<T> {
public void Foo () {
}
}
public class SimpleClass {
public void GenericFoo<K> () {
}
}
class MethodBaseOverloadTestDoesNotCauseCompilerError : MethodBase
{
public override MethodAttributes Attributes
{
get { throw new NotImplementedException (); }
}
public override MethodImplAttributes GetMethodImplementationFlags ()
{
throw new NotImplementedException ();
}
public override ParameterInfo[] GetParameters ()
{
throw new NotImplementedException ();
}
public override object Invoke (object obj, BindingFlags invokeAttr, Binder binder, object[] parameters, global::System.Globalization.CultureInfo culture)
{
throw new NotImplementedException ();
}
public override RuntimeMethodHandle MethodHandle
{
get { throw new NotImplementedException (); }
}
public override Type DeclaringType
{
get { throw new NotImplementedException (); }
}
public override object[] GetCustomAttributes (Type attributeType, bool inherit)
{
throw new NotImplementedException ();
}
public override object[] GetCustomAttributes (bool inherit)
{
throw new NotImplementedException ();
}
public override bool IsDefined (Type attributeType, bool inherit)
{
throw new NotImplementedException ();
}
public override MemberTypes MemberType
{
get { throw new NotImplementedException (); }
}
public override string Name
{
get { throw new NotImplementedException (); }
}
public override Type ReflectedType
{
get { throw new NotImplementedException (); }
}
}
[TestFixture]
public class MethodBaseTest
{
public static MethodInfo Where<T> (T a) {
return (MethodInfo) MethodBase.GetCurrentMethod ();
}
public class Foo<K>
{
public static MethodInfo Where<T> (T a, K b) {
return (MethodInfo) MethodBase.GetCurrentMethod ();
}
}
[Test]
public void GetCurrentMethodDropsAllGenericArguments ()
{
MethodInfo a = Where<int> (10);
MethodInfo b = Foo<int>.Where <double> (10, 10);
Assert.IsTrue (a.IsGenericMethodDefinition, "#1");
Assert.IsTrue (b.IsGenericMethodDefinition, "#2");
Assert.IsTrue (b.DeclaringType.IsGenericTypeDefinition, "#3");
Assert.AreSame (a, typeof (MethodBaseTest).GetMethod ("Where"), "#4");
Assert.AreSame (b, typeof (Foo<>).GetMethod ("Where"), "#5");
}
[Test] // GetMethodFromHandle (RuntimeMethodHandle)
public void GetMethodFromHandle1_Handle_Generic ()
{
G<string> instance = new G<string> ();
Type t = instance.GetType ();
MethodBase mb1 = t.GetMethod ("M");
RuntimeMethodHandle mh = mb1.MethodHandle;
RuntimeTypeHandle th = t.TypeHandle;
try {
MethodBase.GetMethodFromHandle (mh);
Assert.Fail ("#1");
} catch (ArgumentException ex) {
// Cannot resolve method Void M(System.__Canon)
// because the declaring type of the method
// handle MonoTests.System.Reflection.MethodBaseTest+G`1[T]
// is generic. Explicitly provide the declaring type to
// GetMethodFromHandle
}
}
[Test] // GetMethodFromHandle (RuntimeMethodHandle)
public void GetMethodFromHandle1_Handle_Zero ()
{
RuntimeMethodHandle mh = new RuntimeMethodHandle ();
try {
MethodBase.GetMethodFromHandle (mh);
Assert.Fail ("#1");
} catch (ArgumentException ex) {
// Handle is not initialized
Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
Assert.IsNull (ex.ParamName, "#5");
}
}
[Test]
public void GetMethodFromHandle ()
{
Type t = typeof (object);
RuntimeMethodHandle rmh = t.GetConstructor (Type.EmptyTypes).MethodHandle;
MethodBase mb = MethodBase.GetMethodFromHandle (rmh);
Assert.IsNotNull (mb, "#1");
Assert.AreEqual (t, mb.DeclaringType, "#2");
Assert.AreEqual (".ctor", mb.Name, "#3");
ParameterInfo [] parameters = mb.GetParameters ();
Assert.IsNotNull (parameters, "#4");
Assert.AreEqual (0, parameters.Length, "#5");
}
[Test]
public void GetMethodFromHandle_NonGenericType_DeclaringTypeZero ()
{
Type t = typeof (object);
RuntimeMethodHandle rmh = t.GetConstructor (Type.EmptyTypes).MethodHandle;
MethodBase mb = MethodBase.GetMethodFromHandle (rmh, new RuntimeTypeHandle ());
Assert.IsNotNull (mb, "#1");
Assert.AreEqual (t, mb.DeclaringType, "#2");
Assert.AreEqual (".ctor", mb.Name, "#3");
ParameterInfo [] parameters = mb.GetParameters ();
Assert.IsNotNull (parameters, "#4");
Assert.AreEqual (0, parameters.Length, "#5");
}
[Test] // GetMethodFromHandle (RuntimeMethodHandle, RuntimeTypeHandle)
public void GetMethodFromHandle2_DeclaringType_Zero ()
{
RuntimeTypeHandle th = new RuntimeTypeHandle ();
Type t = typeof (G<>);
RuntimeMethodHandle mh = t.GetMethod ("M").MethodHandle;
MethodBase mb = MethodBase.GetMethodFromHandle (mh, th);
Assert.IsNotNull (mb, "#1");
Assert.AreEqual (t, mb.DeclaringType, "#2");
Assert.AreEqual ("M", mb.Name, "#3");
ParameterInfo [] parameters = mb.GetParameters ();
Assert.IsNotNull (parameters, "#4");
Assert.AreEqual (1, parameters.Length, "#5");
Assert.AreEqual (t.GetGenericArguments () [0] , parameters [0].ParameterType, "#6");
}
[Test] // GetMethodFromHandle (RuntimeMethodHandle, RuntimeTypeHandle)
public void GetMethodFromHandle2_Handle_Generic ()
{
G<string> instance = new G<string> ();
Type t = instance.GetType ();
MethodBase mb1 = t.GetMethod ("M");
RuntimeMethodHandle mh = mb1.MethodHandle;
RuntimeTypeHandle th = t.TypeHandle;
MethodBase mb2 = MethodBase.GetMethodFromHandle (mh, th);
Assert.IsNotNull (mb2, "#1");
Assert.AreEqual (t, mb2.DeclaringType, "#2");
Assert.AreEqual ("M", mb2.Name, "#3");
ParameterInfo [] parameters = mb2.GetParameters ();
Assert.IsNotNull (parameters, "#4");
Assert.AreEqual (1, parameters.Length, "#5");
Assert.AreEqual (typeof (string), parameters [0].ParameterType, "#6");
}
[Test] // GetMethodFromHandle (RuntimeMethodHandle, RuntimeTypeHandle)
public void GetMethodFromHandle2_Handle_Zero ()
{
RuntimeTypeHandle th = typeof (G<>).TypeHandle;
RuntimeMethodHandle mh = new RuntimeMethodHandle ();
try {
MethodBase.GetMethodFromHandle (mh, th);
Assert.Fail ("#1");
} catch (ArgumentException ex) {
// Handle is not initialized
Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
Assert.IsNull (ex.ParamName, "#5");
}
}
public class G<T>
{
public void M (T t)
{
}
}
[Test]
public void GetMethodFromHandle_Handle_Generic_Method ()
{
MethodInfo mi = typeof (SimpleClass).GetMethod ("GenericFoo");
RuntimeMethodHandle handle = mi.MethodHandle;
MethodBase res = MethodBase.GetMethodFromHandle (handle);
Assert.AreEqual (mi, res, "#1");
res = MethodBase.GetMethodFromHandle (handle, typeof (SimpleClass).TypeHandle);
Assert.AreEqual (mi, res, "#2");
}
[Test]
public void GetMethodFromHandle_Handle_Generic_Method_Instance ()
{
MethodInfo mi = typeof (SimpleClass).GetMethod ("GenericFoo").MakeGenericMethod (typeof (int));
RuntimeMethodHandle handle = mi.MethodHandle;
MethodBase res = MethodBase.GetMethodFromHandle (handle);
Assert.AreEqual (mi, res, "#1");
res = MethodBase.GetMethodFromHandle (handle, typeof (SimpleClass).TypeHandle);
Assert.AreEqual (mi, res, "#2");
}
[Test]
public void GetMethodFromHandle_Handle_Generic_Method_On_Generic_Class ()
{
MethodInfo mi = typeof (Generic<>).GetMethod ("GenericFoo");
RuntimeMethodHandle handle = mi.MethodHandle;
MethodBase res;
try {
MethodBase.GetMethodFromHandle (handle);
Assert.Fail ("#1");
} catch (ArgumentException) {
}
mi = typeof (Generic<int>).GetMethod ("GenericFoo").MakeGenericMethod (typeof (int));
handle = mi.MethodHandle;
try {
MethodBase.GetMethodFromHandle (handle);
Assert.Fail ("#2");
} catch (ArgumentException) {
}
mi = typeof (Generic<>).GetMethod ("GenericFoo").MakeGenericMethod (typeof (int));
handle = mi.MethodHandle;
try {
MethodBase.GetMethodFromHandle (handle);
Assert.Fail ("#3");
} catch (ArgumentException) {
}
res = MethodBase.GetMethodFromHandle(handle, typeof (Generic<int>).TypeHandle);
Assert.AreEqual (typeof (Generic<int>), res.DeclaringType, "#4");
res = MethodBase.GetMethodFromHandle(handle, typeof (Generic<double>).TypeHandle);
Assert.AreEqual (typeof (Generic<double>), res.DeclaringType, "#5");
res = MethodBase.GetMethodFromHandle(handle, typeof (Generic<>).TypeHandle);
Assert.AreEqual (typeof (Generic<>), res.DeclaringType, "#6");
try {
MethodBase.GetMethodFromHandle(handle, typeof (AnotherGeneric<double>).TypeHandle);
Assert.Fail ("#7");
} catch (ArgumentException) {
}
}
[Test]
public void GetMethodFromHandle_Handle_Method_On_Generic_Class ()
{
MethodInfo mi = typeof (Generic<>).GetMethod ("Foo");
RuntimeMethodHandle handle = mi.MethodHandle;
MethodBase res;
try {
MethodBase.GetMethodFromHandle(handle);
Assert.Fail ("#1");
} catch (ArgumentException) {
}
res = MethodBase.GetMethodFromHandle(handle, typeof (Generic<>).TypeHandle);
Assert.AreEqual (res, mi, "#2");
mi = typeof (Generic<int>).GetMethod ("Foo");
handle = mi.MethodHandle;
try {
MethodBase.GetMethodFromHandle(handle);
Assert.Fail ("#3");
} catch (ArgumentException) {
}
res = MethodBase.GetMethodFromHandle(handle, typeof (Generic<int>).TypeHandle);
Assert.AreEqual (typeof (Generic<int>), res.DeclaringType, "#4");
res = MethodBase.GetMethodFromHandle(handle, typeof (Generic<double>).TypeHandle);
Assert.AreEqual (typeof (Generic<double>), res.DeclaringType, "#5");
res = MethodBase.GetMethodFromHandle(handle, typeof (Generic<>).TypeHandle);
Assert.AreEqual (typeof (Generic<>), res.DeclaringType, "#6");
try {
MethodBase.GetMethodFromHandle(handle, typeof (AnotherGeneric<double>).TypeHandle);
Assert.Fail ("#7");
} catch (ArgumentException) {
}
}
// test case adapted from http://www.chrishowie.com/2010/11/24/mutable-strings-in-mono/
public class FakeString {
public int length;
public char start_char;
}
private static FakeString UnsafeConversion<T> (T thing) where T : FakeString
{
return thing;
}
[Test]
#if MOBILE
[Category ("NotWorking")] // #10552
#endif
public void MutableString ()
{
var m = typeof (MethodBaseTest).GetMethod ("UnsafeConversion", BindingFlags.NonPublic | BindingFlags.Static);
try {
var m2 = m.MakeGenericMethod (typeof (string));
Assert.Fail ("MakeGenericMethod");
}
catch (ArgumentException) {
}
}
}
}

Some files were not shown because too many files have changed in this diff Show More