Repository navigation
Expand file tree
/
Copy pathExpectedDifferences.cs
More file actions
108 lines (101 loc) · 4.22 KB
/
Copy pathExpectedDifferences.cs
File metadata and controls
108 lines (101 loc) · 4.22 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
101
102
103
104
105
106
107
108
// SPDX-FileCopyrightText: Copyright (c) 2005-2026, OPC Federation AISBL
// SPDX-License-Identifier: MIT
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using NUnit.Framework;
namespace Opc.Ua.Interop.Tests
{
/// <summary>
/// Known defects of a peer's stack, read from "expected-differences.json"
/// next to the peer: { "<check or test name>": { "reason": "...",
/// "issue": "<upstream link>" } }. A listed failure is reported as
/// inconclusive with its reason instead of failing the 2.0 tests; a
/// listed check that passes is reported with a warning so the list is
/// pruned once the stack is fixed.
/// </summary>
public static class ExpectedDifferences
{
public const string FileName = "expected-differences.json";
private static readonly Lazy<Dictionary<string, string>> s_differences = new(Load);
/// <summary>
/// The reason a check or test is expected to fail with this peer, or
/// null when it is expected to pass.
/// </summary>
public static string Reason(string name)
{
if (s_differences.Value.TryGetValue(name, out string? reason))
{
return reason;
}
// A key ending with '*' lists every test whose name starts with
// the rest, e.g. all checks of one security configuration.
foreach (KeyValuePair<string, string> entry in s_differences.Value)
{
if (entry.Key.EndsWith('*', StringComparison.Ordinal) &&
name.StartsWith(entry.Key.Substring(0, entry.Key.Length - 1), StringComparison.Ordinal))
{
return entry.Value;
}
}
return null!;
}
/// <summary>
/// Ends a test whose check failed as expected, or warns when a listed
/// check passed.
/// </summary>
public static void Apply(string name, bool passed, string failure)
{
string reason = Reason(name);
if (reason == null)
{
return;
}
if (passed)
{
Assert.Warn($"'{name}' is listed in {FileName} ({reason}) but passed; remove the entry.");
return;
}
Assert.Inconclusive($"Expected difference of this peer: {reason}{Environment.NewLine}{failure}");
}
/// <summary>
/// The reason a test of this stack against the peer is expected to
/// fail, looked up by "Fixture.Test" (e.g.
/// "LegacyServerInteropTests.ConnectWithWrongPasswordIsRejected").
/// </summary>
public static string ReasonForTest(string fixture, string testName)
{
return Reason(fixture + "." + testName);
}
private static Dictionary<string, string> Load()
{
var differences = new Dictionary<string, string>(StringComparer.Ordinal);
// Next to the peer, or up to two levels up for peers run from a
// build output folder (milo/target/milo-peer.jar,
// async-opcua/target/release/async-opcua-peer).
string folder = LegacyPeerProcess.PeerDirectory;
string file = Path.Combine(folder, FileName);
for (int level = 0; level < 2 && !File.Exists(file) && Path.GetDirectoryName(folder) != null; level++)
{
folder = Path.GetDirectoryName(folder)!;
file = Path.Combine(folder!, FileName);
}
if (!File.Exists(file))
{
return differences;
}
using var document = JsonDocument.Parse(File.ReadAllText(file));
foreach (JsonProperty entry in document.RootElement.EnumerateObject())
{
string reason = (entry.Value.TryGetProperty("reason", out JsonElement r) ? r.GetString() : string.Empty)!;
if (entry.Value.TryGetProperty("issue", out JsonElement issue))
{
reason += " (" + issue.GetString() + ")";
}
differences[entry.Name] = reason!;
}
return differences;
}
}
}