@@ -6,17 +6,19 @@ public sealed class BinaryReaderTests
6
6
[ TestMethod ]
7
7
public void ReadGuid ( )
8
8
{
9
- byte [ ] bytes = new byte [ ] { 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 , 0x09 , 0x0A , 0x0B , 0x0C , 0x0D , 0x0E , 0x0F , 0x10 } ;
9
+ byte [ ] bytes = [ 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 , 0x09 , 0x0A , 0x0B , 0x0C , 0x0D , 0x0E , 0x0F , 0x10 ] ;
10
10
using MemoryStream stream = new ( bytes ) ;
11
11
using CfbBinaryReader reader = new ( stream ) ;
12
12
Guid guid = reader . ReadGuid ( ) ;
13
13
Assert . AreEqual ( new Guid ( bytes ) , guid ) ;
14
+
15
+ Assert . ThrowsException < EndOfStreamException > ( ( ) => reader . ReadGuid ( ) ) ;
14
16
}
15
17
16
18
[ TestMethod ]
17
19
public void ReadFileTime ( )
18
20
{
19
- byte [ ] bytes = new byte [ ] { 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 } ;
21
+ byte [ ] bytes = [ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ] ;
20
22
using MemoryStream stream = new ( bytes ) ;
21
23
using CfbBinaryReader reader = new ( stream ) ;
22
24
DateTime actual = reader . ReadFileTime ( ) ;
@@ -29,7 +31,39 @@ public void ReadFileTime()
29
31
public void ReadHeader ( string fileName )
30
32
{
31
33
using FileStream stream = File . OpenRead ( fileName ) ;
32
- using CfbBinaryReader reader = new ( stream ) ;
34
+ using MemoryStream memoryStream = new ( ) ;
35
+ stream . CopyAllTo ( memoryStream ) ;
36
+
37
+ using CfbBinaryReader reader = new ( memoryStream ) ;
33
38
Header header = reader . ReadHeader ( ) ;
39
+
40
+ stream . CopyAllTo ( memoryStream ) ;
41
+ memoryStream . WriteByte ( 1 ) ; // Corrupt signature
42
+ Assert . ThrowsException < FormatException > ( ( ) => reader . ReadHeader ( ) ) ;
43
+
44
+ stream . CopyAllTo ( memoryStream ) ;
45
+ memoryStream . Position = 24 ;
46
+ memoryStream . WriteByte ( 1 ) ; // Corrupt CLSID
47
+ Assert . ThrowsException < FormatException > ( ( ) => reader . ReadHeader ( ) ) ;
48
+
49
+ stream . CopyAllTo ( memoryStream ) ;
50
+ memoryStream . Position = 26 ;
51
+ memoryStream . WriteByte ( 1 ) ; // Corrupt Major version
52
+ Assert . ThrowsException < FormatException > ( ( ) => reader . ReadHeader ( ) ) ;
53
+
54
+ stream . CopyAllTo ( memoryStream ) ;
55
+ memoryStream . Position = 28 ;
56
+ memoryStream . WriteByte ( 1 ) ; // Corrupt byte order
57
+ Assert . ThrowsException < FormatException > ( ( ) => reader . ReadHeader ( ) ) ;
58
+
59
+ stream . CopyAllTo ( memoryStream ) ;
60
+ memoryStream . Position = 32 ;
61
+ memoryStream . WriteByte ( 1 ) ; // Corrupt mini sector shift
62
+ Assert . ThrowsException < FormatException > ( ( ) => reader . ReadHeader ( ) ) ;
63
+
64
+ stream . CopyAllTo ( memoryStream ) ;
65
+ memoryStream . Position = 32 ;
66
+ memoryStream . WriteByte ( 1 ) ; // Corrupt mini sector shift
67
+ Assert . ThrowsException < FormatException > ( ( ) => reader . ReadHeader ( ) ) ;
34
68
}
35
69
}
0 commit comments