Projekt

Obecné

Profil

Stáhnout (4.24 KB) Statistiky
| Větev: | Tag: | Revize:
1 cbf3b0c6 Pultak
using System.IO;
2
using System.Threading.Tasks;
3
using LDClient.detection;
4
using LDClient.utils;
5
using Moq;
6
using NUnit.Framework;
7
8
namespace LDClientTests.detection; 
9
10
internal class InfoFetcherTests {
11 a7d16717 Pultak
    private AInfoFetcher _defaultFetcher;
12
    private AInfoFetcher _fetcherWithoutPars;
13 cbf3b0c6 Pultak
        
14
15
    private readonly string[] _defaultArguments = new[] { "argument 1", "argument 2" , "argument 3"};
16
    private const uint DefaultMaxAttempts = 5;
17
18
    private Mock<IProcessUtils> _mockProcessUtils;
19
    private Mock<IFileUtils> _mockFileUtils;
20
21
    [SetUp]
22
    public void Setup() {
23
        _mockProcessUtils = new Mock<IProcessUtils>(MockBehavior.Strict);
24
25
        _mockFileUtils = new Mock<IFileUtils>(MockBehavior.Strict);
26
    
27
28 a7d16717 Pultak
        _defaultFetcher = new T32RemFetcher(DefaultMaxAttempts, 50, "info.txt", "executable.exe",
29 cbf3b0c6 Pultak
            _defaultArguments, 0, 50) {
30
            FileUtils = _mockFileUtils.Object,
31
            ProcessUtils = _mockProcessUtils.Object
32
        };
33
34
35 a7d16717 Pultak
        _fetcherWithoutPars = new T32RemFetcher(DefaultMaxAttempts, 50, "info.txt", "executable.exe",
36 cbf3b0c6 Pultak
            null, 0, 50) {
37
            FileUtils = _mockFileUtils.Object,
38
            ProcessUtils = _mockProcessUtils.Object
39
        };
40
    }
41
42
    [Test]
43
    public async Task FetchDataAsync_ExecuteAll_ExecutedAndFetched() {
44
45
        _mockProcessUtils.Setup(x => x.ExecuteNewProcess(It.IsAny<string>(), It.IsAny<string>(),
46
            It.IsAny<int>(), It.IsAny<int>())).Returns(true);
47
        _mockFileUtils.Setup(x => x.ReadFileAllLines(It.IsAny<string>())).
48
            Returns(DebuggerInfoParserTests.CorrectFileContent.Split("\n"));
49
50
51 0728c021 silhavyj
        bool result = await _defaultFetcher.FetchDataAsync();
52 cbf3b0c6 Pultak
53
        Assert.IsTrue(result);
54
        _mockProcessUtils.Verify(x => x.ExecuteNewProcess(It.IsAny<string>(), It.IsAny<string>(),
55
            It.IsAny<int>(), It.IsAny<int>()), Times.Exactly(_defaultArguments.Length));
56
57
        Assert.AreEqual(DebuggerInfoParserTests.BodySerialNumber, _defaultFetcher.BodySerialNumber);
58
        Assert.AreEqual(DebuggerInfoParserTests.HeadSerialNumber, _defaultFetcher.HeadSerialNumber);
59
60
    }
61
62
63
64
    [Test]
65
    public async Task FetchDataAsync_ExecuteNonExistentProgram_ExecutionFailed() {
66
67
        _mockProcessUtils.Setup(x => x.ExecuteNewProcess(It.IsAny<string>(), It.IsAny<string>(),
68
            It.IsAny<int>(), It.IsAny<int>())).Returns(false);
69
        _mockFileUtils.Setup(x => x.ReadFileAllLines(It.IsAny<string>())).
70
            Returns(new []{""});
71
72
73 0728c021 silhavyj
        bool result = await _defaultFetcher.FetchDataAsync();
74 cbf3b0c6 Pultak
75
76
        Assert.IsFalse(result);
77
        _mockProcessUtils.Verify(x => x.ExecuteNewProcess(It.IsAny<string>(), It.IsAny<string>(),
78
            It.IsAny<int>(), It.IsAny<int>()), Times.Exactly(1));
79
        _mockFileUtils.Verify(x => x.ReadFileAllLines(It.IsAny<string>()), Times.Never);
80
    }
81
82
    [Test]
83
    public async Task FetchDataAsync_ExecuteWithoutParameters_NotExecuted() {
84
        _mockProcessUtils.Setup(x => x.ExecuteNewProcess(It.IsAny<string>(), It.IsAny<string>(),
85
            It.IsAny<int>(), It.IsAny<int>())).Returns(false);
86
        _mockFileUtils.Setup(x => x.ReadFileAllLines(It.IsAny<string>())).
87
            Returns(new[] { "" });
88
89 0728c021 silhavyj
        bool result = await _fetcherWithoutPars.FetchDataAsync();
90 cbf3b0c6 Pultak
            
91
        Assert.IsFalse(result);
92
        _mockProcessUtils.Verify(x => x.ExecuteNewProcess(It.IsAny<string>(), It.IsAny<string>(),
93
            It.IsAny<int>(), It.IsAny<int>()), Times.Never);
94
        _mockFileUtils.Verify(x => x.ReadFileAllLines(It.IsAny<string>()), Times.Never);
95
96
    }
97
98
99
    [Test]
100
    public async Task FetchDataAsync_ExecuteInfoNotCreated_FetchFailed() {
101
        _mockProcessUtils.Setup(x => x.ExecuteNewProcess(It.IsAny<string>(), It.IsAny<string>(),
102
            It.IsAny<int>(), It.IsAny<int>())).Returns(true);
103
        _mockFileUtils.Setup(x => x.ReadFileAllLines(It.IsAny<string>())).Throws(new FileNotFoundException());
104
105 0728c021 silhavyj
        bool result = await _defaultFetcher.FetchDataAsync();
106 cbf3b0c6 Pultak
        Assert.IsFalse(result);
107
108
        _mockProcessUtils.Verify(x => x.ExecuteNewProcess(It.IsAny<string>(), It.IsAny<string>(),
109
            It.IsAny<int>(), It.IsAny<int>()), Times.Exactly(_defaultArguments.Length));
110
        _mockFileUtils.Verify(x => x.ReadFileAllLines(It.IsAny<string>()), Times.Exactly((int)DefaultMaxAttempts));
111
112
    }
113
114
115
116
117
}