JavaNetDatagramSocketTest.java revision cc05ad238516f1303687aba4a978e24e57c0c07a
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 tests.support.Support_PortManager;
27
28import java.io.IOException;
29import java.net.DatagramPacket;
30import java.net.DatagramSocket;
31import java.net.InetAddress;
32import java.net.InetSocketAddress;
33import java.net.SocketAddress;
34import java.net.SocketTimeoutException;
35
36/*
37 * This class tests the security permissions which are documented in
38 * http://java.sun.com/j2se/1.5.0/docs/guide/security/permissions.html#PermsAndMethods
39 * for class java.net.DatagramSocket
40 */
41@TestTargetClass(DatagramSocket.class)
42public class JavaNetDatagramSocketTest extends TestCase {
43
44    SecurityManager old;
45
46    @Override
47    protected void setUp() throws Exception {
48        old = System.getSecurityManager();
49        super.setUp();
50    }
51
52    @Override
53    protected void tearDown() throws Exception {
54        System.setSecurityManager(old);
55        super.tearDown();
56    }
57
58    @TestTargets({
59        @TestTargetNew(
60            level = TestLevel.PARTIAL,
61            notes = "Verifies that java.net.DatagramSocket constructor calls checkListen of security manager.",
62            method = "DatagramSocket",
63            args = {int.class}
64        ),
65        @TestTargetNew(
66            level = TestLevel.PARTIAL,
67            notes = "Verifies that java.net.DatagramSocket constructor calls checkListen of security manager.",
68            method = "DatagramSocket",
69            args = {java.net.SocketAddress.class}
70        ),
71        @TestTargetNew(
72            level = TestLevel.PARTIAL,
73            notes = "Verifies that java.net.DatagramSocket constructor calls checkListen of security manager.",
74            method = "DatagramSocket",
75            args = {int.class, java.net.InetAddress.class}
76        )
77    })
78    public void test_ctor() throws IOException {
79        class TestSecurityManager extends SecurityManager {
80            boolean called = false;
81            String host = null;
82            int port = 0;
83            void reset(){
84                called = false;
85                host = null;
86                port = 0;
87            }
88            @Override
89            public void checkListen(int port) {
90                called = true;
91                this.port = port;
92                super.checkListen(port);
93            }
94        }
95
96        TestSecurityManager s = new TestSecurityManager();
97        System.setSecurityManager(s);
98
99        int port = Support_PortManager.getNextPortForUDP();
100        s.reset();
101        DatagramSocket s1 = new DatagramSocket(port);
102        assertTrue("java.net.DatagramSocket ctor must call checkListen on security manager", s.called);
103        assertEquals("Argument of checkListen is not correct", port, s.port);
104
105        s.reset();
106        DatagramSocket s2 = new DatagramSocket();
107        assertTrue("java.net.DatagramSocket ctor must call checkListen on security manager", s.called);
108        assertEquals("Argument of checkListen is not correct", 0, s.port);
109
110        port = Support_PortManager.getNextPortForUDP();
111        s.reset();
112        DatagramSocket s3 = new DatagramSocket(new InetSocketAddress(port));
113        assertTrue("java.net.DatagramSocket ctor must call checkListen on security manager", s.called);
114        assertEquals("Argument of checkListen is not correct", port, s.port);
115
116        port = Support_PortManager.getNextPortForUDP();
117        s.reset();
118        DatagramSocket s4 = new DatagramSocket(port, InetAddress.getLocalHost());
119        assertTrue("java.net.DatagramSocket ctor must call checkListen on security manager", s.called);
120        assertEquals("Argument of checkListen is not correct", port, s.port);
121
122    }
123
124
125    @TestTargetNew(
126        level = TestLevel.PARTIAL,
127        notes = "Verifies that java.net.DatagramSocket receive calls checkAccept of security manager.",
128        method = "receive",
129        args = {java.net.DatagramPacket.class}
130    )
131    public void test_receive() throws IOException {
132        class TestSecurityManager extends SecurityManager {
133            boolean called = false;
134            String host = null;
135            int port = 0;
136            void reset(){
137                called = false;
138                host = null;
139                port = 0;
140            }
141            @Override
142            public void checkAccept(String host, int port) {
143                this.host = host;
144                this.port = port;
145                this.called = true;
146                super.checkAccept(host, port);
147            }
148        }
149
150        final int port = Support_PortManager.getNextPortForUDP();
151        DatagramSocket s1 = new DatagramSocket(port);
152        //s1.setSoTimeout(100);
153
154        Thread sender = new Thread(){
155            public void run(){
156                try {
157                    DatagramPacket sendPacket = new DatagramPacket(new byte[256], 256, InetAddress.getLocalHost(), port);
158                    DatagramSocket sender = new DatagramSocket();
159                    while(!isInterrupted()){
160                        sender.send(sendPacket);
161                        Thread.sleep(10);
162                    }
163                }
164                catch(InterruptedException e){
165                    // expected
166                }
167                catch(Exception e){
168                    fail("unexpected exception " + e);
169                }
170            }
171        };
172        sender.start();
173
174        DatagramPacket p = new DatagramPacket(new byte[256], 0, 256);
175
176        TestSecurityManager s = new TestSecurityManager();
177        System.setSecurityManager(s);
178
179        s.reset();
180        assert(s1.getInetAddress()==null);
181        assertTrue(s1.getInetAddress()==null);
182        try {
183            s1.receive(p);
184        }
185        catch(Exception e){
186            fail("unexpected exception " + e);
187        }
188        sender.interrupt();
189        assertTrue("java.net.DatagramSocket.receive must call checkAccept on security manager", s.called);
190    }
191
192}
193