-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractBufferTest.cs
216 lines (179 loc) · 7.65 KB
/
AbstractBufferTest.cs
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
// This file is part of bugreport.
// Copyright (c) 2006-2009 The bugreport Developers.
// See AUTHORS.txt for details.
// Licensed under the GNU General Public License, Version 3 (GPLv3).
// See LICENSE.txt for details.
using System;
using NUnit.Framework;
namespace bugreport
{
[TestFixture]
public class AbstractBufferTest
{
[Test]
public void Copy()
{
var values = AbstractValue.GetNewBuffer(4);
var buffer = new AbstractBuffer(values);
var newBuffer = new AbstractBuffer(buffer);
Assert.AreNotSame(newBuffer, buffer);
for (var index = 0; index < newBuffer.Length; index++)
{
Assert.AreSame(newBuffer[index], buffer[index]);
}
}
[Test]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void InvalidPointerAnd()
{
var one = new AbstractValue(0x1);
var buffer = new AbstractBuffer(new[] {one});
var modifiedBuffer = buffer.DoOperation(OperatorEffect.Sub, new AbstractValue(3));
modifiedBuffer.DoOperation(OperatorEffect.And, new AbstractValue(0xf));
}
[Test]
public void OverflowDoesntLoseIncrement()
{
var buffer = AbstractValue.GetNewBuffer(16);
var pointer = new AbstractBuffer(buffer);
var value = new AbstractValue(0x41);
value = value.AddTaint();
pointer[0] = value;
pointer = pointer.DoOperation(OperatorEffect.Add, new AbstractValue(1));
pointer[16] = value;
pointer = pointer.DoOperation(OperatorEffect.Sub, new AbstractValue(1));
Assert.AreEqual(0x41, pointer[0].Value);
Assert.IsTrue(value.IsTainted);
Assert.AreEqual(0x41, pointer[17].Value);
Assert.IsTrue(pointer[17].IsTainted);
}
[Test]
public void OverflowZeroSizeBuffer()
{
var f = new AbstractBuffer(new AbstractValue[] {});
Assert.IsFalse(f[0].IsInitialized);
}
[Test]
public void PointerAdd()
{
var one = new AbstractValue(0x1);
var two = new AbstractValue(0x2);
var three = new AbstractValue(0x3);
var four = new AbstractValue(0x4);
var values = AbstractValue.GetNewBuffer(4);
values[0] = one;
values[1] = two;
values[2] = three;
values[3] = four;
var buffer = new AbstractBuffer(values);
Assert.AreEqual(one, buffer[0]);
var modifiedBuffer = buffer.DoOperation(OperatorEffect.Add, new AbstractValue(2));
Assert.AreEqual(three, modifiedBuffer[0]);
}
[Test]
public void PointerAnd()
{
var one = new AbstractValue(0x1);
var two = new AbstractValue(0x2);
var three = new AbstractValue(0x3);
var four = new AbstractValue(0x4);
var values = AbstractValue.GetNewBuffer(4);
values[0] = one;
values[1] = two;
values[2] = three;
values[3] = four;
var buffer = new AbstractBuffer(values);
Assert.AreEqual(one, buffer[0]);
var modifiedBuffer = buffer.DoOperation(OperatorEffect.Add, new AbstractValue(3));
Assert.AreEqual(four, modifiedBuffer[0]);
var andedBuffer = modifiedBuffer.DoOperation(OperatorEffect.And, new AbstractValue(0xfffffff0));
Assert.AreEqual(one, andedBuffer[0]);
}
[Test]
public void PointerAssignment()
{
var values = AbstractValue.GetNewBuffer(4);
var buffer = new AbstractBuffer(values);
var assignedBuffer = buffer.DoOperation(OperatorEffect.Assignment, AbstractValue.Zero);
Assert.AreNotSame(buffer, assignedBuffer);
}
[Test]
public void PointerOverflowByOne()
{
var buffer = AbstractValue.GetNewBuffer(16);
var pointer = new AbstractBuffer(buffer);
var value = pointer[16];
Assert.IsTrue(value.IsOutOfBounds);
Assert.IsFalse(value.IsInitialized);
Assert.AreEqual(AbstractValue.UNKNOWN, value.Value);
}
[Test]
public void PointerOverflowStillRetainsOldValues()
{
var test1 = new AbstractValue(0x41);
var test2 = new AbstractValue(0x42);
var buffer = AbstractValue.GetNewBuffer(2);
buffer[0] = test1;
buffer[1] = test2;
var pointer = new AbstractBuffer(buffer);
// Accessing pointer[2] will cause the AbstractBuffer to extend..
Assert.IsTrue(pointer[2].IsOutOfBounds, " value is not out of bounds");
Assert.IsFalse(pointer[2].IsInitialized);
Assert.AreEqual(AbstractValue.UNKNOWN, pointer[2].Value);
// And then we make sure the in bounds values stay the same
Assert.IsFalse(pointer[0].IsOutOfBounds);
Assert.IsFalse(pointer[1].IsOutOfBounds);
Assert.AreEqual(0x41, pointer[0].Value);
Assert.AreEqual(0x42, pointer[1].Value);
}
[Test]
public void PointerOverflowTwiceStillRetainsOriginalValues()
{
var buffer = AbstractValue.GetNewBuffer(16);
var pointer = new AbstractBuffer(buffer);
// Access beyond buffer bounds forcing buffer to expand
Assert.IsTrue(pointer[17].IsOutOfBounds);
Assert.IsFalse(pointer[17].IsInitialized);
Assert.AreEqual(AbstractValue.UNKNOWN, pointer[17].Value);
pointer[17] = new AbstractValue(0x41414141);
// Access beyond previously expanded bounds to force 2nd expand
Assert.IsTrue(pointer[64].IsOutOfBounds);
Assert.IsFalse(pointer[64].IsInitialized);
Assert.AreEqual(AbstractValue.UNKNOWN, pointer[64].Value);
// check that value set outside of bounds is still the same as well
Assert.IsTrue(pointer[17].IsOutOfBounds);
Assert.AreEqual(0x41414141, pointer[17].Value);
}
[Test]
public void PointerSub()
{
var one = new AbstractValue(0x1);
var two = new AbstractValue(0x2);
var three = new AbstractValue(0x3);
var four = new AbstractValue(0x4);
var values = AbstractValue.GetNewBuffer(4);
values[0] = one;
values[1] = two;
values[2] = three;
values[3] = four;
var buffer = new AbstractBuffer(values);
Assert.AreEqual(one, buffer[0]);
var modifiedBuffer = buffer.DoOperation(OperatorEffect.Add, new AbstractValue(2));
Assert.AreEqual(three, modifiedBuffer[0]);
var subbedBuffer = modifiedBuffer.DoOperation(OperatorEffect.Sub, new AbstractValue(2));
Assert.AreEqual(one, subbedBuffer[0]);
}
[Test]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void PointerSubUnderflow()
{
new AbstractBuffer(new AbstractValue[] {}).DoOperation(OperatorEffect.Sub, new AbstractValue(1));
}
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void PointerUnknownOperation()
{
new AbstractBuffer(new AbstractValue[] {}).DoOperation(OperatorEffect.Unknown, null);
}
}
}