2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / corlib / Test / System.Security.Policy / ApplicationDirectoryTest.cs
1 //
2 // ApplicationDirectoryTest.cs - NUnit Test Cases for ApplicationDirectory
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2004 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 using NUnit.Framework;
30 using System;
31 using System.IO;
32 using System.Security.Policy;
33 using System.Text;
34
35 namespace MonoTests.System.Security.Policy {
36
37         [TestFixture]
38         public class ApplicationDirectoryTest {
39
40                 private string Invalid (bool exception) 
41                 {
42                         StringBuilder sb = new StringBuilder ();
43                         foreach (char c in Path.InvalidPathChars)
44                                 sb.Append (c);
45                         if ((exception) && (sb.Length < 1))
46                                 throw new ArgumentException ("no invalid chars");
47                         return sb.ToString ();
48                 }
49
50                 [Test]
51                 [ExpectedException (typeof (ArgumentNullException))]
52                 public void ApplicationDirectory_Null ()
53                 {
54                         new ApplicationDirectory (null);
55                 }
56
57                 [Test]
58                 [ExpectedException (typeof (FormatException))]
59                 public void ApplicationDirectory_Empty ()
60                 {
61                         new ApplicationDirectory (String.Empty);
62                 }
63
64                 [Test]
65 #if !NET_2_0
66                 [ExpectedException (typeof (ArgumentException))]
67 #endif
68                 public void ApplicationDirectory_Invalid ()
69                 {
70                         new ApplicationDirectory (Invalid (false));
71                 }
72
73                 [Test]
74                 public void ApplicationDirectory_String ()
75                 {
76                         ApplicationDirectory ad = new ApplicationDirectory ("mono");
77 #if NET_2_0
78                         Assert.AreEqual ("mono", ad.Directory, "Directory");
79 #else
80                         Assert.AreEqual ("file://MONO", ad.Directory, "Directory");
81 #endif
82                 }
83
84                 [Test]
85                 public void ApplicationDirectory_FileUrl ()
86                 {
87                         ApplicationDirectory ad = new ApplicationDirectory ("file://MONO");
88                         Assert.AreEqual ("file://MONO", ad.Directory, "Directory");
89                 }
90
91                 [Test]
92                 public void ApplicationDirectory_HttpUrl ()
93                 {
94                         ApplicationDirectory ad = new ApplicationDirectory ("http://www.go-mono.com/");
95                         Assert.AreEqual ("http://www.go-mono.com/", ad.Directory, "Directory");
96                 }
97
98                 [Test]
99                 public void Copy ()
100                 {
101                         ApplicationDirectory ad = new ApplicationDirectory ("novell");
102 #if NET_2_0
103                         Assert.AreEqual ("novell", ad.Directory, "Directory");
104 #else
105                         Assert.AreEqual ("file://NOVELL", ad.Directory, "Directory");
106 #endif
107                         ApplicationDirectory copy = (ApplicationDirectory)ad.Copy ();
108                         Assert.IsTrue (ad.Equals (copy), "ad.Equals(copy)");
109                         Assert.IsTrue (copy.Equals (ad), "copy.Equals(ad)");
110                         Assert.IsFalse (Object.ReferenceEquals (ad, copy), "Copy");
111                         Assert.AreEqual (ad.GetHashCode (), copy.GetHashCode (), "GetHashCode");
112                         Assert.AreEqual (ad.ToString (), copy.ToString (), "ToString");
113                 }
114
115                 [Test]
116                 public void Equals ()
117                 {
118                         ApplicationDirectory ad1 = new ApplicationDirectory ("mono");
119                         Assert.IsFalse (ad1.Equals (null), "Equals(null)");
120                         Assert.IsFalse (ad1.Equals (String.Empty), "Equals(String.Empty)");
121                         Assert.IsFalse (ad1.Equals ("mono"), "Equals(mono)");
122                         Assert.IsTrue (ad1.Equals (ad1), "Equals(self)");
123                         ApplicationDirectory ad2 = new ApplicationDirectory (ad1.Directory);
124                         Assert.IsTrue (ad1.Equals (ad2), "Equals(ad2)");
125                         Assert.IsTrue (ad2.Equals (ad1), "Equals(ad2)");
126                         ApplicationDirectory ad3 = new ApplicationDirectory ("..");
127                         Assert.IsFalse (ad2.Equals (ad3), "Equals(ad3)");
128                 }
129
130                 [Test]
131                 public void GetHashCode_ ()
132                 {
133                         string linux = "/unix/path/mono";
134                         ApplicationDirectory ad1 = new ApplicationDirectory (linux);
135 #if NET_2_0
136                         Assert.AreEqual (linux, ad1.Directory);
137                         Assert.AreEqual (linux.GetHashCode (), ad1.GetHashCode (), "GetHashCode-Linux");
138 #else
139                         Assert.AreEqual ("file://UNIX/PATH/mono", ad1.Directory);
140                         Assert.AreEqual ("file://UNIX/PATH/mono".GetHashCode (), ad1.GetHashCode (), "GetHashCode-Linux");
141 #endif
142
143                         string windows = "\\windows\\path\\mono";
144                         ApplicationDirectory ad2 = new ApplicationDirectory (windows);
145 #if NET_2_0
146                         Assert.AreEqual (windows, ad2.Directory);
147                         Assert.AreEqual (windows.GetHashCode (), ad2.GetHashCode (), "GetHashCode-Windows");
148 #else
149                         Assert.AreEqual ("file://WINDOWS/PATH/mono", ad2.Directory);
150                         Assert.AreEqual ("file://WINDOWS/PATH/mono".GetHashCode (), ad2.GetHashCode (), "GetHashCode-Windows");
151 #endif
152                         Assert.IsFalse ((ad1.GetHashCode () == ad2.GetHashCode ()), "GetHashCode-Compare");
153                 }
154
155                 [Test]
156                 public void ToString_ ()
157                 {
158                         ApplicationDirectory ad = new ApplicationDirectory ("file://MONO");
159                         string ts = ad.ToString ();
160                         Assert.IsTrue (ts.StartsWith ("<System.Security.Policy.ApplicationDirectory"), "Tag");
161                         Assert.IsTrue (ts.IndexOf ("version=\"1\"") > 0, "Directory");
162                         Assert.IsTrue (ts.IndexOf ("<Directory>file://MONO</Directory>") > 0, "Directory");
163                 }
164
165 #if NET_2_0
166                 [Test]
167                 [ExpectedException (typeof (ArgumentException))]
168                 public void Equals_Invalid ()
169                 {
170                         // funny one
171                         string appdir = Invalid (true);
172                         // constructor is ok with an invalid path...
173                         ApplicationDirectory ad = new ApplicationDirectory (appdir);
174                         // we can copy it...
175                         ApplicationDirectory copy = (ApplicationDirectory)ad.Copy ();
176                         // we can't get it's hash code
177                         Assert.AreEqual (appdir.GetHashCode (), ad.GetHashCode (), "GetHashCode");
178                         // we can convert it to string...
179                         Assert.IsTrue (ad.ToString ().IndexOf (appdir) > 0, "ToString");
180                         // ... but it throws in Equals - with self!
181                         Assert.IsTrue (ad.Equals (ad), "Equals(self)");
182                 }
183
184                 [Test]
185                 [ExpectedException (typeof (ArgumentException))]
186                 public void ToString_Invalid ()
187                 {
188                         new ApplicationDirectory (Invalid (true)).ToString ();
189                 }
190 #endif
191         }
192 }