[System] Try to fix Cookies tests
[mono.git] / mcs / class / System / Test / System.Threading / SemaphoreTest.cs
1 //
2 // SemaphoreTest.cs - Unit tests for System.Threading.Semaphore
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 #if NET_2_0
30
31 using NUnit.Framework;
32
33 using System;
34 using System.Security.AccessControl;
35 using System.Threading;
36
37 namespace MonoTests.System.Threading {
38
39         [TestFixture]
40         public class SemaphoreTest {
41
42                 [Test]
43                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
44                 public void Constructor_IntInt_NegativeInitialCount ()
45                 {
46                         new Semaphore (-1, 1);
47                 }
48
49                 [Test]
50                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
51                 public void Constructor_IntInt_ZeroMaximumCount ()
52                 {
53                         new Semaphore (0, 0);
54                 }
55
56                 [Test]
57                 [ExpectedException (typeof (ArgumentException))]
58                 public void Constructor_IntInt_InitialBiggerThanMaximum ()
59                 {
60                         new Semaphore (2, 1);
61                 }
62
63                 [Test]
64                 public void Constructor_IntInt ()
65                 {
66                         new Semaphore (1, 2);
67                 }
68
69                 [Test]
70                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
71                 public void Constructor_IntIntString_NegativeInitialCount ()
72                 {
73                         new Semaphore (-1, 1, "mono");
74                 }
75
76                 [Test]
77                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
78                 public void Constructor_IntIntString_ZeroMaximumCount ()
79                 {
80                         new Semaphore (0, 0, "mono");
81                 }
82
83                 [Test]
84                 [ExpectedException (typeof (ArgumentException))]
85                 public void Constructor_IntIntString_InitialBiggerThanMaximum ()
86                 {
87                         new Semaphore (2, 1, "mono");
88                 }
89
90                 [Test]
91                 public void Constructor_IntIntString_NullName ()
92                 {
93                         new Semaphore (0, 1, null);
94                 }
95
96                 [Test]
97                 public void Constructor_IntIntStringBool_NegativeInitialCount ()
98                 {
99                         bool created = true;
100                         try {
101                                 new Semaphore (-1, 1, "mono", out created);
102                         }
103                         catch (ArgumentOutOfRangeException) {
104                                 Assert.IsTrue (created, "Created");
105                         }
106                 }
107
108                 [Test]
109                 public void Constructor_IntIntStringBool_ZeroMaximumCount ()
110                 {
111                         bool created = true;
112                         try {
113                                 new Semaphore (0, 0, "mono", out created);
114                         }
115                         catch (ArgumentOutOfRangeException) {
116                                 Assert.IsTrue (created, "Created");
117                         }
118                 }
119
120                 [Test]
121                 public void Constructor_IntIntStringBool_InitialBiggerThanMaximum ()
122                 {
123                         bool created = true;
124                         try {
125                                 new Semaphore (2, 1, "mono", out created);
126                         }
127                         catch (ArgumentException) {
128                                 Assert.IsTrue (created, "Created");
129                         }
130                 }
131
132                 [Test]
133                 public void Constructor_IntIntStringBool_NullName ()
134                 {
135                         bool created = false;
136                         new Semaphore (0, 1, null, out created);
137                         Assert.IsTrue (created, "Created");
138                 }
139
140                 [Test]
141                 public void Constructor_IntIntStringBoolSecurity_NegativeInitialCount ()
142                 {
143                         bool created = true;
144                         try {
145                                 new Semaphore (-1, 1, "mono", out created, null);
146                         }
147                         catch (ArgumentOutOfRangeException) {
148                                 Assert.IsTrue (created, "Created");
149                         }
150                 }
151
152                 [Test]
153                 public void Constructor_IntIntStringBoolSecurity_ZeroMaximumCount ()
154                 {
155                         bool created = true;
156                         try {
157                                 new Semaphore (0, 0, "mono", out created, null);
158                         }
159                         catch (ArgumentOutOfRangeException) {
160                                 Assert.IsTrue (created, "Created");
161                         }
162                 }
163
164                 [Test]
165                 public void Constructor_IntIntStringBoolSecurity_InitialBiggerThanMaximum ()
166                 {
167                         bool created = true;
168                         try {
169                                 new Semaphore (2, 1, "mono", out created, null);
170                         }
171                         catch (ArgumentException) {
172                                 Assert.IsTrue (created, "Created");
173                         }
174                 }
175
176                 [Test]
177                 public void Constructor_IntIntStringBoolSecurity_NullName ()
178                 {
179                         bool created = false;
180                         new Semaphore (0, 1, null, out created, null);
181                         Assert.IsTrue (created, "Created");
182                 }
183
184                 [Test]
185                 public void Constructor_IntIntStringBoolSecurity ()
186                 {
187                         bool created = false;
188                         SemaphoreSecurity ss = new SemaphoreSecurity ();
189                         new Semaphore (0, 1, "secure", out created, ss);
190                         Assert.IsTrue (created, "Created");
191                 }
192
193                 [Test]
194                 [ExpectedException (typeof (ArgumentNullException))]
195                 public void OpenExisting_NullName ()
196                 {
197                         Semaphore.OpenExisting (null);
198                 }
199
200                 [Test]
201                 [ExpectedException (typeof (ArgumentException))]
202                 public void OpenExisting_EmptyName ()
203                 {
204                         Semaphore.OpenExisting (String.Empty);
205                 }
206
207                 [Test]
208                 [ExpectedException (typeof (ArgumentException))]
209                 public void OpenExisting_TooLongName ()
210                 {
211                         Semaphore.OpenExisting (new String (' ', 261));
212                 }
213
214                 [Test]
215                 [ExpectedException (typeof (WaitHandleCannotBeOpenedException))]
216                 public void OpenExisting_Unexisting ()
217                 {
218                         Semaphore.OpenExisting (new String ('a', 260));
219                 }
220
221                 [Test]
222                 [Category ("NotWorking")] // not implemented in Mono
223                 public void OpenExisting_BadRights ()
224                 {
225                         Semaphore s = new Semaphore (0, 1, "bad-rights");
226                         SemaphoreRights rights = (SemaphoreRights) Int32.MinValue;
227                         Semaphore existing = Semaphore.OpenExisting ("bad-rights", rights);
228                         // rights bits aren't validated
229                         Assert.IsNotNull (existing, "OpenExisting");
230                         Assert.IsFalse (Object.ReferenceEquals (s, existing), "!ref");
231                 }
232
233                 [Test]
234                 [Category ("NotWorking")] // not implemented in Mono
235                 public void AccessControl_Unnamed ()
236                 {
237                         Semaphore s = new Semaphore (0, 1, null);
238                         SemaphoreSecurity ss = s.GetAccessControl ();
239                         Assert.IsNotNull (ss, "GetAccessControl");
240                         s.SetAccessControl (ss);
241                 }
242
243                 [Test]
244                 [ExpectedException (typeof (ArgumentNullException))]
245                 [Category ("NotWorking")] // not implemented in Mono
246                 public void SetAccessControl_Null ()
247                 {
248                         Semaphore s = new Semaphore (0, 1, null);
249                         s.SetAccessControl (null);
250                 }
251
252                 [Test]
253                 public void Release ()
254                 {
255                         Semaphore s = new Semaphore (0, 1, null);
256                         s.Release ();
257                 }
258
259                 [Test]
260                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
261                 public void Release_Zero ()
262                 {
263                         Semaphore s = new Semaphore (0, 1, null);
264                         s.Release (0);
265                 }
266         }
267 }
268
269 #endif