View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.scm.provider.svn.svnexe.command;
20  
21  import java.io.File;
22  
23  import org.apache.maven.scm.ScmTestCase;
24  import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
25  import org.codehaus.plexus.util.Os;
26  import org.codehaus.plexus.util.cli.Commandline;
27  import org.junit.Test;
28  
29  import static org.junit.Assert.assertEquals;
30  import static org.junit.Assert.assertNotEquals;
31  
32  /**
33   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
34   *
35   */
36  public class SvnCommandLineUtilsTest extends ScmTestCase {
37      @Test
38      public void testCryptPassword() throws Exception {
39          /* FIXME Plexus does not quote the crypted password on Windows which is actually incorrect at the moment
40           * it would cause wildcard expansion with cmd: https://github.com/codehaus-plexus/plexus-utils/issues/37.
41           */
42          SvnScmProviderRepository repo = new SvnScmProviderRepository(
43                  "https://svn.apache.org/repos/asf/maven/scm/trunk", "username", "password");
44          String clString =
45                  SvnCommandLineUtils.cryptPassword(SvnCommandLineUtils.getBaseSvnCommandLine(new File("."), repo));
46          Commandline expectedCmd =
47                  new Commandline("svn --username username --password ***** --no-auth-cache --non-interactive");
48          expectedCmd.setWorkingDirectory(new File(".").getAbsolutePath());
49          assertEquals(expectedCmd.toString(), clString);
50  
51          repo = new SvnScmProviderRepository("https://svn.apache.org/repos/asf/maven/scm/trunk", "username", null);
52          clString = SvnCommandLineUtils.cryptPassword(SvnCommandLineUtils.getBaseSvnCommandLine(new File("."), repo));
53          assertCommandLine(
54                  "svn --username username --no-auth-cache --non-interactive",
55                  new File("."),
56                  SvnCommandLineUtils.getBaseSvnCommandLine(new File("."), repo));
57  
58          repo = new SvnScmProviderRepository(
59                  "https://svn.apache.org/repos/asf/maven/scm/trunk", "username", "password with spaces");
60          clString = SvnCommandLineUtils.cryptPassword(SvnCommandLineUtils.getBaseSvnCommandLine(new File("."), repo));
61          expectedCmd = new Commandline("svn --username username --password ***** --no-auth-cache --non-interactive");
62          expectedCmd.setWorkingDirectory(new File(".").getAbsolutePath());
63          assertEquals(expectedCmd.toString(), clString);
64  
65          repo = new SvnScmProviderRepository(
66                  "https://svn.apache.org/repos/asf/maven/scm/trunk", "username", "password'with'single'quotes");
67          clString = SvnCommandLineUtils.cryptPassword(SvnCommandLineUtils.getBaseSvnCommandLine(new File("."), repo));
68          expectedCmd = new Commandline("svn --username username --password ***** --no-auth-cache --non-interactive");
69          expectedCmd.setWorkingDirectory(new File(".").getAbsolutePath());
70          assertEquals(expectedCmd.toString(), clString);
71  
72          repo = new SvnScmProviderRepository(
73                  "https://svn.apache.org/repos/asf/maven/scm/trunk",
74                  "username",
75                  "password'with'single'quotes and spaces");
76          clString = SvnCommandLineUtils.cryptPassword(SvnCommandLineUtils.getBaseSvnCommandLine(new File("."), repo));
77          expectedCmd = new Commandline("svn --username username --password ***** --no-auth-cache --non-interactive");
78          expectedCmd.setWorkingDirectory(new File(".").getAbsolutePath());
79          assertEquals(expectedCmd.toString(), clString);
80  
81          repo = new SvnScmProviderRepository(
82                  "https://svn.apache.org/repos/asf/maven/scm/trunk", "username", "password\"with\"double\"quotes");
83          clString = SvnCommandLineUtils.cryptPassword(SvnCommandLineUtils.getBaseSvnCommandLine(new File("."), repo));
84          expectedCmd = new Commandline("svn --username username --password ***** --no-auth-cache --non-interactive");
85          expectedCmd.setWorkingDirectory(new File(".").getAbsolutePath());
86          assertEquals(expectedCmd.toString(), clString);
87  
88          repo = new SvnScmProviderRepository(
89                  "https://svn.apache.org/repos/asf/maven/scm/trunk",
90                  "username",
91                  "password\"with\"double\"quotes and spaces");
92          clString = SvnCommandLineUtils.cryptPassword(SvnCommandLineUtils.getBaseSvnCommandLine(new File("."), repo));
93          expectedCmd = new Commandline("svn --username username --password ***** --no-auth-cache --non-interactive");
94          expectedCmd.setWorkingDirectory(new File(".").getAbsolutePath());
95          // FIXME https://github.com/codehaus-plexus/plexus-utils/issues/36
96          if (Os.isFamily(Os.FAMILY_WINDOWS)) {
97              assertNotEquals(expectedCmd.toString(), clString);
98          } else {
99              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 }