From 030b3433bf19c9920726ae6ebbae2cee9a194682 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henning=20Bj=C3=B8rgo?= Date: Sun, 8 Dec 2024 15:44:57 +0100 Subject: [PATCH] Tests --- .../FunctionalityTest.cs | 21 +++++++- .../PDU/ReceivedMessageTypeParserTests.cs | 12 +++++ .../PDU/SmsDecoderTests.cs | 48 +++++++++++++++++++ src/HeboTech.ATLib/DTOs/Sms.cs | 2 +- src/HeboTech.ATLib/DTOs/SmsStatusReport.cs | 4 +- src/HeboTech.ATLib/PDU/SmsDecoder.cs | 8 +--- .../PDU/SmsStatusReportDecoder.cs | 2 +- src/HeboTech.ATLib/Parsers/AtChannel.cs | 6 ++- 8 files changed, 90 insertions(+), 13 deletions(-) create mode 100644 src/HeboTech.ATLib.Tests/PDU/ReceivedMessageTypeParserTests.cs create mode 100644 src/HeboTech.ATLib.Tests/PDU/SmsDecoderTests.cs diff --git a/src/HeboTech.ATLib.TestConsole/FunctionalityTest.cs b/src/HeboTech.ATLib.TestConsole/FunctionalityTest.cs index 145fa96..a700245 100644 --- a/src/HeboTech.ATLib.TestConsole/FunctionalityTest.cs +++ b/src/HeboTech.ATLib.TestConsole/FunctionalityTest.cs @@ -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; @@ -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(); diff --git a/src/HeboTech.ATLib.Tests/PDU/ReceivedMessageTypeParserTests.cs b/src/HeboTech.ATLib.Tests/PDU/ReceivedMessageTypeParserTests.cs new file mode 100644 index 0000000..b88da7c --- /dev/null +++ b/src/HeboTech.ATLib.Tests/PDU/ReceivedMessageTypeParserTests.cs @@ -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 + { + } +} diff --git a/src/HeboTech.ATLib.Tests/PDU/SmsDecoderTests.cs b/src/HeboTech.ATLib.Tests/PDU/SmsDecoderTests.cs new file mode 100644 index 0000000..3d70d4f --- /dev/null +++ b/src/HeboTech.ATLib.Tests/PDU/SmsDecoderTests.cs @@ -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); + } + } +} diff --git a/src/HeboTech.ATLib/DTOs/Sms.cs b/src/HeboTech.ATLib/DTOs/Sms.cs index 14fc575..e7d45a8 100644 --- a/src/HeboTech.ATLib/DTOs/Sms.cs +++ b/src/HeboTech.ATLib/DTOs/Sms.cs @@ -2,7 +2,7 @@ namespace HeboTech.ATLib.DTOs { - public class Sms + public abstract class Sms { protected Sms(MessageTypeIndicatorInbound messageTypeIndicator) { diff --git a/src/HeboTech.ATLib/DTOs/SmsStatusReport.cs b/src/HeboTech.ATLib/DTOs/SmsStatusReport.cs index ce64d0f..0d17e13 100644 --- a/src/HeboTech.ATLib/DTOs/SmsStatusReport.cs +++ b/src/HeboTech.ATLib/DTOs/SmsStatusReport.cs @@ -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; } diff --git a/src/HeboTech.ATLib/PDU/SmsDecoder.cs b/src/HeboTech.ATLib/PDU/SmsDecoder.cs index bbc0cff..d03b557 100644 --- a/src/HeboTech.ATLib/PDU/SmsDecoder.cs +++ b/src/HeboTech.ATLib/PDU/SmsDecoder.cs @@ -11,12 +11,8 @@ public static Sms Decode(ReadOnlySpan 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++]; diff --git a/src/HeboTech.ATLib/PDU/SmsStatusReportDecoder.cs b/src/HeboTech.ATLib/PDU/SmsStatusReportDecoder.cs index 496628e..11cf4b5 100644 --- a/src/HeboTech.ATLib/PDU/SmsStatusReportDecoder.cs +++ b/src/HeboTech.ATLib/PDU/SmsStatusReportDecoder.cs @@ -70,7 +70,7 @@ public static SmsStatusReport Decode(ReadOnlySpan bytes, int timestampYear ReadOnlySpan 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); } } } diff --git a/src/HeboTech.ATLib/Parsers/AtChannel.cs b/src/HeboTech.ATLib/Parsers/AtChannel.cs index a1df47d..0e64219 100644 --- a/src/HeboTech.ATLib/Parsers/AtChannel.cs +++ b/src/HeboTech.ATLib/Parsers/AtChannel.cs @@ -76,12 +76,14 @@ public void EnableDebug(Action debugAction) { this.debugAction = debugAction ?? throw new ArgumentNullException(nameof(debugAction)); debugEnabled = true; + debugAction($"##### DEBUG ENABLED #####"); } public void DisableDebug() { debugEnabled = false; debugAction = default; + debugAction($"##### DEBUG DISABLED #####"); } /// @@ -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) { @@ -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) {