Skip to content

Commit

Permalink
test: Decrypt multi segment AEAD data
Browse files Browse the repository at this point in the history
  • Loading branch information
HMBSbige committed Aug 12, 2021
1 parent de1b304 commit 6051cef
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Socks5/Utils/Socks5TestUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static class Socks5TestUtils
/// <summary>
/// 使用 HTTP1.1 204 测试 SOCKS5 CONNECT
/// <para>Example:</para>
/// <para>https://www.google.com/generate_204</para>
/// <para>http://www.google.com/generate_204</para>
/// <para>http://connectivitycheck.gstatic.com/generate_204</para>
/// <para>http://connect.rom.miui.com/generate_204</para>
/// <para>http://cp.cloudflare.com</para>
Expand Down
21 changes: 10 additions & 11 deletions UnitTest/AEADShadowsocksCryptoTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,24 @@ private static void TestAEAD(string method, string password, string str, string
private static void TestTcpDecrypt(string method, string password, string str, string encHex)
{
Span<byte> origin = Encoding.UTF8.GetBytes(str);
var enc = new ReadOnlySequence<byte>(encHex.FromHex());
var encBuffer = encHex.FromHex();

Span<byte> buffer = new byte[enc.Length];
Span<byte> buffer = new byte[encBuffer.Length];

using var crypto = (AEADShadowsocksCrypto)ShadowsocksCrypto.Create(method, password);

var e0 = enc.Slice(0, crypto.SaltLength);
var secondIndex = RandomNumberGenerator.GetInt32(crypto.SaltLength, encBuffer.Length);
var multiSequence = TestUtils.GetMultiSegmentSequence(encBuffer, crypto.SaltLength, secondIndex);
Assert.IsFalse(multiSequence.IsSingleSegment);
Assert.AreEqual(encBuffer.LongLength, multiSequence.Length);

var e0 = multiSequence.Slice(0, crypto.SaltLength);
var o2 = crypto.DecryptTCP(ref e0, buffer);
Assert.AreEqual(crypto.SaltLength, e0.Length);
Assert.AreEqual(0, o2);

var e1Length = crypto.SaltLength + AEADShadowsocksCrypto.ChunkOverheadSize + 1;
var e1 = enc.Slice(0, e1Length);
var o0 = crypto.DecryptTCP(ref e1, buffer);
Assert.AreEqual(e1Length - crypto.SaltLength, e1.Length);
Assert.AreEqual(0, o0);

var remain = enc.Slice(crypto.SaltLength);
var o1 = crypto.DecryptTCP(ref remain, buffer[o0..]);
var remain = multiSequence;
var o1 = crypto.DecryptTCP(ref remain, buffer);
Assert.AreEqual(0, remain.Length);
Assert.AreEqual(origin.Length, o1);

Expand Down
23 changes: 23 additions & 0 deletions UnitTest/BufferSegment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Buffers;

namespace UnitTest
{
internal class BufferSegment : ReadOnlySequenceSegment<byte>
{
public BufferSegment(Memory<byte> memory)
{
Memory = memory;
}

public BufferSegment Append(Memory<byte> memory)
{
var segment = new BufferSegment(memory)
{
RunningIndex = RunningIndex + Memory.Length
};
Next = segment;
return segment;
}
}
}
33 changes: 33 additions & 0 deletions UnitTest/TestUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Microsoft;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Buffers;
using System.Linq;

namespace UnitTest
{
internal static class TestUtils
{
public static ReadOnlySequence<byte> GetMultiSegmentSequence(Memory<byte> source, params int[] index)
{
Requires.Argument(index.LongLength > 1, nameof(index), @"index length must >1");
var orderedIndex = index.OrderBy(x => x);
var first = new BufferSegment(source[..orderedIndex.First()]);

var last = first;
var length = index[0];

foreach (var i in index.Skip(1))
{
last = last.Append(source.Slice(length, i - length));
length = i;
}

last = last.Append(source[length..]);

var sequence = new ReadOnlySequence<byte>(first, 0, last, last.Memory.Length);
Assert.AreEqual(source.Length, sequence.Length);
return sequence;
}
}
}

0 comments on commit 6051cef

Please sign in to comment.