Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Licensed to the Apache Software Foundation (ASF) under one or more
0003  * contributor license agreements.  See the NOTICE file distributed with
0004  * this work for additional information regarding copyright ownership.
0005  * The ASF licenses this file to You under the Apache License, Version 2.0
0006  * (the "License"); you may not use this file except in compliance with
0007  * the License.  You may obtain a copy of the License at
0008  *
0009  *    http://www.apache.org/licenses/LICENSE-2.0
0010  *
0011  * Unless required by applicable law or agreed to in writing, software
0012  * distributed under the License is distributed on an "AS IS" BASIS,
0013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014  * See the License for the specific language governing permissions and
0015  * limitations under the License.
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 }