mirror of
https://github.com/opengram-server/opengram.git
synced 2026-07-22 13:40:17 +03:00
Initial commit
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
namespace MyTelegram.MTProto.Tests;
|
||||
|
||||
public class AesHelperTest : TestsFor<AesHelper>
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(@"52 BF FD 82 77 DF 92 00 DE 7B DB F7 A3 05 1E AE
|
||||
D0 83 8A 45 D2 20 ED DB 5D EC 68 6A BD FC CA AB",
|
||||
@"40 EF 69 0A 99 B9 33 56 D1 62 77 F0 40 5A 1D 8E",
|
||||
@"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
|
||||
@"0",
|
||||
// @"00 00 00 00 00 00 00 00",
|
||||
// @"01 00 00 00 CE FD 3C 64",
|
||||
// @"40 00 00 00",
|
||||
// @"63 24 16 05 95 D8 D3 0A 76 09 85 17 57 C1 7B 1F
|
||||
//13 D0 B9 0B 68 2D 71 9D 71 BF 95 55 2A EE 76 1E
|
||||
//CD E2 5B 32 08 17 ED 48 94 1A 08 F9 81 00 00 00
|
||||
//15 C4 B5 1C 01 00 00 00 A4 BD 15 12 08 F5 27 CE",
|
||||
@"15 00 00 00 00 00 00 00 00 01 00 00 00 CE FD 3C
|
||||
64 40 00 00 00 63 24 16 05 95 D8 D3 0A 76 09 85
|
||||
17 57 C1 7B 1F 13 D0 B9 0B 68 2D 71 9D 71 BF 95
|
||||
55 2A EE 76 1E CD E2 5B 32 08 17 ED 48 94 1A 08
|
||||
F9 81 00 00 00 15 C4 B5 1C 01 00 00 00 A4 BD 15
|
||||
12 08 F5 27 CE",
|
||||
// @"52 BF FD 82 77 DF 92 00 DE 7B DB F7 A3 05 1E AE
|
||||
//D0 83 8A 45 D2 20 ED DB 5D EC 68 6A BD FC CA AB",
|
||||
@"40 EF 69 0A 99 B9 33 56 D1 62 77 F0 40 5A 1D 94",
|
||||
@"2D 28 86 16 B1 BE 35 07 34 D2 56 5E 57 59 02 AB",
|
||||
@"5",
|
||||
@"E6 8A 83 90 46 3B EC 5D DB C8 E4 29 C3 E9 36 26
|
||||
AE 58 32 96 D6 2C 57 B6 E7 AE 60 93 0E F4 72 1A
|
||||
6D 40 62 DA 49 74 83 CE 5A 71 89 E5 0B 79 A1 46
|
||||
80 FA 28 DD BE 30 7E 3D 3E A1 3A FF D0 2E FD 67
|
||||
22 D8 90 0F 3D E7 EA 74 0A 8F C3 C1 E3 D0 5C AF
|
||||
3F 20 73 31 7F"
|
||||
)]
|
||||
public void Ctr128Test(string receiveKey,
|
||||
string receiveIv,
|
||||
string ecounter,
|
||||
string n,
|
||||
//string authKeyId,
|
||||
//string messageId,
|
||||
//string messageDataLength,
|
||||
//string messageData,
|
||||
string buffer,
|
||||
//string receiveKey2,
|
||||
string receiveIv2,
|
||||
string ecounter2,
|
||||
string n2,
|
||||
string expectedData)
|
||||
{
|
||||
var span = buffer.ToBytes();
|
||||
var expectedBytes = expectedData.ToBytes();
|
||||
//var key = receiveKey.ToBytes();
|
||||
var outputSpan = new byte[span.Length];
|
||||
|
||||
Sut.CtrEncrypt(span, outputSpan, receiveKey.ToBytes(), receiveIv.ToBytes());
|
||||
|
||||
expectedBytes.ShouldBeEquivalentTo(outputSpan);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
namespace MyTelegram.MTProto.Tests;
|
||||
|
||||
public class ClientData : IClientData
|
||||
{
|
||||
public ProtocolType MtProtoType { get; set; }
|
||||
public bool IsFirstPacketParsed { get; set; }
|
||||
public byte[] SendKey { get; set; } = [];
|
||||
public byte[] ReceiveKey { get; set; } = [];
|
||||
public long AuthKeyId { get; set; }
|
||||
public string ConnectionId { get; set; } = null!;
|
||||
public bool ObfuscationEnabled { get; set; }
|
||||
public int CurrentPacketLength { get; set; }
|
||||
public int SkipCount { get; set; }
|
||||
public byte[] SendIv { get; set; } = [];
|
||||
public byte[] ReceiveIv { get; set; } = [];
|
||||
public ulong ReceiveCount { get; set; }
|
||||
public ulong SendCount { get; set; }
|
||||
public ArrayPool<byte> ArrayPool { get; set; } = ArrayPool<byte>.Shared;
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace MyTelegram.MTProto.Tests;
|
||||
|
||||
public class FirstPacketParserTest : TestsFor<FirstPacketParser>
|
||||
{
|
||||
private readonly Mock<ILogger<FirstPacketParser>> _loggerMock;
|
||||
public FirstPacketParserTest()
|
||||
{
|
||||
_loggerMock = new();
|
||||
Inject<IAesHelper>(new AesHelper());
|
||||
Inject(_loggerMock.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParseFirstPacket()
|
||||
{
|
||||
var nonce = @"E2 B5 09 29 B6 1A 48 C4 28 5C D7 02 01 A3 98 E5 C3 1E 81 A9 0C 34 B9 7A 0F 1C BC E9 0B 41 75 C9
|
||||
9B 25 73 E7 36 4A 14 71 EF D6 77 7D 73 B2 45 5A 5E 3E BF 31 65 04 71 07 70 F7 7F 86 6A 9E 48 BC".ToBytes();
|
||||
var sendKey = @"28 5C D7 02 01 A3 98 E5 C3 1E 81 A9 0C 34 B9 7A 0F 1C BC E9 0B 41 75 C9 9B 25 73 E7 36 4A 14 71"
|
||||
.ToBytes();
|
||||
var sendIv = "EF D6 77 7D 73 B2 45 5A 5E 3E BF 31 65 04 71 07".ToBytes();
|
||||
var receiveKey =
|
||||
@"07 71 04 65 31 BF 3E 5E 5A 45 B2 73 7D 77 D6 EF 71 14 4A 36 E7 73 25 9B C9 75 41 0B E9 BC 1C 0F"
|
||||
.ToBytes();
|
||||
var receiveIv = "7A B9 34 0C A9 81 1E C3 E5 98 A3 01 02 D7 5C 28".ToBytes();
|
||||
|
||||
var d = Sut.Parse(nonce);
|
||||
|
||||
d.SendKey.ShouldBeEquivalentTo(sendKey);
|
||||
d.SendIv.ShouldBeEquivalentTo(sendIv);
|
||||
d.ReceiveKey.ShouldBeEquivalentTo(receiveKey);
|
||||
d.ReceiveIv.ShouldBeEquivalentTo(receiveIv);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
|
||||
<ConfigureAwait />
|
||||
</Weavers>
|
||||
@@ -0,0 +1,30 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
|
||||
<!-- This file was generated by Fody. Manual changes to this file will be lost when your project is rebuilt. -->
|
||||
<xs:element name="Weavers">
|
||||
<xs:complexType>
|
||||
<xs:all>
|
||||
<xs:element name="ConfigureAwait" minOccurs="0" maxOccurs="1">
|
||||
<xs:complexType>
|
||||
<xs:attribute name="ContinueOnCapturedContext" type="xs:boolean" />
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
</xs:all>
|
||||
<xs:attribute name="VerifyAssembly" type="xs:boolean">
|
||||
<xs:annotation>
|
||||
<xs:documentation>'true' to run assembly verification (PEVerify) on the target assembly after all weavers have been executed.</xs:documentation>
|
||||
</xs:annotation>
|
||||
</xs:attribute>
|
||||
<xs:attribute name="VerifyIgnoreCodes" type="xs:string">
|
||||
<xs:annotation>
|
||||
<xs:documentation>A comma-separated list of error codes that can be safely ignored in assembly verification.</xs:documentation>
|
||||
</xs:annotation>
|
||||
</xs:attribute>
|
||||
<xs:attribute name="GenerateXsd" type="xs:boolean">
|
||||
<xs:annotation>
|
||||
<xs:documentation>'false' to turn off automatic generation of the XML Schema file.</xs:documentation>
|
||||
</xs:annotation>
|
||||
</xs:attribute>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
</xs:schema>
|
||||
@@ -0,0 +1,6 @@
|
||||
global using Xunit;
|
||||
global using MyTelegram.TestBase;
|
||||
global using MyTelegram.MTProto;
|
||||
global using Shouldly;
|
||||
global using Moq;
|
||||
global using System.Buffers;
|
||||
@@ -0,0 +1,62 @@
|
||||
using MyTelegram.Abstractions;
|
||||
|
||||
namespace MyTelegram.MTProto.Tests;
|
||||
|
||||
public class MtpMessageEncoderTest : TestsFor<MtpMessageEncoder>
|
||||
{
|
||||
private readonly Mock<IMessageIdHelper> _messageIdHelperMock;
|
||||
public MtpMessageEncoderTest()
|
||||
{
|
||||
_messageIdHelperMock = new();
|
||||
Inject<IAesHelper>(new AesHelper());
|
||||
Inject(_messageIdHelperMock.Object);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(@"52 BF FD 82 77 DF 92 00 DE 7B DB F7 A3 05 1E AE
|
||||
D0 83 8A 45 D2 20 ED DB 5D EC 68 6A BD FC CA AB",
|
||||
@"40 EF 69 0A 99 B9 33 56 D1 62 77 F0 40 5A 1D 8E",
|
||||
@"0",
|
||||
@"01 00 00 00 CE FD 3C 64",
|
||||
@"63 24 16 05 95 D8 D3 0A 76 09 85 17 57 C1 7B 1F
|
||||
13 D0 B9 0B 68 2D 71 9D 71 BF 95 55 2A EE 76 1E
|
||||
CD E2 5B 32 08 17 ED 48 94 1A 08 F9 81 00 00 00
|
||||
15 C4 B5 1C 01 00 00 00 A4 BD 15 12 08 F5 27 CE",
|
||||
@"E6 8A 83 90 46 3B EC 5D DB C8 E4 29 C3 E9 36 26
|
||||
AE 58 32 96 D6 2C 57 B6 E7 AE 60 93 0E F4 72 1A
|
||||
6D 40 62 DA 49 74 83 CE 5A 71 89 E5 0B 79 A1 46
|
||||
80 FA 28 DD BE 30 7E 3D 3E A1 3A FF D0 2E FD 67
|
||||
22 D8 90 0F 3D E7 EA 74 0A 8F C3 C1 E3 D0 5C AF
|
||||
3F 20 73 31 7F"
|
||||
)]
|
||||
public void EncodeTest(
|
||||
string receiveKey,
|
||||
string receiveIv,
|
||||
string n,
|
||||
string messageId,
|
||||
string messageData,
|
||||
string expectedData)
|
||||
{
|
||||
var d = new ClientData
|
||||
{
|
||||
MtProtoType = ProtocolType.Abridge,
|
||||
ReceiveKey = receiveKey.ToBytes(),
|
||||
ReceiveIv = receiveIv.ToBytes(),
|
||||
ReceiveCount = uint.Parse(n),
|
||||
ObfuscationEnabled = true
|
||||
};
|
||||
|
||||
var data = messageData.ToBytes();
|
||||
var m = new UnencryptedMessageResponse(0, data, string.Empty, 0);
|
||||
var encodedBytes = ArrayPool<byte>.Shared.Rent(data.Length + 21);
|
||||
Span<byte> span = encodedBytes;
|
||||
var messageId2 = BitConverter.ToInt64(messageId.ToBytes());
|
||||
_messageIdHelperMock.Setup(p => p.GenerateMessageId())
|
||||
.Returns(messageId2);
|
||||
var expectedBuffer = expectedData.ToBytes();
|
||||
|
||||
var count = Sut.Encode(d, m, span);
|
||||
|
||||
span[..count].ToArray().ShouldBeEquivalentTo(expectedBuffer);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
<IsTestProject>true</IsTestProject>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoFixture.Xunit2" />
|
||||
<PackageReference Include="AutoFixture.AutoMoq" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" />
|
||||
<PackageReference Include="Shouldly" />
|
||||
<PackageReference Include="xunit" />
|
||||
<PackageReference Include="xunit.runner.visualstudio">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="coverlet.collector">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\MyTelegram.MTProto\MyTelegram.MTProto.csproj" />
|
||||
<ProjectReference Include="..\..\src\MyTelegram.TestBase\MyTelegram.TestBase.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Update="Fody">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user