Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: test/JsValue.cpp

Issue 10173031: Don`t use references to JsEngine to avoid use-after-free errors,switch to shared_ptr instead (Closed)
Patch Set: Created April 18, 2013, 4:15 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: test/JsValue.cpp
===================================================================
--- a/test/JsValue.cpp
+++ b/test/JsValue.cpp
@@ -1,15 +1,15 @@
#include <AdblockPlus.h>
#include <gtest/gtest.h>
TEST(JsValueTest, UndefinedValue)
{
- AdblockPlus::JsEngine jsEngine;
- AdblockPlus::JsValuePtr value = jsEngine.Evaluate("undefined");
+ AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
+ AdblockPlus::JsValuePtr value = jsEngine->Evaluate("undefined");
ASSERT_TRUE(value->IsUndefined());
ASSERT_FALSE(value->IsNull());
ASSERT_FALSE(value->IsString());
ASSERT_FALSE(value->IsBool());
ASSERT_FALSE(value->IsNumber());
ASSERT_FALSE(value->IsObject());
ASSERT_FALSE(value->IsArray());
ASSERT_FALSE(value->IsFunction());
@@ -20,18 +20,18 @@ TEST(JsValueTest, UndefinedValue)
ASSERT_ANY_THROW(value->SetProperty("foo", false));
ASSERT_ANY_THROW(value->GetClassName());
ASSERT_ANY_THROW(value->GetOwnPropertyNames());
ASSERT_ANY_THROW(value->Call());
}
TEST(JsValueTest, NullValue)
{
- AdblockPlus::JsEngine jsEngine;
- AdblockPlus::JsValuePtr value = jsEngine.Evaluate("null");
+ AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
+ AdblockPlus::JsValuePtr value = jsEngine->Evaluate("null");
ASSERT_FALSE(value->IsUndefined());
ASSERT_TRUE(value->IsNull());
ASSERT_FALSE(value->IsString());
ASSERT_FALSE(value->IsBool());
ASSERT_FALSE(value->IsNumber());
ASSERT_FALSE(value->IsObject());
ASSERT_FALSE(value->IsArray());
ASSERT_FALSE(value->IsFunction());
@@ -42,18 +42,18 @@ TEST(JsValueTest, NullValue)
ASSERT_ANY_THROW(value->SetProperty("foo", false));
ASSERT_ANY_THROW(value->GetClassName());
ASSERT_ANY_THROW(value->GetOwnPropertyNames());
ASSERT_ANY_THROW(value->Call());
}
TEST(JsValueTest, StringValue)
{
- AdblockPlus::JsEngine jsEngine;
- AdblockPlus::JsValuePtr value = jsEngine.Evaluate("'123'");
+ AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
+ AdblockPlus::JsValuePtr value = jsEngine->Evaluate("'123'");
ASSERT_FALSE(value->IsUndefined());
ASSERT_FALSE(value->IsNull());
ASSERT_TRUE(value->IsString());
ASSERT_FALSE(value->IsBool());
ASSERT_FALSE(value->IsNumber());
ASSERT_FALSE(value->IsObject());
ASSERT_FALSE(value->IsArray());
ASSERT_FALSE(value->IsFunction());
@@ -65,18 +65,18 @@ TEST(JsValueTest, StringValue)
ASSERT_ANY_THROW(value->SetProperty("foo", false));
ASSERT_ANY_THROW(value->GetClassName());
ASSERT_ANY_THROW(value->GetOwnPropertyNames());
ASSERT_ANY_THROW(value->Call());
}
TEST(JsValueTest, IntValue)
{
- AdblockPlus::JsEngine jsEngine;
- AdblockPlus::JsValuePtr value = jsEngine.Evaluate("123");
+ AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
+ AdblockPlus::JsValuePtr value = jsEngine->Evaluate("123");
ASSERT_FALSE(value->IsUndefined());
ASSERT_FALSE(value->IsNull());
ASSERT_FALSE(value->IsString());
ASSERT_FALSE(value->IsBool());
ASSERT_TRUE(value->IsNumber());
ASSERT_FALSE(value->IsObject());
ASSERT_FALSE(value->IsArray());
ASSERT_FALSE(value->IsFunction());
@@ -88,18 +88,18 @@ TEST(JsValueTest, IntValue)
ASSERT_ANY_THROW(value->SetProperty("foo", false));
ASSERT_ANY_THROW(value->GetClassName());
ASSERT_ANY_THROW(value->GetOwnPropertyNames());
ASSERT_ANY_THROW(value->Call());
}
TEST(JsValueTest, BoolValue)
{
- AdblockPlus::JsEngine jsEngine;
- AdblockPlus::JsValuePtr value = jsEngine.Evaluate("true");
+ AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
+ AdblockPlus::JsValuePtr value = jsEngine->Evaluate("true");
ASSERT_FALSE(value->IsUndefined());
ASSERT_FALSE(value->IsNull());
ASSERT_FALSE(value->IsString());
ASSERT_TRUE(value->IsBool());
ASSERT_FALSE(value->IsNumber());
ASSERT_FALSE(value->IsObject());
ASSERT_FALSE(value->IsArray());
ASSERT_FALSE(value->IsFunction());
@@ -110,51 +110,51 @@ TEST(JsValueTest, BoolValue)
ASSERT_ANY_THROW(value->SetProperty("foo", false));
ASSERT_ANY_THROW(value->GetClassName());
ASSERT_ANY_THROW(value->GetOwnPropertyNames());
ASSERT_ANY_THROW(value->Call());
}
TEST(JsValueTest, ObjectValue)
{
- AdblockPlus::JsEngine jsEngine;
+ AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
const std::string source("\
function Foo() {\
this.x = 2;\
this.toString = function() {return 'foo';};\
this.valueOf = function() {return 123;};\
};\
new Foo()");
- AdblockPlus::JsValuePtr value = jsEngine.Evaluate(source);
+ AdblockPlus::JsValuePtr value = jsEngine->Evaluate(source);
ASSERT_FALSE(value->IsUndefined());
ASSERT_FALSE(value->IsNull());
ASSERT_FALSE(value->IsString());
ASSERT_FALSE(value->IsBool());
ASSERT_FALSE(value->IsNumber());
ASSERT_TRUE(value->IsObject());
ASSERT_FALSE(value->IsArray());
ASSERT_FALSE(value->IsFunction());
ASSERT_EQ("foo", value->AsString());
ASSERT_EQ(123, value->AsInt());
ASSERT_TRUE(value->AsBool());
ASSERT_ANY_THROW(value->AsList());
ASSERT_EQ(2, value->GetProperty("x")->AsInt());
value->SetProperty("x", 12);
ASSERT_EQ(12, value->GetProperty("x")->AsInt());
- value->SetProperty("x", jsEngine.NewValue(15));
+ value->SetProperty("x", jsEngine->NewValue(15));
ASSERT_EQ(15, value->GetProperty("x")->AsInt());
ASSERT_EQ("Foo", value->GetClassName());
ASSERT_EQ(3u, value->GetOwnPropertyNames().size());
ASSERT_ANY_THROW(value->Call());
}
TEST(JsValueTest, ArrayValue)
{
- AdblockPlus::JsEngine jsEngine;
- AdblockPlus::JsValuePtr value = jsEngine.Evaluate("[5,8,12]");
+ AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
+ AdblockPlus::JsValuePtr value = jsEngine->Evaluate("[5,8,12]");
ASSERT_FALSE(value->IsUndefined());
ASSERT_FALSE(value->IsNull());
ASSERT_FALSE(value->IsString());
ASSERT_FALSE(value->IsBool());
ASSERT_FALSE(value->IsNumber());
ASSERT_TRUE(value->IsObject());
ASSERT_TRUE(value->IsArray());
ASSERT_FALSE(value->IsFunction());
@@ -164,42 +164,42 @@ TEST(JsValueTest, ArrayValue)
ASSERT_EQ(8, value->AsList()[1]->AsInt());
ASSERT_EQ(3, value->GetProperty("length")->AsInt());
ASSERT_EQ("Array", value->GetClassName());
ASSERT_ANY_THROW(value->Call());
}
TEST(JsValueTest, FunctionValue)
{
- AdblockPlus::JsEngine jsEngine;
- AdblockPlus::JsValuePtr value = jsEngine.Evaluate("(function(foo, bar) {return this.x + '/' + foo + '/' + bar;})");
+ AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
+ AdblockPlus::JsValuePtr value = jsEngine->Evaluate("(function(foo, bar) {return this.x + '/' + foo + '/' + bar;})");
ASSERT_FALSE(value->IsUndefined());
ASSERT_FALSE(value->IsNull());
ASSERT_FALSE(value->IsString());
ASSERT_FALSE(value->IsBool());
ASSERT_FALSE(value->IsNumber());
ASSERT_TRUE(value->IsObject());
ASSERT_FALSE(value->IsArray());
ASSERT_TRUE(value->IsFunction());
ASSERT_TRUE(value->AsBool());
ASSERT_ANY_THROW(value->AsList());
ASSERT_EQ(2, value->GetProperty("length")->AsInt());
- AdblockPlus::JsValuePtr thisPtr = jsEngine.Evaluate("({x:2})");
+ AdblockPlus::JsValuePtr thisPtr = jsEngine->Evaluate("({x:2})");
AdblockPlus::JsValueList params;
- params.push_back(jsEngine.NewValue(5));
- params.push_back(jsEngine.NewValue("xyz"));
+ params.push_back(jsEngine->NewValue(5));
+ params.push_back(jsEngine->NewValue("xyz"));
ASSERT_EQ("2/5/xyz", value->Call(params, thisPtr)->AsString());
}
TEST(JsValueTest, ThrowingCoversion)
{
- AdblockPlus::JsEngine jsEngine;
+ AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
const std::string source("\
function Foo() {\
this.toString = function() {throw 'test1';};\
this.valueOf = function() {throw 'test2';};\
};\
new Foo()");
- AdblockPlus::JsValuePtr value = jsEngine.Evaluate(source);
+ AdblockPlus::JsValuePtr value = jsEngine->Evaluate(source);
ASSERT_EQ("", value->AsString());
ASSERT_EQ(0, value->AsInt());
}
« src/JsEngine.cpp ('K') | « test/JsEngine.cpp ('k') | test/WebRequest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld