Description:
[grader] new test on accessing PROBLEM_HOME and openning files git-svn-id: http://theory.cpe.ku.ac.th/grader/judge/trunk/scripts@133 6386c4cd-e34a-4fa8-8920-d93eb39b512e
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r35:8a388d27ae0c - - 8 files changed: 133 inserted, 0 deleted

@@ -0,0 +1,64
1 + #!/usr/bin/ruby
2 +
3 + problem_home = ENV['PROBLEM_HOME']
4 + require "#{problem_home}/script/test_dsl.rb"
5 +
6 + if ARGV.length < 2
7 + puts "Usage: check <language> <test-number> [<output-file>]"
8 + exit(0)
9 + end
10 +
11 + language = ARGV[0]
12 + test_num = ARGV[1].to_i
13 + if ARGV.length >= 3
14 + output_file_name = ARGV[2]
15 + else
16 + output_file_name = "output.txt"
17 + end
18 +
19 + load "#{problem_home}/test_cases/all_tests.cfg"
20 + problem = Problem.get_instance
21 +
22 + output_file = File.new(output_file_name, "r")
23 + answer_file = File.new("#{problem_home}/test_cases/#{test_num}/answer-#{test_num}.txt")
24 + result_file = File.new("check_result", "w")
25 +
26 + output_file_content = output_file.read
27 + answer_file_content = answer_file.read
28 +
29 + report_correct = lambda {
30 + result_file.write "Correct\n"
31 + result_file.write problem.get_score(test_num)
32 + result_file.write "\n"
33 + result_file.close
34 + exit(0)
35 + }
36 +
37 + report_wrong = lambda {
38 + result_file.write "Incorrect\n"
39 + result_file.write "0\n"
40 + result_file.close
41 + exit(0)
42 + }
43 +
44 + ##################
45 + # Your code here #
46 + ##################
47 +
48 + ########### THIS IS FOR CHECKING TEXT ##########
49 +
50 + # check visible text
51 +
52 + out_items = output_file_content.split
53 + ans_items = answer_file_content.split
54 +
55 + if out_items.length != ans_items.length
56 + report_wrong.call
57 + else
58 + out_items.length.times do |i|
59 + if out_items[i]!=ans_items[i]
60 + report_wrong.call
61 + end
62 + end
63 + report_correct.call
64 + end
@@ -0,0 +1,2
1 + yes
2 +
@@ -0,0 +1,2
1 + hello (this won't be read)
2 +
@@ -0,0 +1,12
1 + problem do
2 + num_tests 1
3 + full_score 10
4 + time_limit_each 1
5 + mem_limit_each 64
6 + score_each 10
7 +
8 + run 1 do
9 + tests 1
10 + end
11 +
12 + end
@@ -0,0 +1,12
1 + #include <stdlib.h>
2 + #include <stdio.h>
3 +
4 + int main()
5 + {
6 + char* prob_home = getenv("PROBLEM_HOME");
7 + if(prob_home!=NULL)
8 + printf("yes\n");
9 + else
10 + printf("no\n");
11 + exit(0);
12 + }
@@ -0,0 +1,13
1 + #include <stdlib.h>
2 + #include <stdio.h>
3 +
4 + int main()
5 + {
6 + FILE *fp = fopen("/bin/ls","r");
7 + if(fp!=NULL) {
8 + printf("yes\n");
9 + fclose(fp);
10 + } else
11 + printf("no\n");
12 + exit(0);
13 + }
@@ -76,6 +76,9
76 76 "#{problem_home}/script/box")
77 77 end
78 78
79 + # Hide PROBLEM_HOME
80 + ENV['PROBLEM_HOME'] = nil
81 +
79 82 # Run the program.
80 83 run_command = "/usr/bin/time -f \"#{time_output_format}\" 2>run_result #{problem_home}/script/box -a 2 -f -t #{time_limit} -m #{mem_limit} -i #{input_file_name} -o output.txt #{program_name}"
81 84 log "Running test #{test_num}..."
@@ -83,6 +86,9
83 86 log
84 87 system(run_command)
85 88
89 + # Restore PROBLEM_HOME
90 + ENV['PROBLEM_HOME'] = problem_home
91 +
86 92 # Create the result file.
87 93 result_file = File.new("result", "w")
88 94 comment_file = File.new("comment", "w")
@@ -97,6 +97,28
97 97 :comment => /^FAILED:/})
98 98 end
99 99
100 + it "should not allow malicious submission to see PROBLEM_HOME" do
101 + problem_test_yesno = stub(Problem,
102 + :id => 1, :name => 'test_yesno',
103 + :full_score => 10)
104 + grader_should(:grade => "yesno_access_problem_home.c",
105 + :on => problem_test_yesno,
106 + :and_report => {
107 + :score => 0,
108 + :comment => /^FAILED:/})
109 + end
110 +
111 + it "should not allow malicious submission to open files" do
112 + problem_test_yesno = stub(Problem,
113 + :id => 1, :name => 'test_yesno',
114 + :full_score => 10)
115 + grader_should(:grade => "yesno_open_file.c",
116 + :on => problem_test_yesno,
117 + :and_report => {
118 + :score => 0,
119 + :comment => /^FAILED:/})
120 + end
121 +
100 122 def grader_should(args)
101 123 @user1 = stub(User,
102 124 :id => 1, :login => 'user1')
You need to be logged in to leave comments. Login now