You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by vy...@apache.org on 2024/07/29 13:50:23 UTC

(logging-log4j2) 09/12: Consolidate parameterized and non-parameterized tests

This is an automated email from the ASF dual-hosted git repository.

vy pushed a commit to branch fuzzing
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git

commit 284369039ee909444e5da46499c0a619e1f27499
Author: Volkan Yazıcı <vo...@yazi.ci>
AuthorDate: Mon Jul 29 13:28:34 2024 +0200

    Consolidate parameterized and non-parameterized tests
---
 ...thParamFuzzer.java => PatternLayoutFuzzer.java} | 18 +++---
 .../core/fuzz/PatternLayoutWithoutParamFuzzer.java | 44 --------------
 .../org/apache/logging/log4j/fuzz/FuzzingUtil.java | 67 ++++++++++++++++++++--
 ...amFuzzer.java => JsonTemplateLayoutFuzzer.java} | 18 +++---
 .../fuzz/JsonTemplateLayoutWithParamFuzzer.java    | 42 --------------
 5 files changed, 79 insertions(+), 110 deletions(-)

diff --git a/log4j-core-fuzz-test/src/main/java/org/apache/logging/log4j/core/fuzz/PatternLayoutWithParamFuzzer.java b/log4j-core-fuzz-test/src/main/java/org/apache/logging/log4j/core/fuzz/PatternLayoutFuzzer.java
similarity index 70%
rename from log4j-core-fuzz-test/src/main/java/org/apache/logging/log4j/core/fuzz/PatternLayoutWithParamFuzzer.java
rename to log4j-core-fuzz-test/src/main/java/org/apache/logging/log4j/core/fuzz/PatternLayoutFuzzer.java
index ca3d636c18..d66114c73c 100644
--- a/log4j-core-fuzz-test/src/main/java/org/apache/logging/log4j/core/fuzz/PatternLayoutWithParamFuzzer.java
+++ b/log4j-core-fuzz-test/src/main/java/org/apache/logging/log4j/core/fuzz/PatternLayoutFuzzer.java
@@ -17,28 +17,28 @@
 package org.apache.logging.log4j.core.fuzz;
 
 import static org.apache.logging.log4j.fuzz.FuzzingUtil.createLoggerContext;
-import static org.apache.logging.log4j.fuzz.FuzzingUtil.logWithParams;
+import static org.apache.logging.log4j.fuzz.FuzzingUtil.fuzzLogger;
 
 import com.code_intelligence.jazzer.api.FuzzedDataProvider;
 import org.apache.logging.log4j.Logger;
 import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.fuzz.FuzzingUtil.LoggerFacade;
 import org.apache.logging.log4j.fuzz.LayoutTesterAppender;
 
-public final class PatternLayoutWithParamFuzzer {
+public final class PatternLayoutFuzzer {
 
-    private static final LoggerContext LOGGER_CONTEXT;
+    private static final LoggerFacade LOGGER = createLogger();
 
-    private static final Logger LOGGER;
-
-    static {
-        LOGGER_CONTEXT = createLoggerContext(LayoutTesterAppender.PLUGIN_NAME, configBuilder -> configBuilder
+    private static LoggerFacade createLogger() {
+        final LoggerContext loggerContext = createLoggerContext(LayoutTesterAppender.PLUGIN_NAME, configBuilder -> configBuilder
                 .newLayout("PatternLayout")
                 // Enforce using a single message-based converter, i.e., `MessagePatternConverter`
                 .addAttribute("pattern", "%m"));
-        LOGGER = LOGGER_CONTEXT.getLogger(PatternLayoutWithParamFuzzer.class);
+        final Logger logger = loggerContext.getLogger(PatternLayoutFuzzer.class);
+        return LoggerFacade.ofLog4jLogger(logger);
     }
 
     public static void fuzzerTestOneInput(final FuzzedDataProvider dataProvider) {
-        logWithParams(LOGGER, dataProvider);
+        fuzzLogger(LOGGER, dataProvider);
     }
 }
diff --git a/log4j-core-fuzz-test/src/main/java/org/apache/logging/log4j/core/fuzz/PatternLayoutWithoutParamFuzzer.java b/log4j-core-fuzz-test/src/main/java/org/apache/logging/log4j/core/fuzz/PatternLayoutWithoutParamFuzzer.java
deleted file mode 100644
index 6130c2de76..0000000000
--- a/log4j-core-fuzz-test/src/main/java/org/apache/logging/log4j/core/fuzz/PatternLayoutWithoutParamFuzzer.java
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to you under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.logging.log4j.core.fuzz;
-
-import static org.apache.logging.log4j.fuzz.FuzzingUtil.createLoggerContext;
-import static org.apache.logging.log4j.fuzz.FuzzingUtil.logWithoutParams;
-
-import com.code_intelligence.jazzer.api.FuzzedDataProvider;
-import org.apache.logging.log4j.Logger;
-import org.apache.logging.log4j.core.LoggerContext;
-import org.apache.logging.log4j.fuzz.LayoutTesterAppender;
-
-public final class PatternLayoutWithoutParamFuzzer {
-
-    private static final LoggerContext LOGGER_CONTEXT;
-
-    private static final Logger LOGGER;
-
-    static {
-        LOGGER_CONTEXT = createLoggerContext(LayoutTesterAppender.PLUGIN_NAME, configBuilder -> configBuilder
-                .newLayout("PatternLayout")
-                // Enforce using a single message-based converter, i.e., `MessagePatternConverter`
-                .addAttribute("pattern", "%m"));
-        LOGGER = LOGGER_CONTEXT.getLogger(PatternLayoutWithoutParamFuzzer.class);
-    }
-
-    public static void fuzzerTestOneInput(final FuzzedDataProvider dataProvider) {
-        logWithoutParams(LOGGER, dataProvider);
-    }
-}
diff --git a/log4j-fuzz-test/src/main/java/org/apache/logging/log4j/fuzz/FuzzingUtil.java b/log4j-fuzz-test/src/main/java/org/apache/logging/log4j/fuzz/FuzzingUtil.java
index 36ff8a9103..dc7f8ce1c4 100644
--- a/log4j-fuzz-test/src/main/java/org/apache/logging/log4j/fuzz/FuzzingUtil.java
+++ b/log4j-fuzz-test/src/main/java/org/apache/logging/log4j/fuzz/FuzzingUtil.java
@@ -35,6 +35,8 @@ import org.apache.logging.log4j.core.config.builder.api.RootLoggerComponentBuild
 import org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration;
 import org.apache.logging.log4j.util.Strings;
 
+import static java.util.Objects.requireNonNull;
+
 public final class FuzzingUtil {
 
     private static final int MAX_STRING_LENGTH = 30;
@@ -68,13 +70,66 @@ public final class FuzzingUtil {
         return Configurator.initialize(config);
     }
 
-    public static void logWithoutParams(final Logger logger, final FuzzedDataProvider dataProvider) {
+    /**
+     * Contract modelling an API-agnostic logger.
+     * This allows {@link #fuzzLogger(LoggerFacade, FuzzedDataProvider)} to accept all logger implementations; of Log4j API, of SLF4J, etc.
+     */
+    public interface LoggerFacade {
+
+        void log(String message);
+
+        void log(String message, Throwable throwable);
+
+        void log(String message, Object[] parameters);
+
+        static LoggerFacade ofLog4jLogger(final Logger logger) {
+            requireNonNull(logger, "logger");
+            return new Log4jLoggerFacade(logger);
+        }
+    }
+
+    private static final class Log4jLoggerFacade implements LoggerFacade {
+
+        private final Logger logger;
+
+        private Log4jLoggerFacade(final Logger logger) {
+            this.logger = logger;
+        }
+
+        @Override
+        public void log(final String message) {
+            logger.error(message);
+        }
+
+        @Override
+        public void log(final String message, final Throwable throwable) {
+            logger.error(message, throwable);
+        }
+
+        @Override
+        public void log(final String message, final Object[] parameters) {
+            logger.error(message, parameters);
+        }
+    }
+
+    public static void fuzzLogger(final LoggerFacade logger, final FuzzedDataProvider dataProvider) {
+        requireNonNull(logger, "logger");
+        requireNonNull(dataProvider, "dataProvider");
+        final boolean parameterized = dataProvider.consumeBoolean();
+        if (parameterized) {
+            fuzzLoggerWithParams(logger, dataProvider);
+        } else {
+            fuzzLoggerWithoutParams(logger, dataProvider);
+        }
+    }
+
+    private static void fuzzLoggerWithoutParams(final LoggerFacade logger, final FuzzedDataProvider dataProvider) {
         final String message = dataProvider.consumeString(MAX_STRING_LENGTH);
         final Throwable throwable = createThrowableLogParam(dataProvider);
         if (throwable != null) {
-            logger.error(message, throwable);
+            logger.log(message, throwable);
         } else {
-            logger.error(message);
+            logger.log(message);
         }
     }
 
@@ -88,7 +143,7 @@ public final class FuzzingUtil {
         return new Exception(errorMessage);
     }
 
-    public static void logWithParams(final Logger logger, final FuzzedDataProvider dataProvider) {
+    private static void fuzzLoggerWithParams(final LoggerFacade logger, final FuzzedDataProvider dataProvider) {
         final int paramCount = dataProvider.consumeInt(1, 3);
         final String message = Strings.repeat("{}", paramCount);
         final Throwable throwable = createThrowableLogParam(dataProvider);
@@ -97,9 +152,9 @@ public final class FuzzingUtil {
                 .toArray(Object[]::new);
         if (throwable != null) {
             params[paramCount] = throwable;
-            logger.error(message, params);
+            logger.log(message, params);
         } else {
-            logger.error(message, params);
+            logger.log(message, params);
         }
     }
 
diff --git a/log4j-layout-template-json-fuzz-test/src/main/java/org/apache/logging/log4j/layout/template/json/fuzz/JsonTemplateLayoutWithoutParamFuzzer.java b/log4j-layout-template-json-fuzz-test/src/main/java/org/apache/logging/log4j/layout/template/json/fuzz/JsonTemplateLayoutFuzzer.java
similarity index 70%
rename from log4j-layout-template-json-fuzz-test/src/main/java/org/apache/logging/log4j/layout/template/json/fuzz/JsonTemplateLayoutWithoutParamFuzzer.java
rename to log4j-layout-template-json-fuzz-test/src/main/java/org/apache/logging/log4j/layout/template/json/fuzz/JsonTemplateLayoutFuzzer.java
index 5094c8fd40..f1e9c1d866 100644
--- a/log4j-layout-template-json-fuzz-test/src/main/java/org/apache/logging/log4j/layout/template/json/fuzz/JsonTemplateLayoutWithoutParamFuzzer.java
+++ b/log4j-layout-template-json-fuzz-test/src/main/java/org/apache/logging/log4j/layout/template/json/fuzz/JsonTemplateLayoutFuzzer.java
@@ -17,26 +17,26 @@
 package org.apache.logging.log4j.layout.template.json.fuzz;
 
 import static org.apache.logging.log4j.fuzz.FuzzingUtil.createLoggerContext;
-import static org.apache.logging.log4j.fuzz.FuzzingUtil.logWithoutParams;
+import static org.apache.logging.log4j.fuzz.FuzzingUtil.fuzzLogger;
 
 import com.code_intelligence.jazzer.api.FuzzedDataProvider;
 import org.apache.logging.log4j.Logger;
 import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.fuzz.FuzzingUtil;
 
-public final class JsonTemplateLayoutWithoutParamFuzzer {
+public final class JsonTemplateLayoutFuzzer {
 
-    private static final LoggerContext LOGGER_CONTEXT;
+    private static final FuzzingUtil.LoggerFacade LOGGER = createLogger();
 
-    private static final Logger LOGGER;
-
-    static {
-        LOGGER_CONTEXT = createLoggerContext(
+    private static FuzzingUtil.LoggerFacade createLogger() {
+        final LoggerContext loggerContext = createLoggerContext(
                 JsonEncodingLayoutTesterAppender.PLUGIN_NAME,
                 configBuilder -> configBuilder.newLayout("JsonTemplateLayout"));
-        LOGGER = LOGGER_CONTEXT.getLogger(JsonTemplateLayoutWithoutParamFuzzer.class);
+        final Logger logger = loggerContext.getLogger(JsonTemplateLayoutFuzzer.class);
+        return FuzzingUtil.LoggerFacade.ofLog4jLogger(logger);
     }
 
     public static void fuzzerTestOneInput(final FuzzedDataProvider dataProvider) {
-        logWithoutParams(LOGGER, dataProvider);
+        fuzzLogger(LOGGER, dataProvider);
     }
 }
diff --git a/log4j-layout-template-json-fuzz-test/src/main/java/org/apache/logging/log4j/layout/template/json/fuzz/JsonTemplateLayoutWithParamFuzzer.java b/log4j-layout-template-json-fuzz-test/src/main/java/org/apache/logging/log4j/layout/template/json/fuzz/JsonTemplateLayoutWithParamFuzzer.java
deleted file mode 100644
index a696bb9ea7..0000000000
--- a/log4j-layout-template-json-fuzz-test/src/main/java/org/apache/logging/log4j/layout/template/json/fuzz/JsonTemplateLayoutWithParamFuzzer.java
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to you under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.logging.log4j.layout.template.json.fuzz;
-
-import static org.apache.logging.log4j.fuzz.FuzzingUtil.createLoggerContext;
-import static org.apache.logging.log4j.fuzz.FuzzingUtil.logWithParams;
-
-import com.code_intelligence.jazzer.api.FuzzedDataProvider;
-import org.apache.logging.log4j.Logger;
-import org.apache.logging.log4j.core.LoggerContext;
-
-public final class JsonTemplateLayoutWithParamFuzzer {
-
-    private static final LoggerContext LOGGER_CONTEXT;
-
-    private static final Logger LOGGER;
-
-    static {
-        LOGGER_CONTEXT = createLoggerContext(
-                JsonEncodingLayoutTesterAppender.PLUGIN_NAME,
-                configBuilder -> configBuilder.newLayout("JsonTemplateLayout"));
-        LOGGER = LOGGER_CONTEXT.getLogger(JsonTemplateLayoutWithoutParamFuzzer.class);
-    }
-
-    public static void fuzzerTestOneInput(final FuzzedDataProvider dataProvider) {
-        logWithParams(LOGGER, dataProvider);
-    }
-}