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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
|
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Copyright (c) 2008 Novell, Inc. (http://www.novell.com)
//
// Authors:
// Alan McGovern (amcgovern@novell.com)
//
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.IO.Packaging;
using System.Linq;
using System.Text;
using NUnit.Framework;
namespace MonoTests.System.IO.Packaging {
[TestFixture]
public class PackageTest : TestBase {
//static void Main (string [] args)
//{
// PackageTest t = new PackageTest ();
// t.FixtureSetup ();
// t.Setup ();
// t.RelationshipPartGetStream ();
//}
string path = "test.package";
public override void Setup ()
{
if (File.Exists (path))
File.Delete (path);
}
public override void TearDown ()
{
try {
if (package != null)
package.Close ();
} catch {
// FIXME: This shouldn't be required when i implement this
}
if (File.Exists (path))
File.Delete (path);
}
[Test]
public void CheckContentFile ()
{
MemoryStream stream = new MemoryStream ();
package = Package.Open (stream, FileMode.Create, FileAccess.ReadWrite);
package.CreatePart (uris[0], "custom/type");
package.CreateRelationship (uris[1], TargetMode.External, "relType");
package.Close ();
package = Package.Open (new MemoryStream (stream.ToArray ()), FileMode.Open, FileAccess.ReadWrite);
package.Close ();
package = Package.Open (new MemoryStream (stream.ToArray ()), FileMode.Open, FileAccess.ReadWrite);
Assert.AreEqual (2, package.GetParts ().Count (), "#1");
Assert.AreEqual (1, package.GetRelationships ().Count (), "#2");
}
[Test]
[ExpectedException (typeof (FileFormatException))]
public void CorruptStream ()
{
stream = new FakeStream (true, true, true);
stream.Write (new byte [1024], 0, 1024);
package = Package.Open (stream);
}
[Test]
[ExpectedException (typeof (NotSupportedException))]
public void FileShareReadWrite ()
{
package = Package.Open (path, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
}
[Test]
[ExpectedException (typeof (FileNotFoundException))]
public void OpenNonExistantPath ()
{
package = Package.Open (path, FileMode.Open);
}
[Test]
public void NonExistantPath ()
{
package = Package.Open (path);
}
[Test]
public void PreExistingPath ()
{
package = Package.Open (path);
package.Close ();
package = Package.Open (path);
}
[Test]
public void Close_FileStreamNotClosed ()
{
using (var stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.ReadWrite)) {
var package = Package.Open (stream, FileMode.OpenOrCreate);
package.CreatePart (uris [0], contentType);
package.Close ();
stream.Read (new byte [1024], 0, 1024);
}
}
[Test]
public void Close_MemoryStreamNotClosed ()
{
using (var stream = new MemoryStream ()) {
var package = Package.Open (stream, FileMode.OpenOrCreate);
package.CreatePart (uris [0], contentType);
package.Close ();
stream.Read (new byte [1024], 0, 1024);
}
}
[Test]
public void Close_Twice ()
{
var package = Package.Open (new MemoryStream (), FileMode.OpenOrCreate);
package.Close ();
package.Close ();
}
[Test]
public void Dispose_Twice ()
{
var package = Package.Open (new MemoryStream (), FileMode.OpenOrCreate);
((IDisposable) package).Dispose ();
((IDisposable) package).Dispose ();
}
[Test]
public void CreatePath ()
{
package = Package.Open (path, FileMode.Create);
Assert.AreEqual (FileAccess.ReadWrite, package.FileOpenAccess, "#1");
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void CreatePathReadonly ()
{
package = Package.Open (path, FileMode.Create, FileAccess.Read);
package.Close ();
}
[Test]
public void CreatePathTwice ()
{
package = Package.Open (path, FileMode.Create);
package.Close ();
package = Package.Open (path, FileMode.Open);
Assert.AreEqual (FileAccess.ReadWrite, package.FileOpenAccess);
}
[Test]
public void OpenPackageMultipleTimes ()
{
var filename = Path.GetTempFileName ();
try {
using (var file = File.Open (filename, FileMode.OpenOrCreate)) {
var package = Package.Open (file, FileMode.OpenOrCreate);
var part = package.CreatePart (new Uri ("/test", UriKind.Relative), "test/type");
using (var stream = part.GetStream ())
stream.Write (new byte [1024 * 1024], 0, 1024 * 1024);
package.Close ();
}
for (int i = 0; i < 10; i++) {
using (var file = File.Open (filename, FileMode.OpenOrCreate))
using (var package = Package.Open (file)) {
package.GetParts ();
package.GetRelationships ();
}
}
} finally {
if (File.Exists (filename))
File.Delete (filename);
}
}
[Test]
public void OpenPathReadonly ()
{
package = Package.Open (path, FileMode.Create);
package.CreatePart (uris[0], contentType);
package.CreateRelationship (uris[1], TargetMode.External, "relType");
package.Close ();
package = Package.Open (path, FileMode.Open, FileAccess.Read);
Assert.AreEqual (2, package.GetParts ().Count (), "#1");
Assert.AreEqual (1, package.GetRelationships ().Count (), "#2");
Assert.AreEqual (FileAccess.Read, package.FileOpenAccess, "Should be read access");
try {
package.CreatePart (uris [0], contentType);
Assert.Fail ("Cannot modify a read-only package");
} catch (IOException) {
}
try {
package.CreateRelationship (uris [0], TargetMode.Internal, contentType);
Assert.Fail ("Cannot modify a read-only package");
} catch (IOException) {
}
try {
package.DeletePart (uris [0]);
Assert.Fail ("Cannot modify a read-only package");
} catch (IOException) {
}
try {
package.DeleteRelationship (contentType);
Assert.Fail ("Cannot modify a read-only package");
} catch (IOException) {
}
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void ReadableStream ()
{
stream = new FakeStream (true, false, false);
package = Package.Open (stream);
}
[Test]
[ExpectedException (typeof (FileFormatException))]
public void ReadableSeekableStream ()
{
stream = new FakeStream (true, false, true);
package = Package.Open (stream);
try {
package.DeleteRelationship (contentType);
Assert.Fail ("Cannot modify a read-only package");
} catch (IOException) {
}
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void ReadOnlyAccess ()
{
stream = new FakeStream (true, false, true);
package = Package.Open (path, FileMode.CreateNew, FileAccess.Read);
try {
package.DeleteRelationship (contentType);
Assert.Fail ("Cannot modify a read-only package");
} catch (IOException) {
}
}
[Test]
[Category ("NotWorking")]
[Ignore ("I'm not supposed to write to the relation stream unless i'm flushing")]
public void RelationshipPartGetStream ()
{
package = Package.Open (path);
package.CreateRelationship (uris [0], TargetMode.External, "rel");
PackagePart p = package.GetPart (relationshipUri);
Assert.IsNotNull (p, "#0");
Stream s = p.GetStream ();
Assert.AreEqual (0, s.Length, "#1");
Assert.IsTrue (s.CanRead, "#2");
Assert.IsTrue (s.CanSeek, "#3");
Assert.IsFalse (s.CanTimeout, "#4");
Assert.IsTrue (s.CanWrite, "#5");
}
[Test]
[ExpectedException (typeof (IOException))]
public void SetFileModeOnUnwriteableStream ()
{
stream = new FakeStream (true, false, true);
package = Package.Open (stream, FileMode.Truncate);
}
[Test]
[ExpectedException (typeof (NotSupportedException))]
public void SetAppendOnWriteableStream ()
{
stream = new FakeStream (true, true, true);
package = Package.Open (stream, FileMode.Append);
}
[Test]
public void SetCreateNewOnWriteableStream ()
{
package = Package.Open (stream, FileMode.CreateNew);
}
[Test]
[ExpectedException(typeof(IOException))]
public void SetCreateNewOnWriteableStream2 ()
{
stream = new FakeStream (true, true, true);
stream.Write (new byte [1000], 0, 1000);
package = Package.Open (stream, FileMode.CreateNew);
Assert.AreEqual (0, stream.Length, "#1");
}
[Test]
public void SetCreateOnWriteableStream ()
{
stream = new FakeStream (true, true, true);
package = Package.Open (stream, FileMode.Create);
}
[Test]
[ExpectedException (typeof (FileFormatException))]
public void SetOpenOnWriteableStream ()
{
stream = new FakeStream (true, true, true);
package = Package.Open (stream, FileMode.Open);
}
[Test]
public void SetOpenOrCreateOnWriteableStream ()
{
stream = new FakeStream (true, true, true);
package = Package.Open (stream, FileMode.OpenOrCreate);
}
[Test]
[ExpectedException (typeof (NotSupportedException))]
public void SetTruncateOnWriteableStream ()
{
stream = new FakeStream (true, true, true);
package = Package.Open (stream, FileMode.Truncate);
}
[Test]
[ExpectedException (typeof (NotSupportedException))]
public void SetTruncateOnWriteablePath ()
{
stream = new FakeStream (true, true, true);
File.Create (path).Close ();
package = Package.Open (path, FileMode.Truncate);
}
[Test]
[ExpectedException (typeof (FileFormatException))]
public void StreamOpen ()
{
stream = new FakeStream (true, true, true);
package = Package.Open (stream, FileMode.Open);
}
[Test]
public void StreamCreate ()
{
stream = new FakeStream (true, true, true);
package = Package.Open (stream, FileMode.Create);
}
[Test]
[ExpectedException (typeof (IOException))]
public void UnusableStream ()
{
stream = new FakeStream (false, false, false);
package = Package.Open (stream);
}
// Bug - I'm passing in FileAccess.Write but it thinks I've passed FileAccess.Read
[Test]
[ExpectedException (typeof (ArgumentException))]
public void WriteAccessDoesntExist ()
{
package = Package.Open (path, FileMode.OpenOrCreate, FileAccess.Write);
}
[Test]
public void ReadWriteAccessDoesntExist ()
{
package = Package.Open (path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
}
[Test]
[ExpectedException (typeof (FileFormatException))]
public void WriteOnlyAccessExists ()
{
File.Create (path).Close ();
package = Package.Open (path, FileMode.OpenOrCreate, FileAccess.Write);
}
[Test]
public void Check_ZipDateTime ()
{
using (var zipStream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.ReadWrite))
using (package = Package.Open (zipStream, FileMode.OpenOrCreate)) {
var part = package.CreatePart (new Uri ("/test", UriKind.Relative), "test/type");
using (var stream = part.GetStream ())
stream.Write (new byte [1024 * 1024], 0, 1024 * 1024);
}
using (var stream = new FileStream (path, FileMode.Open, FileAccess.Read))
using (var archive = new ZipArchive(stream))
{
foreach (var entry in archive.Entries)
{
Assert.AreEqual (DateTime.Now.Year, entry.LastWriteTime.Year);
Assert.AreEqual (DateTime.Now.Month, entry.LastWriteTime.Month);
Assert.AreEqual (DateTime.Now.Day, entry.LastWriteTime.Day);
}
}
}
}
}
|