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