forked from openwpm/OpenWPM
-
Notifications
You must be signed in to change notification settings - Fork 2
/
instrument_object.html
223 lines (188 loc) · 8.34 KB
/
instrument_object.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<!doctype html>
<html>
<head>
<title>Test page for JS Instrument's instrumentObject method</title>
</head>
<body>
<h3>Test page for JS Instrument's instrumentObject method</h3>
<p> This page defines several types of objects and tests the js
instrument's instrumentObject() method. <br/>
NOTE: The platform config option `manager_params['testing']` must be set
to True otherwise the instrumentObject method won't be exposed to this
test script.
</p>
<script type="text/javascript">
function interactWithTestObjects() {
/*
* Interact with object instrumented non-recursively
*/
// get and set prop1 (data property 1)
console.log("prop1 before set",window.test.prop1);
window.test.prop1 = 'blah1';
console.log("prop1 after set",window.test.prop1);
// get and set prop2 (data property 2)
console.log("prop2 before set",window.test.prop2);
window.test.prop2 = 'blah2';
console.log("prop2 after set",window.test.prop2);
// get and set objProp (data property)
console.log("objProp before set",window.test.objProp);
window.test.objProp = {'key': 'value'};
console.log("objProp after set",window.test.objProp);
// get and set prop3 (accessor property)
console.log("prop3 before set",window.test.prop3);
window.test.prop3 = 'blah3';
console.log("prop3 after set",window.test.prop3);
// call object method
window.test.method1('hello', {'world': true});
// overwrite a method and call
window.test.method1 = function(arg) {console.log("new method1", arg);};
window.test.method1('new argument');
// overwrite a property with a function and call
window.test.prop1 = function(arg) {console.log("prop now method", arg);};
window.test.prop1('now accepting arugments');
// overwrite a function with property
window.test.method1 = 'now static';
console.log("method turned static",window.test.method1);
// get nested object (should be recorded)
console.log("nested object",window.test.nestedObj);
// interact with nested object (should not be recorded)
console.log("nested prop1",window.test.nestedObj.prop1);
console.log("nested prop2",window.test.nestedObj.prop2);
console.log("nested method call",window.test.nestedObj.method1());
/*
* Interact with object instrumented recursively
*/
// get and set of nested property
console.log("window.test2.nestedObj.prop1 (before set)=",
window.test2.nestedObj.prop1);
window.test2.nestedObj.prop1 = "updatedprop1";
console.log("window.test2.nestedObj.prop1 (after set)=",
window.test2.nestedObj.prop1);
console.log("window.test2.nestedObj.prop2 =",
window.test2.nestedObj.prop2);
// get, call, and set of nested method
console.log("window.test2.nestedObj.method1 =",
window.test2.nestedObj.method1);
window.test2.nestedObj.method1('arg-before');
window.test2.nestedObj.method1 = function (arg) {console.log("Updated", arg);};
window.test2.nestedObj.method1('arg-after');
// get and set of double nested property
console.log("window.test2.nestedObj.doubleNested.prop1 (before set)= ",
window.test2.nestedObj.doubleNested.prop1);
window.test2.nestedObj.doubleNested.prop1 = 'doubleprop1';
console.log("window.test2.nestedObj.doubleNested.prop1 (after set)= ",
window.test2.nestedObj.doubleNested.prop1);
// get, call, and set of double nested method
console.log("window.test2.nestedObj.doubleNested.method1 =",
window.test2.nestedObj.doubleNested.method1);
window.test2.nestedObj.doubleNested.method1('blah');
window.test2.nestedObj.doubleNested.method1 = function(arg) {
console.log("doubleNestedupdated",arg);
};
window.test2.nestedObj.doubleNested.method1('blah');
// test recursion limit (default is 5)
// at the end of recursion limit object gets are logged
// should not log object get, should log prop get
console.log("level 5 depth property = ",
window.test2.l1.l2.l3.l4.l5.prop);
// should log object get, should not log prop get
console.log("level 6 depth property = ",
window.test2.l1.l2.l3.l4.l5.l6.prop);
/*
* Interact with object that has sets prevented
*/
// Try to set
window.test3.prop1 = 'newprop1'; // should work
window.test3.method1 = function(){console.log("set success");};
window.test3.obj1 = {'new':'object'};
window.test3.obj1.prop2 = 'newprop2'; // should work
window.test3.obj1.method2 = function(){console.log("set2 success");};
window.test3.obj1.obj2 = {'new':'object2'};
// Ensure gets / calls are the originals
console.log("window.test3.prop1", window.test3.prop1);
console.log("window.test3.method1", window.test3.method1());
console.log("window.test3.obj1", window.test3.obj1);
console.log("window.test3.obj1.prop2", window.test3.obj1.prop2);
console.log("window.test3.obj1.method2", window.test3.obj1.method2());
console.log("window.test3.obj1.obj2", window.test3.obj1.obj2);
}
// Build test object
window.test = {};
// create a method
test.method1 = function(arg1, arg2) {
console.log('method1', arg1, arg2);
};
// create data property
test.prop1 = 'prop1';
// create an object data property
test.objProp = {'hello': 'world'};
// create data property with defineProperty
Object.defineProperty(test, 'prop2', {
value: 'prop2',
enumerable: true,
configurable: true,
writable: true
});
// create accessor property
var valueHolder = "default-value";
Object.defineProperty(test, 'prop3', {
get: function() { return valueHolder; },
set: function(newValue) { valueHolder = newValue; },
enumerable: true,
configurable: true
});
// add a nested object property
window.test.nestedObj = {};
window.test.nestedObj.prop1 = 'default1';
window.test.nestedObj.prop2 = 'default2';
window.test.nestedObj.method1 = function() {
console.log("nestedObj call 1");
}
// Build a nested test object to be instrumented recursively
window.test2 = {};
window.test2.nestedObj = {};
window.test2.nestedObj.prop1 = 'default1';
window.test2.nestedObj.prop2 = 'default2';
window.test2.nestedObj.method1 = function(arg) {
console.log("nestedObj call 1",arg);
}
window.test2.nestedObj.doubleNested = {};
window.test2.nestedObj.doubleNested.prop1 = 'double default';
window.test2.nestedObj.doubleNested.method1 = function(arg) {
console.log("doubleNested call 1",arg);
};
// Nest to default instrumentation depth
window.test2.l1 = {};
window.test2.l1.l2 = {};
window.test2.l1.l2.l3 = {};
window.test2.l1.l2.l3.l4 = {};
window.test2.l1.l2.l3.l4.l5 = {};
window.test2.l1.l2.l3.l4.l5.prop = 'level5prop';
window.test2.l1.l2.l3.l4.l5.l6 = {};
window.test2.l1.l2.l3.l4.l5.l6.prop = 'level6prop';
// Build a test object for `preventSets` instrumentation
window.test3 = {};
window.test3.prop1 = 'default1';
window.test3.method1 = function(){console.log("window.test3.method1");};
window.test3.obj1 = {
'prop2': 'default2',
'method2': function(){console.log("window.test3.obj1.method2");},
'obj2': {'testobj':'nested'}
};
// Instrument object non-recursively
console.log("Instrumenting window.test!");
window.instrumentObject(window.test, "window.test");
// Instrument object recursively
console.log("Instrumenting window.test2!");
window.instrumentObject(window.test2, "window.test2", {'recursive': true});
// Instrument object with setting prevented
console.log("Instrumenting window.test3!");
window.instrumentObject(window.test3, "window.test3", {
'recursive': true,
'depth': 1,
'preventSets': true
});
interactWithTestObjects();
</script>
</body>
</html>