1 /* 2 * Copyright (C) 2022 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 17 package android.net; 18 19 import static android.system.OsConstants.SOCK_DGRAM; 20 import static android.system.OsConstants.SOCK_STREAM; 21 22 23 import static org.junit.Assert.assertEquals; 24 import static org.junit.Assert.assertTrue; 25 import static org.mockito.Mockito.mock; 26 27 import android.os.Build; 28 29 import androidx.test.filters.SmallTest; 30 31 import com.android.testutils.DevSdkIgnoreRule; 32 import com.android.testutils.DevSdkIgnoreRunner; 33 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 import org.mockito.Mock; 37 38 import java.net.DatagramSocket; 39 import java.net.InetSocketAddress; 40 import java.net.ServerSocket; 41 import java.net.Socket; 42 43 @RunWith(DevSdkIgnoreRunner.class) 44 @SmallTest 45 @DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.R) 46 public class QosSocketInfoTest { 47 @Mock 48 private Network mMockNetwork = mock(Network.class); 49 50 @Test testConstructWithSock()51 public void testConstructWithSock() throws Exception { 52 ServerSocket server = new ServerSocket(); 53 ServerSocket server6 = new ServerSocket(); 54 55 InetSocketAddress clientAddr = new InetSocketAddress("127.0.0.1", 0); 56 InetSocketAddress serverAddr = new InetSocketAddress("127.0.0.1", 0); 57 InetSocketAddress clientAddr6 = new InetSocketAddress("::1", 0); 58 InetSocketAddress serverAddr6 = new InetSocketAddress("::1", 0); 59 server.bind(serverAddr); 60 server6.bind(serverAddr6); 61 Socket socket = new Socket(serverAddr.getAddress(), server.getLocalPort(), 62 clientAddr.getAddress(), clientAddr.getPort()); 63 Socket socket6 = new Socket(serverAddr6.getAddress(), server6.getLocalPort(), 64 clientAddr6.getAddress(), clientAddr6.getPort()); 65 QosSocketInfo sockInfo = new QosSocketInfo(mMockNetwork, socket); 66 QosSocketInfo sockInfo6 = new QosSocketInfo(mMockNetwork, socket6); 67 assertTrue(sockInfo.getLocalSocketAddress() 68 .equals(new InetSocketAddress(socket.getLocalAddress(), socket.getLocalPort()))); 69 assertTrue(sockInfo.getRemoteSocketAddress() 70 .equals((InetSocketAddress) socket.getRemoteSocketAddress())); 71 assertEquals(SOCK_STREAM, sockInfo.getSocketType()); 72 assertTrue(sockInfo6.getLocalSocketAddress() 73 .equals(new InetSocketAddress(socket6.getLocalAddress(), socket6.getLocalPort()))); 74 assertTrue(sockInfo6.getRemoteSocketAddress() 75 .equals((InetSocketAddress) socket6.getRemoteSocketAddress())); 76 assertEquals(SOCK_STREAM, sockInfo6.getSocketType()); 77 socket.close(); 78 socket6.close(); 79 server.close(); 80 server6.close(); 81 } 82 83 @Test testConstructWithDatagramSock()84 public void testConstructWithDatagramSock() throws Exception { 85 InetSocketAddress clientAddr = new InetSocketAddress("127.0.0.1", 0); 86 InetSocketAddress serverAddr = new InetSocketAddress("127.0.0.1", 0); 87 InetSocketAddress clientAddr6 = new InetSocketAddress("::1", 0); 88 InetSocketAddress serverAddr6 = new InetSocketAddress("::1", 0); 89 DatagramSocket socket = new DatagramSocket(null); 90 socket.setReuseAddress(true); 91 socket.bind(clientAddr); 92 socket.connect(serverAddr); 93 DatagramSocket socket6 = new DatagramSocket(null); 94 socket6.setReuseAddress(true); 95 socket6.bind(clientAddr); 96 socket6.connect(serverAddr); 97 QosSocketInfo sockInfo = new QosSocketInfo(mMockNetwork, socket); 98 QosSocketInfo sockInfo6 = new QosSocketInfo(mMockNetwork, socket6); 99 assertTrue(sockInfo.getLocalSocketAddress() 100 .equals((InetSocketAddress) socket.getLocalSocketAddress())); 101 assertTrue(sockInfo.getRemoteSocketAddress() 102 .equals((InetSocketAddress) socket.getRemoteSocketAddress())); 103 assertEquals(SOCK_DGRAM, sockInfo.getSocketType()); 104 assertTrue(sockInfo6.getLocalSocketAddress() 105 .equals((InetSocketAddress) socket6.getLocalSocketAddress())); 106 assertTrue(sockInfo6.getRemoteSocketAddress() 107 .equals((InetSocketAddress) socket6.getRemoteSocketAddress())); 108 assertEquals(SOCK_DGRAM, sockInfo6.getSocketType()); 109 socket.close(); 110 } 111 } 112