Skip to content

Commit 079be85

Browse files
committed
Enforce maximum length in entity instance ids
Signed-off-by: Hal Spang <halspang@microsoft.com>
1 parent 1f02d44 commit 079be85

2 files changed

Lines changed: 60 additions & 0 deletions

File tree

src/Abstractions/Entities/EntityInstanceId.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public EntityInstanceId(string name, string key)
2828
Check.NotNull(key);
2929
this.Name = name.ToLowerInvariant();
3030
this.Key = key;
31+
32+
if (this.ToString().Length > 100)
33+
{
34+
throw new ArgumentException("entity instance ids may not exceed 100 characters in length.");
35+
}
3136
}
3237

3338
/// <summary>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using Microsoft.DurableTask.Entities;
5+
6+
namespace Microsoft.DurableTask.Abstractions.Tests.Entities;
7+
8+
public class EntityInstanceIdTests
9+
{
10+
[Fact]
11+
public void TestValidEntityInstanceId()
12+
{
13+
var entityId = new EntityInstanceId("entity", "key1");
14+
Assert.Equal("@entity@key1", entityId.ToString());
15+
}
16+
17+
[Fact]
18+
public void TestEntityNameLowercased()
19+
{
20+
var entityId = new EntityInstanceId("Entity", "key1");
21+
Assert.Equal("entity", entityId.Name);
22+
Assert.Equal("@entity@key1", entityId.ToString());
23+
}
24+
25+
[Fact]
26+
public void TestEntityNameWithAtSymbolThrows()
27+
{
28+
Assert.Throws<ArgumentException>(() => new EntityInstanceId("entity@name", "key1"));
29+
}
30+
31+
[Fact]
32+
public void TestEntityInstanceIdExceedsMaxLengthThrows()
33+
{
34+
string longName = new string('a', 50);
35+
string longKey = new string('b', 51);
36+
Assert.Throws<ArgumentException>(() => new EntityInstanceId(longName, longKey));
37+
}
38+
39+
[Fact]
40+
public void TestFromStringValid()
41+
{
42+
var entityId = EntityInstanceId.FromString("@entity@key1");
43+
Assert.Equal("entity", entityId.Name);
44+
Assert.Equal("key1", entityId.Key);
45+
}
46+
47+
[Fact]
48+
public void TestFromStringInvalidThrows()
49+
{
50+
Assert.Throws<ArgumentException>(() => EntityInstanceId.FromString("invalid"));
51+
Assert.Throws<ArgumentException>(() => EntityInstanceId.FromString("@entitykey1"));
52+
Assert.Throws<ArgumentException>(() => EntityInstanceId.FromString("@@key1"));
53+
Assert.Throws<ArgumentException>(() => EntityInstanceId.FromString($"@entity@{new string('a', 100)}"));
54+
}
55+
}

0 commit comments

Comments
 (0)