1 // Copyright 2022 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 //////////////////////////////////////////////////////////////////////////////// 16 17 package com.google.crypto.tink.internal.testing; 18 19 import com.google.crypto.tink.monitoring.MonitoringClient; 20 import com.google.crypto.tink.monitoring.MonitoringKeysetInfo; 21 import java.util.ArrayList; 22 import java.util.Collections; 23 import java.util.HashMap; 24 import java.util.List; 25 26 /** 27 * Fake MonitoringClient. 28 * 29 * <p>It logs all log and logFailure calls of its logger objects into two lists that can be 30 * retrieved later. 31 */ 32 public final class FakeMonitoringClient implements MonitoringClient { 33 34 /** LogEntry */ 35 public static final class LogEntry { 36 private final MonitoringKeysetInfo keysetInfo; 37 private final MonitoringKeysetInfo.Entry keyInfo; 38 private final String primitive; 39 private final String api; 40 private final int keyId; 41 private final long numBytesAsInput; 42 LogEntry( MonitoringKeysetInfo keysetInfo, MonitoringKeysetInfo.Entry keyInfo, String primitive, String api, int keyId, long numBytesAsInput)43 private LogEntry( 44 MonitoringKeysetInfo keysetInfo, 45 MonitoringKeysetInfo.Entry keyInfo, 46 String primitive, 47 String api, 48 int keyId, 49 long numBytesAsInput) { 50 this.keysetInfo = keysetInfo; 51 this.keyInfo = keyInfo; 52 this.primitive = primitive; 53 this.api = api; 54 this.keyId = keyId; 55 this.numBytesAsInput = numBytesAsInput; 56 } 57 getKeysetInfo()58 public MonitoringKeysetInfo getKeysetInfo() { 59 return keysetInfo; 60 } 61 getKeyInfo()62 public MonitoringKeysetInfo.Entry getKeyInfo() { 63 return keyInfo; 64 } 65 getPrimitive()66 public String getPrimitive() { 67 return primitive; 68 } 69 getApi()70 public String getApi() { 71 return api; 72 } 73 getKeyId()74 public int getKeyId() { 75 return keyId; 76 } 77 getNumBytesAsInput()78 public long getNumBytesAsInput() { 79 return numBytesAsInput; 80 } 81 } 82 83 /** LogFailureEntry */ 84 public static final class LogFailureEntry { 85 private final String primitive; 86 private final String api; 87 private final MonitoringKeysetInfo keysetInfo; 88 LogFailureEntry( MonitoringKeysetInfo keysetInfo, String primitive, String api)89 private LogFailureEntry( 90 MonitoringKeysetInfo keysetInfo, 91 String primitive, 92 String api) { 93 this.keysetInfo = keysetInfo; 94 this.primitive = primitive; 95 this.api = api; 96 } 97 getPrimitive()98 public String getPrimitive() { 99 return primitive; 100 } 101 getApi()102 public String getApi() { 103 return api; 104 } 105 getKeysetInfo()106 public MonitoringKeysetInfo getKeysetInfo() { 107 return keysetInfo; 108 } 109 } 110 111 private final List<LogEntry> logEntries = new ArrayList<>(); 112 private final List<LogFailureEntry> logFailureEntries = new ArrayList<>(); 113 addLogEntry(LogEntry entry)114 private synchronized void addLogEntry(LogEntry entry) { 115 logEntries.add(entry); 116 } 117 addLogFailureEntry(LogFailureEntry entry)118 private synchronized void addLogFailureEntry(LogFailureEntry entry) { 119 logFailureEntries.add(entry); 120 } 121 122 private final class Logger implements MonitoringClient.Logger { 123 private final MonitoringKeysetInfo keysetInfo; 124 private final HashMap<Integer, MonitoringKeysetInfo.Entry> entries; 125 private final String primitive; 126 private final String api; 127 128 @Override log(int keyId, long numBytesAsInput)129 public void log(int keyId, long numBytesAsInput) { 130 if (!entries.containsKey(keyId)) { 131 throw new IllegalStateException("keyId not found in keysetInfo: " + keyId); 132 } 133 addLogEntry( 134 new LogEntry(keysetInfo, entries.get(keyId), primitive, api, keyId, numBytesAsInput)); 135 } 136 137 @Override logFailure()138 public void logFailure() { 139 addLogFailureEntry(new LogFailureEntry(keysetInfo, primitive, api)); 140 } 141 Logger(MonitoringKeysetInfo keysetInfo, String primitive, String api)142 private Logger(MonitoringKeysetInfo keysetInfo, String primitive, String api) { 143 this.keysetInfo = keysetInfo; 144 this.primitive = primitive; 145 this.api = api; 146 entries = new HashMap<>(); 147 for (MonitoringKeysetInfo.Entry entry : keysetInfo.getEntries()) { 148 entries.put(entry.getKeyId(), entry); 149 } 150 } 151 } 152 FakeMonitoringClient()153 public FakeMonitoringClient() { 154 } 155 156 @Override createLogger(MonitoringKeysetInfo keysetInfo, String primitive, String api)157 public Logger createLogger(MonitoringKeysetInfo keysetInfo, String primitive, String api) { 158 return new Logger(keysetInfo, primitive, api); 159 } 160 161 /** Clears all log and log failure entries. */ clear()162 public synchronized void clear() { 163 logEntries.clear(); 164 logFailureEntries.clear(); 165 } 166 167 /** Returns all log entries. */ getLogEntries()168 public synchronized List<LogEntry> getLogEntries() { 169 return Collections.unmodifiableList(logEntries); 170 } 171 172 /** Returns all log failure entries. */ getLogFailureEntries()173 public synchronized List<LogFailureEntry> getLogFailureEntries() { 174 return Collections.unmodifiableList(logFailureEntries); 175 } 176 } 177