aboutsummaryrefslogtreecommitdiff
path: root/src/dtf/test/WixToolsetTests.Dtf.Compression/MisbehavingStreamContext.cs
blob: 2531f3bcb7cf568b03b3150f3be3b00c59a62028 (plain)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.

using System;
using System.IO;
using System.Collections.Generic;
using WixToolset.Dtf.Compression;

namespace WixToolset.Dtf.Test
{
    public class MisbehavingStreamContext : ArchiveFileStreamContext
    {
        public const string EXCEPTION = "Test exception.";

        private bool throwEx;
        private bool getName;
        private bool openArchive;
        private bool closeArchive;
        private bool openFile;
        private bool closeFile;
        private int closeFileCount;

        public MisbehavingStreamContext(
            string cabinetFile,
            string directory,
            IDictionary<string, string> files,
            bool throwEx,
            bool getName,
            bool openArchive,
            bool closeArchive,
            bool openFile,
            bool closeFile)
            : base(cabinetFile, directory, files)
        {
            this.throwEx = throwEx;
            this.getName = getName;
            this.openArchive = openArchive;
            this.closeArchive = closeArchive;
            this.openFile = openFile;
            this.closeFile = closeFile;
        }

        public override string GetArchiveName(int archiveNumber)
        {
            if (!this.getName)
            {
                if (throwEx)
                {
                    throw new Exception(EXCEPTION);
                }
                else
                {
                    return null;
                }
            }
            return base.GetArchiveName(archiveNumber);
        }

        public override Stream OpenArchiveWriteStream(
            int archiveNumber,
            string archiveName,
            bool truncate,
            CompressionEngine compressionEngine)
        {
            if (!this.openArchive)
            {
                if (throwEx)
                {
                    throw new Exception(EXCEPTION);
                }
                else
                {
                    return null;
                }
            }
            return base.OpenArchiveWriteStream(
                archiveNumber, archiveName, truncate, compressionEngine);
        }

        public override void CloseArchiveWriteStream(
            int archiveNumber,
            string archiveName,
            Stream stream)
        {
            if (!this.closeArchive)
            {
                if (throwEx)
                {
                    this.closeArchive = true;
                    throw new Exception(EXCEPTION);
                }
                return;
            }
            base.CloseArchiveWriteStream(archiveNumber, archiveName, stream);
        }

        public override Stream OpenFileReadStream(
            string path,
            out FileAttributes attributes,
            out DateTime lastWriteTime)
        {
            if (!this.openFile)
            {
                if (throwEx)
                {
                    throw new Exception(EXCEPTION);
                }
                else
                {
                    attributes = FileAttributes.Normal;
                    lastWriteTime = DateTime.MinValue;
                    return null;
                }
            }
            return base.OpenFileReadStream(path, out attributes, out lastWriteTime);
        }

        public override void CloseFileReadStream(string path, Stream stream)
        {
            if (!this.closeFile && ++closeFileCount == 2)
            {
                if (throwEx)
                {
                    throw new Exception(EXCEPTION);
                }
                return;
            }
            base.CloseFileReadStream(path, stream);
        }

        public override Stream OpenArchiveReadStream(
            int archiveNumber,
            string archiveName,
            CompressionEngine compressionEngine)
        {
            if (!this.openArchive)
            {
                if (throwEx)
                {
                    throw new Exception(EXCEPTION);
                }
                else
                {
                    return null;
                }
            }
            return base.OpenArchiveReadStream(archiveNumber, archiveName, compressionEngine);
        }

        public override void CloseArchiveReadStream(
            int archiveNumber,
            string archiveName,
            Stream stream)
        {
            if (!this.closeArchive)
            {
                if (throwEx)
                {
                    this.closeArchive = true;
                    throw new Exception(EXCEPTION);
                }
                return;
            }
            base.CloseArchiveReadStream(archiveNumber, archiveName, stream);
        }

        public override Stream OpenFileWriteStream(
            string path,
            long fileSize,
            DateTime lastWriteTime)
        {
            if (!this.openFile)
            {
                if (throwEx)
                {
                    throw new Exception(EXCEPTION);
                }
                else
                {
                    return null;
                }
            }
            return base.OpenFileWriteStream(path, fileSize, lastWriteTime);
        }

        public override void CloseFileWriteStream(
            string path,
            Stream stream,
            FileAttributes attributes,
            DateTime lastWriteTime)
        {
            if (!this.closeFile && ++closeFileCount == 2)
            {
                if (throwEx)
                {
                    throw new Exception(EXCEPTION);
                }
                return;
            }
            base.CloseFileWriteStream(path, stream, attributes, lastWriteTime);
        }
    }
}