Core - V8Serialization incorrectly returns negative numbers

- CEF doesn't differentiate between UINT and INT
  so we have to do some additional bounds checking
  To make sure the correct value is returned
- Add xUnit tests

Issue #3858
This commit is contained in:
Alex Maitland
2021-10-24 10:41:53 +10:00
parent fe9904e952
commit c832f7b7be
3 changed files with 148 additions and 1 deletions
@@ -42,7 +42,22 @@ namespace CefSharp
}
else if (obj->IsInt())
{
list->SetInt(index, obj->GetIntValue());
// CEF doesn't differentiate between UINT and INT
// so we have to do some additional bounds checking
// To make sure the correct value is returned
// CEF IPC doesn't support UINT so we can only
// support int and double.
// https://github.com/cefsharp/CefSharp/issues/3858
auto dValue = obj->GetDoubleValue();
if (dValue < INT_MIN || dValue > INT_MAX)
{
list->SetDouble(index, dValue);
}
else
{
list->SetInt(index, obj->GetIntValue());
}
}
else if (obj->IsDouble())
{
@@ -0,0 +1,30 @@
// Copyright © 2021 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
using System.Threading.Tasks;
using CefSharp.Example;
using CefSharp.OffScreen;
using Xunit;
namespace CefSharp.Test
{
public class ChromiumWebBrowserOffScreenFixture : IAsyncLifetime
{
public ChromiumWebBrowser Browser { get; private set; }
Task IAsyncLifetime.DisposeAsync()
{
Browser?.Dispose();
return Task.CompletedTask;
}
Task IAsyncLifetime.InitializeAsync()
{
Browser = new ChromiumWebBrowser(CefExample.HelloWorldUrl);
return Browser.LoadUrlAsync();
}
}
}
@@ -0,0 +1,102 @@
// Copyright © 2021 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
namespace CefSharp.Test.Javascript
{
[Collection(CefSharpFixtureCollection.Key)]
public class EvaluateScriptAsyncFacts : IClassFixture<ChromiumWebBrowserOffScreenFixture>
{
private readonly ITestOutputHelper output;
private readonly CefSharpFixture collectionFixture;
private readonly ChromiumWebBrowserOffScreenFixture classFixture;
public EvaluateScriptAsyncFacts(ITestOutputHelper output, CefSharpFixture collectionFixture, ChromiumWebBrowserOffScreenFixture classFixture)
{
this.output = output;
this.collectionFixture = collectionFixture;
this.classFixture = classFixture;
}
[Theory]
[InlineData(double.MaxValue, "Number.MAX_VALUE")]
[InlineData(double.MaxValue / 2, "Number.MAX_VALUE / 2")]
//https://github.com/cefsharp/CefSharp/issues/3858
public async Task CanEvaluateDoubleComputation(double expectedValue, string script)
{
var browser = classFixture.Browser;
Assert.False(browser.IsLoading);
var javascriptResponse = await browser.EvaluateScriptAsync(script);
Assert.True(javascriptResponse.Success);
var actualType = javascriptResponse.Result.GetType();
Assert.Equal(typeof(double), actualType);
Assert.Equal(expectedValue, (double)javascriptResponse.Result, 5);
output.WriteLine("Script {0} : Result {1}", script, javascriptResponse.Result);
}
[Theory]
[InlineData(0.5d)]
[InlineData(1.5d)]
[InlineData(-0.5d)]
[InlineData(-1.5d)]
[InlineData(100000.24500d)]
[InlineData(-100000.24500d)]
[InlineData((double)uint.MaxValue)]
[InlineData((double)int.MaxValue + 1)]
[InlineData((double)int.MaxValue + 10)]
[InlineData((double)int.MinValue - 1)]
[InlineData((double)int.MinValue - 10)]
[InlineData(((double)uint.MaxValue * 2))]
[InlineData(((double)uint.MaxValue * 2) + 0.1)]
//https://github.com/cefsharp/CefSharp/issues/3858
public async Task CanEvaluateDoubleValues(double num)
{
var browser = classFixture.Browser;
Assert.False(browser.IsLoading);
var javascriptResponse = await browser.EvaluateScriptAsync(num.ToString());
Assert.True(javascriptResponse.Success);
var actualType = javascriptResponse.Result.GetType();
Assert.Equal(typeof(double), actualType);
Assert.Equal(num, (double)javascriptResponse.Result, 5);
output.WriteLine("Expected {0} : Actual {1}", num, javascriptResponse.Result);
}
[Theory]
[InlineData(0)]
[InlineData(-1)]
[InlineData(100)]
[InlineData(int.MaxValue)]
[InlineData(int.MinValue)]
//https://github.com/cefsharp/CefSharp/issues/3858
public async Task CanEvaluateIntValues(object num)
{
var browser = classFixture.Browser;
Assert.False(browser.IsLoading);
var javascriptResponse = await browser.EvaluateScriptAsync(num.ToString());
Assert.True(javascriptResponse.Success);
var actualType = javascriptResponse.Result.GetType();
Assert.Equal(typeof(int), actualType);
Assert.Equal(num, javascriptResponse.Result);
output.WriteLine("Expected {0} : Actual {1}", num, javascriptResponse.Result);
}
}
}