-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractValueTest.cs
203 lines (175 loc) · 6.5 KB
/
AbstractValueTest.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
// 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 AbstractValueTest
{
private AbstractValue pointer;
private AbstractBuffer buffer;
[Test]
public void AddTaint()
{
var clean = new AbstractValue(0x31337);
Assert.IsFalse(clean.IsTainted);
var tainted = clean.AddTaint();
Assert.IsTrue(tainted.IsTainted);
Assert.AreNotSame(clean, tainted);
}
[Test]
public void AddTaintIf()
{
var clean = new AbstractValue(0x31337);
Assert.IsFalse(clean.IsTainted);
var notTainted = clean.AddTaintIf(0 == 1);
Assert.IsFalse(notTainted.IsTainted);
Assert.AreSame(clean, notTainted);
var tainted = clean.AddTaintIf(1 == 1);
Assert.IsTrue(tainted.IsTainted);
Assert.AreNotSame(clean, tainted);
}
[Test]
[ExpectedException(typeof(InvalidOperationException))]
public void AddTaintOnPointer()
{
buffer = new AbstractBuffer(AbstractValue.GetNewBuffer(1));
var clean = new AbstractValue(buffer);
clean.AddTaint();
}
[Test]
public void AssignmentAtByteZero()
{
var values = AbstractValue.GetNewBuffer(16);
pointer = new AbstractValue(values);
pointer.PointsTo[0] = new AbstractValue(0x31337);
Assert.AreEqual(0x31337, pointer.PointsTo[0].Value);
Assert.AreEqual("*0x00031337", pointer.ToString());
}
[Test]
public void AssignmentAtEnd()
{
var values = AbstractValue.GetNewBuffer(16);
pointer = new AbstractValue(values);
pointer.PointsTo[15] = new AbstractValue(0x31337);
Assert.AreEqual(0x31337, pointer.PointsTo[15].Value);
}
[Test]
[ExpectedException(typeof(ArgumentException))]
public void EmptyBuffer()
{
pointer = new AbstractValue(new AbstractBuffer(new AbstractValue[] {}));
}
[Test]
public void Equals()
{
Assert.IsFalse(new AbstractValue().Equals(null));
}
[Test]
public void Initialized()
{
var uninit = new AbstractValue(0xabcdef);
Assert.IsTrue(uninit.IsInitialized);
Assert.AreEqual(0xabcdef, uninit.Value);
}
[Test]
public void IsOutOfBoundsPropertySurviviesCopy()
{
var src = new AbstractValue(0x31337)
{
IsOutOfBounds = true
};
var dest = new AbstractValue(src);
Assert.IsTrue(dest.IsOutOfBounds);
}
[Test]
public void NoPointer()
{
var value = new AbstractValue(2).AddTaint();
Assert.IsNull(value.PointsTo);
StringAssert.Contains("0x00000002", value.ToString());
StringAssert.Contains("t", value.ToString());
}
[Test]
public void NotInitialized()
{
var uninitializedValue = new AbstractValue();
Assert.IsFalse(uninitializedValue.IsInitialized);
Assert.AreEqual(AbstractValue.UNKNOWN, uninitializedValue.Value);
Assert.AreEqual("?", uninitializedValue.ToString());
}
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void NullCopyCtor()
{
pointer = new AbstractValue((AbstractValue)null);
}
[Test]
public void OutOfBoundsPropertySurvivesCopy()
{
var src = new AbstractValue(0x31337)
{
IsOutOfBounds = true
};
var dest = new AbstractValue(src);
Assert.IsTrue(dest.IsOutOfBounds);
}
[Test]
public void Pointer()
{
buffer = new AbstractBuffer(new[] {new AbstractValue(2)});
pointer = new AbstractValue(buffer);
Assert.AreEqual(2, pointer.PointsTo[0].Value);
StringAssert.StartsWith("*0x00000002", pointer.ToString());
}
[Test]
public void PointerHashcodes()
{
pointer = new AbstractValue(new[] {new AbstractValue(1)});
var pointer2 = new AbstractValue(new[] {new AbstractValue(2)});
Assert.AreNotEqual(pointer.GetHashCode(), pointer2.GetHashCode());
}
[Test]
public void PointerPointer()
{
buffer = new AbstractBuffer(new[] {new AbstractValue(2)});
pointer = new AbstractValue(buffer);
var pointerPointer = new AbstractValue(new[] {pointer});
Assert.AreEqual(2, pointerPointer.PointsTo[0].PointsTo[0].Value);
StringAssert.StartsWith("**0x00000002", pointerPointer.ToString());
}
[Test]
public void PointerPointerPointer()
{
buffer = new AbstractBuffer(new[] {new AbstractValue(2)});
pointer = new AbstractValue(buffer);
var pointerPointer = new AbstractValue(new[] {pointer});
var pointerPointerPointer = new AbstractValue(new[] {pointerPointer});
Assert.AreEqual(2, pointerPointerPointer.PointsTo[0].PointsTo[0].PointsTo[0].Value);
StringAssert.StartsWith("***0x00000002", pointerPointerPointer.ToString());
}
[Test]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void RequestedBufferTooLarge()
{
AbstractValue.GetNewBuffer(AbstractValue.MAX_BUFFER_SIZE + 1);
}
[Test]
public void TruncateValue()
{
var dwordValue = new AbstractValue(0xdeadbeef);
var byteValue = dwordValue.TruncateValueToByte();
Assert.AreEqual(0xef, byteValue.Value);
}
[Test]
[ExpectedException(typeof(ArgumentException))]
public void ZeroSizeBuffer()
{
pointer = new AbstractValue(new AbstractValue[] {});
}
}
}