🤬
  • Add LanguageServerOptions for all parameters related to remote language-specific plugin execution.

    PiperOrigin-RevId: 466730537
    Change-Id: I8db01c190460ccfa65bb7986342886e338654ce3
  • Loading...
  • John Y. Kim committed with Copybara-Service 2 years ago
    3620e0ac
    1 parent 56ced2d6
Revision indexing in progress... (symbol navigation in revisions will be accurate after indexed)
  • ■ ■ ■ ■ ■ ■
    main/src/main/java/com/google/tsunami/main/cli/LanguageServerOptions.java
     1 +/*
     2 + * Copyright 2022 Google LLC
     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 +package com.google.tsunami.main.cli;
     17 + 
     18 +import com.beust.jcommander.Parameter;
     19 +import com.beust.jcommander.ParameterException;
     20 +import com.beust.jcommander.Parameters;
     21 +import com.google.tsunami.common.cli.CliOption;
     22 +import com.google.tsunami.common.data.NetworkEndpointUtils;
     23 +import java.nio.file.Files;
     24 +import java.nio.file.Paths;
     25 +import java.util.List;
     26 + 
     27 +/** Command line arguments for Tsunami language servers. */
     28 +@Parameters(separators = "=")
     29 +public final class LanguageServerOptions implements CliOption {
     30 + 
     31 + @Parameter(
     32 + names = "--plugin-server-paths",
     33 + description = "The filename of the language server to run language-spceific plugins.")
     34 + public List<String> pluginServerFilenames;
     35 + 
     36 + @Parameter(
     37 + names = "--plugin-server-ports",
     38 + description =
     39 + "The port of the plugin server to open connection with. If not enough ports were"
     40 + + " specified for the number of language servers specified, an open port will be"
     41 + + " chosen.")
     42 + public List<String> pluginServerPorts;
     43 + 
     44 + @Override
     45 + public void validate() {
     46 + if (pluginServerFilenames != null || pluginServerPorts != null) {
     47 + if (pluginServerFilenames != null && !pluginServerFilenames.isEmpty()) {
     48 + for (String pluginServerFilename : pluginServerFilenames) {
     49 + if (!Files.exists(Paths.get(pluginServerFilename))) {
     50 + throw new ParameterException(
     51 + String.format("Language server path %s does not exist", pluginServerFilename));
     52 + }
     53 + }
     54 + }
     55 + 
     56 + if (pluginServerPorts != null && !pluginServerPorts.isEmpty()) {
     57 + for (String pluginServerPort : pluginServerPorts) {
     58 + try {
     59 + var port = Integer.parseInt(pluginServerPort);
     60 + if (!(port <= NetworkEndpointUtils.MAX_PORT_NUMBER && port > 0)) {
     61 + throw new ParameterException(
     62 + String.format(
     63 + "Port out of range. Expected [0, %s], actual %s.",
     64 + NetworkEndpointUtils.MAX_PORT_NUMBER, pluginServerPort));
     65 + }
     66 + } catch (NumberFormatException e) {
     67 + throw new ParameterException(
     68 + String.format("Port number must be an integer. Got %s instead.", pluginServerPort),
     69 + e);
     70 + }
     71 + }
     72 + }
     73 + 
     74 + if ((pluginServerFilenames != null && pluginServerPorts == null)
     75 + || (pluginServerFilenames == null && pluginServerPorts != null)
     76 + || (pluginServerFilenames.size() != pluginServerPorts.size())) {
     77 + throw new ParameterException(
     78 + String.format(
     79 + "Number of plugin server paths must be equal to number of plugin server ports."
     80 + + " Paths: %s. Ports: %s.",
     81 + pluginServerFilenames.size(), pluginServerPorts.size()));
     82 + }
     83 + }
     84 + }
     85 +}
     86 + 
  • ■ ■ ■ ■ ■ ■
    main/src/test/java/com/google/tsunami/main/cli/LanguageServerOptionsTest.java
     1 +/*
     2 + * Copyright 2022 Google LLC
     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 +package com.google.tsunami.main.cli;
     17 + 
     18 +import static org.junit.Assert.assertThrows;
     19 + 
     20 +import com.beust.jcommander.ParameterException;
     21 +import com.google.common.collect.ImmutableList;
     22 +import com.google.tsunami.common.data.NetworkEndpointUtils;
     23 +import org.junit.Test;
     24 +import org.junit.runner.RunWith;
     25 +import org.junit.runners.JUnit4;
     26 + 
     27 +@RunWith(JUnit4.class)
     28 +public final class LanguageServerOptionsTest {
     29 + 
     30 + @Test
     31 + public void validate_whenPluginServerFilenameDoesNotExist_throwsParameterException() {
     32 + LanguageServerOptions options = new LanguageServerOptions();
     33 + 
     34 + options.pluginServerFilenames = ImmutableList.of("nonexistingfile");
     35 + 
     36 + assertThrows(ParameterException.class, options::validate);
     37 + }
     38 + 
     39 + @Test
     40 + public void validate_whenPortNumberNotInteger_throwsParameterException() {
     41 + LanguageServerOptions options = new LanguageServerOptions();
     42 + 
     43 + options.pluginServerPorts = ImmutableList.of("test");
     44 + 
     45 + assertThrows(ParameterException.class, options::validate);
     46 + }
     47 + 
     48 + @Test
     49 + public void validate_whenPortNumberOutOfRange_throwsParameterException() {
     50 + LanguageServerOptions options = new LanguageServerOptions();
     51 + 
     52 + options.pluginServerPorts = ImmutableList.of("34567", "-1");
     53 + 
     54 + assertThrows(
     55 + "Port out of range. Expected [0, "
     56 + + NetworkEndpointUtils.MAX_PORT_NUMBER
     57 + + "]"
     58 + + ", actual -1",
     59 + ParameterException.class,
     60 + options::validate);
     61 + }
     62 +}
     63 + 
  • ■ ■ ■ ■ ■ ■
    main/src/test/java/com/google/tsunami/main/cli/TsunamiCliTest.java
    skipped 18 lines
    19 19  import static org.mockito.Mockito.times;
    20 20  import static org.mockito.Mockito.verify;
    21 21   
     22 +import com.google.common.collect.ImmutableList;
    22 23  import com.google.common.collect.ImmutableMap;
    23 24  import com.google.inject.AbstractModule;
    24 25  import com.google.inject.Guice;
    skipped 2 lines
    27 28  import com.google.tsunami.common.config.TsunamiConfig;
    28 29  import com.google.tsunami.common.data.NetworkEndpointUtils;
    29 30  import com.google.tsunami.common.time.testing.FakeUtcClockModule;
     31 +import com.google.tsunami.main.cli.server.RemoteServerLoaderModule;
    30 32  import com.google.tsunami.plugin.testing.FailedVulnDetectorBootstrapModule;
    31 33  import com.google.tsunami.plugin.testing.FakePluginExecutionModule;
    32 34  import com.google.tsunami.plugin.testing.FakePortScanner;
    skipped 72 lines
    105 107   install(new FakeServiceFingerprinterBootstrapModule());
    106 108   install(new FakeVulnDetectorBootstrapModule());
    107 109   install(new FakeVulnDetectorBootstrapModule2());
     110 + install(new RemoteServerLoaderModule(ImmutableList.of()));
    108 111   }
    109 112   })
    110 113   .injectMembers(this);
    skipped 170 lines
    281 284   install(new FakePluginExecutionModule());
    282 285   install(new FakePortScannerBootstrapModule());
    283 286   install(new FailedVulnDetectorBootstrapModule());
     287 + install(new RemoteServerLoaderModule(ImmutableList.of()));
    284 288   }
    285 289   })
    286 290   .injectMembers(this);
    skipped 21 lines
Please wait...
Page is in error, reload to recover