Skip to content

Commit 7748901

Browse files
committed
In JavaScriptEngineSwitcher.Jint was made incomplete unification of using of the AllowReflection configuration property
1 parent a0328d2 commit 7748901

File tree

8 files changed

+366
-0
lines changed

8 files changed

+366
-0
lines changed

src/JavaScriptEngineSwitcher.Jint/JintJsEngine.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ public JintJsEngine(JintSettings settings)
120120
{
121121
_jsEngine = new OriginalEngine(options => {
122122
options.Interop.AllowGetType = jintSettings.AllowReflection;
123+
options.Interop.AllowSystemReflection = true;
123124

124125
options
125126
.CancellationToken(_cancellationTokenSource.Token)

test/JavaScriptEngineSwitcher.Tests/ChakraCore/InteropTests.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using System.Reflection;
34

45
using Xunit;
56

@@ -79,6 +80,27 @@ string TestAllowReflectionSetting(bool allowReflection)
7980
Assert.Equal("Object doesn't support property or method 'GetType'", exception.Description);
8081
}
8182

83+
[Fact]
84+
public override void EmbeddingOfInstanceOfAssemblyTypeAndCallingOfItsCreateInstanceMethod()
85+
{
86+
// Arrange
87+
string TestAllowReflectionSetting(bool allowReflection)
88+
{
89+
Assembly assembly = this.GetType().Assembly;
90+
string personTypeName = typeof(Person).FullName;
91+
92+
using (var jsEngine = CreateJsEngine(allowReflection: allowReflection))
93+
{
94+
jsEngine.EmbedHostObject("assembly", assembly);
95+
return jsEngine.Evaluate<string>("assembly.CreateInstance(\"" + personTypeName + "\");");
96+
}
97+
}
98+
99+
// Act and Assert
100+
Assert.Equal("{FirstName=,LastName=}", TestAllowReflectionSetting(true));
101+
Assert.Equal("{FirstName=,LastName=}", TestAllowReflectionSetting(false));
102+
}
103+
82104
#endregion
83105

84106
#region Delegates
@@ -438,6 +460,54 @@ string TestAllowReflectionSetting(bool allowReflection)
438460

439461
#endregion
440462

463+
#region Types with methods
464+
465+
[Fact]
466+
public override void EmbeddingOfTypeAndCallingOfItsGetTypeMethod()
467+
{
468+
// Arrange
469+
string dateTimeTypeName = typeof(DateTime).FullName;
470+
471+
string TestAllowReflectionSetting(bool allowReflection)
472+
{
473+
Type type = typeof(Type);
474+
475+
using (var jsEngine = CreateJsEngine(allowReflection: allowReflection))
476+
{
477+
jsEngine.EmbedHostType("Type", type);
478+
return jsEngine.Evaluate<string>("Type.GetType(\"" + dateTimeTypeName + "\");");
479+
}
480+
}
481+
482+
// Act and Assert
483+
Assert.Equal(dateTimeTypeName, TestAllowReflectionSetting(true));
484+
Assert.Equal(dateTimeTypeName, TestAllowReflectionSetting(false));
485+
}
486+
487+
[Fact]
488+
public override void EmbeddingOfAssemblyTypeAndCallingOfItsLoadMethod()
489+
{
490+
// Arrange
491+
const string reflectionEmitAssemblyName = "System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
492+
493+
string TestAllowReflectionSetting(bool allowReflection)
494+
{
495+
Type assemblyType = typeof(Assembly);
496+
497+
using (var jsEngine = CreateJsEngine(allowReflection: allowReflection))
498+
{
499+
jsEngine.EmbedHostType("Assembly", assemblyType);
500+
return jsEngine.Evaluate<string>("Assembly.Load(\"" + reflectionEmitAssemblyName + "\");");
501+
}
502+
}
503+
504+
// Act and Assert
505+
Assert.Equal(reflectionEmitAssemblyName, TestAllowReflectionSetting(true));
506+
Assert.Equal(reflectionEmitAssemblyName, TestAllowReflectionSetting(false));
507+
}
508+
509+
#endregion
510+
441511
#endregion
442512
}
443513
}

test/JavaScriptEngineSwitcher.Tests/InteropTestsBase.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Drawing;
44
using System.IO;
55
using System.Linq;
6+
using System.Reflection;
67
using System.Text;
78

89
using Xunit;
@@ -496,6 +497,29 @@ public virtual void EmbeddingOfInstanceOfCustomReferenceTypeAndCallingOfItsGetTy
496497
Assert.Equal(targetOutput, output);
497498
}
498499

500+
[Fact]
501+
public virtual void EmbeddingOfInstanceOfAssemblyTypeAndCallingOfItsCreateInstanceMethod()
502+
{
503+
// Arrange
504+
Assembly assembly = this.GetType().Assembly;
505+
string personTypeName = typeof(Person).FullName;
506+
507+
string input = string.Format("assembly.CreateInstance(\"{0}\");", personTypeName);
508+
const string targetOutput = "{FirstName=,LastName=}";
509+
510+
// Act
511+
string output;
512+
513+
using (var jsEngine = CreateJsEngine())
514+
{
515+
jsEngine.EmbedHostObject("assembly", assembly);
516+
output = jsEngine.Evaluate<string>(input);
517+
}
518+
519+
// Assert
520+
Assert.Equal(targetOutput, output);
521+
}
522+
499523
#endregion
500524

501525
#region Delegates
@@ -1360,6 +1384,52 @@ public virtual void EmbeddingOfCustomReferenceTypeWithMethod()
13601384
Assert.Equal(targetOutput, output);
13611385
}
13621386

1387+
[Fact]
1388+
public virtual void EmbeddingOfTypeAndCallingOfItsGetTypeMethod()
1389+
{
1390+
// Arrange
1391+
Type type = typeof(Type);
1392+
string dateTimeTypeName = typeof(DateTime).FullName;
1393+
1394+
string input = string.Format("Type.GetType(\"{0}\");", dateTimeTypeName);
1395+
string targetOutput = dateTimeTypeName;
1396+
1397+
// Act
1398+
string output;
1399+
1400+
using (var jsEngine = CreateJsEngine())
1401+
{
1402+
jsEngine.EmbedHostType("Type", type);
1403+
output = jsEngine.Evaluate<string>(input);
1404+
}
1405+
1406+
// Assert
1407+
Assert.Equal(targetOutput, output);
1408+
}
1409+
1410+
[Fact]
1411+
public virtual void EmbeddingOfAssemblyTypeAndCallingOfItsLoadMethod()
1412+
{
1413+
// Arrange
1414+
Type assemblyType = typeof(Assembly);
1415+
const string reflectionEmitAssemblyName = "System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
1416+
1417+
string input = string.Format("Assembly.Load(\"{0}\");", reflectionEmitAssemblyName);
1418+
const string targetOutput = reflectionEmitAssemblyName;
1419+
1420+
// Act
1421+
string output;
1422+
1423+
using (var jsEngine = CreateJsEngine())
1424+
{
1425+
jsEngine.EmbedHostType("Assembly", assemblyType);
1426+
output = jsEngine.Evaluate<string>(input);
1427+
}
1428+
1429+
// Assert
1430+
Assert.Equal(targetOutput, output);
1431+
}
1432+
13631433
#endregion
13641434

13651435
#region Removal

test/JavaScriptEngineSwitcher.Tests/Jint/InteropTests.cs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#if !NET452
22
using System;
33
using System.IO;
4+
using System.Reflection;
45

56
using Xunit;
67

@@ -80,6 +81,27 @@ string TestAllowReflectionSetting(bool allowReflection)
8081
Assert.Equal("Property 'GetType' of object is not a function", exception.Description);
8182
}
8283

84+
[Fact]
85+
public override void EmbeddingOfInstanceOfAssemblyTypeAndCallingOfItsCreateInstanceMethod()
86+
{
87+
// Arrange
88+
string TestAllowReflectionSetting(bool allowReflection)
89+
{
90+
Assembly assembly = this.GetType().Assembly;
91+
string personTypeName = typeof(Person).FullName;
92+
93+
using (var jsEngine = CreateJsEngine(allowReflection: allowReflection))
94+
{
95+
jsEngine.EmbedHostObject("assembly", assembly);
96+
return jsEngine.Evaluate<string>("assembly.CreateInstance(\"" + personTypeName + "\");");
97+
}
98+
}
99+
100+
// Act and Assert
101+
Assert.Equal("{FirstName=,LastName=}", TestAllowReflectionSetting(true));
102+
Assert.Equal("{FirstName=,LastName=}", TestAllowReflectionSetting(false));
103+
}
104+
83105
#endregion
84106

85107
#region Delegates
@@ -432,6 +454,59 @@ string TestAllowReflectionSetting(bool allowReflection)
432454

433455
#endregion
434456

457+
#region Types with methods
458+
459+
[Fact]
460+
public override void EmbeddingOfTypeAndCallingOfItsGetTypeMethod()
461+
{
462+
// Arrange
463+
string dateTimeTypeName = typeof(DateTime).FullName;
464+
465+
string TestAllowReflectionSetting(bool allowReflection)
466+
{
467+
Type type = typeof(Type);
468+
469+
using (var jsEngine = CreateJsEngine(allowReflection: allowReflection))
470+
{
471+
jsEngine.EmbedHostType("Type", type);
472+
return jsEngine.Evaluate<string>("Type.GetType(\"" + dateTimeTypeName + "\");");
473+
}
474+
}
475+
476+
// Act and Assert
477+
var exception1 = Assert.Throws<JsRuntimeException>(() => TestAllowReflectionSetting(false));
478+
Assert.Equal("Runtime error", exception1.Category);
479+
Assert.Equal("Property 'GetType' of object is not a function", exception1.Description);
480+
481+
var exception2 = Assert.Throws<JsRuntimeException>(() => TestAllowReflectionSetting(false));
482+
Assert.Equal("Runtime error", exception2.Category);
483+
Assert.Equal("Property 'GetType' of object is not a function", exception2.Description);
484+
}
485+
486+
[Fact]
487+
public override void EmbeddingOfAssemblyTypeAndCallingOfItsLoadMethod()
488+
{
489+
// Arrange
490+
const string reflectionEmitAssemblyName = "System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
491+
492+
string TestAllowReflectionSetting(bool allowReflection)
493+
{
494+
Type assemblyType = typeof(Assembly);
495+
496+
using (var jsEngine = CreateJsEngine(allowReflection: allowReflection))
497+
{
498+
jsEngine.EmbedHostType("Assembly", assemblyType);
499+
return jsEngine.Evaluate<string>("Assembly.Load(\"" + reflectionEmitAssemblyName + "\");");
500+
}
501+
}
502+
503+
// Act and Assert
504+
Assert.Equal(reflectionEmitAssemblyName, TestAllowReflectionSetting(true));
505+
Assert.Equal(reflectionEmitAssemblyName, TestAllowReflectionSetting(false));
506+
}
507+
508+
#endregion
509+
435510
#endregion
436511
}
437512
}

test/JavaScriptEngineSwitcher.Tests/Msie/InteropTests.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using System.Reflection;
34

45
using Xunit;
56

@@ -80,6 +81,27 @@ string TestAllowReflectionSetting(bool allowReflection)
8081
Assert.Equal("Object doesn't support property or method 'GetType'", exception.Description);
8182
}
8283

84+
[Fact]
85+
public override void EmbeddingOfInstanceOfAssemblyTypeAndCallingOfItsCreateInstanceMethod()
86+
{
87+
// Arrange
88+
string TestAllowReflectionSetting(bool allowReflection)
89+
{
90+
Assembly assembly = this.GetType().Assembly;
91+
string personTypeName = typeof(Person).FullName;
92+
93+
using (var jsEngine = CreateJsEngine(allowReflection: allowReflection))
94+
{
95+
jsEngine.EmbedHostObject("assembly", assembly);
96+
return jsEngine.Evaluate<string>("assembly.CreateInstance(\"" + personTypeName + "\");");
97+
}
98+
}
99+
100+
// Act and Assert
101+
Assert.Equal("{FirstName=,LastName=}", TestAllowReflectionSetting(true));
102+
Assert.Equal("{FirstName=,LastName=}", TestAllowReflectionSetting(false));
103+
}
104+
83105
#endregion
84106

85107
#region Delegates
@@ -215,6 +237,54 @@ string TestAllowReflectionSetting(bool allowReflection)
215237

216238
#endregion
217239

240+
#region Types with methods
241+
242+
[Fact]
243+
public override void EmbeddingOfTypeAndCallingOfItsGetTypeMethod()
244+
{
245+
// Arrange
246+
string dateTimeTypeName = typeof(DateTime).FullName;
247+
248+
string TestAllowReflectionSetting(bool allowReflection)
249+
{
250+
Type type = typeof(Type);
251+
252+
using (var jsEngine = CreateJsEngine(allowReflection: allowReflection))
253+
{
254+
jsEngine.EmbedHostType("Type", type);
255+
return jsEngine.Evaluate<string>("Type.GetType(\"" + dateTimeTypeName + "\");");
256+
}
257+
}
258+
259+
// Act and Assert
260+
Assert.Equal(dateTimeTypeName, TestAllowReflectionSetting(true));
261+
Assert.Equal(dateTimeTypeName, TestAllowReflectionSetting(false));
262+
}
263+
264+
[Fact]
265+
public override void EmbeddingOfAssemblyTypeAndCallingOfItsLoadMethod()
266+
{
267+
// Arrange
268+
const string reflectionEmitAssemblyName = "System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
269+
270+
string TestAllowReflectionSetting(bool allowReflection)
271+
{
272+
Type assemblyType = typeof(Assembly);
273+
274+
using (var jsEngine = CreateJsEngine(allowReflection: allowReflection))
275+
{
276+
jsEngine.EmbedHostType("Assembly", assemblyType);
277+
return jsEngine.Evaluate<string>("Assembly.Load(\"" + reflectionEmitAssemblyName + "\");");
278+
}
279+
}
280+
281+
// Act and Assert
282+
Assert.Equal(reflectionEmitAssemblyName, TestAllowReflectionSetting(true));
283+
Assert.Equal(reflectionEmitAssemblyName, TestAllowReflectionSetting(false));
284+
}
285+
286+
#endregion
287+
218288
#endregion
219289
}
220290
}

0 commit comments

Comments
 (0)