-
-
Notifications
You must be signed in to change notification settings - Fork 20
fix traceroute/ improve dest parser #127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,56 @@ | ||||||
| using System.CommandLine.Parsing; | ||||||
| using System.Text.RegularExpressions; | ||||||
|
|
||||||
| namespace Meshtastic.Cli.Utilities; | ||||||
|
|
||||||
| public static class ArgumentParsers | ||||||
| { | ||||||
| public static uint? NodeIdParser(ArgumentResult result) | ||||||
| { | ||||||
| if (result.Tokens.Count == 0) | ||||||
| { | ||||||
| return null; | ||||||
| } | ||||||
| else if (result.Tokens.Count > 1) | ||||||
| { | ||||||
| result.ErrorMessage = $"Argument --{result.Argument.Name} expects one argument but got {result.Tokens.Count}"; | ||||||
| return null; | ||||||
| } | ||||||
|
|
||||||
| var nodeStr = result.Tokens[0].Value.ToLowerInvariant(); | ||||||
|
|
||||||
| var hexMatch = Regex.Match(nodeStr, "^(!|0x)(?<num>([a-f|0-9][a-f|0-9]){1,4})$"); | ||||||
|
||||||
| var hexMatch = Regex.Match(nodeStr, "^(!|0x)(?<num>([a-f|0-9][a-f|0-9]){1,4})$"); | |
| var hexMatch = Regex.Match(nodeStr, "^(!|0x)(?<num>([a-f0-9][a-f0-9]){1,4})$"); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| using Meshtastic.Cli.Utilities; | ||
| using System.CommandLine; | ||
|
|
||
| namespace Meshtastic.Test.Utilities; | ||
|
|
||
| public class ArgumentParsersTests | ||
| { | ||
| [TestCase("!DEADBEEF", 3735928559u, false)] | ||
| [TestCase("!deadbeef", 3735928559u, false)] | ||
| [TestCase("!deadbeef0", null, true)] | ||
| [TestCase("!d", null, true)] | ||
| [TestCase("0xDEADBEEF", 3735928559u, false)] | ||
| [TestCase("0xdeadbeef", 3735928559u, false)] | ||
| [TestCase("0xd", null, true)] | ||
| [TestCase("0x00", 0u, false)] | ||
| [TestCase("0xFFF", null, true)] | ||
| [TestCase("4294967295", uint.MaxValue, false)] | ||
| [TestCase("42949672950", null, true)] | ||
| [TestCase("429496729a", null, true)] | ||
| public void NodeIdParser_ParsesHexAndDecimal(string valueForParse, uint? expected, bool withError) | ||
| { | ||
| var opt = new Option<uint?>("--dest", | ||
| description: "Destination node address for command", | ||
| parseArgument: ArgumentParsers.NodeIdParser); | ||
|
|
||
| var result = opt.Parse(["--dest", valueForParse]); | ||
| if (!withError) | ||
| { | ||
| result.Errors.Should().HaveCount(0); | ||
| result.GetValueForOption(opt).Should().Be(expected); | ||
| } | ||
| else | ||
| { | ||
| result.Errors.Should().HaveCountGreaterThanOrEqualTo(1); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| namespace Meshtastic.Utilities; | ||
|
|
||
| public static class PacketUtils | ||
| { | ||
| /// <summary> | ||
| /// Generate next random packet id | ||
| /// </summary> | ||
| public static uint GenerateRandomPacketId() | ||
| { | ||
| return (uint)Random.Shared.NextInt64(0, (long)uint.MaxValue + 1); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The regex pattern requires hex strings to have an even number of characters (pairs) with the pattern
([a-f|0-9][a-f|0-9]){1,4}, which means it only accepts 2, 4, 6, or 8 hex digits. This rejects valid hex values with odd-length strings like "0xabc" or "!f". Consider changing the pattern to[a-f0-9]{1,8}to accept any hex string from 1 to 8 characters, which would properly parse all valid uint32 hex values.