Skip to content

Commit

Permalink
Unit test for code page 1251
Browse files Browse the repository at this point in the history
Issue #33
  • Loading branch information
fubar-coder committed Aug 1, 2018
1 parent 5f27bac commit 1350da4
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
52 changes: 50 additions & 2 deletions test/FubarDev.FtpServer.Tests/FtpCommandCollectorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Xunit;
Expand Down Expand Up @@ -157,10 +158,57 @@ public void TestMultipleWithArgumentWithLineFeedInStepTwo()
Assert.True(collector.IsEmpty);
}

private IEnumerable<FtpCommand> Collect(FtpCommandCollector collector, string data)
[Fact]
public void TestWithCyrillicTextWithWindows1251Encoding()
{
var encoding = CodePagesEncodingProvider.Instance.GetEncoding(codepage: 1251);
var collector = new FtpCommandCollector(() => encoding);

const string cyrillicSymbols = "абвгдеёжзийклмнопрстуфхцчшщыъьэюя";

var expectedCommands = new[] { new FtpCommand("TEST", cyrillicSymbols) };

var stringToTest = string.Format("TEST {0}\r\n", cyrillicSymbols);
var actualCommands = Collect(collector, stringToTest).ToArray();

// Test failed
// TELNET: Unknown command received - skipping 0xFF
// 0xFF == 'я' (maybe?)
Assert.Equal(expectedCommands, actualCommands, new FtpCommandComparer());
Assert.True(collector.IsEmpty);
}

private static IEnumerable<ReadOnlyMemory<byte>> EscapeIAC(byte[] data)
{
var startIndex = 0;
for (var i = 0; i != data.Length; ++i)
{
if (data[i] == 0xFF)
{
var length = i - startIndex + 1;
yield return new ReadOnlyMemory<byte>(data, startIndex, length);
startIndex = i;
}
}

var remaining = data.Length - startIndex;
if (remaining != 0)
{
yield return new ReadOnlyMemory<byte>(data, startIndex, remaining);
}
}

private static IEnumerable<FtpCommand> Collect(FtpCommandCollector collector, string data)
{
var temp = collector.Encoding.GetBytes(data);
return collector.Collect(temp, 0, temp.Length);
foreach (var escapedData in EscapeIAC(temp).Select(x => x.Span.ToArray()))
{
var collected = collector.Collect(escapedData, 0, escapedData.Length);
foreach (var command in collected)
{
yield return command;
}
}
}

private class FtpCommandComparer : IComparer<FtpCommand>, IEqualityComparer<FtpCommand>
Expand Down
5 changes: 3 additions & 2 deletions test/FubarDev.FtpServer.Tests/FubarDev.FtpServer.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<TargetFramework>netcoreapp2.1</TargetFramework>
<OutputType>Exe</OutputType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.0" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.0-beta007" PrivateAssets="All" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="4.5.0" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\FubarDev.FtpServer\FubarDev.FtpServer.csproj" />
</ItemGroup>
<Import Project="../../Global.props" />
</Project>
</Project>

0 comments on commit 1350da4

Please sign in to comment.