diff --git a/java/com/google/gerrit/server/plugins/AutoRegisterModules.java b/java/com/google/gerrit/server/plugins/AutoRegisterModules.java index fde61ff3c8..9d93ed2fda 100644 --- a/java/com/google/gerrit/server/plugins/AutoRegisterModules.java +++ b/java/com/google/gerrit/server/plugins/AutoRegisterModules.java @@ -158,7 +158,7 @@ class AutoRegisterModules { return; } - if (is("org.apache.sshd.server.Command", clazz)) { + if (is("org.apache.sshd.server.command.Command", clazz)) { sshGen.export(export, clazz); } else if (is("javax.servlet.http.HttpServlet", clazz)) { httpGen.export(export, clazz); diff --git a/java/com/google/gerrit/server/plugins/PluginGuiceEnvironment.java b/java/com/google/gerrit/server/plugins/PluginGuiceEnvironment.java index c032c46675..87e1ca9291 100644 --- a/java/com/google/gerrit/server/plugins/PluginGuiceEnvironment.java +++ b/java/com/google/gerrit/server/plugins/PluginGuiceEnvironment.java @@ -589,7 +589,7 @@ public class PluginGuiceEnvironment { return false; } - if (is("org.apache.sshd.server.Command", type)) { + if (is("org.apache.sshd.server.command.Command", type)) { return false; } diff --git a/javatests/com/google/gerrit/server/BUILD b/javatests/com/google/gerrit/server/BUILD index 60d9d69fcb..248c7d110f 100644 --- a/javatests/com/google/gerrit/server/BUILD +++ b/javatests/com/google/gerrit/server/BUILD @@ -63,6 +63,7 @@ junit_tests( "//java/com/google/gerrit/server/schema", "//java/com/google/gerrit/server/schema/testing", "//java/com/google/gerrit/server/util/time", + "//java/com/google/gerrit/sshd", "//java/com/google/gerrit/testing:assertable-executor", "//java/com/google/gerrit/testing:gerrit-test-util", "//java/com/google/gerrit/truth", diff --git a/javatests/com/google/gerrit/server/plugins/AutoRegisterModulesTest.java b/javatests/com/google/gerrit/server/plugins/AutoRegisterModulesTest.java new file mode 100644 index 0000000000..55c9bc36e3 --- /dev/null +++ b/javatests/com/google/gerrit/server/plugins/AutoRegisterModulesTest.java @@ -0,0 +1,101 @@ +// Copyright (C) 2020 The Android Open Source Project +// +// Licensed 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 com.google.gerrit.server.plugins; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.google.common.collect.Lists; +import com.google.gerrit.extensions.annotations.Export; +import com.google.gerrit.extensions.annotations.Listen; +import com.google.gerrit.sshd.SshCommand; +import java.io.IOException; +import java.io.InputStream; +import java.lang.annotation.Annotation; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import java.util.jar.Manifest; +import org.junit.Test; + +public class AutoRegisterModulesTest { + + @Test + public void shouldRegisterSshCommand() throws InvalidPluginException { + ModuleGenerator sshModule = mock(ModuleGenerator.class); + PluginGuiceEnvironment env = mock(PluginGuiceEnvironment.class); + + when(env.hasSshModule()).thenReturn(true); + when(env.newSshModuleGenerator()).thenReturn(sshModule); + + PluginContentScanner scanner = new TestPluginContextScanner(); + ClassLoader classLoader = this.getClass().getClassLoader(); + + AutoRegisterModules objectUnderTest = + new AutoRegisterModules("test_plugin_name", env, scanner, classLoader); + objectUnderTest.discover(); + + verify(sshModule).setPluginName("test_plugin_name"); + verify(sshModule).export(any(Export.class), eq(TestSshCommand.class)); + } + + @Export(value = "test") + public static class TestSshCommand extends SshCommand { + @Override + protected void run() throws UnloggedFailure, Failure, Exception {} + } + + private static class TestPluginContextScanner implements PluginContentScanner { + + @Override + public Manifest getManifest() throws IOException { + return null; + } + + @Override + public Map, Iterable> scan( + String pluginName, Iterable> annotations) + throws InvalidPluginException { + Map, Iterable> extensions = new HashMap<>(); + extensions.put( + Export.class, + Lists.newArrayList( + new ExtensionMetaData( + "com.google.gerrit.server.plugins.AutoRegisterModulesTest$TestSshCommand", + "com.google.gerrit.extensions.annotations.Export"))); + extensions.put(Listen.class, Lists.newArrayList()); + return extensions; + } + + @Override + public Optional getEntry(String resourcePath) throws IOException { + return null; + } + + @Override + public InputStream getInputStream(PluginEntry entry) throws IOException { + return null; + } + + @Override + public Enumeration entries() { + return null; + } + } +} diff --git a/polygerrit-ui/app/elements/plugins/gr-dom-hooks/gr-dom-hooks.js b/polygerrit-ui/app/elements/plugins/gr-dom-hooks/gr-dom-hooks.js index 5ef9dbf272..b998733b45 100644 --- a/polygerrit-ui/app/elements/plugins/gr-dom-hooks/gr-dom-hooks.js +++ b/polygerrit-ui/app/elements/plugins/gr-dom-hooks/gr-dom-hooks.js @@ -36,7 +36,12 @@ GrDomHooksManager.prototype._getHookName = function(endpointName, if (opt_moduleName) { return endpointName + ' ' + opt_moduleName; } else { - return this._plugin.getPluginName() + '-autogenerated-' + endpointName; + // lowercase in case plugin's name contains uppercase letters + // TODO: this still can not prevent if plugin has invalid char + // other than uppercase, but is the first step + // https://html.spec.whatwg.org/multipage/custom-elements.html#valid-custom-element-name + const pluginName = this._plugin.getPluginName() || 'unknown_plugin'; + return pluginName.toLowerCase() + '-autogenerated-' + endpointName; } };