Skip to content

Commit 396549e

Browse files
committed
More validator unit tests and simplifications in UUValidator
1 parent f51ffc2 commit 396549e

File tree

3 files changed

+49
-27
lines changed

3 files changed

+49
-27
lines changed

MimeKit/Encodings/UUValidator.cs

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -141,29 +141,21 @@ unsafe bool ScanBeginMarker (ref byte* inptr, byte* inend)
141141
if (nsaved != 0 && nsaved != (byte) '\n') {
142142
byte* start = inptr;
143143

144-
// only lines containing whitespace are allowed before the begin marker
145-
while (inptr < inend && *inptr != (byte) '\n') {
146-
if (!invalidPretext && !(*inptr).IsWhitespace ()) {
147-
// FIXME: should this really be emitted for *each* character before the 'begin'?
148-
reader.OnMimeComplianceViolation (MimeComplianceViolation.InvalidUUEncodePretext, streamOffset + (int) (inptr - start), lineNumber);
149-
invalidPretext = true;
150-
}
151-
144+
// skip ahead to the next line...
145+
while (inptr < inend && *inptr != (byte) '\n')
152146
inptr++;
153-
}
147+
148+
streamOffset += (int) (inptr - start);
154149

155150
if (inptr == inend) {
156-
streamOffset += (int) (inptr - start);
157151
nsaved = *(inptr - 1);
158-
return false;
152+
break;
159153
}
160154

161-
streamOffset += (int) (inptr - start);
162-
163-
SkipByte (ref inptr);
155+
nsaved = ReadByte (ref inptr);
164156

165157
if (inptr == inend)
166-
return false;
158+
break;
167159
}
168160

169161
nsaved = ReadByte (ref inptr);
@@ -183,7 +175,7 @@ unsafe bool ScanBeginMarker (ref byte* inptr, byte* inend)
183175

184176
state = UUValidatorState.B;
185177
if (inptr == inend)
186-
return false;
178+
break;
187179
}
188180

189181
if (state == UUValidatorState.B) {
@@ -199,7 +191,7 @@ unsafe bool ScanBeginMarker (ref byte* inptr, byte* inend)
199191

200192
state = UUValidatorState.Be;
201193
if (inptr == inend)
202-
return false;
194+
break;
203195
}
204196

205197
if (state == UUValidatorState.Be) {
@@ -215,7 +207,7 @@ unsafe bool ScanBeginMarker (ref byte* inptr, byte* inend)
215207

216208
state = UUValidatorState.Beg;
217209
if (inptr == inend)
218-
return false;
210+
break;
219211
}
220212

221213
if (state == UUValidatorState.Beg) {
@@ -231,7 +223,7 @@ unsafe bool ScanBeginMarker (ref byte* inptr, byte* inend)
231223

232224
state = UUValidatorState.Begi;
233225
if (inptr == inend)
234-
return false;
226+
break;
235227
}
236228

237229
if (state == UUValidatorState.Begi) {
@@ -247,7 +239,7 @@ unsafe bool ScanBeginMarker (ref byte* inptr, byte* inend)
247239

248240
state = UUValidatorState.Begin;
249241
if (inptr == inend)
250-
return false;
242+
break;
251243
}
252244

253245
if (state == UUValidatorState.Begin) {
@@ -265,7 +257,7 @@ unsafe bool ScanBeginMarker (ref byte* inptr, byte* inend)
265257
nsaved = 0;
266258

267259
if (inptr == inend)
268-
return false;
260+
break;
269261
}
270262

271263
if (state == UUValidatorState.FileMode) {
@@ -282,7 +274,7 @@ unsafe bool ScanBeginMarker (ref byte* inptr, byte* inend)
282274
}
283275

284276
if (inptr == inend)
285-
return false;
277+
break;
286278

287279
if (nsaved < 3) {
288280
// file mode is too short
@@ -303,7 +295,7 @@ unsafe bool ScanBeginMarker (ref byte* inptr, byte* inend)
303295
nsaved = 0;
304296

305297
if (inptr == inend)
306-
return false;
298+
break;
307299
}
308300

309301
if (state == UUValidatorState.FileName) {
@@ -312,14 +304,13 @@ unsafe bool ScanBeginMarker (ref byte* inptr, byte* inend)
312304
while (inptr < inend && *inptr != (byte) '\n')
313305
inptr++;
314306

307+
streamOffset += (int) (inptr - start);
308+
315309
if (inptr == inend) {
316310
// need to keep reading until we hit the end of the line
317-
streamOffset += (int) (inptr - start);
318-
return false;
311+
break;
319312
}
320313

321-
streamOffset += (int) (inptr - start);
322-
323314
SkipByte (ref inptr);
324315

325316
state = UUValidatorState.Payload;

UnitTests/Encodings/Base64ValidatorTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,18 @@ public void TestValidateInvalidInput_CharactersAfterPadding ()
147147
TestValidateInvalidInput (text, violations);
148148
}
149149

150+
[Test]
151+
public void TestValidateInvalidInput_IncompleteQuantumAfterPaddingStart ()
152+
{
153+
const string text = "VGhpcyBpcyB0aGUgcGxhaW4gdGV4dCBtZXNzYWdlIQ=\r\n=";
154+
var violations = new List<MimeComplianceViolationEventArgs> {
155+
new MimeComplianceViolationEventArgs (MimeComplianceViolation.IncompleteBase64Quantum, 44, 1),
156+
new MimeComplianceViolationEventArgs (MimeComplianceViolation.IncompleteBase64Quantum, 46, 2)
157+
};
158+
159+
TestValidateInvalidInput (text, violations);
160+
}
161+
150162
[TestCase (4096)]
151163
[TestCase (1024)]
152164
[TestCase (16)]

UnitTests/Encodings/QuotedPrintableValidatorTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public void TestValidateBufferSizeDos (int bufferSize)
7171

7272
[TestCase ("=XA", 1)]
7373
[TestCase ("=AX", 2)]
74+
[TestCase ("=A\n", 2)]
7475
public void TestValidateInvalidHexSequence (string hex, int offset)
7576
{
7677
string text = $"This is some quoted printable text with an invalid {hex} sequence.";
@@ -103,5 +104,23 @@ public void TestValidateInvalidSoftBreak ()
103104
Assert.That (reader.ComplianceViolations[0].StreamOffset, Is.EqualTo (text.LastIndexOf ('s')));
104105
Assert.That (reader.ComplianceViolations[0].LineNumber, Is.EqualTo (1));
105106
}
107+
108+
[TestCase ("invalid trailing =", MimeComplianceViolation.InvalidQuotedPrintableEncoding)]
109+
[TestCase ("invalid trailing =A", MimeComplianceViolation.InvalidQuotedPrintableEncoding)]
110+
[TestCase ("invalid trailing =\r", MimeComplianceViolation.InvalidQuotedPrintableSoftBreak)]
111+
public void TestValidateInvalidFlush (string text, MimeComplianceViolation violation)
112+
{
113+
var rawData = Encoding.ASCII.GetBytes (text);
114+
var reader = new ComplianceMimeReader ();
115+
var validator = new QuotedPrintableValidator (reader, 0, 1);
116+
117+
validator.Write (rawData, 0, rawData.Length);
118+
validator.Flush ();
119+
120+
Assert.That (reader.ComplianceViolations.Count, Is.EqualTo (1));
121+
Assert.That (reader.ComplianceViolations[0].Violation, Is.EqualTo (violation));
122+
Assert.That (reader.ComplianceViolations[0].StreamOffset, Is.EqualTo (text.Length));
123+
Assert.That (reader.ComplianceViolations[0].LineNumber, Is.EqualTo (1));
124+
}
106125
}
107126
}

0 commit comments

Comments
 (0)