-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathBinaryReaderExtensionsTests.cs
More file actions
165 lines (130 loc) · 5.03 KB
/
BinaryReaderExtensionsTests.cs
File metadata and controls
165 lines (130 loc) · 5.03 KB
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
/*
Technitium Library
Copyright (C) 2026 Shreyas Zare (shreyas@technitium.com)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.IO;
using System.Linq;
using System.Text;
using TechnitiumLibrary.IO;
namespace TechnitiumLibrary.UnitTests.TechnitiumLibrary.IO
{
[TestClass]
public sealed class BinaryReaderExtensionsTests
{
private static BinaryReader ReaderOf(params byte[] bytes)
{
return new BinaryReader(new MemoryStream(bytes));
}
// -----------------------------------------------
// ReadLength()
// -----------------------------------------------
[TestMethod]
public void ReadLength_ShouldReadSingleByteLengths()
{
// GIVEN
BinaryReader reader = ReaderOf(0x05);
// WHEN
int length = reader.ReadLength();
// THEN
Assert.AreEqual(5, length);
Assert.AreEqual(1, reader.BaseStream.Position);
}
[TestMethod]
public void ReadLength_ShouldReadMultiByteBigEndianLengths()
{
// GIVEN
// 0x82 => 2-byte length follows → value = 0x01 0x2C → 300 decimal
BinaryReader reader = ReaderOf(0x82, 0x01, 0x2C);
// WHEN
int length = reader.ReadLength();
// THEN
Assert.AreEqual(300, length);
Assert.AreEqual(3, reader.BaseStream.Position);
}
[TestMethod]
public void ReadLength_ShouldThrow_WhenLengthPrefixTooLarge()
{
// GIVEN
// lower 7 bits = 0x05, meaning "next 5 bytes", exceeding allowed 4
BinaryReader reader = ReaderOf(0x85);
// WHEN-THEN
Assert.ThrowsExactly<IOException>(() => reader.ReadLength());
}
// -----------------------------------------------
// ReadBuffer()
// -----------------------------------------------
[TestMethod]
public void ReadBuffer_ShouldReturnBytes_WhenLengthPrefixed()
{
// GIVEN
// length=3, then bytes 0xAA, 0xBB, 0xCC
BinaryReader reader = ReaderOf(0x03, 0xAA, 0xBB, 0xCC);
// WHEN
byte[] data = reader.ReadBuffer();
// THEN
Assert.HasCount(3, data);
CollectionAssert.AreEqual(new byte[] { 0xAA, 0xBB, 0xCC }, data);
}
// -----------------------------------------------
// ReadShortString()
// -----------------------------------------------
[TestMethod]
public void ReadShortString_ShouldDecodeUtf8StringCorrectly()
{
// GIVEN
string text = "Hello";
byte[] encoded = Encoding.UTF8.GetBytes(text);
byte[] bytes = new byte[] { (byte)encoded.Length }.Concat(encoded).ToArray();
BinaryReader reader = ReaderOf(bytes);
// WHEN
string result = reader.ReadShortString();
// THEN
Assert.AreEqual(text, result);
}
[TestMethod]
public void ReadShortString_ShouldUseSpecifiedEncoding()
{
// GIVEN
string text = "Å";
Encoding encoding = Encoding.UTF32;
byte[] encoded = encoding.GetBytes(text);
byte[] bytes = new byte[] { (byte)encoded.Length }.Concat(encoded).ToArray();
BinaryReader reader = ReaderOf(bytes);
// WHEN
string result = reader.ReadShortString(encoding);
// THEN
Assert.AreEqual(text, result);
}
// -----------------------------------------------
// ReadDateTime()
// -----------------------------------------------
[TestMethod]
public void ReadDateTime_ShouldConvertEpochMilliseconds()
{
// GIVEN
DateTime expected = new DateTime(2024, 01, 01, 12, 00, 00, DateTimeKind.Utc);
long millis = (long)(expected - DateTime.UnixEpoch).TotalMilliseconds;
byte[] encoded = BitConverter.GetBytes(millis);
// Normalize to little-endian, which BinaryReader expects
if (!BitConverter.IsLittleEndian)
Array.Reverse(encoded);
BinaryReader reader = ReaderOf(encoded);
// WHEN
DateTime result = reader.ReadDateTime();
// THEN
Assert.AreEqual(expected, result);
}
}
}