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

Side by Side Diff: test/JsEngine.cpp

Issue 10310030: Convert references to FileSystem & Co. into shared pointers (avoid use after free) (Closed)
Patch Set: Created April 18, 2013, 11:59 a.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 ThrowingErrorCallback errorCallback; 69 AdblockPlus::JsEngine jsEngine;
70 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, 0, &errorCallback); 70 jsEngine.SetErrorCallback(AdblockPlus::ErrorCallbackPtr(new ThrowingErrorCallb ack()));
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 StubFileSystem fileSystem; 80 AdblockPlus::JsEngine jsEngine;
81 ThrowingErrorCallback errorCallback; 81 jsEngine.SetErrorCallback(AdblockPlus::ErrorCallbackPtr(new ThrowingErrorCallb ack()));
82 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), &fileSystem, 0, 82 jsEngine.SetFileSystem(AdblockPlus::FileSystemPtr(new StubFileSystem()));
83 &errorCallback);
84 jsEngine.Load("hello.js"); 83 jsEngine.Load("hello.js");
85 AdblockPlus::JsValuePtr result = jsEngine.Evaluate("hello()"); 84 AdblockPlus::JsValuePtr result = jsEngine.Evaluate("hello()");
86 ASSERT_TRUE(result->IsString()); 85 ASSERT_TRUE(result->IsString());
87 ASSERT_EQ("Hello", result->AsString()); 86 ASSERT_EQ("Hello", result->AsString());
88 } 87 }
89 88
90 TEST(JsEngineTest, LoadBadStreamFails) 89 TEST(JsEngineTest, LoadBadStreamFails)
91 { 90 {
92 BadFileSystem fileSystem; 91 AdblockPlus::JsEngine jsEngine;
93 ThrowingErrorCallback errorCallback; 92 jsEngine.SetErrorCallback(AdblockPlus::ErrorCallbackPtr(new ThrowingErrorCallb ack()));
94 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), &fileSystem, 0, 93 jsEngine.SetFileSystem(AdblockPlus::FileSystemPtr(new BadFileSystem()));
95 &errorCallback);
96 ASSERT_ANY_THROW(jsEngine.Load("hello.js")); 94 ASSERT_ANY_THROW(jsEngine.Load("hello.js"));
97 } 95 }
98 96
99 TEST(JsEngineTest, RuntimeExceptionIsThrown) 97 TEST(JsEngineTest, RuntimeExceptionIsThrown)
100 { 98 {
101 ThrowingErrorCallback errorCallback; 99 AdblockPlus::JsEngine jsEngine;
102 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, 0, &errorCallback); 100 jsEngine.SetErrorCallback(AdblockPlus::ErrorCallbackPtr(new ThrowingErrorCallb ack()));
103 ASSERT_THROW(jsEngine.Evaluate("doesnotexist()"), AdblockPlus::JsError); 101 ASSERT_THROW(jsEngine.Evaluate("doesnotexist()"), AdblockPlus::JsError);
104 } 102 }
105 103
106 TEST(JsEngineTest, CompileTimeExceptionIsThrown) 104 TEST(JsEngineTest, CompileTimeExceptionIsThrown)
107 { 105 {
108 ThrowingErrorCallback errorCallback; 106 AdblockPlus::JsEngine jsEngine;
109 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, 0, &errorCallback); 107 jsEngine.SetErrorCallback(AdblockPlus::ErrorCallbackPtr(new ThrowingErrorCallb ack()));
110 ASSERT_THROW(jsEngine.Evaluate("'foo'bar'"), AdblockPlus::JsError); 108 ASSERT_THROW(jsEngine.Evaluate("'foo'bar'"), AdblockPlus::JsError);
111 } 109 }
112 110
113 TEST(JsEngineTest, ValueCreation) 111 TEST(JsEngineTest, ValueCreation)
114 { 112 {
115 ThrowingErrorCallback errorCallback; 113 AdblockPlus::JsEngine jsEngine;
116 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, 0, &errorCallback); 114 jsEngine.SetErrorCallback(AdblockPlus::ErrorCallbackPtr(new ThrowingErrorCallb ack()));
117 AdblockPlus::JsValuePtr value; 115 AdblockPlus::JsValuePtr value;
118 116
119 value = jsEngine.NewValue("foo"); 117 value = jsEngine.NewValue("foo");
120 ASSERT_TRUE(value->IsString()); 118 ASSERT_TRUE(value->IsString());
121 ASSERT_EQ("foo", value->AsString()); 119 ASSERT_EQ("foo", value->AsString());
122 120
123 value = jsEngine.NewValue(12); 121 value = jsEngine.NewValue(12);
124 ASSERT_TRUE(value->IsNumber()); 122 ASSERT_TRUE(value->IsNumber());
125 ASSERT_EQ(12, value->AsInt()); 123 ASSERT_EQ(12, value->AsInt());
126 124
127 value = jsEngine.NewValue(true); 125 value = jsEngine.NewValue(true);
128 ASSERT_TRUE(value->IsBool()); 126 ASSERT_TRUE(value->IsBool());
129 ASSERT_TRUE(value->AsBool()); 127 ASSERT_TRUE(value->AsBool());
130 128
131 value = jsEngine.NewObject(); 129 value = jsEngine.NewObject();
132 ASSERT_TRUE(value->IsObject()); 130 ASSERT_TRUE(value->IsObject());
133 ASSERT_EQ(0u, value->GetOwnPropertyNames().size()); 131 ASSERT_EQ(0u, value->GetOwnPropertyNames().size());
134 } 132 }
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