-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathMockIptablesSystemFactory.cs
More file actions
63 lines (56 loc) · 2.15 KB
/
Copy pathMockIptablesSystemFactory.cs
File metadata and controls
63 lines (56 loc) · 2.15 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using SystemInteract;
using IPTables.Net.Iptables;
using IPTables.Net.Iptables.Adapter.Client;
using IPTables.Net.Iptables.TableSync;
namespace IPTables.Net.TestFramework
{
public class MockIptablesSystemFactory : ISystemFactory
{
public List<KeyValuePair<String, String>> ExecutionLog = new List<KeyValuePair<string, string>>();
public Dictionary<KeyValuePair<String, String>,StreamReader[]> MockOutputs = new Dictionary<KeyValuePair<string, string>, StreamReader[]>();
private readonly bool _strict;
public MockIptablesSystemFactory(bool strict = false)
{
_strict = strict;
}
public ISystemProcess StartProcess(string command, string arguments)
{
var exe = new KeyValuePair<string, string>(command, arguments);
ExecutionLog.Add(exe);
StreamReader output = null, error = null;
if (MockOutputs.TryGetValue(exe, out var mo))
{
if(mo.Length >= 1)
{
output = mo[0];
}
if (MockOutputs[exe].Length >= 2)
{
error = mo[1];
}
}
else if(_strict)
{
throw new Exception("Mock output \"" + command + "\" " + arguments + " not found");
}
return new MockIptablesSystemProcess(output,error);
}
public Stream Open(string path, FileMode mode, FileAccess access)
{
throw new NotImplementedException();
}
public void TestSync<TSync>(IIPTablesAdapterClient client, IpTablesRuleSet rulesOriginal, IpTablesRuleSet rulesNew, TSync sync, List<string> expectedCommands = null) where TSync: IRuleSync
{
IpTablesChain chain = rulesOriginal.Chains.First();
chain.Sync(client, rulesNew.Chains.First().Rules, sync);
if (expectedCommands != null)
{
Assert.Equal(expectedCommands, ExecutionLog.Select(a => a.Value).ToList());
}
}
}
}