1 /* 2 * Copyright 2016 The Android Open Source Project 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 org.conscrypt; 18 19 import static org.junit.Assert.assertFalse; 20 import static org.junit.Assert.assertTrue; 21 22 import java.nio.ByteBuffer; 23 import java.util.ArrayList; 24 25 public class TestSessionBuilder { 26 private int type; 27 28 private boolean sessionDataSet; 29 private byte[] sessionData; 30 private int sessionDataLength; 31 32 private boolean certificatesSet; 33 private ArrayList<byte[]> certificates = new ArrayList<byte[]>(); 34 private int certificatesLength; 35 private ArrayList<Integer> certificateLengths = new ArrayList<Integer>(); 36 37 private boolean ocspDataSet; 38 private ArrayList<byte[]> ocspDatas = new ArrayList<byte[]>(); 39 private int ocspDatasLength; 40 private ArrayList<Integer> ocspDataLengths = new ArrayList<Integer>(); 41 42 private boolean tlsSctDataSet; 43 private byte[] tlsSctData; 44 private int tlsSctDataLength; 45 46 public TestSessionBuilder setType(int type) { 47 this.type = type; 48 return this; 49 } 50 51 public TestSessionBuilder setSessionData(byte[] sessionData) { 52 sessionDataSet = true; 53 this.sessionData = sessionData; 54 sessionDataLength = sessionData.length; 55 return this; 56 } 57 58 public TestSessionBuilder setSessionDataLength(int sessionDataLength) { 59 assertTrue("call setSessionData first", sessionDataSet); 60 this.sessionDataLength = sessionDataLength; 61 return this; 62 } 63 64 public TestSessionBuilder addCertificate(byte[] certificate) { 65 certificatesSet = true; 66 certificates.add(certificate); 67 certificateLengths.add(certificate.length); 68 certificatesLength = certificates.size(); 69 return this; 70 } 71 72 public TestSessionBuilder setCertificatesLength(int certificatesLength) { 73 assertTrue("call addCertificate first", certificatesSet); 74 this.certificatesLength = certificatesLength; 75 return this; 76 } 77 78 public TestSessionBuilder setCertificateLength(int certIndex, int certLength) { 79 assertTrue("call addCertificate first", certificatesSet); 80 certificateLengths.set(certIndex, certLength); 81 return this; 82 } 83 84 public TestSessionBuilder setOcspDataEmpty() { 85 ocspDataSet = true; 86 return this; 87 } 88 89 public TestSessionBuilder addOcspData(byte[] ocspData) { 90 ocspDataSet = true; 91 ocspDatas.add(ocspData); 92 ocspDataLengths.add(ocspData.length); 93 ocspDatasLength = ocspDatas.size(); 94 return this; 95 } 96 97 public TestSessionBuilder setOcspDatasLength(int ocspDatasLength) { 98 assertTrue("Call addOcspData before setting length", ocspDataSet); 99 this.ocspDatasLength = ocspDatasLength; 100 return this; 101 } 102 103 public TestSessionBuilder setOcspDataLength(int ocspDataIndex, int ocspDataLength) { 104 assertTrue("Call addOcspData before setting length", ocspDataSet); 105 this.ocspDataLengths.set(ocspDataIndex, ocspDataLength); 106 return this; 107 } 108 109 public TestSessionBuilder setTlsSctData(byte[] tlsSctData) { 110 tlsSctDataSet = true; 111 this.tlsSctData = tlsSctData.clone(); 112 tlsSctDataLength = tlsSctData.length; 113 return this; 114 } 115 116 public TestSessionBuilder setTlsSctDataLength(int tlsSctDataLength) { 117 assertTrue("Call setTlsSctData before setting length", tlsSctDataSet); 118 this.tlsSctDataLength = tlsSctDataLength; 119 return this; 120 } 121 122 public TestSessionBuilder setTlsSctDataEmpty() { 123 tlsSctDataSet = true; 124 return this; 125 } 126 127 public byte[] build() { 128 assertTrue("Must set session data", sessionDataSet); 129 assertTrue("Must call addCertificate at least once", certificatesSet); 130 131 ByteBuffer buf = ByteBuffer.allocate(4096); 132 buf.putInt(type); 133 134 buf.putInt(sessionDataLength); 135 buf.put(sessionData); 136 137 buf.putInt(certificatesLength); 138 for (int i = 0; i < certificates.size(); i++) { 139 buf.putInt(certificateLengths.get(i)); 140 buf.put(certificates.get(i)); 141 } 142 143 if (ocspDataSet) { 144 buf.putInt(ocspDatasLength); 145 for (int i = 0; i < ocspDatas.size(); i++) { 146 buf.putInt(ocspDataLengths.get(i)); 147 buf.put(ocspDatas.get(i)); 148 } 149 150 if (tlsSctDataSet) { 151 if (tlsSctData == null) { 152 buf.putInt(0); 153 } else { 154 buf.putInt(tlsSctDataLength); 155 buf.put(tlsSctData); 156 } 157 } 158 } else { 159 assertFalse("If ocspData is not set, then tlsSctData must not be set", tlsSctDataSet); 160 } 161 162 buf.flip(); 163 byte[] output = new byte[buf.remaining()]; 164 buf.get(output); 165 return output; 166 } 167 } 168