8181919: Refactor test/java/io/File/GetXSpace.sh to java test

Reviewed-by: naoto
This commit is contained in:
Brian Burkhalter 2020-08-14 08:12:13 -07:00
parent 552a73301c
commit a963aab1a8
2 changed files with 63 additions and 98 deletions
test/jdk/java/io/File

@ -23,12 +23,10 @@
/**
* @test
* @bug 4057701 6286712 6364377
* @requires (os.family == "linux" | os.family == "mac" | os.family == "windows")
* @run build GetXSpace
* @run shell GetXSpace.sh
* @bug 4057701 6286712 6364377 8181919
* @requires (os.family == "linux" | os.family == "mac" |
* os.family == "windows")
* @summary Basic functionality of File.get-X-Space methods.
* @key randomness
*/
import java.io.BufferedReader;
@ -38,11 +36,13 @@ import java.io.InputStreamReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.FileStore;
import java.nio.file.Path;
import java.security.Permission;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.lang.System.err;
import static java.lang.System.out;
public class GetXSpace {
@ -61,6 +61,12 @@ public class GetXSpace {
private static int pass = 0;
private static Throwable first;
static void reset() {
fail = 0;
pass = 0;
first = null;
}
static void pass() {
pass++;
}
@ -338,7 +344,8 @@ public class GetXSpace {
}
}
private static void testFile(String dirName) {
private static int testFile(Path dir) {
String dirName = dir.toString();
out.format("--- Testing %s%n", dirName);
ArrayList<Space> l;
try {
@ -347,9 +354,18 @@ public class GetXSpace {
throw new RuntimeException(dirName + " can't get file system information", x);
}
compare(l.get(0));
if (fail != 0) {
err.format("%d tests: %d failure(s); first: %s%n",
fail + pass, fail, first);
} else {
out.format("all %d tests passed%n", fail + pass);
}
return fail != 0 ? 1 : 0;
}
private static void testDF() {
private static int testDF() {
out.println("--- Testing df");
// Find all of the partitions on the machine and verify that the size
// returned by "df" is equivalent to File.getXSpace() values.
@ -381,20 +397,51 @@ public class GetXSpace {
}
}
}
}
public static void main(String [] args) {
if (args.length > 0) {
testFile(args[0]);
} else {
testDF();
}
System.setSecurityManager(null);
if (fail != 0) {
throw new RuntimeException((fail + pass) + " tests: "
+ fail + " failure(s), first", first);
err.format("%d tests: %d failure(s); first: %s%n",
fail + pass, fail, first);
} else {
out.format("all %d tests passed%n", fail + pass);
}
return fail != 0 ? 1 : 0;
}
private static void perms(File file, boolean allow) throws IOException {
file.setExecutable(allow, false);
file.setReadable(allow, false);
file.setWritable(allow, false);
}
private static void deny(Path path) throws IOException {
perms(path.toFile(), false);
}
private static void allow(Path path) throws IOException {
perms(path.toFile(), true);
}
public static void main(String[] args) throws Exception {
int failedTests = testDF();
reset();
Path tmpDir = Files.createTempDirectory(null);
Path tmpSubdir = Files.createTempDirectory(tmpDir, null);
Path tmpFile = Files.createTempFile(tmpSubdir, "foo", null);
deny(tmpSubdir);
failedTests += testFile(tmpFile);
allow(tmpSubdir);
Files.delete(tmpFile);
Files.delete(tmpSubdir);
Files.delete(tmpDir);
if (failedTests > 0) {
throw new RuntimeException(failedTests + " test(s) failed");
}
}
}

@ -1,82 +0,0 @@
#
# Copyright (c) 2006, 2020, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 only, as
# published by the Free Software Foundation.
#
# This code is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# version 2 for more details (a copy is included in the LICENSE file that
# accompanied this code).
#
# You should have received a copy of the GNU General Public License version
# 2 along with this work; if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
# or visit www.oracle.com if you need additional information or have any
# questions.
#
#
# set platform-dependent variable
OS=`uname -s`
case "$OS" in
Linux | Darwin ) TMP=/tmp ;;
Windows_98 ) return ;;
CYGWIN_* ) TMP="c:/temp" ;;
Windows* ) SID=`sid`; TMP="c:/temp" ;;
* )
echo "Unrecognized system! ${OS}"
exit 1
;;
esac
TMP1=${TMP}/tmp1_$$
FAIL=0;
deny() {
case "$OS" in
Windows* ) chacl -d ${SID}:f $* ;;
* ) chmod 000 $* ;;
esac
}
allow() {
case "$OS" in
Windows* ) chacl -g ${SID}:f $* ;;
* ) chmod 777 $* ;;
esac
}
runTest() {
${TESTJAVA}/bin/java ${TESTVMOPTS} -cp ${TESTCLASSES} GetXSpace $*
if [ $? -eq 0 ]
then echo "Passed"
else
echo "FAILED"
FAIL=`expr ${FAIL} + 1`
fi
}
# df output
runTest
# readable file in an unreadable directory
mkdir -p ${TMP1}
touch ${TMP1}/foo
deny ${TMP1}
runTest ${TMP1}/foo
allow ${TMP1}
rm -rf ${TMP1}
if [ ${FAIL} -ne 0 ]
then
echo ""
echo "${FAIL} test(s) failed"
exit 1
fi