view mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/HgVcsRootTest.java @ 1068:2d8aab94494f Lakhnau-2020.2.x release-86063

TW-70693
author victory.bedrosova
date Thu, 18 Mar 2021 18:30:34 +0100
parents 471eaf5420c8
children
line wrap: on
line source
/*
 * Copyright 2000-2018 JetBrains s.r.o.
 *
 * 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 jetbrains.buildServer.buildTriggers.vcs.mercurial;

import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.HgVcsRoot;
import jetbrains.buildServer.util.TestFor;
import jetbrains.buildServer.vcs.VcsRoot;
import jetbrains.buildServer.vcs.impl.VcsRootImpl;
import org.testng.annotations.Test;

import java.io.File;

import static jetbrains.buildServer.buildTriggers.vcs.mercurial.VcsRootBuilder.vcsRoot;
import static org.testng.Assert.*;
import static org.testng.AssertJUnit.assertEquals;

/**
 * @author Pavel.Sher
 */
@Test
public class HgVcsRootTest extends BaseMercurialTestCase {

  public void test_url_without_credentials() {
    VcsRootImpl vcsRoot = createVcsRoot("http://host.com/path");
    HgVcsRoot root = new HgVcsRoot(vcsRoot);
    assertEquals("http://user:pwd@host.com/path", root.getRepositoryUrlWithCredentials());
  }

  public void test_url_with_credentials() {
    VcsRootImpl vcsRoot = createVcsRoot("http://user:pwd@host.com/path");
    HgVcsRoot root = new HgVcsRoot(vcsRoot);
    assertEquals("http://user:pwd@host.com/path", root.getRepositoryUrlWithCredentials());
  }

  public void test_url_with_username() {
    VcsRootImpl vcsRoot = createVcsRoot("http://user@host.com/path");
    HgVcsRoot root = new HgVcsRoot(vcsRoot);
    assertEquals("http://user:pwd@host.com/path", root.getRepositoryUrlWithCredentials());
  }

  public void test_url_with_at_after_slash() {
    VcsRootImpl vcsRoot = createVcsRoot("http://host.com/path@");
    HgVcsRoot root = new HgVcsRoot(vcsRoot);
    assertEquals("http://user:pwd@host.com/path@", root.getRepositoryUrlWithCredentials());
  }

  public void test_url_with_at_in_username() {
    VcsRootImpl vcsRoot = createVcsRoot("http://host.com/path", "my.name@gmail.com", "1234");
    HgVcsRoot root = new HgVcsRoot(vcsRoot);
    assertEquals("http://my.name%40gmail.com:1234@host.com/path", root.getRepositoryUrlWithCredentials());
  }

  /** TW-13768 */
  public void test_underscore_in_host() {
		VcsRootImpl vcsRoot = createVcsRoot("http://Klekovkin.SDK_GARANT:8000/", "my.name@gmail.com", "1234");
    HgVcsRoot root = new HgVcsRoot(vcsRoot);
		assertEquals("http://my.name%40gmail.com:1234@Klekovkin.SDK_GARANT:8000/", root.getRepositoryUrlWithCredentials());
	}

  /** TW-13768 */
  public void test_underscore_in_host_with_credentials_in_url() {
    VcsRootImpl vcsRoot = createVcsRoot("http://me:mypass@Klekovkin.SDK_GARANT:8000/");
    HgVcsRoot root = new HgVcsRoot(vcsRoot);
		assertEquals("http://me:mypass@Klekovkin.SDK_GARANT:8000/", root.getRepositoryUrlWithCredentials());
  }

  public void test_windows_path() throws Exception {
    VcsRootImpl vcsRoot = createVcsRoot("c:\\windows\\path");
    HgVcsRoot root = new HgVcsRoot(vcsRoot);
    assertEquals("c:\\windows\\path", root.getRepositoryUrlWithCredentials());
  }

  public void test_file_scheme_has_no_credentials() {
    VcsRootImpl vcsRoot = createVcsRoot("file:///path/to/repo", "my.name@gmail.com", "1234");
    HgVcsRoot root = new HgVcsRoot(vcsRoot);
    assertEquals("file:///path/to/repo", root.getRepositoryUrlWithCredentials());
  }

  //TW-18262
  public void ampersand_in_password() {
    VcsRootImpl vcsRoot = createVcsRoot("http://some.org/path", "user", "m&n");
    HgVcsRoot root = new HgVcsRoot(vcsRoot);
    assertEquals("http://user:m%26n@some.org/path", root.getRepositoryUrlWithCredentials());
  }

  //TW-18835
  public void test_ssh() {
    VcsRootImpl vcsRoot = createVcsRoot("ssh://ourserver.com/mercurialrepo/", "user", "pwd");
    HgVcsRoot root = new HgVcsRoot(vcsRoot);
    assertEquals("ssh://user:pwd@ourserver.com/mercurialrepo/", root.getRepositoryUrlWithCredentials());
  }

  @TestFor(issues = {"TW-36401", "TW-50054"})
  public void test_disabling_custom_clone_dirs() throws Exception {
    File cloneDir = new File("");
    VcsRoot root = vcsRoot().withCloneRepositoryTo(cloneDir).withUrl("http://some.org/repo").build();
    HgVcsRoot hgRoot1 = new HgVcsRoot(root);
    assertNull(hgRoot1.getCustomClonePath());
    setInternalProperty(Constants.CUSTOM_CLONE_PATH_ENABLED, "true");
    HgVcsRoot hgRoot2 = new HgVcsRoot(root);
    assertEquals(cloneDir.getAbsolutePath(), hgRoot2.getCustomClonePath());
  }

  @TestFor(issues = {"TW-36401", "TW-50054"})
  public void custom_clone_path_whitelist() throws Exception {
    assertFalse(HgVcsRoot.isAllowedCustomClonePath(null));
    assertFalse(HgVcsRoot.isAllowedCustomClonePath("/some/path"));
    setInternalProperty(Constants.CUSTOM_CLONE_PATH_WHITELIST, "/some/path1;/some/path2/*");
    assertTrue(HgVcsRoot.isAllowedCustomClonePath("/some/path1"));
    assertTrue(HgVcsRoot.isAllowedCustomClonePath("/some/path2/repo1"));
    assertFalse(HgVcsRoot.isAllowedCustomClonePath("/some/path2/a/b/c"));
    assertFalse(HgVcsRoot.isAllowedCustomClonePath("/other/path"));
    assertFalse(HgVcsRoot.isAllowedCustomClonePath("/some/path21"));

    File tempDir = myTempFiles.createTempDir();
    VcsRoot root = vcsRoot().withCloneRepositoryTo(tempDir).withUrl("http://some.org/repo").build();
    HgVcsRoot hgRoot = new HgVcsRoot(root);
    assertNull(hgRoot.getCustomClonePath());
    setInternalProperty(Constants.CUSTOM_CLONE_PATH_WHITELIST, tempDir.getCanonicalPath());
    hgRoot = new HgVcsRoot(root);
    assertEquals(hgRoot.getCustomClonePath(), tempDir.getCanonicalPath());
  }

  public void hg_path_whitelist() throws Exception {
    assertFalse(HgVcsRoot.isAllowedHgPath(null));
    assertFalse(HgVcsRoot.isAllowedHgPath("/some/path"));
    setInternalProperty(Constants.CUSTOM_SERVER_HG_PATH_WHITELIST, "/some/path1;/some/path2");
    assertTrue(HgVcsRoot.isAllowedHgPath("/some/path1"));
    assertTrue(HgVcsRoot.isAllowedHgPath("/some/path2"));
    assertFalse(HgVcsRoot.isAllowedHgPath("/some/other/path"));
    assertFalse(HgVcsRoot.isAllowedHgPath("/some/path11"));
    assertFalse(HgVcsRoot.isAllowedHgPath("/some/path1/hg"));

    ServerHgPathProvider serverHgPathProvider = new ServerHgPathProvider(new ServerPluginConfigBuilder().build());
    HgVcsRoot root = new HgVcsRoot(vcsRoot().withHgPath("/some/other/path").withUrl("http://some.org/repo").build());
    assertEquals("hg", serverHgPathProvider.getHgPath(root));

    serverHgPathProvider = new ServerHgPathProvider(new ServerPluginConfigBuilder().hgPath("/server/hg/path").build());
    root = new HgVcsRoot(vcsRoot().withHgPath("/some/other/path").withUrl("http://some.org/repo").build());
    assertEquals("/server/hg/path", serverHgPathProvider.getHgPath(root));

    root = new HgVcsRoot(vcsRoot().withHgPath("/some/path1").withUrl("http://some.org/repo").build());
    assertEquals("/some/path1", serverHgPathProvider.getHgPath(root));
    root = new HgVcsRoot(vcsRoot().withHgPath("/some/path2").withUrl("http://some.org/repo").build());
    assertEquals("/some/path2", serverHgPathProvider.getHgPath(root));

    serverHgPathProvider = new ServerHgPathProvider(new ServerPluginConfigBuilder().hgPath("/server/hg/path").build());
    root = new HgVcsRoot(vcsRoot().withHgPath("%teamcity.hg.agent.path%").withUrl("http://some.org/repo").build());
    assertEquals("/server/hg/path", serverHgPathProvider.getHgPath(root));

    serverHgPathProvider = new ServerHgPathProvider(new ServerPluginConfigBuilder().build());
    root = new HgVcsRoot(vcsRoot().withHgPath("%teamcity.hg.agent.path%").withUrl("http://some.org/repo").build());
    assertEquals("hg", serverHgPathProvider.getHgPath(root));
  }

  public void disable_custom_hg_config() throws Exception {
    String customConfig = "[extensions]\nsome.ext =";
    VcsRoot root = vcsRoot().withUrl("http://some.org/repo").withCustomConfig(customConfig).build();
    assertEquals(customConfig, new HgVcsRoot(root).getCustomHgConfig());
    assertEquals("", new HgVcsRoot(root).getCustomHgConfigServer());
    setInternalProperty(Constants.CUSTOM_HG_CONFIG_ENABLED, "true");
    assertEquals(customConfig, new HgVcsRoot(root).getCustomHgConfigServer());
  }

  @TestFor(issues = "TW-70054")
  public void exception_on_command_injection() throws Exception {
    try {
      new HgVcsRoot(vcsRoot().withUrl("--config=hooks.pre-identify=whoami>/tmp/JRNJRN").build());
    } catch (IllegalArgumentException e) {
      // expected
      return;
    }
    fail("Expection not thrown ");
  }

  @TestFor(issues = "TW-70541")
  public void exception_on_command_injection_cwd() throws Exception {
    try {
      new HgVcsRoot(vcsRoot().withUrl("--cwd=/home/user/.BuildServer/system/artifacts/Test/bad_artifact/411/").build());
    } catch (IllegalArgumentException e) {
      // expected
      return;
    }
    fail("Expection not thrown ");
  }

  private VcsRootImpl createVcsRoot(String url) {
    return createVcsRoot(url, "user", "pwd");
  }

  private VcsRootImpl createVcsRoot(String url, String userName, String password) {
    VcsRootImpl vcsRoot = new VcsRootImpl(1, Constants.VCS_NAME);
    vcsRoot.addProperty(Constants.HG_COMMAND_PATH_PROP, "hg.exe");
    vcsRoot.addProperty(Constants.REPOSITORY_PROP, url);
    vcsRoot.addProperty(Constants.USERNAME, userName);
    vcsRoot.addProperty(Constants.PASSWORD, password);
    return vcsRoot;
  }
}