Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hbjorgo committed Dec 8, 2024
1 parent 36d41c2 commit 030b343
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 13 deletions.
21 changes: 19 additions & 2 deletions src/HeboTech.ATLib.TestConsole/FunctionalityTest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using HeboTech.ATLib.DTOs;
using HeboTech.ATLib.Events;
using HeboTech.ATLib.Modems.Cinterion;
using HeboTech.ATLib.Modems.D_LINK;
using HeboTech.ATLib.Modems.Generic;
using HeboTech.ATLib.Parsers;
using System;
Expand All @@ -13,10 +12,28 @@ namespace HeboTech.ATLib.TestConsole
{
public static class FunctionalityTest
{
static readonly string debugPath = System.IO.Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
"Downloads",
"atlog",
"log.txt"
);

public static async Task RunAsync(System.IO.Stream stream, string pin)
{
using AtChannel atChannel = AtChannel.Create(stream);
//atChannel.EnableDebug((string line) => Console.WriteLine(line));
atChannel.EnableDebug((string line) =>
{
string formattedLine = $"({DateTime.Now}) {line}";
try
{
System.IO.File.AppendAllLines(debugPath, [formattedLine]);
}
catch (Exception e)
{
//Console.WriteLine($"Error while logging: {e}");
}
});
using IMC55i modem = new MC55i(atChannel);
//using IDWM222 modem = new DWM222(atChannel);
atChannel.Open();
Expand Down
12 changes: 12 additions & 0 deletions src/HeboTech.ATLib.Tests/PDU/ReceivedMessageTypeParserTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HeboTech.ATLib.Tests.PDU
{
internal class ReceivedMessageTypeParserTests
{
}
}
48 changes: 48 additions & 0 deletions src/HeboTech.ATLib.Tests/PDU/SmsDecoderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using HeboTech.ATLib.DTOs;
using HeboTech.ATLib.PDU;
using System;
using Xunit;

namespace HeboTech.ATLib.Tests.PDU
{
public class SmsDecoderTests
{
[Theory]
[InlineData("07917238010010F5040BC87238880900F10000993092516195800AE8329BFD4697D9EC37", "+27831000015", "27838890001", "29.03.2099 15:16:59 +02:00", "hellohello")]
[InlineData("07911326040000F0040B911346610089F60000208062917314800CC8F71D14969741F977FD07", "+31624000000", "+31641600986", "26.08.2002 19:37:41 +02:00", "How are you?")]
public void Decode_SmsDeliver(string data, string serviceCenterNumber, string senderNumber, string timestamp, string message)
{
var bytes = Convert.FromHexString(data);
Sms sms = SmsDecoder.Decode(bytes, SmsStatus.REC_UNREAD);
SmsDeliver smsDeliver = sms as SmsDeliver;

Assert.NotNull(sms);
Assert.NotNull(smsDeliver);
Assert.Equal(serviceCenterNumber, smsDeliver.ServiceCenterNumber.ToString());
Assert.Equal(senderNumber, smsDeliver.SenderNumber.ToString());
Assert.Equal(DateTime.Parse(timestamp), smsDeliver.Timestamp);
Assert.Equal(message, smsDeliver.Message);
}

[Theory]
[InlineData("06916309002100067708A025057218422180218500404221802185004000", "+3690001200", "52502781", "08.12.2024 12:58:00 +01:00", "08.12.2024 12:58:00 +01:00", 119, SmsDeliveryStatus.Message_received_by_SME)]
[InlineData("06918509002100067808A025057218422180219500404221802195004000", "+5890001200", "52502781", "08.12.2024 12:59:00 +01:00", "08.12.2024 12:59:00 +01:00", 120, SmsDeliveryStatus.Message_received_by_SME)]
[InlineData("06918509002100067808A025057218422180219500404221802195004000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "+5890001200", "52502781", "08.12.2024 12:59:00 +01:00", "08.12.2024 12:59:00 +01:00", 120, SmsDeliveryStatus.Message_received_by_SME)]
public void Decode_SmsStatusReport(string data, string serviceCenterNumber, string senderNumber, string serviceCenterTimestamp, string dischargeTimestamp, int messageReference, SmsDeliveryStatus status)
{
var bytes = Convert.FromHexString(data);
Sms sms = SmsDecoder.Decode(bytes, SmsStatus.REC_UNREAD);
SmsStatusReport smsStatusReport = sms as SmsStatusReport;

Assert.NotNull(sms);
Assert.NotNull(smsStatusReport);
Assert.Equal(serviceCenterNumber, smsStatusReport.ServiceCenterAddress.ToString());
Assert.Equal(senderNumber, smsStatusReport.RecipientAddress.ToString());
Assert.Equal(DateTime.Parse(serviceCenterTimestamp), smsStatusReport.ServiceCenterTimestamp);
Assert.Equal(DateTime.Parse(dischargeTimestamp), smsStatusReport.DischargeTime);
Assert.Equal(status, smsStatusReport.Status);
Assert.Equal(MessageTypeIndicatorInbound.SMS_STATUS_REPORT, smsStatusReport.MessageTypeIndicator);
Assert.Equal(messageReference, smsStatusReport.MessageReference);
}
}
}
2 changes: 1 addition & 1 deletion src/HeboTech.ATLib/DTOs/Sms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace HeboTech.ATLib.DTOs
{
public class Sms
public abstract class Sms
{
protected Sms(MessageTypeIndicatorInbound messageTypeIndicator)
{
Expand Down
4 changes: 3 additions & 1 deletion src/HeboTech.ATLib/DTOs/SmsStatusReport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ namespace HeboTech.ATLib.DTOs
{
public class SmsStatusReport : Sms
{
public SmsStatusReport(int messageReference, PhoneNumberDTO recipientAddress, DateTimeOffset serviceCenterTimestamp, DateTimeOffset dischargeTime, SmsDeliveryStatus status)
public SmsStatusReport(int messageReference, PhoneNumberDTO recipientAddress, PhoneNumberDTO serviceCenterAddress, DateTimeOffset serviceCenterTimestamp, DateTimeOffset dischargeTime, SmsDeliveryStatus status)
: base(MessageTypeIndicatorInbound.SMS_STATUS_REPORT, messageReference)
{
RecipientAddress = recipientAddress;
ServiceCenterAddress = serviceCenterAddress;
ServiceCenterTimestamp = serviceCenterTimestamp;
DischargeTime = dischargeTime;
Status = status;
}

public PhoneNumberDTO RecipientAddress { get; }
public PhoneNumberDTO ServiceCenterAddress { get; }
public DateTimeOffset ServiceCenterTimestamp { get; }
public DateTimeOffset DischargeTime { get; }
public SmsDeliveryStatus Status { get; }
Expand Down
8 changes: 2 additions & 6 deletions src/HeboTech.ATLib/PDU/SmsDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,8 @@ public static Sms Decode(ReadOnlySpan<byte> bytes, SmsStatus status, int timesta

// SMSC information
byte smsc_length = bytes[offset++];
PhoneNumberDTO serviceCenterNumber = null;
if (smsc_length > 0)
{
serviceCenterNumber = PhoneNumberDecoder.DecodePhoneNumber(bytes[offset..(offset += smsc_length)]);
}

// Skip SMSC information - handle it in each individual parser.
offset += smsc_length;
// Header
byte headerByte = bytes[offset++];

Expand Down
2 changes: 1 addition & 1 deletion src/HeboTech.ATLib/PDU/SmsStatusReportDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static SmsStatusReport Decode(ReadOnlySpan<byte> bytes, int timestampYear
ReadOnlySpan<byte> tp_dt = bytes[offset..(offset += 7)];
byte tp_st = bytes[offset++];

return new SmsStatusReport(tp_mr, PhoneNumberDecoder.DecodePhoneNumber(tp_ra), TpduTime.DecodeTimestamp(tp_scts, timestampYearOffset), TpduTime.DecodeTimestamp(tp_dt, timestampYearOffset), (SmsDeliveryStatus) tp_st);
return new SmsStatusReport(tp_mr, PhoneNumberDecoder.DecodePhoneNumber(tp_ra), serviceCenterNumber, TpduTime.DecodeTimestamp(tp_scts, timestampYearOffset), TpduTime.DecodeTimestamp(tp_dt, timestampYearOffset), (SmsDeliveryStatus) tp_st);
}
}
}
6 changes: 4 additions & 2 deletions src/HeboTech.ATLib/Parsers/AtChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,14 @@ public void EnableDebug(Action<string> debugAction)
{
this.debugAction = debugAction ?? throw new ArgumentNullException(nameof(debugAction));
debugEnabled = true;
debugAction($"##### DEBUG ENABLED #####");
}

public void DisableDebug()
{
debugEnabled = false;
debugAction = default;
debugAction($"##### DEBUG DISABLED #####");
}

/// <summary>
Expand Down Expand Up @@ -181,7 +183,7 @@ private async Task ReaderLoopAsync(CancellationToken cancellationToken = default
{
line1 = await atReader.ReadAsync(cancellationToken);
if (debugEnabled)
debugAction($"In: {line1}");
debugAction($"In (line1): {line1}");
}
catch (OperationCanceledException)
{
Expand All @@ -198,7 +200,7 @@ private async Task ReaderLoopAsync(CancellationToken cancellationToken = default
{
line2 = await atReader.ReadAsync(cancellationToken);
if (debugEnabled)
debugAction($"In: {line2}");
debugAction($"In (line2): {line2}");
}
catch (OperationCanceledException)
{
Expand Down

0 comments on commit 030b343

Please sign in to comment.