tests/libjava-mauve/src/gnu/testlet/java/io/FilePermission/traversal2.java
branchjk_new_structure
changeset 1541 75c2e24dea9a
parent 1540 92ac284961c1
child 1542 be11db817bcf
equal deleted inserted replaced
1540:92ac284961c1 1541:75c2e24dea9a
     1 // Copyright (C) 2006, 2007 Red Hat, Inc.
       
     2 // Written by Gary Benson <gbenson@redhat.com>
       
     3 
       
     4 // This file is part of Mauve.
       
     5 
       
     6 // Mauve is free software; you can redistribute it and/or modify
       
     7 // it under the terms of the GNU General Public License as published by
       
     8 // the Free Software Foundation; either version 2, or (at your option)
       
     9 // any later version.
       
    10 
       
    11 // Mauve is distributed in the hope that it will be useful,
       
    12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    14 // GNU General Public License for more details.
       
    15 
       
    16 // You should have received a copy of the GNU General Public License
       
    17 // along with Mauve; see the file COPYING.  If not, write to
       
    18 // the Free Software Foundation, 59 Temple Place - Suite 330,
       
    19 // Boston, MA 02111-1307, USA.
       
    20 
       
    21 // Tags: JDK1.2
       
    22 
       
    23 package gnu.testlet.java.io.FilePermission;
       
    24 
       
    25 import java.io.File;
       
    26 import java.io.FilePermission;
       
    27 import java.util.LinkedList;
       
    28 
       
    29 import gnu.testlet.Testlet;
       
    30 import gnu.testlet.TestHarness;
       
    31 
       
    32 public class traversal2 implements Testlet
       
    33 {
       
    34   public void test (TestHarness harness)
       
    35   {
       
    36     try {
       
    37       harness.checkPoint("setup");
       
    38 
       
    39       String[] ways_to_access = new String[] {
       
    40 	"dir",     // via the directory
       
    41 	"rlink",   // via a relative link to the directory
       
    42 	"alink"};  // via an absolute link to the directory
       
    43 
       
    44       String[] ways_to_escape = new String[] {
       
    45 	"..",      // via the directory
       
    46 	"rlink",   // via a relative link out of the directory
       
    47 	"alink"};  // via an absolute link out of the directory
       
    48 
       
    49       String[] item_states = new String[] {
       
    50 	"present", // the file exists
       
    51 	"absent"}; // the file does not exist
       
    52 
       
    53       LinkedList cleanup = new LinkedList();
       
    54       try {
       
    55 	File tempdir = new File(harness.getTempDirectory(), "mauve-testdir");
       
    56 	harness.check(tempdir.isDirectory() || tempdir.mkdir());
       
    57 	cleanup.add(tempdir);
       
    58 
       
    59 	File testdir = new File(tempdir, "dir");
       
    60 	harness.check(testdir.isDirectory() || testdir.mkdir());
       
    61 	cleanup.add(testdir);
       
    62 
       
    63 	File link = new File(tempdir, "rlink");
       
    64 	harness.check(Runtime.getRuntime().exec(new String[] {
       
    65 	  "ln", "-s", testdir.getName(), link.getPath()
       
    66 	  }).waitFor() == 0);
       
    67 	cleanup.add(link);
       
    68 
       
    69 	link = new File(tempdir, "alink");
       
    70 	harness.check(Runtime.getRuntime().exec(new String[] {
       
    71 	  "ln", "-s", testdir.getPath(), link.getPath()
       
    72 	  }).waitFor() == 0);
       
    73 	cleanup.add(link);
       
    74 
       
    75 	File file = new File(tempdir, "file-present");
       
    76 	harness.check(file.isFile() || file.createNewFile());
       
    77 	cleanup.add(file);
       
    78 	  
       
    79 	file = new File(tempdir, "file-absent");
       
    80 	harness.check(!file.exists());
       
    81 
       
    82 	link = new File(testdir, "rlink");
       
    83 	harness.check(Runtime.getRuntime().exec(new String[] {
       
    84 	  "ln", "-s", "..", link.getPath()
       
    85 	  }).waitFor() == 0);
       
    86 	cleanup.add(link);
       
    87 
       
    88 	link = new File(testdir, "alink");
       
    89 	harness.check(Runtime.getRuntime().exec(new String[] {
       
    90 	  "ln", "-s", tempdir.getPath(), link.getPath()
       
    91 	  }).waitFor() == 0);
       
    92 	cleanup.add(link);
       
    93 
       
    94 	harness.checkPoint("test");
       
    95 	for (int i = 0; i < ways_to_access.length; i++) {
       
    96 	  String how_to_access = ways_to_access[i];
       
    97 
       
    98 	  FilePermission a = new FilePermission(new File(
       
    99 	    new File(tempdir, how_to_access), "-").getPath(), "read");
       
   100 
       
   101 	  for (int j = 0; j < ways_to_escape.length; j++) {
       
   102 	    String how_to_escape = ways_to_escape[j];
       
   103 	    for (int k = 0; k < item_states.length; k++) {
       
   104 	      String item = "file-" + item_states[k];
       
   105 
       
   106 	      FilePermission b = new FilePermission(new File(
       
   107 	        new File(testdir, how_to_escape), item).getPath(), "read");
       
   108 
       
   109 	      harness.debug("\na = " + a);
       
   110 	      harness.debug("b = " + b);
       
   111 
       
   112 	      harness.check(!a.implies(b));
       
   113 	    }
       
   114 	  }
       
   115 	}
       
   116       }
       
   117       finally {
       
   118 	for (int i = cleanup.size() - 1; i >= 0; i--)
       
   119 	  ((File) cleanup.get(i)).delete();
       
   120       }
       
   121     }
       
   122     catch (Throwable ex) {
       
   123       harness.debug(ex);
       
   124       harness.check(false, "Unexpected exception");
       
   125     }
       
   126   }
       
   127 }