Skip to content

Commit

Permalink
Test case for __proto__ prop (#948)
Browse files Browse the repository at this point in the history
  • Loading branch information
KurtGokhan authored Aug 30, 2021
1 parent 8186a1d commit d8282ef
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
39 changes: 32 additions & 7 deletions Jint.Tests/Runtime/EngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ static EngineTests()
public EngineTests(ITestOutputHelper output)
{
_engine = new Engine()
.SetValue("log", new Action<object>( o => output.WriteLine(o.ToString())))
.SetValue("log", new Action<object>(o => output.WriteLine(o.ToString())))
.SetValue("assert", new Action<bool>(Assert.True))
.SetValue("equal", new Action<object, object>(Assert.Equal))
;
Expand Down Expand Up @@ -1260,7 +1260,7 @@ public void JsonParserShouldHandleEmptyString()
{
var ex = Assert.Throws<ParserException>(() => _engine.Evaluate("JSON.parse('');"));
Assert.Equal("Line 1: Unexpected end of input", ex.Message);
}
}

[Fact]
[ReplaceCulture("fr-FR")]
Expand Down Expand Up @@ -1514,10 +1514,35 @@ public void ShouldExecuteDromaeoBase64()
}

[Fact]
public void ShouldExecuteKnockoutWithoutErrorWhenTolerant()
public void ShouldExecuteKnockoutWithoutErrorWhetherTolerantOrIntolerant()
{
var content = GetEmbeddedFile("knockout-3.4.0.js");
_engine.Execute(content, new ParserOptions { Tolerant = true });
_engine.Execute(content, new ParserOptions { Tolerant = false });
}

[Fact]
public void ShouldAllowProtoProperty()
{
var code = "if({ __proto__: [] } instanceof Array) {}";
_engine.Execute(code);
_engine.Execute($"eval('{code}')");
_engine.Execute($"new Function('{code}')");
}

[Fact]
public void ShouldNotAllowDuplicateProtoProperty()
{
var code = "if({ __proto__: [], __proto__:[] } instanceof Array) {}";

Exception ex = Assert.Throws<ParserException>(() => _engine.Execute(code, new ParserOptions { Tolerant = false }));
Assert.Contains("Duplicate __proto__ fields are not allowed in object literals", ex.Message);

ex = Assert.Throws<JavaScriptException>(() => _engine.Execute($"eval('{code}')"));
Assert.Contains("Duplicate __proto__ fields are not allowed in object literals", ex.Message);

Assert.Throws<JavaScriptException>(() => _engine.Execute($"new Function('{code}')"));
Assert.Contains("Duplicate __proto__ fields are not allowed in object literals", ex.Message);
}

[Fact]
Expand Down Expand Up @@ -2795,8 +2820,8 @@ function output(x) {
};
";
_engine.Execute(program);
var result1 = (ObjectInstance)_engine.Evaluate("output(x1)");
var result2 = (ObjectInstance)_engine.Evaluate("output(x2)");
var result1 = (ObjectInstance) _engine.Evaluate("output(x1)");
var result2 = (ObjectInstance) _engine.Evaluate("output(x2)");

Assert.Equal(9, TypeConverter.ToNumber(result1.Get("TestDictionarySum1")));
Assert.Equal(9, TypeConverter.ToNumber(result1.Get("TestDictionarySum2")));
Expand Down Expand Up @@ -2846,13 +2871,13 @@ function concat(x, a, b) {
var r = [...arr2, ...arr1];
");

var arrayInstance = (ArrayInstance)_engine.GetValue("r");
var arrayInstance = (ArrayInstance) _engine.GetValue("r");
Assert.Equal(arrayInstance[0], 3);
Assert.Equal(arrayInstance[1], 4);
Assert.Equal(arrayInstance[2], 1);
Assert.Equal(arrayInstance[3], 2);

arrayInstance = (ArrayInstance)_engine.GetValue("s");
arrayInstance = (ArrayInstance) _engine.GetValue("s");
Assert.Equal(arrayInstance[0], 'a');
Assert.Equal(arrayInstance[1], 'b');
Assert.Equal(arrayInstance[2], 'c');
Expand Down
2 changes: 1 addition & 1 deletion Jint/Native/Function/FunctionConstructor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,4 @@ internal FunctionInstance InstantiateFunctionObject(FunctionDeclaration function
return functionObject;
}
}
}
}

0 comments on commit d8282ef

Please sign in to comment.