PackageTest.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (c) 2008 Novell, Inc. (http://www.novell.com)
  21. //
  22. // Authors:
  23. // Alan McGovern ([email protected])
  24. //
  25. using System;
  26. using System.Collections.Generic;
  27. using System.IO;
  28. using System.IO.Packaging;
  29. using System.Linq;
  30. using System.Text;
  31. using NUnit.Framework;
  32. namespace MonoTests.System.IO.Packaging {
  33. [TestFixture]
  34. public class PackageTest : TestBase {
  35. //static void Main (string [] args)
  36. //{
  37. // PackageTest t = new PackageTest ();
  38. // t.FixtureSetup ();
  39. // t.Setup ();
  40. // t.RelationshipPartGetStream ();
  41. //}
  42. string path = "test.package";
  43. public override void Setup ()
  44. {
  45. if (File.Exists (path))
  46. File.Delete (path);
  47. }
  48. public override void TearDown ()
  49. {
  50. try {
  51. if (package != null)
  52. package.Close ();
  53. } catch {
  54. // FIXME: This shouldn't be required when i implement this
  55. }
  56. if (File.Exists (path))
  57. File.Delete (path);
  58. }
  59. [Test]
  60. public void CheckContentFile ()
  61. {
  62. MemoryStream stream = new MemoryStream ();
  63. package = Package.Open (stream, FileMode.Create, FileAccess.ReadWrite);
  64. package.CreatePart (uris[0], "custom/type");
  65. package.CreateRelationship (uris[1], TargetMode.External, "relType");
  66. package.Close ();
  67. package = Package.Open (new MemoryStream (stream.ToArray ()), FileMode.Open, FileAccess.ReadWrite);
  68. package.Close ();
  69. package = Package.Open (new MemoryStream (stream.ToArray ()), FileMode.Open, FileAccess.ReadWrite);
  70. Assert.AreEqual (2, package.GetParts ().Count (), "#1");
  71. Assert.AreEqual (1, package.GetRelationships ().Count (), "#2");
  72. }
  73. [Test]
  74. [ExpectedException (typeof (FileFormatException))]
  75. public void CorruptStream ()
  76. {
  77. stream = new FakeStream (true, true, true);
  78. stream.Write (new byte [1024], 0, 1024);
  79. package = Package.Open (stream);
  80. }
  81. [Test]
  82. [ExpectedException (typeof (NotSupportedException))]
  83. public void FileShareReadWrite ()
  84. {
  85. package = Package.Open (path, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
  86. }
  87. [Test]
  88. [ExpectedException (typeof (FileNotFoundException))]
  89. public void OpenNonExistantPath ()
  90. {
  91. package = Package.Open (path, FileMode.Open);
  92. }
  93. [Test]
  94. public void NonExistantPath ()
  95. {
  96. package = Package.Open (path);
  97. }
  98. [Test]
  99. public void PreExistingPath ()
  100. {
  101. package = Package.Open (path);
  102. package.Close ();
  103. package = Package.Open (path);
  104. }
  105. [Test]
  106. public void Close_FileStreamNotClosed ()
  107. {
  108. using (var stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.ReadWrite)) {
  109. var package = Package.Open (stream, FileMode.OpenOrCreate);
  110. package.CreatePart (uris [0], contentType);
  111. package.Close ();
  112. stream.Read (new byte [1024], 0, 1024);
  113. }
  114. }
  115. [Test]
  116. public void Close_MemoryStreamNotClosed ()
  117. {
  118. using (var stream = new MemoryStream ()) {
  119. var package = Package.Open (stream, FileMode.OpenOrCreate);
  120. package.CreatePart (uris [0], contentType);
  121. package.Close ();
  122. stream.Read (new byte [1024], 0, 1024);
  123. }
  124. }
  125. [Test]
  126. public void Close_Twice ()
  127. {
  128. var package = Package.Open (new MemoryStream (), FileMode.OpenOrCreate);
  129. package.Close ();
  130. package.Close ();
  131. }
  132. [Test]
  133. public void Dispose_Twice ()
  134. {
  135. var package = Package.Open (new MemoryStream (), FileMode.OpenOrCreate);
  136. ((IDisposable) package).Dispose ();
  137. ((IDisposable) package).Dispose ();
  138. }
  139. [Test]
  140. public void CreatePath ()
  141. {
  142. package = Package.Open (path, FileMode.Create);
  143. Assert.AreEqual (FileAccess.ReadWrite, package.FileOpenAccess, "#1");
  144. }
  145. [Test]
  146. [ExpectedException (typeof (ArgumentException))]
  147. public void CreatePathReadonly ()
  148. {
  149. package = Package.Open (path, FileMode.Create, FileAccess.Read);
  150. package.Close ();
  151. }
  152. [Test]
  153. public void CreatePathTwice ()
  154. {
  155. package = Package.Open (path, FileMode.Create);
  156. package.Close ();
  157. package = Package.Open (path, FileMode.Open);
  158. Assert.AreEqual (FileAccess.ReadWrite, package.FileOpenAccess);
  159. }
  160. [Test]
  161. public void OpenPackageMultipleTimes ()
  162. {
  163. var filename = Path.GetTempFileName ();
  164. try {
  165. using (var file = File.Open (filename, FileMode.OpenOrCreate)) {
  166. var package = Package.Open (file, FileMode.OpenOrCreate);
  167. var part = package.CreatePart (new Uri ("/test", UriKind.Relative), "test/type");
  168. using (var stream = part.GetStream ())
  169. stream.Write (new byte [1024 * 1024], 0, 1024 * 1024);
  170. package.Close ();
  171. }
  172. for (int i = 0; i < 10; i++) {
  173. using (var file = File.Open (filename, FileMode.OpenOrCreate))
  174. using (var package = Package.Open (file)) {
  175. package.GetParts ();
  176. package.GetRelationships ();
  177. }
  178. }
  179. } finally {
  180. if (File.Exists (filename))
  181. File.Delete (filename);
  182. }
  183. }
  184. [Test]
  185. public void OpenPathReadonly ()
  186. {
  187. package = Package.Open (path, FileMode.Create);
  188. package.CreatePart (uris[0], contentType);
  189. package.CreateRelationship (uris[1], TargetMode.External, "relType");
  190. package.Close ();
  191. package = Package.Open (path, FileMode.Open, FileAccess.Read);
  192. Assert.AreEqual (2, package.GetParts ().Count (), "#1");
  193. Assert.AreEqual (1, package.GetRelationships ().Count (), "#2");
  194. Assert.AreEqual (FileAccess.Read, package.FileOpenAccess, "Should be read access");
  195. try {
  196. package.CreatePart (uris [0], contentType);
  197. Assert.Fail ("Cannot modify a read-only package");
  198. } catch (IOException) {
  199. }
  200. try {
  201. package.CreateRelationship (uris [0], TargetMode.Internal, contentType);
  202. Assert.Fail ("Cannot modify a read-only package");
  203. } catch (IOException) {
  204. }
  205. try {
  206. package.DeletePart (uris [0]);
  207. Assert.Fail ("Cannot modify a read-only package");
  208. } catch (IOException) {
  209. }
  210. try {
  211. package.DeleteRelationship (contentType);
  212. Assert.Fail ("Cannot modify a read-only package");
  213. } catch (IOException) {
  214. }
  215. }
  216. [Test]
  217. [ExpectedException (typeof (ArgumentException))]
  218. public void ReadableStream ()
  219. {
  220. stream = new FakeStream (true, false, false);
  221. package = Package.Open (stream);
  222. }
  223. [Test]
  224. [ExpectedException (typeof (FileFormatException))]
  225. public void ReadableSeekableStream ()
  226. {
  227. stream = new FakeStream (true, false, true);
  228. package = Package.Open (stream);
  229. try {
  230. package.DeleteRelationship (contentType);
  231. Assert.Fail ("Cannot modify a read-only package");
  232. } catch (IOException) {
  233. }
  234. }
  235. [Test]
  236. [ExpectedException (typeof (ArgumentException))]
  237. public void ReadOnlyAccess ()
  238. {
  239. stream = new FakeStream (true, false, true);
  240. package = Package.Open (path, FileMode.CreateNew, FileAccess.Read);
  241. try {
  242. package.DeleteRelationship (contentType);
  243. Assert.Fail ("Cannot modify a read-only package");
  244. } catch (IOException) {
  245. }
  246. }
  247. [Test]
  248. [Category ("NotWorking")]
  249. [Ignore ("I'm not supposed to write to the relation stream unless i'm flushing")]
  250. public void RelationshipPartGetStream ()
  251. {
  252. package = Package.Open (path);
  253. package.CreateRelationship (uris [0], TargetMode.External, "rel");
  254. PackagePart p = package.GetPart (relationshipUri);
  255. Assert.IsNotNull (p, "#0");
  256. Stream s = p.GetStream ();
  257. Assert.AreEqual (0, s.Length, "#1");
  258. Assert.IsTrue (s.CanRead, "#2");
  259. Assert.IsTrue (s.CanSeek, "#3");
  260. Assert.IsFalse (s.CanTimeout, "#4");
  261. Assert.IsTrue (s.CanWrite, "#5");
  262. }
  263. [Test]
  264. [ExpectedException (typeof (IOException))]
  265. public void SetFileModeOnUnwriteableStream ()
  266. {
  267. stream = new FakeStream (true, false, true);
  268. package = Package.Open (stream, FileMode.Truncate);
  269. }
  270. [Test]
  271. [ExpectedException (typeof (NotSupportedException))]
  272. public void SetAppendOnWriteableStream ()
  273. {
  274. stream = new FakeStream (true, true, true);
  275. package = Package.Open (stream, FileMode.Append);
  276. }
  277. [Test]
  278. public void SetCreateNewOnWriteableStream ()
  279. {
  280. package = Package.Open (stream, FileMode.CreateNew);
  281. }
  282. [Test]
  283. [ExpectedException(typeof(IOException))]
  284. public void SetCreateNewOnWriteableStream2 ()
  285. {
  286. stream = new FakeStream (true, true, true);
  287. stream.Write (new byte [1000], 0, 1000);
  288. package = Package.Open (stream, FileMode.CreateNew);
  289. Assert.AreEqual (0, stream.Length, "#1");
  290. }
  291. [Test]
  292. public void SetCreateOnWriteableStream ()
  293. {
  294. stream = new FakeStream (true, true, true);
  295. package = Package.Open (stream, FileMode.Create);
  296. }
  297. [Test]
  298. [ExpectedException (typeof (FileFormatException))]
  299. public void SetOpenOnWriteableStream ()
  300. {
  301. stream = new FakeStream (true, true, true);
  302. package = Package.Open (stream, FileMode.Open);
  303. }
  304. [Test]
  305. public void SetOpenOrCreateOnWriteableStream ()
  306. {
  307. stream = new FakeStream (true, true, true);
  308. package = Package.Open (stream, FileMode.OpenOrCreate);
  309. }
  310. [Test]
  311. [ExpectedException (typeof (NotSupportedException))]
  312. public void SetTruncateOnWriteableStream ()
  313. {
  314. stream = new FakeStream (true, true, true);
  315. package = Package.Open (stream, FileMode.Truncate);
  316. }
  317. [Test]
  318. [ExpectedException (typeof (NotSupportedException))]
  319. public void SetTruncateOnWriteablePath ()
  320. {
  321. stream = new FakeStream (true, true, true);
  322. File.Create (path).Close ();
  323. package = Package.Open (path, FileMode.Truncate);
  324. }
  325. [Test]
  326. [ExpectedException (typeof (FileFormatException))]
  327. public void StreamOpen ()
  328. {
  329. stream = new FakeStream (true, true, true);
  330. package = Package.Open (stream, FileMode.Open);
  331. }
  332. [Test]
  333. public void StreamCreate ()
  334. {
  335. stream = new FakeStream (true, true, true);
  336. package = Package.Open (stream, FileMode.Create);
  337. }
  338. [Test]
  339. [ExpectedException (typeof (IOException))]
  340. public void UnusableStream ()
  341. {
  342. stream = new FakeStream (false, false, false);
  343. package = Package.Open (stream);
  344. }
  345. // Bug - I'm passing in FileAccess.Write but it thinks I've passed FileAccess.Read
  346. [Test]
  347. [ExpectedException (typeof (ArgumentException))]
  348. public void WriteAccessDoesntExist ()
  349. {
  350. package = Package.Open (path, FileMode.OpenOrCreate, FileAccess.Write);
  351. }
  352. [Test]
  353. public void ReadWriteAccessDoesntExist ()
  354. {
  355. package = Package.Open (path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
  356. }
  357. [Test]
  358. [ExpectedException (typeof (FileFormatException))]
  359. public void WriteOnlyAccessExists ()
  360. {
  361. File.Create (path).Close ();
  362. package = Package.Open (path, FileMode.OpenOrCreate, FileAccess.Write);
  363. }
  364. }
  365. }