| 1 |
/* |
| 2 |
* Copyright 1999-2010 University of Chicago |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 |
* express or implied. |
| 12 |
* |
| 13 |
* See the License for the specific language governing permissions and limitations under the License. |
| 14 |
*/ |
| 15 |
package org.globus.gsi.gridmap; |
| 16 |
|
| 17 |
import org.globus.gsi.gridmap.GridMap; |
| 18 |
import org.globus.gsi.gridmap.GridMapLocal; |
| 19 |
|
| 20 |
import java.io.ByteArrayInputStream; |
| 21 |
import java.io.IOException; |
| 22 |
import junit.framework.TestCase; |
| 23 |
import org.apache.commons.logging.Log; |
| 24 |
import org.apache.commons.logging.LogFactory; |
| 25 |
|
| 26 |
public class GridMapTest extends TestCase { |
| 27 |
|
| 28 |
private Log logger = LogFactory.getLog(GridMapTest.class); |
| 29 |
|
| 30 |
private static final String GRIDMAP_1 = "\"/O=Grid/O=Globus/OU=mcs.anl.gov/CN=Katarzyna (Kate) Keahey\" keahey,kate\r\n" |
| 31 |
+ "# this is a comment\r\n" |
| 32 |
+ " \r\n" |
| 33 |
+ "\"/O=Grid/O=Globus/OU=mcs.anl.gov/CN=Michael Dvorak\" dvorak2\r\n" |
| 34 |
+ "\"/O=Grid/O=Globus/OU=mcs.anl.gov/CN=Michael Dvorak\" dvorak\n" |
| 35 |
+ "\"/DC=org/DC=doe grids/OU=People/UID=32845324/[email protected]/[email protected]\" jdoe,doej\n" |
| 36 |
+ "\"/DC=org/DC=doe grids/OU=People/UID=3284532/USERID=7878/UID=8989/[email protected]\" john_doe\n" |
| 37 |
+ "\"/DC=org/DC=doe grids/OU=People/UID=32845/[email protected]/[email protected]\" doe\n" |
| 38 |
+ "\"/DC=org/DC=doegrids/OU=Services/Email=admin@mcs/CN=host/effable.mcs.anl.gov\" host\n" |
| 39 |
+ "user1DN user1\n" |
| 40 |
+ "\ttestDN\tuser1\n"; |
| 41 |
|
| 42 |
private static final String GRIDMAP_2 = "\"/O=Grid/O=Globus/OU=mcs.anl.gov/CN=Katarzyna (Kate) Keahey\"\r\n" |
| 43 |
+ "# this is a comment\r\n" + " \r\n" |
| 44 |
+ "\"/O=Grid/O=Globus/OU=mcs.anl.gov/CN=Michael Dvorak\" dvorak\r\n" + "dvorak\n"; |
| 45 |
|
| 46 |
private static final String GRIDMAP_3 = "\"/O=myCA/CN=\\\"Foo Bar\\\"\" account\r\n"; |
| 47 |
|
| 48 |
private static final String GRIDMAP_4 = "/C=DE/ST=Baden-Württemberg/O=University of Stuttgart/OU=HLRS/CN=Oliver Mangold/[email protected] test1"; |
| 49 |
|
| 50 |
public void test1() { |
| 51 |
|
| 52 |
GridMap map = new GridMap(); |
| 53 |
|
| 54 |
try { |
| 55 |
map.load(new ByteArrayInputStream(GRIDMAP_4.getBytes())); |
| 56 |
logger.debug("Test 1 " + map.getUserID("test1")); |
| 57 |
assertTrue(map.load(new ByteArrayInputStream(GRIDMAP_1.getBytes()))); |
| 58 |
} catch (IOException e) { |
| 59 |
fail(e.getMessage()); |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
assertEquals("user1", map.getUserID("user1DN")); |
| 64 |
assertEquals("user1", map.getUserID("testDN")); |
| 65 |
assertEquals("keahey", map.getUserID("/O=Grid/O=Globus/OU=mcs.anl.gov/CN=Katarzyna (Kate) Keahey")); |
| 66 |
|
| 67 |
assertEquals("dvorak2", map.getUserID("/O=Grid/O=Globus/OU=mcs.anl.gov/CN=Michael Dvorak")); |
| 68 |
|
| 69 |
assertEquals(null, map.getUserID("/O=Grid/O=Globus/OU=mcs.anl.gov/CN=Jarek Gawor")); |
| 70 |
|
| 71 |
assertEquals("jdoe", map |
| 72 |
.getUserID("/DC=org/DC=doe grids/OU=People/UID=32845324/[email protected]/[email protected]")); |
| 73 |
|
| 74 |
assertEquals("jdoe", map |
| 75 |
.getUserID("/DC=org/DC=doe grids/OU=People/USERID=32845324/[email protected]/[email protected]")); |
| 76 |
|
| 77 |
assertEquals("john_doe", map |
| 78 |
.getUserID("/DC=org/DC=doe grids/OU=People/UID=3284532/USERID=7878/UID=8989/[email protected]")); |
| 79 |
|
| 80 |
assertEquals("john_doe", map |
| 81 |
.getUserID("/DC=org/DC=doe grids/OU=People/UID=3284532/UID=7878/UID=8989/[email protected]")); |
| 82 |
|
| 83 |
assertEquals("john_doe", map |
| 84 |
.getUserID("/DC=org/DC=doe grids/OU=People/UID=3284532/USERID=7878/UID=8989/[email protected]")); |
| 85 |
|
| 86 |
assertEquals("doe", map |
| 87 |
.getUserID("/DC=org/DC=doe grids/OU=People/UID=32845/[email protected]/[email protected]")); |
| 88 |
|
| 89 |
assertEquals("doe", map |
| 90 |
.getUserID("/DC=org/DC=doe grids/OU=People/UID=32845/[email protected]/[email protected]")); |
| 91 |
|
| 92 |
assertEquals("host", map |
| 93 |
.getUserID("/DC=org/DC=doegrids/OU=Services/Email=admin@mcs/CN=host/effable.mcs.anl.gov")); |
| 94 |
|
| 95 |
String[] rs = null; |
| 96 |
String[] expected = null; |
| 97 |
|
| 98 |
rs = map.getUserIDs("/DC=org/DC=doe grids/OU=People/UID=32845324/[email protected]/[email protected]"); |
| 99 |
expected = new String[] { "jdoe", "doej" }; |
| 100 |
|
| 101 |
assertEquals(expected.length, rs.length); |
| 102 |
for (int i = 0; i < expected.length; i++) { |
| 103 |
assertEquals(expected[i], rs[i]); |
| 104 |
} |
| 105 |
|
| 106 |
rs = map.getUserIDs("/O=Grid/O=Globus/OU=mcs.anl.gov/CN=Katarzyna (Kate) Keahey"); |
| 107 |
expected = new String[] { "keahey", "kate" }; |
| 108 |
|
| 109 |
assertEquals(expected.length, rs.length); |
| 110 |
for (int i = 0; i < expected.length; i++) { |
| 111 |
assertEquals(expected[i], rs[i]); |
| 112 |
} |
| 113 |
|
| 114 |
rs = map.getUserIDs("/O=Grid/O=Globus/OU=mcs.anl.gov/CN=Michael Dvorak"); |
| 115 |
expected = new String[] { "dvorak2", "dvorak" }; |
| 116 |
|
| 117 |
assertEquals(expected.length, rs.length); |
| 118 |
for (int i = 0; i < expected.length; i++) { |
| 119 |
assertEquals(expected[i], rs[i]); |
| 120 |
} |
| 121 |
|
| 122 |
} |
| 123 |
|
| 124 |
public void testLocalLookup() { |
| 125 |
GridMap map = new GridMapLocal(); |
| 126 |
|
| 127 |
String username = System.getProperty("user.name"); |
| 128 |
|
| 129 |
assertEquals(username, map.getUserID("whatever")); |
| 130 |
|
| 131 |
System.setProperty("user.name", "ROot"); |
| 132 |
|
| 133 |
assertEquals(null, map.getUserID("whatever")); |
| 134 |
|
| 135 |
System.setProperty("user.name", "AdminISTRATOR"); |
| 136 |
|
| 137 |
assertEquals(null, map.getUserID("whatever")); |
| 138 |
|
| 139 |
System.setProperty("user.name", username); |
| 140 |
|
| 141 |
assertEquals(username, map.getUserID("whatever2")); |
| 142 |
} |
| 143 |
|
| 144 |
public void test3() { |
| 145 |
|
| 146 |
GridMap map = new GridMap(); |
| 147 |
|
| 148 |
try { |
| 149 |
map.load(new ByteArrayInputStream(GRIDMAP_1.getBytes())); |
| 150 |
} catch (IOException e) { |
| 151 |
fail(e.getMessage()); |
| 152 |
return; |
| 153 |
} |
| 154 |
|
| 155 |
assertEquals(true, map.checkUser("/O=Grid/O=Globus/OU=mcs.anl.gov/CN=Katarzyna (Kate) Keahey", "keahey")); |
| 156 |
|
| 157 |
assertEquals(true, map.checkUser("/O=Grid/O=Globus/OU=mcs.anl.gov/CN=Katarzyna (Kate) Keahey", "kate")); |
| 158 |
|
| 159 |
assertEquals(true, map.checkUser("/O=Grid/O=Globus/OU=mcs.anl.gov/CN=Michael Dvorak", "dvorak")); |
| 160 |
|
| 161 |
assertEquals(false, map.checkUser("/O=Grid/O=Globus/OU=mcs.anl.gov/CN=Michael Dvorak", "dvorakkkkk")); |
| 162 |
|
| 163 |
assertEquals(false, map.checkUser("/O=Grid/O=Globus/OU=mcs.anl.gov/CN=Michael Dvorakkkk", "dvorak")); |
| 164 |
|
| 165 |
assertEquals(false, map.checkUser("/O=Grid/O=Globus/OU=mcs.anl.gov/CN=Jarek Gawor", "gawor")); |
| 166 |
} |
| 167 |
|
| 168 |
public void test4() { |
| 169 |
|
| 170 |
GridMap map = new GridMap(); |
| 171 |
|
| 172 |
try { |
| 173 |
map.load(new ByteArrayInputStream(GRIDMAP_1.getBytes())); |
| 174 |
} catch (IOException e) { |
| 175 |
fail(e.getMessage()); |
| 176 |
return; |
| 177 |
} |
| 178 |
|
| 179 |
assertEquals("/O=Grid/O=Globus/OU=mcs.anl.gov/CN=Michael Dvorak", map.getGlobusID("dvorak")); |
| 180 |
|
| 181 |
assertEquals("/O=Grid/O=Globus/OU=mcs.anl.gov/CN=Katarzyna (Kate) Keahey", map.getGlobusID("keahey")); |
| 182 |
|
| 183 |
assertEquals("/O=Grid/O=Globus/OU=mcs.anl.gov/CN=Katarzyna (Kate) Keahey", map.getGlobusID("kate")); |
| 184 |
|
| 185 |
assertEquals(null, map.getGlobusID("gawor")); |
| 186 |
|
| 187 |
} |
| 188 |
|
| 189 |
public void test5() { |
| 190 |
|
| 191 |
GridMap map = new GridMap(); |
| 192 |
|
| 193 |
try { |
| 194 |
map.load(new ByteArrayInputStream(GRIDMAP_1.getBytes())); |
| 195 |
} catch (IOException e) { |
| 196 |
fail(e.getMessage()); |
| 197 |
return; |
| 198 |
} |
| 199 |
|
| 200 |
map.map("/O=Grid/O=Globus/OU=mcs.anl.gov/CN=Jarek Gawor", "gawor"); |
| 201 |
|
| 202 |
assertEquals("gawor", map.getUserID("/O=Grid/O=Globus/OU=mcs.anl.gov/CN=Jarek Gawor")); |
| 203 |
|
| 204 |
map.map("/O=Grid/O=Globus/OU=mcs.anl.gov/CN=Michael Dvorak", "dvorak2"); |
| 205 |
|
| 206 |
assertEquals(true, map.checkUser("/O=Grid/O=Globus/OU=mcs.anl.gov/CN=Michael Dvorak", "dvorak")); |
| 207 |
|
| 208 |
assertEquals(true, map.checkUser("/O=Grid/O=Globus/OU=mcs.anl.gov/CN=Michael Dvorak", "dvorak2")); |
| 209 |
|
| 210 |
} |
| 211 |
|
| 212 |
public void testIgnoreErrors() { |
| 213 |
|
| 214 |
GridMap map = new GridMap(); |
| 215 |
|
| 216 |
try { |
| 217 |
map.load(new ByteArrayInputStream(GRIDMAP_2.getBytes())); |
| 218 |
fail("did not throw exception"); |
| 219 |
} catch (IOException e) { |
| 220 |
} |
| 221 |
|
| 222 |
assertTrue(map.getUserID("/O=Grid/O=Globus/OU=mcs.anl.gov/CN=Michael Dvorak") == null); |
| 223 |
|
| 224 |
map.setIgnoreErrors(false); |
| 225 |
|
| 226 |
try { |
| 227 |
map.load(new ByteArrayInputStream(GRIDMAP_2.getBytes())); |
| 228 |
fail("did not throw exception"); |
| 229 |
} catch (IOException e) { |
| 230 |
} |
| 231 |
|
| 232 |
assertTrue(map.getUserID("/O=Grid/O=Globus/OU=mcs.anl.gov/CN=Michael Dvorak") == null); |
| 233 |
|
| 234 |
map.setIgnoreErrors(true); |
| 235 |
|
| 236 |
try { |
| 237 |
assertFalse(map.load(new ByteArrayInputStream(GRIDMAP_2.getBytes()))); |
| 238 |
} catch (IOException e) { |
| 239 |
e.printStackTrace(); |
| 240 |
fail("Unexpected error"); |
| 241 |
} |
| 242 |
|
| 243 |
assertEquals("dvorak", map.getUserID("/O=Grid/O=Globus/OU=mcs.anl.gov/CN=Michael Dvorak")); |
| 244 |
} |
| 245 |
|
| 246 |
public void test6() throws Exception { |
| 247 |
|
| 248 |
GridMap map = new GridMap(); |
| 249 |
|
| 250 |
try { |
| 251 |
map.load(new ByteArrayInputStream(GRIDMAP_3.getBytes())); |
| 252 |
} catch (IOException e) { |
| 253 |
fail(e.getMessage()); |
| 254 |
return; |
| 255 |
} |
| 256 |
|
| 257 |
assertTrue((map.getAllGlobusID("account").length) == 1); |
| 258 |
String[] globusId = map.getAllGlobusID("account"); |
| 259 |
logger.debug("** " + globusId[0]); |
| 260 |
assertEquals("account", map.getUserID("/O=myCA/CN=\"Foo Bar\"")); |
| 261 |
} |
| 262 |
} |