Erik Kline | ade3a8c | 2016-10-19 17:42:01 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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.util; |
| 18 | |
Erik Kline | 84714bf | 2017-05-19 09:29:48 +0900 | [diff] [blame] | 19 | import static android.net.util.PacketReader.DEFAULT_RECV_BUF_SIZE; |
Remi NGUYEN VAN | 231b52b | 2019-01-29 15:38:52 +0900 | [diff] [blame] | 20 | import static android.system.OsConstants.AF_INET6; |
| 21 | import static android.system.OsConstants.IPPROTO_UDP; |
| 22 | import static android.system.OsConstants.SOCK_DGRAM; |
| 23 | import static android.system.OsConstants.SOCK_NONBLOCK; |
| 24 | import static android.system.OsConstants.SOL_SOCKET; |
| 25 | import static android.system.OsConstants.SO_SNDTIMEO; |
| 26 | |
Hugo Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 27 | import static org.junit.Assert.assertEquals; |
| 28 | import static org.junit.Assert.assertFalse; |
| 29 | import static org.junit.Assert.assertTrue; |
| 30 | import static org.junit.Assert.fail; |
Erik Kline | ade3a8c | 2016-10-19 17:42:01 +0900 | [diff] [blame] | 31 | |
Erik Kline | f840e07 | 2017-07-10 20:17:30 +0900 | [diff] [blame] | 32 | import android.os.Handler; |
| 33 | import android.os.HandlerThread; |
Erik Kline | ade3a8c | 2016-10-19 17:42:01 +0900 | [diff] [blame] | 34 | import android.system.ErrnoException; |
| 35 | import android.system.Os; |
| 36 | import android.system.StructTimeval; |
| 37 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 38 | import androidx.test.filters.SmallTest; |
| 39 | import androidx.test.runner.AndroidJUnit4; |
| 40 | |
Remi NGUYEN VAN | 231b52b | 2019-01-29 15:38:52 +0900 | [diff] [blame] | 41 | import org.junit.After; |
| 42 | import org.junit.Before; |
| 43 | import org.junit.Test; |
| 44 | import org.junit.runner.RunWith; |
| 45 | |
Erik Kline | ade3a8c | 2016-10-19 17:42:01 +0900 | [diff] [blame] | 46 | import java.io.FileDescriptor; |
Erik Kline | ade3a8c | 2016-10-19 17:42:01 +0900 | [diff] [blame] | 47 | import java.net.DatagramPacket; |
| 48 | import java.net.DatagramSocket; |
| 49 | import java.net.Inet6Address; |
| 50 | import java.net.InetAddress; |
| 51 | import java.net.InetSocketAddress; |
| 52 | import java.net.SocketException; |
| 53 | import java.util.Arrays; |
| 54 | import java.util.concurrent.CountDownLatch; |
| 55 | import java.util.concurrent.TimeUnit; |
| 56 | |
Erik Kline | ade3a8c | 2016-10-19 17:42:01 +0900 | [diff] [blame] | 57 | /** |
Erik Kline | 84714bf | 2017-05-19 09:29:48 +0900 | [diff] [blame] | 58 | * Tests for PacketReader. |
Erik Kline | ade3a8c | 2016-10-19 17:42:01 +0900 | [diff] [blame] | 59 | * |
| 60 | * @hide |
| 61 | */ |
Hugo Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 62 | @RunWith(AndroidJUnit4.class) |
| 63 | @SmallTest |
Erik Kline | 84714bf | 2017-05-19 09:29:48 +0900 | [diff] [blame] | 64 | public class PacketReaderTest { |
Erik Kline | ade3a8c | 2016-10-19 17:42:01 +0900 | [diff] [blame] | 65 | static final InetAddress LOOPBACK6 = Inet6Address.getLoopbackAddress(); |
| 66 | static final StructTimeval TIMEO = StructTimeval.fromMillis(500); |
| 67 | |
| 68 | protected CountDownLatch mLatch; |
| 69 | protected FileDescriptor mLocalSocket; |
| 70 | protected InetSocketAddress mLocalSockName; |
| 71 | protected byte[] mLastRecvBuf; |
Erik Kline | f840e07 | 2017-07-10 20:17:30 +0900 | [diff] [blame] | 72 | protected boolean mStopped; |
| 73 | protected HandlerThread mHandlerThread; |
Erik Kline | 84714bf | 2017-05-19 09:29:48 +0900 | [diff] [blame] | 74 | protected PacketReader mReceiver; |
Erik Kline | ade3a8c | 2016-10-19 17:42:01 +0900 | [diff] [blame] | 75 | |
Erik Kline | 84714bf | 2017-05-19 09:29:48 +0900 | [diff] [blame] | 76 | class UdpLoopbackReader extends PacketReader { |
Erik Kline | f840e07 | 2017-07-10 20:17:30 +0900 | [diff] [blame] | 77 | public UdpLoopbackReader(Handler h) { |
| 78 | super(h); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | protected FileDescriptor createFd() { |
| 83 | FileDescriptor s = null; |
| 84 | try { |
Remi NGUYEN VAN | 231b52b | 2019-01-29 15:38:52 +0900 | [diff] [blame] | 85 | s = Os.socket(AF_INET6, SOCK_DGRAM | SOCK_NONBLOCK, IPPROTO_UDP); |
Erik Kline | f840e07 | 2017-07-10 20:17:30 +0900 | [diff] [blame] | 86 | Os.bind(s, LOOPBACK6, 0); |
| 87 | mLocalSockName = (InetSocketAddress) Os.getsockname(s); |
| 88 | Os.setsockoptTimeval(s, SOL_SOCKET, SO_SNDTIMEO, TIMEO); |
| 89 | } catch (ErrnoException|SocketException e) { |
| 90 | closeFd(s); |
| 91 | fail(); |
| 92 | return null; |
| 93 | } |
| 94 | |
| 95 | mLocalSocket = s; |
| 96 | return s; |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | protected void handlePacket(byte[] recvbuf, int length) { |
| 101 | mLastRecvBuf = Arrays.copyOf(recvbuf, length); |
| 102 | mLatch.countDown(); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | protected void onStart() { |
| 107 | mStopped = false; |
| 108 | mLatch.countDown(); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | protected void onStop() { |
| 113 | mStopped = true; |
| 114 | mLatch.countDown(); |
| 115 | } |
| 116 | }; |
| 117 | |
Hugo Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 118 | @Before |
Erik Kline | ade3a8c | 2016-10-19 17:42:01 +0900 | [diff] [blame] | 119 | public void setUp() { |
| 120 | resetLatch(); |
| 121 | mLocalSocket = null; |
| 122 | mLocalSockName = null; |
| 123 | mLastRecvBuf = null; |
Erik Kline | f840e07 | 2017-07-10 20:17:30 +0900 | [diff] [blame] | 124 | mStopped = false; |
Erik Kline | ade3a8c | 2016-10-19 17:42:01 +0900 | [diff] [blame] | 125 | |
Erik Kline | 84714bf | 2017-05-19 09:29:48 +0900 | [diff] [blame] | 126 | mHandlerThread = new HandlerThread(PacketReaderTest.class.getSimpleName()); |
Erik Kline | f840e07 | 2017-07-10 20:17:30 +0900 | [diff] [blame] | 127 | mHandlerThread.start(); |
Erik Kline | ade3a8c | 2016-10-19 17:42:01 +0900 | [diff] [blame] | 128 | } |
| 129 | |
Hugo Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 130 | @After |
Erik Kline | f840e07 | 2017-07-10 20:17:30 +0900 | [diff] [blame] | 131 | public void tearDown() throws Exception { |
| 132 | if (mReceiver != null) { |
| 133 | mHandlerThread.getThreadHandler().post(() -> { mReceiver.stop(); }); |
| 134 | waitForActivity(); |
| 135 | } |
Erik Kline | ade3a8c | 2016-10-19 17:42:01 +0900 | [diff] [blame] | 136 | mReceiver = null; |
Erik Kline | f840e07 | 2017-07-10 20:17:30 +0900 | [diff] [blame] | 137 | mHandlerThread.quit(); |
| 138 | mHandlerThread = null; |
Erik Kline | ade3a8c | 2016-10-19 17:42:01 +0900 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | void resetLatch() { mLatch = new CountDownLatch(1); } |
| 142 | |
| 143 | void waitForActivity() throws Exception { |
Erik Kline | f840e07 | 2017-07-10 20:17:30 +0900 | [diff] [blame] | 144 | try { |
| 145 | mLatch.await(1000, TimeUnit.MILLISECONDS); |
| 146 | } finally { |
| 147 | resetLatch(); |
| 148 | } |
Erik Kline | ade3a8c | 2016-10-19 17:42:01 +0900 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | void sendPacket(byte[] contents) throws Exception { |
| 152 | final DatagramSocket sender = new DatagramSocket(); |
| 153 | sender.connect(mLocalSockName); |
| 154 | sender.send(new DatagramPacket(contents, contents.length)); |
| 155 | sender.close(); |
| 156 | } |
| 157 | |
Hugo Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 158 | @Test |
Erik Kline | ade3a8c | 2016-10-19 17:42:01 +0900 | [diff] [blame] | 159 | public void testBasicWorking() throws Exception { |
Erik Kline | f840e07 | 2017-07-10 20:17:30 +0900 | [diff] [blame] | 160 | final Handler h = mHandlerThread.getThreadHandler(); |
| 161 | mReceiver = new UdpLoopbackReader(h); |
| 162 | |
| 163 | h.post(() -> { mReceiver.start(); }); |
| 164 | waitForActivity(); |
Erik Kline | ade3a8c | 2016-10-19 17:42:01 +0900 | [diff] [blame] | 165 | assertTrue(mLocalSockName != null); |
| 166 | assertEquals(LOOPBACK6, mLocalSockName.getAddress()); |
| 167 | assertTrue(0 < mLocalSockName.getPort()); |
| 168 | assertTrue(mLocalSocket != null); |
Erik Kline | f840e07 | 2017-07-10 20:17:30 +0900 | [diff] [blame] | 169 | assertFalse(mStopped); |
Erik Kline | ade3a8c | 2016-10-19 17:42:01 +0900 | [diff] [blame] | 170 | |
| 171 | final byte[] one = "one 1".getBytes("UTF-8"); |
| 172 | sendPacket(one); |
| 173 | waitForActivity(); |
| 174 | assertEquals(1, mReceiver.numPacketsReceived()); |
| 175 | assertTrue(Arrays.equals(one, mLastRecvBuf)); |
Erik Kline | f840e07 | 2017-07-10 20:17:30 +0900 | [diff] [blame] | 176 | assertFalse(mStopped); |
Erik Kline | ade3a8c | 2016-10-19 17:42:01 +0900 | [diff] [blame] | 177 | |
| 178 | final byte[] two = "two 2".getBytes("UTF-8"); |
| 179 | sendPacket(two); |
| 180 | waitForActivity(); |
| 181 | assertEquals(2, mReceiver.numPacketsReceived()); |
| 182 | assertTrue(Arrays.equals(two, mLastRecvBuf)); |
Erik Kline | f840e07 | 2017-07-10 20:17:30 +0900 | [diff] [blame] | 183 | assertFalse(mStopped); |
Erik Kline | ade3a8c | 2016-10-19 17:42:01 +0900 | [diff] [blame] | 184 | |
| 185 | mReceiver.stop(); |
| 186 | waitForActivity(); |
| 187 | assertEquals(2, mReceiver.numPacketsReceived()); |
| 188 | assertTrue(Arrays.equals(two, mLastRecvBuf)); |
Erik Kline | f840e07 | 2017-07-10 20:17:30 +0900 | [diff] [blame] | 189 | assertTrue(mStopped); |
| 190 | mReceiver = null; |
| 191 | } |
| 192 | |
Erik Kline | 84714bf | 2017-05-19 09:29:48 +0900 | [diff] [blame] | 193 | class NullPacketReader extends PacketReader { |
| 194 | public NullPacketReader(Handler h, int recvbufsize) { |
Erik Kline | f840e07 | 2017-07-10 20:17:30 +0900 | [diff] [blame] | 195 | super(h, recvbufsize); |
| 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public FileDescriptor createFd() { return null; } |
| 200 | } |
| 201 | |
Hugo Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 202 | @Test |
Erik Kline | f840e07 | 2017-07-10 20:17:30 +0900 | [diff] [blame] | 203 | public void testMinimalRecvBufSize() throws Exception { |
| 204 | final Handler h = mHandlerThread.getThreadHandler(); |
| 205 | |
| 206 | for (int i : new int[]{-1, 0, 1, DEFAULT_RECV_BUF_SIZE-1}) { |
Erik Kline | 84714bf | 2017-05-19 09:29:48 +0900 | [diff] [blame] | 207 | final PacketReader b = new NullPacketReader(h, i); |
Erik Kline | f840e07 | 2017-07-10 20:17:30 +0900 | [diff] [blame] | 208 | assertEquals(DEFAULT_RECV_BUF_SIZE, b.recvBufSize()); |
| 209 | } |
Erik Kline | ade3a8c | 2016-10-19 17:42:01 +0900 | [diff] [blame] | 210 | } |
| 211 | } |