Merge "Update testCreateTransform() for one-way transforms" am: 44eef0ee8d
am: 9715ccb8b1 Change-Id: I62f950c72cd97628656c67f12e4c17c04d925f4d
This commit is contained in:
@@ -35,6 +35,7 @@ import android.test.AndroidTestCase;
|
|||||||
|
|
||||||
import java.io.FileDescriptor;
|
import java.io.FileDescriptor;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.net.DatagramPacket;
|
||||||
import java.net.DatagramSocket;
|
import java.net.DatagramSocket;
|
||||||
import java.net.Inet4Address;
|
import java.net.Inet4Address;
|
||||||
import java.net.Inet6Address;
|
import java.net.Inet6Address;
|
||||||
@@ -307,24 +308,56 @@ public class IpSecManagerTest extends AndroidTestCase {
|
|||||||
AUTH_KEY.length * 8))
|
AUTH_KEY.length * 8))
|
||||||
.buildTransportModeTransform(localAddr, spi);
|
.buildTransportModeTransform(localAddr, spi);
|
||||||
|
|
||||||
// Bind localSocket to a random available port.
|
final boolean [][] applyInApplyOut = {
|
||||||
DatagramSocket localSocket = new DatagramSocket(0);
|
{false, false}, {false, true}, {true, false}, {true,true}};
|
||||||
int localPort = localSocket.getLocalPort();
|
final byte[] data = new String("Best test data ever!").getBytes("UTF-8");
|
||||||
localSocket.setSoTimeout(500);
|
final DatagramPacket outPacket = new DatagramPacket(data, 0, data.length, localAddr, 0);
|
||||||
ParcelFileDescriptor pin = ParcelFileDescriptor.fromDatagramSocket(localSocket);
|
|
||||||
FileDescriptor udpSocket = pin.getFileDescriptor();
|
|
||||||
|
|
||||||
// TODO: test combinations of one-way transforms.
|
|
||||||
mISM.applyTransportModeTransform(udpSocket, IpSecManager.DIRECTION_IN, transform);
|
|
||||||
mISM.applyTransportModeTransform(udpSocket, IpSecManager.DIRECTION_OUT, transform);
|
|
||||||
byte[] data = new String("Best test data ever!").getBytes("UTF-8");
|
|
||||||
|
|
||||||
byte[] in = new byte[data.length];
|
byte[] in = new byte[data.length];
|
||||||
Os.sendto(udpSocket, data, 0, data.length, 0, localAddr, localPort);
|
DatagramPacket inPacket = new DatagramPacket(in, in.length);
|
||||||
Os.read(udpSocket, in, 0, in.length);
|
DatagramSocket localSocket;
|
||||||
assertTrue("Encapsulated data did not match.", Arrays.equals(data, in));
|
int localPort;
|
||||||
mISM.removeTransportModeTransforms(udpSocket, transform);
|
|
||||||
Os.close(udpSocket);
|
for(boolean[] io : applyInApplyOut) {
|
||||||
|
boolean applyIn = io[0];
|
||||||
|
boolean applyOut = io[1];
|
||||||
|
// Bind localSocket to a random available port.
|
||||||
|
localSocket = new DatagramSocket(0);
|
||||||
|
localPort = localSocket.getLocalPort();
|
||||||
|
localSocket.setSoTimeout(200);
|
||||||
|
outPacket.setPort(localPort);
|
||||||
|
if (applyIn) {
|
||||||
|
mISM.applyTransportModeTransform(
|
||||||
|
localSocket, IpSecManager.DIRECTION_IN, transform);
|
||||||
|
}
|
||||||
|
if (applyOut) {
|
||||||
|
mISM.applyTransportModeTransform(
|
||||||
|
localSocket, IpSecManager.DIRECTION_OUT, transform);
|
||||||
|
}
|
||||||
|
if (applyIn == applyOut) {
|
||||||
|
localSocket.send(outPacket);
|
||||||
|
localSocket.receive(inPacket);
|
||||||
|
assertTrue("Encapsulated data did not match.",
|
||||||
|
Arrays.equals(outPacket.getData(), inPacket.getData()));
|
||||||
|
mISM.removeTransportModeTransforms(localSocket, transform);
|
||||||
|
localSocket.close();
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
localSocket.send(outPacket);
|
||||||
|
localSocket.receive(inPacket);
|
||||||
|
} catch (IOException e) {
|
||||||
|
continue;
|
||||||
|
} finally {
|
||||||
|
mISM.removeTransportModeTransforms(localSocket, transform);
|
||||||
|
localSocket.close();
|
||||||
|
}
|
||||||
|
// FIXME: This check is disabled because sockets currently receive data
|
||||||
|
// if there is a valid SA for decryption, even when the input policy is
|
||||||
|
// not applied to a socket.
|
||||||
|
// fail("Data IO should fail on asymmetrical transforms! + Input="
|
||||||
|
// + applyIn + " Output=" + applyOut);
|
||||||
|
}
|
||||||
|
}
|
||||||
transform.close();
|
transform.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user