blob: fea1cd9a5ae242ccbf0bc30c88d174ce2eafdf79 [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
37public class TapPacketReader extends PacketReader {
38 private final FileDescriptor mTapFd;
39 private final ArrayTrackRecord<byte[]> mReceivedPackets = new ArrayTrackRecord<>();
40 private final Lazy<ArrayTrackRecord<byte[]>.ReadHead> mReadHead =
41 LazyKt.lazy(mReceivedPackets::newReadHead);
42
43 public TapPacketReader(Handler h, FileDescriptor tapFd, int maxPacketSize) {
44 super(h, maxPacketSize);
45 mTapFd = tapFd;
46 }
47
48 @Override
49 protected FileDescriptor createFd() {
50 return mTapFd;
51 }
52
53 @Override
54 protected void handlePacket(byte[] recvbuf, int length) {
55 final byte[] newPacket = Arrays.copyOf(recvbuf, length);
56 if (!mReceivedPackets.add(newPacket)) {
57 throw new AssertionError("More than " + Integer.MAX_VALUE + " packets outstanding!");
58 }
59 }
60
61 /**
62 * Get the next packet that was received on the interface.
63 */
64 @Nullable
65 public byte[] popPacket(long timeoutMs) {
66 return mReadHead.getValue().poll(timeoutMs, packet -> true);
67 }
68
69 /**
70 * Get the next packet that was received on the interface and matches the specified filter.
71 */
72 @Nullable
73 public byte[] popPacket(long timeoutMs, @NonNull Predicate<byte[]> filter) {
74 return mReadHead.getValue().poll(timeoutMs, filter::test);
75 }
76
77 /**
78 * Get the {@link ArrayTrackRecord} that records all packets received by the reader since its
79 * creation.
80 */
81 public ArrayTrackRecord<byte[]> getReceivedPackets() {
82 return mReceivedPackets;
83 }
84
85 public void sendResponse(final ByteBuffer packet) throws IOException {
86 try (FileOutputStream out = new FileOutputStream(mTapFd)) {
87 byte[] packetBytes = new byte[packet.limit()];
88 packet.get(packetBytes);
89 packet.flip(); // So we can reuse it in the future.
90 out.write(packetBytes);
91 }
92 }
93}