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 java.nio.charset.StandardCharsets.UTF_8; 20 21 import com.google.protobuf.ByteString; 22 import io.grpc.Status; 23 import io.grpc.alts.internal.Handshaker.HandshakerResp; 24 import io.grpc.alts.internal.Handshaker.HandshakerResult; 25 import io.grpc.alts.internal.Handshaker.HandshakerStatus; 26 import io.grpc.alts.internal.Handshaker.Identity; 27 import java.nio.ByteBuffer; 28 import java.nio.ByteOrder; 29 import java.security.SecureRandom; 30 import java.util.Random; 31 32 /** A class for mocking ALTS Handshaker Responses. */ 33 class MockAltsHandshakerResp { 34 private static final String TEST_ERROR_DETAILS = "handshake error"; 35 private static final String TEST_APPLICATION_PROTOCOL = "grpc"; 36 private static final String TEST_RECORD_PROTOCOL = "ALTSRP_GCM_AES128"; 37 private static final String TEST_OUT_FRAME = "output frame"; 38 private static final String TEST_LOCAL_ACCOUNT = "local@developer.gserviceaccount.com"; 39 private static final String TEST_PEER_ACCOUNT = "peer@developer.gserviceaccount.com"; 40 private static final byte[] TEST_KEY_DATA = initializeTestKeyData(); 41 private static final int FRAME_HEADER_SIZE = 4; 42 getTestErrorDetails()43 static String getTestErrorDetails() { 44 return TEST_ERROR_DETAILS; 45 } 46 getTestPeerAccount()47 static String getTestPeerAccount() { 48 return TEST_PEER_ACCOUNT; 49 } 50 initializeTestKeyData()51 private static byte[] initializeTestKeyData() { 52 Random random = new SecureRandom(); 53 byte[] randombytes = new byte[AltsChannelCrypter.getKeyLength()]; 54 random.nextBytes(randombytes); 55 return randombytes; 56 } 57 getTestKeyData()58 static byte[] getTestKeyData() { 59 return TEST_KEY_DATA; 60 } 61 62 /** Returns a mock output frame. */ getOutFrame()63 static ByteString getOutFrame() { 64 int frameSize = TEST_OUT_FRAME.length(); 65 ByteBuffer buffer = ByteBuffer.allocate(FRAME_HEADER_SIZE + frameSize); 66 buffer.order(ByteOrder.LITTLE_ENDIAN); 67 buffer.putInt(frameSize); 68 buffer.put(TEST_OUT_FRAME.getBytes(UTF_8)); 69 buffer.flip(); 70 return ByteString.copyFrom(buffer); 71 } 72 73 /** Returns a mock error handshaker response. */ getErrorResponse()74 static HandshakerResp getErrorResponse() { 75 HandshakerResp.Builder resp = HandshakerResp.newBuilder(); 76 resp.setStatus( 77 HandshakerStatus.newBuilder() 78 .setCode(Status.Code.UNKNOWN.value()) 79 .setDetails(TEST_ERROR_DETAILS) 80 .build()); 81 return resp.build(); 82 } 83 84 /** Returns a mock normal handshaker response. */ getOkResponse(int bytesConsumed)85 static HandshakerResp getOkResponse(int bytesConsumed) { 86 HandshakerResp.Builder resp = HandshakerResp.newBuilder(); 87 resp.setOutFrames(getOutFrame()); 88 resp.setBytesConsumed(bytesConsumed); 89 resp.setStatus(HandshakerStatus.newBuilder().setCode(Status.Code.OK.value()).build()); 90 return resp.build(); 91 } 92 93 /** Returns a mock normal handshaker response. */ getEmptyOutFrameResponse(int bytesConsumed)94 static HandshakerResp getEmptyOutFrameResponse(int bytesConsumed) { 95 HandshakerResp.Builder resp = HandshakerResp.newBuilder(); 96 resp.setBytesConsumed(bytesConsumed); 97 resp.setStatus(HandshakerStatus.newBuilder().setCode(Status.Code.OK.value()).build()); 98 return resp.build(); 99 } 100 101 /** Returns a mock final handshaker response with handshake result. */ getFinishedResponse(int bytesConsumed)102 static HandshakerResp getFinishedResponse(int bytesConsumed) { 103 HandshakerResp.Builder resp = HandshakerResp.newBuilder(); 104 HandshakerResult.Builder result = 105 HandshakerResult.newBuilder() 106 .setApplicationProtocol(TEST_APPLICATION_PROTOCOL) 107 .setRecordProtocol(TEST_RECORD_PROTOCOL) 108 .setPeerIdentity(Identity.newBuilder().setServiceAccount(TEST_PEER_ACCOUNT).build()) 109 .setLocalIdentity(Identity.newBuilder().setServiceAccount(TEST_LOCAL_ACCOUNT).build()) 110 .setKeyData(ByteString.copyFrom(TEST_KEY_DATA)); 111 resp.setOutFrames(getOutFrame()); 112 resp.setBytesConsumed(bytesConsumed); 113 resp.setStatus(HandshakerStatus.newBuilder().setCode(Status.Code.OK.value()).build()); 114 resp.setResult(result.build()); 115 return resp.build(); 116 } 117 } 118