1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package tests.security.permissions;
18
19import dalvik.annotation.TestTargets;
20import dalvik.annotation.TestLevel;
21import dalvik.annotation.TestTargetNew;
22import dalvik.annotation.TestTargetClass;
23
24import junit.framework.TestCase;
25
26import java.io.File;
27import java.io.IOException;
28import java.io.RandomAccessFile;
29import java.security.Permission;
30/*
31 * This class tests the security permissions which are documented in
32 * http://java.sun.com/j2se/1.5.0/docs/guide/security/permissions.html#PermsAndMethods
33 * for class java.io.RandomAccessFile
34 */
35@TestTargetClass(java.io.RandomAccessFile.class)
36public class JavaIoRandomAccessFileTest extends TestCase {
37
38    SecurityManager old;
39
40    @Override
41    protected void setUp() throws Exception {
42        old = System.getSecurityManager();
43        super.setUp();
44    }
45
46    @Override
47    protected void tearDown() throws Exception {
48        System.setSecurityManager(old);
49        super.tearDown();
50    }
51
52    @TestTargets({
53        @TestTargetNew(
54            level = TestLevel.PARTIAL_COMPLETE,
55            notes = "Verifies that RandomAccessFile constructor calls checkRead method of security manager.",
56            method = "RandomAccessFile",
57            args = {java.lang.String.class, java.lang.String.class}
58        ),
59        @TestTargetNew(
60            level = TestLevel.PARTIAL_COMPLETE,
61            notes = "Verifies that RandomAccessFile constructor calls checkRead method of security manager.",
62            method = "RandomAccessFile",
63            args = {java.io.File.class, java.lang.String.class}
64        )
65    })
66    public void test_RandomAccessFile1() throws IOException {
67        class TestSecurityManager extends SecurityManager {
68            boolean called;
69            String file;
70            void reset(){
71                called = false;
72                file = null;
73            }
74            @Override
75            public void checkRead(String file){
76                called = true;
77                this.file = file;
78                super.checkRead(file);
79            }
80            @Override
81            public void checkPermission(Permission p) {
82
83            }
84        }
85
86        long id = new java.util.Date().getTime();
87        String filename = "SecurityPermissionsTest_"+id;
88        File f = File.createTempFile(filename, null);
89        f.deleteOnExit();
90        filename = f.getCanonicalPath();
91
92        TestSecurityManager s = new TestSecurityManager();
93        System.setSecurityManager(s);
94
95        s.reset();
96        new RandomAccessFile(filename, "r");
97        assertTrue("RandomAccessFile(String,String) ctor must call checkRead on security manager", s.called);
98        assertEquals("Argument of checkRead is not correct", filename, s.file);
99
100        s.reset();
101        new RandomAccessFile(f, "r");
102        assertTrue("RandomAccessFile(File, String) ctor must call checkRead on security manager", s.called);
103        assertEquals("Argument of checkRead is not correct", filename, s.file);
104    }
105
106    @TestTargetNew(
107        level = TestLevel.PARTIAL_COMPLETE,
108        notes = "Verifies that RandomAccessFile constructor calls checkRead and checkWrite on security manager.",
109        method = "RandomAccessFile",
110        args = {java.lang.String.class, java.lang.String.class}
111    )
112    public void test_RandomAccessFile2() throws IOException {
113        class TestSecurityManager extends SecurityManager {
114            boolean checkReadCalled;
115            boolean checkWriteCalled;
116            String checkReadFile;
117            String checkWriteFile;
118
119            void reset(){
120                checkReadCalled = false;
121                checkWriteCalled = false;
122                checkReadFile = null;
123                checkWriteFile = null;
124            }
125
126            @Override
127            public void checkRead(String file) {
128                checkReadCalled = true;
129                this.checkReadFile = file;
130                super.checkRead(file);
131            }
132            @Override
133            public void checkWrite(String file) {
134                checkWriteCalled = true;
135                this.checkWriteFile = file;
136                super.checkWrite(file);
137            }
138            @Override
139            public void checkPermission(Permission p) {
140
141            }
142        }
143
144        long id = new java.util.Date().getTime();
145        String filename = "SecurityPermissionsTest_"+id;
146        File f = File.createTempFile(filename, null);
147        f.deleteOnExit();
148        filename = f.getCanonicalPath();
149
150        TestSecurityManager s = new TestSecurityManager();
151        System.setSecurityManager(s);
152
153        s.reset();
154        new RandomAccessFile(filename, "rw");
155        assertTrue("RandomAccessFile(String,String) ctor must call checkRead on security manager", s.checkReadCalled);
156        assertTrue("RandomAccessFile(String,String) ctor must call checkWrite on security manager", s.checkWriteCalled);
157        assertEquals("Argument of checkRead is not correct", filename, s.checkReadFile);
158        assertEquals("Argument of checkWrite is not correct", filename, s.checkWriteFile);
159    }
160}
161