blob: 001b709903b532f454a438ddee581a61f38ae393 [file] [log] [blame]
Chalard Jean48c6c7d2020-06-25 23:39:15 +09001/*
2 * Copyright (C) 2020 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 com.android.testutils;
18
19import android.net.util.PacketReader;
20import android.os.Handler;
21
22import androidx.annotation.NonNull;
23import androidx.annotation.Nullable;
24
25import com.android.net.module.util.ArrayTrackRecord;
26
27import java.io.FileDescriptor;
28import java.io.FileOutputStream;
29import java.io.IOException;
30import java.nio.ByteBuffer;
31import java.util.Arrays;
32import java.util.function.Predicate;
33
34import kotlin.Lazy;
35import kotlin.LazyKt;
36
Chalard Jeane06c5c82020-06-26 00:16:27 +090037/**
38 * A packet reader that runs on a TAP interface.
39 *
40 * It also implements facilities to reply to received packets.
41 */
Chalard Jean48c6c7d2020-06-25 23:39:15 +090042public class TapPacketReader extends PacketReader {
43 private final FileDescriptor mTapFd;
44 private final ArrayTrackRecord<byte[]> mReceivedPackets = new ArrayTrackRecord<>();
45 private final Lazy<ArrayTrackRecord<byte[]>.ReadHead> mReadHead =
46 LazyKt.lazy(mReceivedPackets::newReadHead);
47
48 public TapPacketReader(Handler h, FileDescriptor tapFd, int maxPacketSize) {
49 super(h, maxPacketSize);
50 mTapFd = tapFd;
51 }
52
Remi NGUYEN VAN3b1e9262020-09-23 15:13:40 +090053
54 /**
55 * Attempt to start the FdEventsReader on its handler thread.
56 *
57 * As opposed to {@link android.net.util.FdEventsReader#start()}, this method will not report
58 * failure to start, so it is only appropriate in tests that will fail later if that happens.
59 */
60 public void startAsyncForTest() {
61 getHandler().post(this::start);
62 }
63
Chalard Jean48c6c7d2020-06-25 23:39:15 +090064 @Override
65 protected FileDescriptor createFd() {
66 return mTapFd;
67 }
68
69 @Override
70 protected void handlePacket(byte[] recvbuf, int length) {
71 final byte[] newPacket = Arrays.copyOf(recvbuf, length);
72 if (!mReceivedPackets.add(newPacket)) {
73 throw new AssertionError("More than " + Integer.MAX_VALUE + " packets outstanding!");
74 }
75 }
76
77 /**
Remi NGUYEN VAN3b1e9262020-09-23 15:13:40 +090078 * @deprecated This method does not actually "pop" (which generally means the last packet).
79 * Use {@link #poll(long)}, which has the same behavior, instead.
80 */
81 @Nullable
82 @Deprecated
83 public byte[] popPacket(long timeoutMs) {
84 return poll(timeoutMs);
85 }
86
87 /**
88 * @deprecated This method does not actually "pop" (which generally means the last packet).
89 * Use {@link #poll(long, Predicate)}, which has the same behavior, instead.
90 */
91 @Nullable
92 @Deprecated
93 public byte[] popPacket(long timeoutMs, @NonNull Predicate<byte[]> filter) {
94 return poll(timeoutMs, filter);
95 }
96
97 /**
Chalard Jean48c6c7d2020-06-25 23:39:15 +090098 * Get the next packet that was received on the interface.
99 */
100 @Nullable
Remi NGUYEN VAN3b1e9262020-09-23 15:13:40 +0900101 public byte[] poll(long timeoutMs) {
Chalard Jean48c6c7d2020-06-25 23:39:15 +0900102 return mReadHead.getValue().poll(timeoutMs, packet -> true);
103 }
104
105 /**
106 * Get the next packet that was received on the interface and matches the specified filter.
107 */
108 @Nullable
Remi NGUYEN VAN3b1e9262020-09-23 15:13:40 +0900109 public byte[] poll(long timeoutMs, @NonNull Predicate<byte[]> filter) {
Chalard Jean48c6c7d2020-06-25 23:39:15 +0900110 return mReadHead.getValue().poll(timeoutMs, filter::test);
111 }
112
113 /**
114 * Get the {@link ArrayTrackRecord} that records all packets received by the reader since its
115 * creation.
116 */
117 public ArrayTrackRecord<byte[]> getReceivedPackets() {
118 return mReceivedPackets;
119 }
120
Chalard Jeane06c5c82020-06-26 00:16:27 +0900121 /*
122 * Send a response on the TAP interface.
123 *
124 * The passed ByteBuffer is flipped after use.
125 *
126 * @param packet The packet to send.
127 * @throws IOException if the interface can't be written to.
128 */
Chalard Jean48c6c7d2020-06-25 23:39:15 +0900129 public void sendResponse(final ByteBuffer packet) throws IOException {
130 try (FileOutputStream out = new FileOutputStream(mTapFd)) {
131 byte[] packetBytes = new byte[packet.limit()];
132 packet.get(packetBytes);
133 packet.flip(); // So we can reuse it in the future.
134 out.write(packetBytes);
135 }
136 }
137}