JavaNetSocketTest.java revision f6c387128427e121477c1b32ad35cdcaa5101ba3
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.KnownFailure;
20import dalvik.annotation.TestTargets;
21import dalvik.annotation.TestLevel;
22import dalvik.annotation.TestTargetNew;
23import dalvik.annotation.TestTargetClass;
24
25import junit.framework.TestCase;
26
27import java.io.IOException;
28import java.net.InetAddress;
29import java.net.Socket;
30import java.security.Permission;
31/*
32 * This class tests the security permissions which are documented in
33 * http://java.sun.com/j2se/1.5.0/docs/guide/security/permissions.html#PermsAndMethods
34 * for class java.net.Socket
35 */
36@TestTargetClass(java.net.Socket.class)
37public class JavaNetSocketTest extends TestCase {
38
39    SecurityManager old;
40
41    @Override
42    protected void setUp() throws Exception {
43        old = System.getSecurityManager();
44        super.setUp();
45    }
46
47    @Override
48    protected void tearDown() throws Exception {
49        System.setSecurityManager(old);
50        super.tearDown();
51    }
52
53    @TestTargets({
54        @TestTargetNew(
55            level = TestLevel.PARTIAL,
56            notes = "Verifies that java.net.Socket constructor calls checkConnect on security manager.",
57            method = "Socket",
58            args = {java.lang.String.class, int.class}
59        ),
60        @TestTargetNew(
61            level = TestLevel.PARTIAL,
62            notes = "Verifies that java.net.Socket constructor calls checkConnect on security manager.",
63            method = "Socket",
64            args = {java.lang.String.class, int.class, boolean.class}
65        ),
66        @TestTargetNew(
67            level = TestLevel.PARTIAL,
68            notes = "Verifies that java.net.Socket constructor calls checkConnect on security manager.",
69            method = "Socket",
70            args = {java.lang.String.class, int.class, java.net.InetAddress.class, int.class}
71        ),
72        @TestTargetNew(
73            level = TestLevel.PARTIAL,
74            notes = "Verifies that java.net.Socket constructor calls checkConnect on security manager.",
75            method = "Socket",
76            args = {java.net.InetAddress.class, int.class}
77        ),
78        @TestTargetNew(
79            level = TestLevel.PARTIAL,
80            notes = "Verifies that java.net.Socket constructor calls checkConnect on security manager.",
81            method = "Socket",
82            args = {java.net.InetAddress.class, int.class, boolean.class}
83        ),
84        @TestTargetNew(
85            level = TestLevel.PARTIAL,
86            notes = "Verifies that java.net.Socket constructor calls checkConnect on security manager.",
87            method = "Socket",
88            args = {java.net.InetAddress.class, int.class, java.net.InetAddress.class, int.class}
89        )
90    })
91    @KnownFailure("ToT fixed")
92    public void test_ctor() throws IOException {
93        class TestSecurityManager extends SecurityManager {
94            boolean called = false;
95            String host = null;
96            int port = -1;
97            void reset(){
98                called = false;
99                host = null;
100                port = -1;
101            }
102            @Override
103            public void checkConnect(String host, int port) {
104                this.called = true;
105                this.port = port;
106                this.host = host;
107            }
108            @Override
109            public void checkPermission(Permission permission) {
110
111            }
112        }
113
114        String host = "www.google.ch";
115        int port = 80;
116        String hostAddress = InetAddress.getByName(host).getHostAddress();
117
118        TestSecurityManager s = new TestSecurityManager();
119        System.setSecurityManager(s);
120
121        s.reset();
122        new Socket(host, port);
123        assertTrue("java.net.ServerSocket ctor must call checkConnect on security permissions", s.called);
124        assertEquals("Argument of checkConnect is not correct", hostAddress, s.host);
125        assertEquals("Argument of checkConnect is not correct", port, s.port);
126
127        s.reset();
128        new Socket(host, port, true);
129        assertTrue("java.net.ServerSocket ctor must call checkConnect on security permissions", s.called);
130        assertEquals("Argument of checkConnect is not correct", hostAddress, s.host);
131        assertEquals("Argument of checkConnect is not correct", port, s.port);
132
133        s.reset();
134        new Socket(host, port, InetAddress.getByAddress(new byte[] {0,0,0,0}), 0);
135        assertTrue("java.net.ServerSocket ctor must call checkConnect on security permissions", s.called);
136        assertEquals("Argument of checkConnect is not correct", hostAddress, s.host);
137        assertEquals("Argument of checkConnect is not correct", port, s.port);
138
139        s.reset();
140        new Socket(InetAddress.getByName(host), port);
141        assertTrue("java.net.ServerSocket ctor must call checkConnect on security permissions", s.called);
142        assertEquals("Argument of checkConnect is not correct", hostAddress, s.host);
143        assertEquals("Argument of checkConnect is not correct", port, s.port);
144
145        s.reset();
146        new Socket(InetAddress.getByName(host), port, true);
147        assertTrue("java.net.ServerSocket ctor must call checkConnect on security permissions", s.called);
148        assertEquals("Argument of checkConnect is not correct", hostAddress, s.host);
149        assertEquals("Argument of checkConnect is not correct", port, s.port);
150
151        s.reset();
152        new Socket(InetAddress.getByName(host), port,  InetAddress.getByAddress(new byte[] {0,0,0,0}), 0);
153        assertTrue("java.net.ServerSocket ctor must call checkConnect on security permissions", s.called);
154        assertEquals("Argument of checkConnect is not correct", hostAddress, s.host);
155        assertEquals("Argument of checkConnect is not correct", port, s.port);
156    }
157}
158