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

Side by Side Diff: test/JsEngine.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.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 #include <AdblockPlus.h> 1 #include <AdblockPlus.h>
2 #include <fstream> 2 #include <fstream>
3 #include <gtest/gtest.h> 3 #include <gtest/gtest.h>
4 #include <sstream> 4 #include <sstream>
5 5
6 class BaseFileSystem : public AdblockPlus::FileSystem 6 class BaseFileSystem : public AdblockPlus::FileSystem
7 { 7 {
8 void Write(const std::string& path, 8 void Write(const std::string& path,
9 std::tr1::shared_ptr<std::ostream> content) 9 std::tr1::shared_ptr<std::ostream> content)
10 { 10 {
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 59
60 void Write(const std::string& path, 60 void Write(const std::string& path,
61 std::tr1::shared_ptr<std::ostream> content) 61 std::tr1::shared_ptr<std::ostream> content)
62 { 62 {
63 throw std::runtime_error("No writing"); 63 throw std::runtime_error("No writing");
64 } 64 }
65 }; 65 };
66 66
67 TEST(JsEngineTest, EvaluateAndCall) 67 TEST(JsEngineTest, EvaluateAndCall)
68 { 68 {
69 AdblockPlus::JsEngine jsEngine; 69 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
70 jsEngine.SetErrorCallback(AdblockPlus::ErrorCallbackPtr(new ThrowingErrorCallb ack())); 70 jsEngine->SetErrorCallback(AdblockPlus::ErrorCallbackPtr(new ThrowingErrorCall back()));
71 const std::string source = "function hello() { return 'Hello'; }"; 71 const std::string source = "function hello() { return 'Hello'; }";
72 jsEngine.Evaluate(source); 72 jsEngine->Evaluate(source);
73 AdblockPlus::JsValuePtr result = jsEngine.Evaluate("hello()"); 73 AdblockPlus::JsValuePtr result = jsEngine->Evaluate("hello()");
74 ASSERT_TRUE(result->IsString()); 74 ASSERT_TRUE(result->IsString());
75 ASSERT_EQ("Hello", result->AsString()); 75 ASSERT_EQ("Hello", result->AsString());
76 } 76 }
77 77
78 TEST(JsEngineTest, LoadAndCall) 78 TEST(JsEngineTest, LoadAndCall)
79 { 79 {
80 AdblockPlus::JsEngine jsEngine; 80 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
81 jsEngine.SetErrorCallback(AdblockPlus::ErrorCallbackPtr(new ThrowingErrorCallb ack())); 81 jsEngine->SetErrorCallback(AdblockPlus::ErrorCallbackPtr(new ThrowingErrorCall back()));
82 jsEngine.SetFileSystem(AdblockPlus::FileSystemPtr(new StubFileSystem())); 82 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new StubFileSystem()));
83 jsEngine.Load("hello.js"); 83 jsEngine->Load("hello.js");
84 AdblockPlus::JsValuePtr result = jsEngine.Evaluate("hello()"); 84 AdblockPlus::JsValuePtr result = jsEngine->Evaluate("hello()");
85 ASSERT_TRUE(result->IsString()); 85 ASSERT_TRUE(result->IsString());
86 ASSERT_EQ("Hello", result->AsString()); 86 ASSERT_EQ("Hello", result->AsString());
87 } 87 }
88 88
89 TEST(JsEngineTest, LoadBadStreamFails) 89 TEST(JsEngineTest, LoadBadStreamFails)
90 { 90 {
91 AdblockPlus::JsEngine jsEngine; 91 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
92 jsEngine.SetErrorCallback(AdblockPlus::ErrorCallbackPtr(new ThrowingErrorCallb ack())); 92 jsEngine->SetErrorCallback(AdblockPlus::ErrorCallbackPtr(new ThrowingErrorCall back()));
93 jsEngine.SetFileSystem(AdblockPlus::FileSystemPtr(new BadFileSystem())); 93 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new BadFileSystem()));
94 ASSERT_ANY_THROW(jsEngine.Load("hello.js")); 94 ASSERT_ANY_THROW(jsEngine->Load("hello.js"));
95 } 95 }
96 96
97 TEST(JsEngineTest, RuntimeExceptionIsThrown) 97 TEST(JsEngineTest, RuntimeExceptionIsThrown)
98 { 98 {
99 AdblockPlus::JsEngine jsEngine; 99 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
100 jsEngine.SetErrorCallback(AdblockPlus::ErrorCallbackPtr(new ThrowingErrorCallb ack())); 100 jsEngine->SetErrorCallback(AdblockPlus::ErrorCallbackPtr(new ThrowingErrorCall back()));
101 ASSERT_THROW(jsEngine.Evaluate("doesnotexist()"), AdblockPlus::JsError); 101 ASSERT_THROW(jsEngine->Evaluate("doesnotexist()"), AdblockPlus::JsError);
102 } 102 }
103 103
104 TEST(JsEngineTest, CompileTimeExceptionIsThrown) 104 TEST(JsEngineTest, CompileTimeExceptionIsThrown)
105 { 105 {
106 AdblockPlus::JsEngine jsEngine; 106 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
107 jsEngine.SetErrorCallback(AdblockPlus::ErrorCallbackPtr(new ThrowingErrorCallb ack())); 107 jsEngine->SetErrorCallback(AdblockPlus::ErrorCallbackPtr(new ThrowingErrorCall back()));
108 ASSERT_THROW(jsEngine.Evaluate("'foo'bar'"), AdblockPlus::JsError); 108 ASSERT_THROW(jsEngine->Evaluate("'foo'bar'"), AdblockPlus::JsError);
109 } 109 }
110 110
111 TEST(JsEngineTest, ValueCreation) 111 TEST(JsEngineTest, ValueCreation)
112 { 112 {
113 AdblockPlus::JsEngine jsEngine; 113 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
114 jsEngine.SetErrorCallback(AdblockPlus::ErrorCallbackPtr(new ThrowingErrorCallb ack())); 114 jsEngine->SetErrorCallback(AdblockPlus::ErrorCallbackPtr(new ThrowingErrorCall back()));
115 AdblockPlus::JsValuePtr value; 115 AdblockPlus::JsValuePtr value;
116 116
117 value = jsEngine.NewValue("foo"); 117 value = jsEngine->NewValue("foo");
118 ASSERT_TRUE(value->IsString()); 118 ASSERT_TRUE(value->IsString());
119 ASSERT_EQ("foo", value->AsString()); 119 ASSERT_EQ("foo", value->AsString());
120 120
121 value = jsEngine.NewValue(12); 121 value = jsEngine->NewValue(12);
122 ASSERT_TRUE(value->IsNumber()); 122 ASSERT_TRUE(value->IsNumber());
123 ASSERT_EQ(12, value->AsInt()); 123 ASSERT_EQ(12, value->AsInt());
124 124
125 value = jsEngine.NewValue(true); 125 value = jsEngine->NewValue(true);
126 ASSERT_TRUE(value->IsBool()); 126 ASSERT_TRUE(value->IsBool());
127 ASSERT_TRUE(value->AsBool()); 127 ASSERT_TRUE(value->AsBool());
128 128
129 value = jsEngine.NewObject(); 129 value = jsEngine->NewObject();
130 ASSERT_TRUE(value->IsObject()); 130 ASSERT_TRUE(value->IsObject());
131 ASSERT_EQ(0u, value->GetOwnPropertyNames().size()); 131 ASSERT_EQ(0u, value->GetOwnPropertyNames().size());
132 } 132 }
133 133
134 TEST(JsEngineTest, CallbackGetSet) 134 TEST(JsEngineTest, CallbackGetSet)
135 { 135 {
136 AdblockPlus::JsEngine jsEngine; 136 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
137 137
138 ASSERT_TRUE(jsEngine.GetErrorCallback()); 138 ASSERT_TRUE(jsEngine->GetErrorCallback());
139 ASSERT_ANY_THROW(jsEngine.SetErrorCallback(AdblockPlus::ErrorCallbackPtr())); 139 ASSERT_ANY_THROW(jsEngine->SetErrorCallback(AdblockPlus::ErrorCallbackPtr()));
140 AdblockPlus::ErrorCallbackPtr errorCallback(new AdblockPlus::DefaultErrorCallb ack()); 140 AdblockPlus::ErrorCallbackPtr errorCallback(new AdblockPlus::DefaultErrorCallb ack());
141 jsEngine.SetErrorCallback(errorCallback); 141 jsEngine->SetErrorCallback(errorCallback);
142 ASSERT_EQ(errorCallback, jsEngine.GetErrorCallback()); 142 ASSERT_EQ(errorCallback, jsEngine->GetErrorCallback());
143 143
144 ASSERT_TRUE(jsEngine.GetFileSystem()); 144 ASSERT_TRUE(jsEngine->GetFileSystem());
145 ASSERT_ANY_THROW(jsEngine.SetFileSystem(AdblockPlus::FileSystemPtr())); 145 ASSERT_ANY_THROW(jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr()));
146 AdblockPlus::FileSystemPtr fileSystem(new AdblockPlus::DefaultFileSystem()); 146 AdblockPlus::FileSystemPtr fileSystem(new AdblockPlus::DefaultFileSystem());
147 jsEngine.SetFileSystem(fileSystem); 147 jsEngine->SetFileSystem(fileSystem);
148 ASSERT_EQ(fileSystem, jsEngine.GetFileSystem()); 148 ASSERT_EQ(fileSystem, jsEngine->GetFileSystem());
149 149
150 ASSERT_TRUE(jsEngine.GetWebRequest()); 150 ASSERT_TRUE(jsEngine->GetWebRequest());
151 ASSERT_ANY_THROW(jsEngine.SetWebRequest(AdblockPlus::WebRequestPtr())); 151 ASSERT_ANY_THROW(jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr()));
152 AdblockPlus::WebRequestPtr webRequest(new AdblockPlus::DefaultWebRequest()); 152 AdblockPlus::WebRequestPtr webRequest(new AdblockPlus::DefaultWebRequest());
153 jsEngine.SetWebRequest(webRequest); 153 jsEngine->SetWebRequest(webRequest);
154 ASSERT_EQ(webRequest, jsEngine.GetWebRequest()); 154 ASSERT_EQ(webRequest, jsEngine->GetWebRequest());
155 } 155 }
OLDNEW
« src/JsEngine.cpp ('K') | « test/GlobalJsObject.cpp ('k') | test/JsValue.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld