001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.scm.provider.svn.svnexe.command;
020
021import java.io.File;
022
023import org.apache.maven.scm.ScmTestCase;
024import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
025import org.codehaus.plexus.util.Os;
026import org.codehaus.plexus.util.cli.Commandline;
027import org.junit.Test;
028
029import static org.junit.Assert.assertEquals;
030import static org.junit.Assert.assertNotEquals;
031
032/**
033 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
034 *
035 */
036public class SvnCommandLineUtilsTest extends ScmTestCase {
037    @Test
038    public void testCryptPassword() throws Exception {
039        /* FIXME Plexus does not quote the crypted password on Windows which is actually incorrect at the moment
040         * it would cause wildcard expansion with cmd: https://github.com/codehaus-plexus/plexus-utils/issues/37.
041         */
042        SvnScmProviderRepository repo = new SvnScmProviderRepository(
043                "https://svn.apache.org/repos/asf/maven/scm/trunk", "username", "password");
044        String clString =
045                SvnCommandLineUtils.cryptPassword(SvnCommandLineUtils.getBaseSvnCommandLine(new File("."), repo));
046        Commandline expectedCmd =
047                new Commandline("svn --username username --password ***** --no-auth-cache --non-interactive");
048        expectedCmd.setWorkingDirectory(new File(".").getAbsolutePath());
049        assertEquals(expectedCmd.toString(), clString);
050
051        repo = new SvnScmProviderRepository("https://svn.apache.org/repos/asf/maven/scm/trunk", "username", null);
052        clString = SvnCommandLineUtils.cryptPassword(SvnCommandLineUtils.getBaseSvnCommandLine(new File("."), repo));
053        assertCommandLine(
054                "svn --username username --no-auth-cache --non-interactive",
055                new File("."),
056                SvnCommandLineUtils.getBaseSvnCommandLine(new File("."), repo));
057
058        repo = new SvnScmProviderRepository(
059                "https://svn.apache.org/repos/asf/maven/scm/trunk", "username", "password with spaces");
060        clString = SvnCommandLineUtils.cryptPassword(SvnCommandLineUtils.getBaseSvnCommandLine(new File("."), repo));
061        expectedCmd = new Commandline("svn --username username --password ***** --no-auth-cache --non-interactive");
062        expectedCmd.setWorkingDirectory(new File(".").getAbsolutePath());
063        assertEquals(expectedCmd.toString(), clString);
064
065        repo = new SvnScmProviderRepository(
066                "https://svn.apache.org/repos/asf/maven/scm/trunk", "username", "password'with'single'quotes");
067        clString = SvnCommandLineUtils.cryptPassword(SvnCommandLineUtils.getBaseSvnCommandLine(new File("."), repo));
068        expectedCmd = new Commandline("svn --username username --password ***** --no-auth-cache --non-interactive");
069        expectedCmd.setWorkingDirectory(new File(".").getAbsolutePath());
070        assertEquals(expectedCmd.toString(), clString);
071
072        repo = new SvnScmProviderRepository(
073                "https://svn.apache.org/repos/asf/maven/scm/trunk",
074                "username",
075                "password'with'single'quotes and spaces");
076        clString = SvnCommandLineUtils.cryptPassword(SvnCommandLineUtils.getBaseSvnCommandLine(new File("."), repo));
077        expectedCmd = new Commandline("svn --username username --password ***** --no-auth-cache --non-interactive");
078        expectedCmd.setWorkingDirectory(new File(".").getAbsolutePath());
079        assertEquals(expectedCmd.toString(), clString);
080
081        repo = new SvnScmProviderRepository(
082                "https://svn.apache.org/repos/asf/maven/scm/trunk", "username", "password\"with\"double\"quotes");
083        clString = SvnCommandLineUtils.cryptPassword(SvnCommandLineUtils.getBaseSvnCommandLine(new File("."), repo));
084        expectedCmd = new Commandline("svn --username username --password ***** --no-auth-cache --non-interactive");
085        expectedCmd.setWorkingDirectory(new File(".").getAbsolutePath());
086        assertEquals(expectedCmd.toString(), clString);
087
088        repo = new SvnScmProviderRepository(
089                "https://svn.apache.org/repos/asf/maven/scm/trunk",
090                "username",
091                "password\"with\"double\"quotes and spaces");
092        clString = SvnCommandLineUtils.cryptPassword(SvnCommandLineUtils.getBaseSvnCommandLine(new File("."), repo));
093        expectedCmd = new Commandline("svn --username username --password ***** --no-auth-cache --non-interactive");
094        expectedCmd.setWorkingDirectory(new File(".").getAbsolutePath());
095        // FIXME https://github.com/codehaus-plexus/plexus-utils/issues/36
096        if (Os.isFamily(Os.FAMILY_WINDOWS)) {
097            assertNotEquals(expectedCmd.toString(), clString);
098        } else {
099            assertEquals(expectedCmd.toString(), clString);
100        }
101
102        repo = new SvnScmProviderRepository(
103                "https://svn.apache.org/repos/asf/maven/scm/trunk",
104                "username",
105                "password\"with\"double\"quotes'and'single'quotes");
106        clString = SvnCommandLineUtils.cryptPassword(SvnCommandLineUtils.getBaseSvnCommandLine(new File("."), repo));
107        expectedCmd = new Commandline("svn --username username --password ***** --no-auth-cache --non-interactive");
108        expectedCmd.setWorkingDirectory(new File(".").getAbsolutePath());
109        assertEquals(expectedCmd.toString(), clString);
110
111        repo = new SvnScmProviderRepository(
112                "https://svn.apache.org/repos/asf/maven/scm/trunk",
113                "username",
114                "password\"with\"double\"quotes'and'single'quotes and spaces");
115        clString = SvnCommandLineUtils.cryptPassword(SvnCommandLineUtils.getBaseSvnCommandLine(new File("."), repo));
116        expectedCmd = new Commandline("svn --username username --password ***** --no-auth-cache --non-interactive");
117        expectedCmd.setWorkingDirectory(new File(".").getAbsolutePath());
118        // FIXME https://github.com/codehaus-plexus/plexus-utils/issues/36
119        if (Os.isFamily(Os.FAMILY_WINDOWS)) {
120            assertNotEquals(expectedCmd.toString(), clString);
121        } else {
122            assertEquals(expectedCmd.toString(), clString);
123        }
124
125        repo = new SvnScmProviderRepository("https://svn.apache.org/repos/asf/maven/scm/trunk", "username", null);
126        assertCommandLine(
127                "svn --username username --no-auth-cache --non-interactive",
128                new File("."),
129                SvnCommandLineUtils.getBaseSvnCommandLine(new File("."), repo));
130    }
131}