0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 package org.apache.spark.network.crypto;
0019
0020 import java.nio.ByteBuffer;
0021 import java.util.Arrays;
0022
0023 import io.netty.buffer.ByteBuf;
0024 import io.netty.buffer.Unpooled;
0025 import org.junit.Test;
0026 import static org.junit.Assert.*;
0027
0028 import org.apache.spark.network.protocol.Encodable;
0029
0030 public class AuthMessagesSuite {
0031
0032 private static int COUNTER = 0;
0033
0034 private static String string() {
0035 return String.valueOf(COUNTER++);
0036 }
0037
0038 private static byte[] byteArray() {
0039 byte[] bytes = new byte[COUNTER++];
0040 for (int i = 0; i < bytes.length; i++) {
0041 bytes[i] = (byte) COUNTER;
0042 } return bytes;
0043 }
0044
0045 private static int integer() {
0046 return COUNTER++;
0047 }
0048
0049 @Test
0050 public void testClientChallenge() {
0051 ClientChallenge msg = new ClientChallenge(string(), string(), integer(), string(), integer(),
0052 byteArray(), byteArray());
0053 ClientChallenge decoded = ClientChallenge.decodeMessage(encode(msg));
0054
0055 assertEquals(msg.appId, decoded.appId);
0056 assertEquals(msg.kdf, decoded.kdf);
0057 assertEquals(msg.iterations, decoded.iterations);
0058 assertEquals(msg.cipher, decoded.cipher);
0059 assertEquals(msg.keyLength, decoded.keyLength);
0060 assertTrue(Arrays.equals(msg.nonce, decoded.nonce));
0061 assertTrue(Arrays.equals(msg.challenge, decoded.challenge));
0062 }
0063
0064 @Test
0065 public void testServerResponse() {
0066 ServerResponse msg = new ServerResponse(byteArray(), byteArray(), byteArray(), byteArray());
0067 ServerResponse decoded = ServerResponse.decodeMessage(encode(msg));
0068 assertTrue(Arrays.equals(msg.response, decoded.response));
0069 assertTrue(Arrays.equals(msg.nonce, decoded.nonce));
0070 assertTrue(Arrays.equals(msg.inputIv, decoded.inputIv));
0071 assertTrue(Arrays.equals(msg.outputIv, decoded.outputIv));
0072 }
0073
0074 private ByteBuffer encode(Encodable msg) {
0075 ByteBuf buf = Unpooled.buffer();
0076 msg.encode(buf);
0077 return buf.nioBuffer();
0078 }
0079
0080 }