Skip to content

Commit b2cce0d

Browse files
committed
fixed bug with matching commands accross segments
1 parent b2eea63 commit b2cce0d

File tree

7 files changed

+49
-13
lines changed

7 files changed

+49
-13
lines changed

Examples/SampleApp/Examples/SessionTracingExample.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static void Run()
3030

3131
var serverTask = server.StartAsync(_cancellationTokenSource.Token);
3232

33-
SampleMailClient.Send();
33+
SampleMailClient.Send(recipients: 1);
3434

3535
serverTask.WaitWithoutException();
3636
}
@@ -62,7 +62,7 @@ private static void OnResponseException(object sender, SmtpResponseExceptionEven
6262
if (e.Exception.Properties.ContainsKey("SmtpSession:Buffer"))
6363
{
6464
var buffer = e.Exception.Properties["SmtpSession:Buffer"] as byte[];
65-
Console.WriteLine("Unknown Line: {0}", Encoding.UTF8.GetString(buffer));
65+
Console.WriteLine("Unrecognized Command: {0}", Encoding.UTF8.GetString(buffer));
6666
}
6767
}
6868

Examples/SampleApp/Program.cs

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ static void Main(string[] args)
3333

3434
//var files = Directory.GetFiles(@"C:\Temp\enron_mail_20150507.tar", "*.*", SearchOption.AllDirectories).ToList();
3535
//Console.WriteLine(files.OrderByDescending(file => new FileInfo(file).Length).First());
36+
37+
//new TokenReaderTests().CanCompareAcrossMultipleSegments();
3638
}
3739

3840
static bool IgnoreCertificateValidationFailureForTestingOnly(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)

Examples/SampleApp/SampleMailClient.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,19 @@ public static void Send(
1414
string password = null,
1515
MimeEntity body = null,
1616
int count = 1,
17+
int recipients = 1,
1718
bool useSsl = false,
1819
int port = 9025)
1920
{
2021
var message = new MimeMessage();
2122

2223
message.From.Add(MailboxAddress.Parse(from ?? "[email protected]"));
23-
message.To.Add(MailboxAddress.Parse(to ?? "[email protected]"));
24+
25+
for (var i = 0; i < recipients; i++)
26+
{
27+
message.To.Add(MailboxAddress.Parse(to ?? $"to_{i}@sample.com"));
28+
}
29+
2430
message.Subject = subject ?? "Hello";
2531
message.Body = body ?? new TextPart("plain")
2632
{

Src/SmtpServer.Tests/SmtpParserTests.cs

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using SmtpServer.Protocol;
88
using SmtpServer.Text;
99
using Xunit;
10-
using static System.Net.Mime.MediaTypeNames;
1110

1211
namespace SmtpServer.Tests
1312
{

Src/SmtpServer.Tests/TokenReaderTests.cs

+35-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Text;
1+
using System;
2+
using System.Text;
23
using SmtpServer.IO;
34
using SmtpServer.Text;
45
using Xunit;
@@ -19,6 +20,39 @@ static TokenReader CreateReader(params string[] values)
1920
return new TokenReader(segments.Build());
2021
}
2122

23+
[Fact]
24+
public void CanCompareAcrossMultipleSegments()
25+
{
26+
// arrange
27+
var reader = CreateReader("A", "BCD", "EF");
28+
29+
Span<char> match = stackalloc char[6];
30+
match[0] = 'A';
31+
match[1] = 'B';
32+
match[2] = 'C';
33+
match[3] = 'D';
34+
match[4] = 'E';
35+
match[5] = 'F';
36+
37+
// assert
38+
Assert.True(reader.TryMake(TryMakeText, out var text));
39+
Assert.False(text.IsSingleSegment);
40+
41+
// assert
42+
Assert.True(text.CaseInsensitiveStringEquals(ref match));
43+
44+
static bool TryMakeText(ref TokenReader reader)
45+
{
46+
if (reader.Peek().Kind == TokenKind.Text)
47+
{
48+
reader.Skip(TokenKind.Text);
49+
return true;
50+
}
51+
52+
return false;
53+
}
54+
}
55+
2256
[Fact]
2357
public void CanTokenizeWord()
2458
{

Src/SmtpServer/IO/BuffersExtension.cs

+2-7
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ internal static bool CaseInsensitiveStringEquals(this ReadOnlySequence<byte> buf
137137
{
138138
var span = buffer.First.Span;
139139

140-
return CaseInsensitiveStringEquals(ref span, ref text, 0);
140+
return text.Length == span.Length && CaseInsensitiveStringEquals(ref span, ref text, 0);
141141
}
142142

143143
var i = 0;
@@ -155,16 +155,11 @@ internal static bool CaseInsensitiveStringEquals(this ReadOnlySequence<byte> buf
155155
i += span.Length;
156156
}
157157

158-
return i > 0;
158+
return i == text.Length;
159159
}
160160

161161
static bool CaseInsensitiveStringEquals(ref ReadOnlySpan<byte> span, ref Span<char> text, int offset)
162162
{
163-
if (text.Length - offset != span.Length)
164-
{
165-
return false;
166-
}
167-
168163
for (var i = 0; i < span.Length; i++)
169164
{
170165
var ch = (char)span[i];

Src/SmtpServer/Protocol/SmtpParser.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ public bool TryMakeRcpt(ref TokenReader reader, out SmtpCommand command, out Smt
283283
{
284284
return false;
285285
}
286-
286+
287287
reader.Skip(TokenKind.Space);
288288

289289
if (TryMakeToLiteral(ref reader) == false)

0 commit comments

Comments
 (0)