| 1 | // 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. |
| 2 | |
| 3 | using System; |
| 4 | using System.IO; |
| 5 | using System.Collections.Generic; |
| 6 | using WixToolset.Dtf.Compression; |
| 7 | |
| 8 | namespace WixToolset.Dtf.Test |
| 9 | { |
| 10 | public class MisbehavingStreamContext : ArchiveFileStreamContext |
| 11 | { |
| 12 | public const string EXCEPTION = "Test exception."; |
| 13 | |
| 14 | private bool throwEx; |
| 15 | private bool getName; |
| 16 | private bool openArchive; |
| 17 | private bool closeArchive; |
| 18 | private bool openFile; |
| 19 | private bool closeFile; |
| 20 | private int closeFileCount; |
| 21 | |
| 22 | public MisbehavingStreamContext( |
| 23 | string cabinetFile, |
| 24 | string directory, |
| 25 | IDictionary<string, string> files, |
| 26 | bool throwEx, |
| 27 | bool getName, |
| 28 | bool openArchive, |
| 29 | bool closeArchive, |
| 30 | bool openFile, |
| 31 | bool closeFile) |
| 32 | : base(cabinetFile, directory, files) |
| 33 | { |
| 34 | this.throwEx = throwEx; |
| 35 | this.getName = getName; |
| 36 | this.openArchive = openArchive; |
| 37 | this.closeArchive = closeArchive; |
| 38 | this.openFile = openFile; |
| 39 | this.closeFile = closeFile; |
| 40 | } |
| 41 | |
| 42 | public override string GetArchiveName(int archiveNumber) |
| 43 | { |
| 44 | if (!this.getName) |
| 45 | { |
| 46 | if (throwEx) |
| 47 | { |
| 48 | throw new Exception(EXCEPTION); |
| 49 | } |
| 50 | else |
| 51 | { |
| 52 | return null; |
| 53 | } |
| 54 | } |
| 55 | return base.GetArchiveName(archiveNumber); |
| 56 | } |
| 57 | |
| 58 | public override Stream OpenArchiveWriteStream( |
| 59 | int archiveNumber, |
| 60 | string archiveName, |
| 61 | bool truncate, |
| 62 | CompressionEngine compressionEngine) |
| 63 | { |
| 64 | if (!this.openArchive) |
| 65 | { |
| 66 | if (throwEx) |
| 67 | { |
| 68 | throw new Exception(EXCEPTION); |
| 69 | } |
| 70 | else |
| 71 | { |
| 72 | return null; |
| 73 | } |
| 74 | } |
| 75 | return base.OpenArchiveWriteStream( |
| 76 | archiveNumber, archiveName, truncate, compressionEngine); |
| 77 | } |
| 78 | |
| 79 | public override void CloseArchiveWriteStream( |
| 80 | int archiveNumber, |
| 81 | string archiveName, |
| 82 | Stream stream) |
| 83 | { |
| 84 | if (!this.closeArchive) |
| 85 | { |
| 86 | if (throwEx) |
| 87 | { |
| 88 | this.closeArchive = true; |
| 89 | throw new Exception(EXCEPTION); |
| 90 | } |
| 91 | return; |
| 92 | } |
| 93 | base.CloseArchiveWriteStream(archiveNumber, archiveName, stream); |
| 94 | } |
| 95 | |
| 96 | public override Stream OpenFileReadStream( |
| 97 | string path, |
| 98 | out FileAttributes attributes, |
| 99 | out DateTime lastWriteTime) |
| 100 | { |
| 101 | if (!this.openFile) |
| 102 | { |
| 103 | if (throwEx) |
| 104 | { |
| 105 | throw new Exception(EXCEPTION); |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | attributes = FileAttributes.Normal; |
| 110 | lastWriteTime = DateTime.MinValue; |
| 111 | return null; |
| 112 | } |
| 113 | } |
| 114 | return base.OpenFileReadStream(path, out attributes, out lastWriteTime); |
| 115 | } |
| 116 | |
| 117 | public override void CloseFileReadStream(string path, Stream stream) |
| 118 | { |
| 119 | if (!this.closeFile && ++closeFileCount == 2) |
| 120 | { |
| 121 | if (throwEx) |
| 122 | { |
| 123 | throw new Exception(EXCEPTION); |
| 124 | } |
| 125 | return; |
| 126 | } |
| 127 | base.CloseFileReadStream(path, stream); |
| 128 | } |
| 129 | |
| 130 | public override Stream OpenArchiveReadStream( |
| 131 | int archiveNumber, |
| 132 | string archiveName, |
| 133 | CompressionEngine compressionEngine) |
| 134 | { |
| 135 | if (!this.openArchive) |
| 136 | { |
| 137 | if (throwEx) |
| 138 | { |
| 139 | throw new Exception(EXCEPTION); |
| 140 | } |
| 141 | else |
| 142 | { |
| 143 | return null; |
| 144 | } |
| 145 | } |
| 146 | return base.OpenArchiveReadStream(archiveNumber, archiveName, compressionEngine); |
| 147 | } |
| 148 | |
| 149 | public override void CloseArchiveReadStream( |
| 150 | int archiveNumber, |
| 151 | string archiveName, |
| 152 | Stream stream) |
| 153 | { |
| 154 | if (!this.closeArchive) |
| 155 | { |
| 156 | if (throwEx) |
| 157 | { |
| 158 | this.closeArchive = true; |
| 159 | throw new Exception(EXCEPTION); |
| 160 | } |
| 161 | return; |
| 162 | } |
| 163 | base.CloseArchiveReadStream(archiveNumber, archiveName, stream); |
| 164 | } |
| 165 | |
| 166 | public override Stream OpenFileWriteStream( |
| 167 | string path, |
| 168 | long fileSize, |
| 169 | DateTime lastWriteTime) |
| 170 | { |
| 171 | if (!this.openFile) |
| 172 | { |
| 173 | if (throwEx) |
| 174 | { |
| 175 | throw new Exception(EXCEPTION); |
| 176 | } |
| 177 | else |
| 178 | { |
| 179 | return null; |
| 180 | } |
| 181 | } |
| 182 | return base.OpenFileWriteStream(path, fileSize, lastWriteTime); |
| 183 | } |
| 184 | |
| 185 | public override void CloseFileWriteStream( |
| 186 | string path, |
| 187 | Stream stream, |
| 188 | FileAttributes attributes, |
| 189 | DateTime lastWriteTime) |
| 190 | { |
| 191 | if (!this.closeFile && ++closeFileCount == 2) |
| 192 | { |
| 193 | if (throwEx) |
| 194 | { |
| 195 | throw new Exception(EXCEPTION); |
| 196 | } |
| 197 | return; |
| 198 | } |
| 199 | base.CloseFileWriteStream(path, stream, attributes, lastWriteTime); |
| 200 | } |
| 201 | } |
| 202 | } |