1. Test File Permission1.1 Feature to be tested:A permission represents access to a system resource. In order for a
resource access to be allowed for a Java application running with a
security manager, the corresponding permission must be explicitly
granted to the code attempting the access via security policy file.File permission is one class of the permissions you can grant to your
Java application.File permission includes:
* read Permission to read.
* write Permission to write (which includes permission to create).
* execute Permission to execute. Allows Runtime.exec to be called.
Corresponds to SecurityManager.checkExec.
* delete Permission to delete. Allows File.delete to be called.
Corresponds to SecurityManager.checkDelete.1.2 Homework
1.2.1 Write a test specification for the above four file permssions. Try
to come up with as many testing scenarios as you can.1.2.2 Write testing code to test File write Permission. Requirements are:* The main test code should be Java program which will be run as a
standalone Java application.
* The security manager should be installed via command line instead of
programmatically. And Policy file should be also specified on command line.
* Both positive and negative test cases are needed.
* Both Windows and Unix-like OS should be supported.
* The whole testing can be started by invoking a single script and then
run automatically. Arguments (such as JDK location, results location)
could be fed into this script on command line.
* Test result should be output to Stand Output. And if the testing fails
somehow (for example, JDK location doesn't exist), logs will be recorded
for investigation.
* Include the scenario of digitally signed code. (optional task)
resource access to be allowed for a Java application running with a
security manager, the corresponding permission must be explicitly
granted to the code attempting the access via security policy file.File permission is one class of the permissions you can grant to your
Java application.File permission includes:
* read Permission to read.
* write Permission to write (which includes permission to create).
* execute Permission to execute. Allows Runtime.exec to be called.
Corresponds to SecurityManager.checkExec.
* delete Permission to delete. Allows File.delete to be called.
Corresponds to SecurityManager.checkDelete.1.2 Homework
1.2.1 Write a test specification for the above four file permssions. Try
to come up with as many testing scenarios as you can.1.2.2 Write testing code to test File write Permission. Requirements are:* The main test code should be Java program which will be run as a
standalone Java application.
* The security manager should be installed via command line instead of
programmatically. And Policy file should be also specified on command line.
* Both positive and negative test cases are needed.
* Both Windows and Unix-like OS should be supported.
* The whole testing can be started by invoking a single script and then
run automatically. Arguments (such as JDK location, results location)
could be fed into this script on command line.
* Test result should be output to Stand Output. And if the testing fails
somehow (for example, JDK location doesn't exist), logs will be recorded
for investigation.
* Include the scenario of digitally signed code. (optional task)
import java.io.IOException;public class FilePermission { public static void givePermission(String fileName) throws IOException{
File file = new File(fileName);
if(file.exists()){// 判断这个文件是不是存在
file.delete();// 删除这个文件
}
file.createNewFile();// 新建这个文件
System.out.println(fileName+" is created");
file.setReadable(true);// 设置这个文件可读权限
file.setWritable(true);// 可写权限
file.setExecutable(true);// 可执行权限
}
public static void main(String[] args) {
String fileName = "E:\\files\\test.txt";
try {
givePermission(fileName);
} catch (IOException e) {
e.printStackTrace();
}
}
}
测试这个文件的权限import java.io.File;public class TestPermission { public static void main(String[] args) {
String fileName = "E:\\files\\test.txt";
File file = new File(fileName);
System.out.println(fileName+" 可读? "+file.canRead());
System.out.println(fileName+" 可写? "+file.canWrite());
System.out.println(fileName+" 可执行? "+file.canExecute());
}
}