1 /* 2 * Copyright 2018 The gRPC Authors 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 17 package io.grpc.alts.internal; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static org.junit.Assert.fail; 21 22 import java.nio.ByteBuffer; 23 import java.nio.ByteOrder; 24 import java.security.GeneralSecurityException; 25 import org.junit.Test; 26 import org.junit.runner.RunWith; 27 import org.junit.runners.JUnit4; 28 29 /** Unit tests for {@link AltsFraming}. */ 30 @RunWith(JUnit4.class) 31 public class AltsFramingTest { 32 33 @Test parserFrameLengthNegativeFails()34 public void parserFrameLengthNegativeFails() throws GeneralSecurityException { 35 AltsFraming.Parser parser = new AltsFraming.Parser(); 36 // frame length + one remaining byte (required) 37 ByteBuffer buffer = ByteBuffer.allocate(AltsFraming.getFrameLengthHeaderSize() + 1); 38 buffer.order(ByteOrder.LITTLE_ENDIAN); 39 buffer.putInt(-1); // write invalid length 40 buffer.put((byte) 0); // write some byte 41 buffer.flip(); 42 43 try { 44 parser.readBytes(buffer); 45 fail("Exception expected"); 46 } catch (IllegalArgumentException ex) { 47 assertThat(ex).hasMessageThat().contains("Invalid frame length"); 48 } 49 } 50 51 @Test parserFrameLengthSmallerMessageTypeFails()52 public void parserFrameLengthSmallerMessageTypeFails() throws GeneralSecurityException { 53 AltsFraming.Parser parser = new AltsFraming.Parser(); 54 // frame length + one remaining byte (required) 55 ByteBuffer buffer = ByteBuffer.allocate(AltsFraming.getFrameLengthHeaderSize() + 1); 56 buffer.order(ByteOrder.LITTLE_ENDIAN); 57 buffer.putInt(AltsFraming.getFrameMessageTypeHeaderSize() - 1); // write invalid length 58 buffer.put((byte) 0); // write some byte 59 buffer.flip(); 60 61 try { 62 parser.readBytes(buffer); 63 fail("Exception expected"); 64 } catch (IllegalArgumentException ex) { 65 assertThat(ex).hasMessageThat().contains("Invalid frame length"); 66 } 67 } 68 69 @Test parserFrameLengthTooLargeFails()70 public void parserFrameLengthTooLargeFails() throws GeneralSecurityException { 71 AltsFraming.Parser parser = new AltsFraming.Parser(); 72 // frame length + one remaining byte (required) 73 ByteBuffer buffer = ByteBuffer.allocate(AltsFraming.getFrameLengthHeaderSize() + 1); 74 buffer.order(ByteOrder.LITTLE_ENDIAN); 75 buffer.putInt(AltsFraming.getMaxDataLength() + 1); // write invalid length 76 buffer.put((byte) 0); // write some byte 77 buffer.flip(); 78 79 try { 80 parser.readBytes(buffer); 81 fail("Exception expected"); 82 } catch (IllegalArgumentException ex) { 83 assertThat(ex).hasMessageThat().contains("Invalid frame length"); 84 } 85 } 86 87 @Test parserFrameLengthMaxOk()88 public void parserFrameLengthMaxOk() throws GeneralSecurityException { 89 AltsFraming.Parser parser = new AltsFraming.Parser(); 90 // length of type header + data 91 int dataLength = AltsFraming.getMaxDataLength(); 92 // complete frame + 1 byte 93 ByteBuffer buffer = 94 ByteBuffer.allocate(AltsFraming.getFrameLengthHeaderSize() + dataLength + 1); 95 buffer.order(ByteOrder.LITTLE_ENDIAN); 96 buffer.putInt(dataLength); // write invalid length 97 buffer.putInt(6); // default message type 98 buffer.put(new byte[dataLength - AltsFraming.getFrameMessageTypeHeaderSize()]); // write data 99 buffer.put((byte) 0); 100 buffer.flip(); 101 102 parser.readBytes(buffer); 103 104 assertThat(parser.isComplete()).isTrue(); 105 assertThat(buffer.remaining()).isEqualTo(1); 106 } 107 108 @Test parserFrameLengthZeroOk()109 public void parserFrameLengthZeroOk() throws GeneralSecurityException { 110 AltsFraming.Parser parser = new AltsFraming.Parser(); 111 int dataLength = AltsFraming.getFrameMessageTypeHeaderSize(); 112 // complete frame + 1 byte 113 ByteBuffer buffer = 114 ByteBuffer.allocate(AltsFraming.getFrameLengthHeaderSize() + dataLength + 1); 115 buffer.order(ByteOrder.LITTLE_ENDIAN); 116 buffer.putInt(dataLength); // write invalid length 117 buffer.putInt(6); // default message type 118 buffer.put((byte) 0); 119 buffer.flip(); 120 121 parser.readBytes(buffer); 122 123 assertThat(parser.isComplete()).isTrue(); 124 assertThat(buffer.remaining()).isEqualTo(1); 125 } 126 } 127