1 2 package jsdsi; 3 4 import jsdsi.ExprTag; 5 import jsdsi.PrefixTag; 6 import jsdsi.RangeTag; 7 import jsdsi.SetTag; 8 import jsdsi.SimpleTag; 9 import jsdsi.StringTag; 10 import jsdsi.Tag; 11 import junit.framework.Test; 12 import junit.framework.TestCase; 13 import junit.framework.TestSuite; 14 15 /*** 16 * Tests tag equality, implication, and intersection. 17 * 18 * @author Sameer Ajmani 19 * @author Sean Radford 20 * @version $Revision: 1.5.6.1 $ $Date: 2005/11/08 03:12:52 $ 21 */ 22 public class TagTest extends TestCase 23 { 24 // Note: if you add a new Tag here, you need to add it to the tags 25 // array below. 26 static StringTag emptyString = new StringTag(""); 27 static StringTag readString = new StringTag("read"); 28 static StringTag writeString = new StringTag("write"); 29 static StringTag fileString = new StringTag("/my/private/file"); 30 static StringTag myString = new StringTag("/my"); 31 static StringTag myPublicString = new StringTag("/my/public"); 32 static StringTag myPrivateString = new StringTag("/my/private"); 33 34 static PrefixTag emptyPrefix = new PrefixTag(""); 35 static PrefixTag myPrefix = new PrefixTag("/my"); 36 static PrefixTag myPublicPrefix = new PrefixTag("/my/public"); 37 static PrefixTag myPrivatePrefix = new PrefixTag("/my/private"); 38 39 static ReversePrefixTag emptyRPrefix = new ReversePrefixTag(""); 40 static ReversePrefixTag myRPrefix = new ReversePrefixTag("/my"); 41 static ReversePrefixTag myPublicRPrefix = new ReversePrefixTag("/my/public"); 42 static ReversePrefixTag myPrivateRPrefix = new ReversePrefixTag("/my/private"); 43 44 static SetTag readWriteSet = new SetTag 45 (new ExprTag[] { readString, writeString }); 46 static SetTag writeReadSet = new SetTag 47 (new ExprTag[] { writeString, readString }); 48 static SetTag myPublicPrivatePrefixSet = new SetTag 49 (new ExprTag[] { myPublicPrefix, myPrivatePrefix }); 50 51 static SimpleTag grant = new SimpleTag("grant", new ExprTag[] { }); 52 static SimpleTag grunt = new SimpleTag("grunt", new ExprTag[] { }); 53 static SimpleTag grantRead = new SimpleTag 54 ("grant", new ExprTag[] { readString }); 55 static SimpleTag grantReadFile = new SimpleTag 56 ("grant", new ExprTag[] { readString, fileString }); 57 static SimpleTag grantFileRead = new SimpleTag 58 ("grant", new ExprTag[] { fileString, readString }); 59 static SimpleTag grantWriteFile = new SimpleTag 60 ("grant", new ExprTag[] { writeString, fileString }); 61 static SimpleTag grantReadWriteFile = new SimpleTag 62 ("grant", new ExprTag[] { readWriteSet, fileString }); 63 64 static RangeTag leGeAlphaRange = new RangeTag 65 ("alpha", "write", false, "read", false); 66 static RangeTag leGtAlphaRange = new RangeTag 67 ("alpha", "write", false, "read", true); 68 static RangeTag ltGeAlphaRange = new RangeTag 69 ("alpha", "write", true, "read", false); 70 static RangeTag ltGtAlphaRange = new RangeTag 71 ("alpha", "write", true, "read", true); 72 73 // TODO: the following RangeTag types need more tests 74 static StringTag numericString = new StringTag("123.45"); 75 static RangeTag leGeNumericRange = new RangeTag 76 ("numeric", "123.99", false, "123.11", false); 77 78 static StringTag dateString = new StringTag("2004-01-21_18:30:23"); 79 static RangeTag leGeDateRange = new RangeTag 80 ("date", "3010-01-01_00:00:00", false, "1976-07-03_07:00:01", false); 81 static RangeTag leGeTimeRange = new RangeTag 82 ("time", "3010-01-01_00:00:00", false, "1976-07-03_07:00:01", false); 83 84 static StringTag binaryString = new StringTag("\001\002\003\004"); 85 static RangeTag leGeBinaryRange = new RangeTag 86 ("binary", "\002\002\002\002", false, "\000\000\000\000", false); 87 88 static ExprTag[] emptyToMyETags = new ExprTag[] { 89 new StringTag(""), 90 new StringTag("/"), 91 new StringTag("/m"), 92 new StringTag("/my"), 93 }; 94 static SetTag emptyToMySet = new SetTag(emptyToMyETags); 95 96 static ExprTag[] emptyToMyPublicETags = new ExprTag[] { 97 new StringTag(""), 98 new StringTag("/"), 99 new StringTag("/m"), 100 new StringTag("/my"), 101 new StringTag("/my/"), 102 new StringTag("/my/p"), 103 new StringTag("/my/pu"), 104 new StringTag("/my/pub"), 105 new StringTag("/my/publ"), 106 new StringTag("/my/publi"), 107 new StringTag("/my/public"), 108 }; 109 static SetTag emptyToMyPublicSet = new SetTag(emptyToMyPublicETags); 110 111 static ExprTag[] emptyToMyPrivateETags = new ExprTag[] { 112 new StringTag(""), 113 new StringTag("/"), 114 new StringTag("/m"), 115 new StringTag("/my"), 116 new StringTag("/my/"), 117 new StringTag("/my/p"), 118 new StringTag("/my/pr"), 119 new StringTag("/my/pri"), 120 new StringTag("/my/priv"), 121 new StringTag("/my/priva"), 122 new StringTag("/my/privat"), 123 new StringTag("/my/private") 124 }; 125 static SetTag emptyToMyPrivateSet = new SetTag(emptyToMyPrivateETags); 126 127 static ExprTag[] myToMyPublicETags = new ExprTag[] { 128 new StringTag("/my"), 129 new StringTag("/my/"), 130 new StringTag("/my/p"), 131 new StringTag("/my/pu"), 132 new StringTag("/my/pub"), 133 new StringTag("/my/publ"), 134 new StringTag("/my/publi"), 135 new StringTag("/my/public"), 136 }; 137 static SetTag myToMyPublicSet = new SetTag(myToMyPublicETags); 138 139 static ExprTag[] myToMyPrivateETags = new ExprTag[] { 140 new StringTag("/my"), 141 new StringTag("/my/"), 142 new StringTag("/my/p"), 143 new StringTag("/my/pr"), 144 new StringTag("/my/pri"), 145 new StringTag("/my/priv"), 146 new StringTag("/my/priva"), 147 new StringTag("/my/privat"), 148 new StringTag("/my/private"), 149 }; 150 static SetTag myToMyPrivateSet = new SetTag(myToMyPrivateETags); 151 152 static Tag[] tags = new Tag[] { 153 emptyString, readString, writeString, fileString, myString, 154 emptyPrefix, myPrefix, myPublicPrefix, myPrivatePrefix, 155 emptyRPrefix, myRPrefix, myPublicRPrefix, myPrivateRPrefix, 156 readWriteSet, writeReadSet, 157 myPublicPrivatePrefixSet, 158 grant, grunt, grantRead, grantReadFile, grantFileRead, 159 grantWriteFile, grantReadWriteFile, leGeAlphaRange, 160 leGtAlphaRange, ltGeAlphaRange, ltGtAlphaRange, 161 numericString, leGeNumericRange, 162 dateString, leGeDateRange, leGeTimeRange, 163 binaryString, leGeBinaryRange, 164 }; 165 166 /*** 167 All pairs of tags that should be equals(), excluding 168 self-equality. A pair need only appear once (i.e., its reverse 169 need not appear). 170 **/ 171 static Tag[][] equalities = new Tag[][] { 172 { readWriteSet, writeReadSet }, 173 }; 174 static boolean expectEquals(Tag tag1, Tag tag2) 175 { 176 for (int i = 0; i < equalities.length; i++) { 177 if (equalities[i][0] == tag1 && equalities[i][1] == tag2) 178 return true; 179 if (equalities[i][0] == tag2 && equalities[i][1] == tag1) 180 return true; 181 } 182 return false; 183 } 184 185 /*** 186 All pairs of tags such that the first element should implies() 187 the second element, excluding self-implication. 188 **/ 189 static Tag[][] implications = new Tag[][] { 190 /* PrefixTag */ 191 { emptyPrefix, emptyString }, 192 { emptyPrefix, myString}, 193 { emptyPrefix, readString }, 194 { emptyPrefix, writeString }, 195 { emptyPrefix, fileString }, 196 { emptyPrefix, numericString }, 197 { emptyPrefix, dateString }, 198 { emptyPrefix, binaryString }, 199 { emptyPrefix, myPrefix }, 200 { emptyPrefix, myPublicPrefix }, 201 { emptyPrefix, myPrivatePrefix }, 202 { emptyPrefix, readWriteSet }, 203 { emptyPrefix, writeReadSet }, 204 { emptyPrefix, readWriteSet }, 205 { emptyPrefix, myPublicPrivatePrefixSet }, 206 { myPrefix, myString }, 207 { myPrefix, fileString }, 208 { myPrefix, myPublicPrefix }, 209 { myPrefix, myPrivatePrefix }, 210 { myPrivatePrefix, fileString }, 211 { myPrefix, myPublicPrivatePrefixSet }, 212 /* ReversePrefixTag */ 213 { emptyRPrefix, emptyString}, 214 { myRPrefix, emptyString}, 215 { myRPrefix, myString}, 216 { myPublicRPrefix, emptyString}, 217 { myPublicRPrefix, myString}, 218 { myPrivateRPrefix, emptyString}, 219 { myPrivateRPrefix, myString}, 220 { myRPrefix, emptyRPrefix}, 221 { myPublicRPrefix, emptyRPrefix}, 222 { myPublicRPrefix, myRPrefix}, 223 { myPrivateRPrefix, emptyRPrefix}, 224 { myPrivateRPrefix, myRPrefix}, 225 /* SetTag */ 226 { readWriteSet, readString }, 227 { readWriteSet, writeString }, 228 { readWriteSet, writeReadSet }, 229 { writeReadSet, readString }, 230 { writeReadSet, writeString }, 231 { writeReadSet, readWriteSet }, 232 { myPublicPrivatePrefixSet, myPrivatePrefix}, 233 { myPublicPrivatePrefixSet, myPublicPrefix}, 234 { myPublicPrivatePrefixSet, fileString}, 235 /* SimpleTag */ 236 { grant, grantRead }, 237 { grant, grantReadFile }, 238 { grant, grantFileRead }, 239 { grant, grantWriteFile }, 240 { grant, grantReadWriteFile }, 241 { grantRead, grantReadFile }, 242 { grantReadWriteFile, grantReadFile }, 243 { grantReadWriteFile, grantWriteFile }, 244 /* RangeTag */ 245 { leGeAlphaRange, readString }, 246 { leGeAlphaRange, writeString }, 247 { leGeAlphaRange, readWriteSet }, 248 { leGeAlphaRange, writeReadSet }, 249 { leGeAlphaRange, leGtAlphaRange }, 250 { leGeAlphaRange, ltGeAlphaRange }, 251 { leGeAlphaRange, ltGtAlphaRange }, 252 { ltGeAlphaRange, readString }, 253 { ltGeAlphaRange, ltGtAlphaRange }, 254 { leGtAlphaRange, writeString }, 255 { leGtAlphaRange, ltGtAlphaRange }, 256 { leGeNumericRange, numericString }, 257 { leGeDateRange, dateString }, 258 { leGeTimeRange, dateString }, 259 { leGeBinaryRange, binaryString }, 260 { leGeBinaryRange, 261 myString} /* this just happens to be so, "/my" = #2F6D79# */ 262 }; 263 static boolean expectImplies(Tag tag1, Tag tag2) 264 { 265 for (int i = 0; i < implications.length; i++) { 266 if (implications[i][0] == tag1 && implications[i][1] == tag2) 267 return true; 268 } 269 return false; 270 } 271 272 /*** 273 All triples of tags such that the third element equals() the 274 first element intersect() the second element, excluding 275 self-intersections, intersections that yield NULL_TAG, and 276 intersections where the result is one of the first two elements 277 (we cover this last case in expectEquals). A given pair of first 278 and second elements need only appear once (i.e., its reverse need 279 not appear). 280 **/ 281 static Tag[][] intersections = new Tag[][] { 282 { emptyPrefix, emptyRPrefix, emptyString}, 283 { emptyPrefix, myRPrefix, emptyToMySet}, 284 { emptyPrefix, myPublicRPrefix, emptyToMyPublicSet}, 285 { emptyPrefix, myPrivateRPrefix, emptyToMyPrivateSet}, 286 { myPrefix, myRPrefix, myString}, 287 { myPrefix, myPublicRPrefix, myToMyPublicSet}, 288 { myPrefix, myPrivateRPrefix, myToMyPrivateSet}, 289 { myPublicPrefix, myPublicRPrefix, myPublicString}, 290 { myPrivatePrefix, myPrivateRPrefix, myPrivateString}, 291 { myRPrefix, myPublicPrivatePrefixSet, Tag.NULL_TAG}, 292 { myPublicRPrefix, myPublicPrivatePrefixSet, myPublicString}, 293 { myPrivateRPrefix, myPublicPrivatePrefixSet, myPrivateString}, 294 { readWriteSet, leGtAlphaRange, writeString}, 295 { readWriteSet, ltGeAlphaRange, readString}, 296 { readWriteSet, leGtAlphaRange, readString}, 297 { writeReadSet, leGtAlphaRange, writeString}, 298 { writeReadSet, ltGeAlphaRange, readString}, 299 { myPublicPrivatePrefixSet, readWriteSet, Tag.NULL_TAG }, 300 { myPublicPrivatePrefixSet, writeReadSet, Tag.NULL_TAG }, 301 { grantRead, grantReadWriteFile, grantReadFile }, 302 { leGtAlphaRange, ltGeAlphaRange, ltGtAlphaRange }, 303 }; 304 static Tag expectIntersect(Tag tag1, Tag tag2) 305 { 306 if (tag1.implies(tag2)) { 307 return tag2; 308 } 309 if (tag2.implies(tag1)) { 310 return tag1; 311 } 312 for (int i = 0; i < intersections.length; i++) { 313 if (intersections[i][0] == tag1 && intersections[i][1] == tag2) 314 return intersections[i][2]; 315 if (intersections[i][0] == tag2 && intersections[i][1] == tag1) 316 return intersections[i][2]; 317 } 318 return Tag.NULL_TAG; 319 } 320 321 322 static class SelfTest extends TestCase 323 { 324 private Tag tag; 325 public SelfTest(Tag t) 326 { 327 super("SelfTest for "+t.toString()); 328 tag = t; 329 } 330 public void runTest() 331 { 332 assertEquals("self equality", tag, tag); 333 assertEquals("self intersection", tag.intersect(tag), tag); 334 assertTrue("self implication", tag.implies(tag)); 335 } 336 } 337 338 static class EqualsTest extends TestCase 339 { 340 Tag tag1; 341 Tag tag2; 342 public EqualsTest(Tag t1, Tag t2) 343 { 344 super("EqualsTest"); 345 tag1 = t1; 346 tag2 = t2; 347 } 348 public void runTest() 349 { 350 // equals must be symmetric 351 if (expectEquals(tag1, tag2)) { 352 assertTrue(tag1+" equals() "+tag2, 353 tag1.equals(tag2)); 354 assertTrue(tag2+" equals() "+tag1, 355 tag2.equals(tag1)); 356 } else { 357 assertFalse(tag1+" not equals() "+tag2, 358 tag1.equals(tag2)); 359 assertFalse(tag2+" not equals() "+tag1, 360 tag2.equals(tag1)); 361 } 362 } 363 } 364 365 static class ImpliesTest extends TestCase 366 { 367 Tag tag1; 368 Tag tag2; 369 public ImpliesTest(Tag t1, Tag t2) 370 { 371 super("ImpliesTest"); 372 tag1 = t1; 373 tag2 = t2; 374 } 375 public void runTest() 376 { 377 if (expectImplies(tag1, tag2)) { 378 assertTrue(tag1+" implies() "+tag2, 379 tag1.implies(tag2)); 380 if (tag2.implies(tag1)) { 381 assertTrue(tag1+" <=> "+tag2, 382 tag1.equals(tag2)); 383 } 384 } else { 385 assertFalse(tag1+" not implies() "+tag2, 386 tag1.implies(tag2)); 387 } 388 } 389 } 390 391 static class IntersectTest extends TestCase 392 { 393 Tag tag1; 394 Tag tag2; 395 public IntersectTest(Tag t1, Tag t2) 396 { 397 super("IntersectTest"); 398 tag1 = t1; 399 tag2 = t2; 400 } 401 public void runTest() 402 { 403 // intersect must be symmetric 404 Tag tag12 = tag1.intersect(tag2); 405 Tag tag21 = tag2.intersect(tag1); 406 Tag expected = expectIntersect(tag1, tag2); 407 assertEquals(tag1+" intersect() "+tag2, expected, tag12); 408 assertEquals(tag2+" intersect() "+tag1, expected, tag21); 409 } 410 } 411 412 public static Test suite() 413 { 414 TestSuite s = new NamedTestSuite("TagTest"); 415 // check tag self-equality, intersection, implication 416 for (int i = 0; i < tags.length; i++) { 417 s.addTest(new SelfTest(tags[i])); 418 } 419 // check tag equality 420 for (int i = 0; i < tags.length; i++) { 421 for (int j = i+1; j < tags.length; j++) { 422 s.addTest(new EqualsTest(tags[i], tags[j])); 423 } 424 } 425 // check tag implication 426 for (int i = 0; i < tags.length; i++) { 427 for (int j = 0; j < tags.length; j++) { 428 if (i == j) continue; // covered in SelfTest 429 s.addTest(new ImpliesTest(tags[i], tags[j])); 430 } 431 } 432 // check tag intersection 433 for (int i = 0; i < tags.length; i++) { 434 for (int j = i+1; j < tags.length; j++) { 435 s.addTest(new IntersectTest(tags[i], tags[j])); 436 } 437 } 438 return s; 439 } 440 }

This page was automatically generated by Maven