|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or without |
| 5 | + * modification, are permitted provided that the following conditions are |
| 6 | + * met: |
| 7 | + * |
| 8 | + * * Redistributions of source code must retain the above copyright |
| 9 | + * notice, this list of conditions and the following disclaimer. |
| 10 | + * * Redistributions in binary form must reproduce the above |
| 11 | + * copyright notice, this list of conditions and the following disclaimer |
| 12 | + * in the documentation and/or other materials provided with the |
| 13 | + * distribution. |
| 14 | + * |
| 15 | + * * Neither the name of Google LLC nor the names of its |
| 16 | + * contributors may be used to endorse or promote products derived from |
| 17 | + * this software without specific prior written permission. |
| 18 | + * |
| 19 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | + */ |
| 31 | + |
| 32 | +package com.google.auth.oauth2; |
| 33 | + |
| 34 | +import static org.junit.Assert.assertEquals; |
| 35 | +import static org.junit.Assert.assertTrue; |
| 36 | +import static org.mockito.Mockito.mock; |
| 37 | +import static org.mockito.Mockito.when; |
| 38 | + |
| 39 | +import ch.qos.logback.classic.spi.ILoggingEvent; |
| 40 | +import com.google.api.client.http.GenericUrl; |
| 41 | +import com.google.api.client.http.HttpRequest; |
| 42 | +import com.google.api.client.http.HttpRequestFactory; |
| 43 | +import com.google.api.client.http.UrlEncodedContent; |
| 44 | +import com.google.api.client.util.GenericData; |
| 45 | +import com.google.gson.JsonParser; |
| 46 | +import com.google.gson.JsonSyntaxException; |
| 47 | +import java.io.IOException; |
| 48 | +import java.util.HashMap; |
| 49 | +import java.util.List; |
| 50 | +import java.util.Map; |
| 51 | +import org.junit.Before; |
| 52 | +import org.junit.Test; |
| 53 | +import org.slf4j.Logger; |
| 54 | +import org.slf4j.LoggerFactory; |
| 55 | +import org.slf4j.event.KeyValuePair; |
| 56 | +import org.slf4j.event.Level; |
| 57 | + |
| 58 | +// part of Slf4jUtils test that needs logback dependency |
| 59 | +public class Slf4jUtilsLogbackTest { |
| 60 | + |
| 61 | + private static final Logger LOGGER = LoggerFactory.getLogger(Slf4jUtilsLogbackTest.class); |
| 62 | + |
| 63 | + private TestEnvironmentProvider testEnvironmentProvider; |
| 64 | + |
| 65 | + @Before |
| 66 | + public void setup() { |
| 67 | + testEnvironmentProvider = new TestEnvironmentProvider(); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testLogWithMDC_slf4jLogger() { |
| 72 | + |
| 73 | + TestAppender testAppender = setupTestLogger(); |
| 74 | + |
| 75 | + Map<String, Object> contextMap = new HashMap<>(); |
| 76 | + contextMap.put("key1", "value1"); |
| 77 | + contextMap.put("key2", "value2"); |
| 78 | + Slf4jUtils.logWithMDC(LOGGER, Level.DEBUG, contextMap, "test message"); |
| 79 | + |
| 80 | + assertEquals(1, testAppender.events.size()); |
| 81 | + assertEquals("test message", testAppender.events.get(0).getMessage()); |
| 82 | + |
| 83 | + // Verify MDC content |
| 84 | + ILoggingEvent event = testAppender.events.get(0); |
| 85 | + assertEquals(2, event.getMDCPropertyMap().size()); |
| 86 | + assertEquals(ch.qos.logback.classic.Level.DEBUG, event.getLevel()); |
| 87 | + assertEquals("value1", event.getMDCPropertyMap().get("key1")); |
| 88 | + assertEquals("value2", event.getMDCPropertyMap().get("key2")); |
| 89 | + |
| 90 | + testAppender.stop(); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void testLogWithMDC_INFO() { |
| 95 | + TestAppender testAppender = setupTestLogger(); |
| 96 | + Slf4jUtils.logWithMDC(LOGGER, Level.INFO, new HashMap<>(), "test message"); |
| 97 | + |
| 98 | + assertEquals(1, testAppender.events.size()); |
| 99 | + assertEquals(ch.qos.logback.classic.Level.INFO, testAppender.events.get(0).getLevel()); |
| 100 | + testAppender.stop(); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void testLogWithMDC_TRACE_notEnabled() { |
| 105 | + TestAppender testAppender = setupTestLogger(); |
| 106 | + Slf4jUtils.logWithMDC(LOGGER, Level.TRACE, new HashMap<>(), "test message"); |
| 107 | + |
| 108 | + assertEquals(0, testAppender.events.size()); |
| 109 | + testAppender.stop(); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void testLogWithMDC_WARN() { |
| 114 | + TestAppender testAppender = setupTestLogger(); |
| 115 | + Slf4jUtils.logWithMDC(LOGGER, Level.WARN, new HashMap<>(), "test message"); |
| 116 | + |
| 117 | + assertEquals(1, testAppender.events.size()); |
| 118 | + assertEquals(ch.qos.logback.classic.Level.WARN, testAppender.events.get(0).getLevel()); |
| 119 | + testAppender.stop(); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + public void testLogWithMDC_ERROR() { |
| 124 | + TestAppender testAppender = setupTestLogger(); |
| 125 | + Slf4jUtils.logWithMDC(LOGGER, Level.ERROR, new HashMap<>(), "test message"); |
| 126 | + |
| 127 | + assertEquals(1, testAppender.events.size()); |
| 128 | + assertEquals(ch.qos.logback.classic.Level.ERROR, testAppender.events.get(0).getLevel()); |
| 129 | + testAppender.stop(); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + public void testLogGenericData() { |
| 134 | + // mimic GOOGLE_SDK_JAVA_LOGGING = true |
| 135 | + testEnvironmentProvider.setEnv(LoggingUtils.GOOGLE_SDK_JAVA_LOGGING, "true"); |
| 136 | + LoggingUtils.setEnvironmentProvider(testEnvironmentProvider); |
| 137 | + |
| 138 | + TestAppender testAppender = setupTestLogger(); |
| 139 | + |
| 140 | + GenericData data = new GenericData(); |
| 141 | + data.put("key1", "value1"); |
| 142 | + data.put("token", "value2"); |
| 143 | + |
| 144 | + LoggerProvider loggerProvider = mock(LoggerProvider.class); |
| 145 | + when(loggerProvider.getLogger()).thenReturn(LOGGER); |
| 146 | + LoggingUtils.logResponsePayload(data, loggerProvider, "test generic data"); |
| 147 | + |
| 148 | + assertEquals(1, testAppender.events.size()); |
| 149 | + List<KeyValuePair> keyValuePairs = testAppender.events.get(0).getKeyValuePairs(); |
| 150 | + assertEquals(2, keyValuePairs.size()); |
| 151 | + for (KeyValuePair kvp : keyValuePairs) { |
| 152 | + |
| 153 | + assertTrue( |
| 154 | + "Key should be either 'key1' or 'token'", |
| 155 | + kvp.key.equals("key1") || kvp.key.equals("token")); |
| 156 | + } |
| 157 | + |
| 158 | + testAppender.stop(); |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + public void testLogRequest() throws IOException { |
| 163 | + // mimic GOOGLE_SDK_JAVA_LOGGING = true |
| 164 | + testEnvironmentProvider.setEnv(LoggingUtils.GOOGLE_SDK_JAVA_LOGGING, "true"); |
| 165 | + LoggingUtils.setEnvironmentProvider(testEnvironmentProvider); |
| 166 | + |
| 167 | + TestAppender testAppender = setupTestLogger(); |
| 168 | + |
| 169 | + GenericData tokenRequest = new GenericData(); |
| 170 | + tokenRequest.set("client_id", "clientId"); |
| 171 | + tokenRequest.set("client_secret", "clientSecret"); |
| 172 | + UrlEncodedContent content = new UrlEncodedContent(tokenRequest); |
| 173 | + |
| 174 | + MockHttpTransportFactory mockHttpTransportFactory = new MockHttpTransportFactory(); |
| 175 | + |
| 176 | + HttpRequestFactory requestFactory = mockHttpTransportFactory.create().createRequestFactory(); |
| 177 | + HttpRequest request = |
| 178 | + requestFactory.buildPostRequest(new GenericUrl(OAuth2Utils.TOKEN_SERVER_URI), content); |
| 179 | + |
| 180 | + LoggerProvider loggerProvider = mock(LoggerProvider.class); |
| 181 | + when(loggerProvider.getLogger()).thenReturn(LOGGER); |
| 182 | + LoggingUtils.logRequest(request, loggerProvider, "test log request"); |
| 183 | + |
| 184 | + assertEquals(1, testAppender.events.size()); |
| 185 | + assertEquals("test log request", testAppender.events.get(0).getMessage()); |
| 186 | + List<KeyValuePair> keyValuePairs = testAppender.events.get(0).getKeyValuePairs(); |
| 187 | + assertEquals(4, keyValuePairs.size()); |
| 188 | + for (KeyValuePair kvp : testAppender.events.get(0).getKeyValuePairs()) { |
| 189 | + assertTrue( |
| 190 | + kvp.key.equals("request.headers") |
| 191 | + || kvp.key.equals("request.payload") |
| 192 | + || kvp.key.equals("request.method") |
| 193 | + || kvp.key.equals("request.url")); |
| 194 | + if (kvp.key.equals("request.headers") || kvp.key.equals("request.payload")) { |
| 195 | + assertTrue(isValidJson((String) kvp.value)); |
| 196 | + } |
| 197 | + } |
| 198 | + testAppender.stop(); |
| 199 | + } |
| 200 | + |
| 201 | + boolean isValidJson(String jsonString) { |
| 202 | + try { |
| 203 | + JsonParser.parseString(jsonString); |
| 204 | + return true; |
| 205 | + } catch (JsonSyntaxException e) { |
| 206 | + return false; |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + private TestAppender setupTestLogger() { |
| 211 | + TestAppender testAppender = new TestAppender(); |
| 212 | + testAppender.start(); |
| 213 | + ((ch.qos.logback.classic.Logger) LOGGER).addAppender(testAppender); |
| 214 | + return testAppender; |
| 215 | + } |
| 216 | +} |
0 commit comments