-
Notifications
You must be signed in to change notification settings - Fork 328
Expand file tree
/
Copy pathRemoteOrchestrationTypeBinderTests.cs
More file actions
100 lines (91 loc) · 4.33 KB
/
Copy pathRemoteOrchestrationTypeBinderTests.cs
File metadata and controls
100 lines (91 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// ----------------------------------------------------------------------------------
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------
namespace DurableTask.AzureServiceFabric.Tests
{
using System;
using System.Collections.Generic;
using DurableTask.AzureServiceFabric.Remote;
using DurableTask.Core;
using DurableTask.Core.History;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
[TestClass]
public class RemoteOrchestrationTypeBinderTests
{
// Mirrors the production formatter settings configured in RemoteOrchestrationServiceClient.PutJsonAsync.
static readonly JsonSerializerSettings RoundTripSettings = new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto,
SerializationBinder = new RemoteOrchestrationTypeBinder()
};
[TestMethod]
public void RoundTripsTaskMessageWithPolymorphicHistoryEvent()
{
var original = new TaskMessage
{
SequenceNumber = 7,
OrchestrationInstance = new OrchestrationInstance
{
InstanceId = "instance-1",
ExecutionId = "execution-1"
},
Event = new ExecutionStartedEvent(eventId: -1, input: "input")
{
Name = "Orchestration",
Version = "1.0",
Tags = new Dictionary<string, string> { ["tag1"] = "value1" }
}
};
string json = JsonConvert.SerializeObject(original, RoundTripSettings);
var deserialized = JsonConvert.DeserializeObject<TaskMessage>(json, RoundTripSettings);
Assert.IsInstanceOfType(deserialized.Event, typeof(ExecutionStartedEvent));
var startedEvent = (ExecutionStartedEvent)deserialized.Event;
Assert.AreEqual("Orchestration", startedEvent.Name);
Assert.AreEqual("input", startedEvent.Input);
Assert.AreEqual("value1", startedEvent.Tags["tag1"]);
}
[TestMethod]
public void AllowsTypesFromDurableTaskCoreAssembly()
{
var binder = new RemoteOrchestrationTypeBinder();
Type bound = binder.BindToType(
typeof(TaskMessage).Assembly.GetName().Name,
typeof(TaskMessage).FullName);
Assert.AreEqual(typeof(TaskMessage), bound);
}
[TestMethod]
public void AllowsTypesFromDurableTaskAzureServiceFabricAssembly()
{
var binder = new RemoteOrchestrationTypeBinder();
Type bound = binder.BindToType(
typeof(TaskMessageItem).Assembly.GetName().Name,
typeof(TaskMessageItem).FullName);
Assert.AreEqual(typeof(TaskMessageItem), bound);
}
[TestMethod]
public void RejectsNonAllowlistedRootType()
{
string json = "{\"$type\":\"System.IO.FileInfo, System.Private.CoreLib\",\"OriginalPath\":\"c:\\\\evil\"}";
Assert.ThrowsException<JsonSerializationException>(
() => JsonConvert.DeserializeObject<object>(json, RoundTripSettings));
}
[TestMethod]
public void RejectsNonAllowlistedNestedType()
{
string json = "{\"$type\":\"DurableTask.Core.History.ExecutionStartedEvent, DurableTask.Core\","
+ "\"Tags\":{\"$type\":\"System.Collections.Generic.SortedDictionary`2[[System.String, System.Private.CoreLib],[System.String, System.Private.CoreLib]], System.Collections\"}}";
Assert.ThrowsException<JsonSerializationException>(
() => JsonConvert.DeserializeObject<HistoryEvent>(json, RoundTripSettings));
}
}
}