Merge pull request #1508 from slluis/fix-20966
[mono.git] / mcs / class / System / Test / System.IO.Compression / DeflateStreamCas.cs
1 //
2 // DeflateStreamCas.cs -CAS unit tests for System.IO.Compression.DeflateStream
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29
30 using NUnit.Framework;
31
32 using System;
33 using System.IO;
34 using System.IO.Compression;
35 using System.Security;
36 using System.Security.Permissions;
37 using System.Threading;
38
39 namespace MonoCasTests.System.IO.Compression {
40
41         [TestFixture]
42         [Category ("CAS")]
43         public class DeflateStreamCas {
44
45                 private const int timeout = 30000;
46                 private string message;
47
48                 static ManualResetEvent reset;
49
50                 [TestFixtureSetUp]
51                 public void FixtureSetUp ()
52                 {
53                         // this occurs with a "clean" stack (full trust)
54                         reset = new ManualResetEvent (false);
55                 }
56
57                 [TestFixtureTearDown]
58                 public void FixtureTearDown ()
59                 {
60                         reset.Close ();
61                 }
62
63                 [SetUp]
64                 public void SetUp ()
65                 {
66                         if (!SecurityManager.SecurityEnabled)
67                                 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
68                 }
69
70                 // async tests (for stack propagation)
71
72                 private void ReadCallback (IAsyncResult ar)
73                 {
74                         DeflateStream s = (DeflateStream)ar.AsyncState;
75                         s.EndRead (ar);
76                         try {
77                                 // can we do something bad here ?
78                                 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
79                                 message = "Expected a SecurityException";
80                         }
81                         catch (SecurityException) {
82                                 message = null;
83                                 reset.Set ();
84                         }
85                         catch (Exception e) {
86                                 message = e.ToString ();
87                         }
88                 }
89
90                 [Test]
91                 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
92                 public void AsyncRead ()
93                 {
94                         DeflateStream cs = new DeflateStream (new MemoryStream (), CompressionMode.Decompress);
95                         message = "AsyncRead";
96                         reset.Reset ();
97                         IAsyncResult r = cs.BeginRead (new byte[0], 0, 0, new AsyncCallback (ReadCallback), cs);
98                         Assert.IsNotNull (r, "IAsyncResult");
99                         if (!reset.WaitOne (timeout, true))
100                                 Assert.Ignore ("Timeout");
101                         Assert.IsNull (message, message);
102                         cs.Close ();
103                 }
104
105                 private void WriteCallback (IAsyncResult ar)
106                 {
107                         DeflateStream s = (DeflateStream)ar.AsyncState;
108                         s.EndWrite (ar);
109                         try {
110                                 // can we do something bad here ?
111                                 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
112                                 message = "Expected a SecurityException";
113                         }
114                         catch (SecurityException) {
115                                 message = null;
116                                 reset.Set ();
117                         }
118                         catch (Exception e) {
119                                 message = e.ToString ();
120                         }
121                 }
122
123                 [Test]
124                 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
125                 public void AsyncWrite ()
126                 {
127                         DeflateStream cs = new DeflateStream (new MemoryStream (), CompressionMode.Compress);
128                         message = "AsyncWrite";
129                         reset.Reset ();
130                         IAsyncResult r = cs.BeginWrite (new byte[1], 0, 1, new AsyncCallback (WriteCallback), cs);
131                         Assert.IsNotNull (r, "IAsyncResult");
132                         if (!reset.WaitOne (timeout, true))
133                                 Assert.Ignore ("Timeout");
134                         Assert.IsNull (message, message);
135 // the Close is currently buggy in Mono
136 //                      cs.Close ();
137                 }
138         }
139 }
140