Skip to content

Commit bddd827

Browse files
authored
Add more Test Coverage (#274)
1 parent 67a5c9e commit bddd827

File tree

2 files changed

+565
-8
lines changed

2 files changed

+565
-8
lines changed

Source/HiveMQtt/Client/internal/Validator.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
namespace HiveMQtt.Client.Internal;
1717

18+
using System.Text;
1819
using System.Text.RegularExpressions;
1920

2021
using HiveMQtt.Client.Exceptions;
@@ -29,9 +30,10 @@ public static void ValidateClientId(string clientId)
2930
{
3031
ArgumentNullException.ThrowIfNull(clientId);
3132

32-
if (clientId.Length > 65535)
33+
var byteCount = Encoding.UTF8.GetByteCount(clientId);
34+
if (byteCount > 65535)
3335
{
34-
throw new HiveMQttClientException("Client identifier must not be longer than 65535 characters.");
36+
throw new HiveMQttClientException("Client identifier must not be longer than 65535 bytes (UTF-8 encoded).");
3537
}
3638

3739
if (clientId.Length == 0)
@@ -57,16 +59,17 @@ public static void ValidateClientId(string clientId)
5759
/// </summary>
5860
/// <param name="topic">The topic name string to validate.</param>
5961
/// <exception cref="ArgumentNullException">Thrown when the topic name is null.</exception>
60-
/// <exception cref="HiveMQttClientException">Thrown when the topic name is longer than 65535 characters or empty.</exception>
62+
/// <exception cref="HiveMQttClientException">Thrown when the topic name is longer than 65535 bytes (UTF-8 encoded) or empty.</exception>
6163
/// <exception cref="HiveMQttClientException">Thrown when the topic name contains any wildcard characters.</exception>
6264
/// <exception cref="HiveMQttClientException">Thrown when the topic name contains any null characters.</exception>
6365
public static void ValidateTopicName(string topic)
6466
{
6567
ArgumentNullException.ThrowIfNull(topic);
6668

67-
if (topic.Length > 65535)
69+
var byteCount = Encoding.UTF8.GetByteCount(topic);
70+
if (byteCount > 65535)
6871
{
69-
throw new HiveMQttClientException("A topic string must not be longer than 65535 characters.");
72+
throw new HiveMQttClientException("A topic string must not be longer than 65535 bytes (UTF-8 encoded).");
7073
}
7174

7275
if (topic.Length == 0)
@@ -91,16 +94,17 @@ public static void ValidateTopicName(string topic)
9194
/// </summary>
9295
/// <param name="topic">The topic filter string to validate.</param>
9396
/// <exception cref="ArgumentNullException">Thrown when the topic filter is null.</exception>
94-
/// <exception cref="HiveMQttClientException">Thrown when the topic filter is longer than 65535 characters or empty.</exception>
97+
/// <exception cref="HiveMQttClientException">Thrown when the topic filter is longer than 65535 bytes (UTF-8 encoded) or empty.</exception>
9598
/// <exception cref="HiveMQttClientException">Thrown when the topic filter contains any null characters.</exception>
9699
/// <exception cref="ArgumentException">Thrown when the topic filter contains invalid wildcard usage.</exception>
97100
public static void ValidateTopicFilter(string topic)
98101
{
99102
ArgumentNullException.ThrowIfNull(topic);
100103

101-
if (topic.Length > 65535)
104+
var byteCount = Encoding.UTF8.GetByteCount(topic);
105+
if (byteCount > 65535)
102106
{
103-
throw new HiveMQttClientException("A topic string must not be longer than 65535 characters.");
107+
throw new HiveMQttClientException("A topic string must not be longer than 65535 bytes (UTF-8 encoded).");
104108
}
105109

106110
if (topic.Length == 0)

0 commit comments

Comments
 (0)