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 = SvnCommandLineUtils.cryptPassword(
45                  SvnCommandLineUtils.getBaseSvnCommandLine(new File("."), repo, false));
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(
53                  SvnCommandLineUtils.getBaseSvnCommandLine(new File("."), repo, false));
54          assertCommandLine(
55                  "svn --username username --no-auth-cache --non-interactive",
56                  new File("."),
57                  SvnCommandLineUtils.getBaseSvnCommandLine(new File("."), repo, false));
58  
59          repo = new SvnScmProviderRepository(
60                  "https://svn.apache.org/repos/asf/maven/scm/trunk", "username", "password with spaces");
61          clString = SvnCommandLineUtils.cryptPassword(
62                  SvnCommandLineUtils.getBaseSvnCommandLine(new File("."), repo, false));
63          expectedCmd = new Commandline("svn --username username --password ***** --no-auth-cache --non-interactive");
64          expectedCmd.setWorkingDirectory(new File(".").getAbsolutePath());
65          assertEquals(expectedCmd.toString(), clString);
66  
67          repo = new SvnScmProviderRepository(
68                  "https://svn.apache.org/repos/asf/maven/scm/trunk", "username", "password'with'single'quotes");
69          clString = SvnCommandLineUtils.cryptPassword(
70                  SvnCommandLineUtils.getBaseSvnCommandLine(new File("."), repo, false));
71          expectedCmd = new Commandline("svn --username username --password ***** --no-auth-cache --non-interactive");
72          expectedCmd.setWorkingDirectory(new File(".").getAbsolutePath());
73          assertEquals(expectedCmd.toString(), clString);
74  
75          repo = new SvnScmProviderRepository(
76                  "https://svn.apache.org/repos/asf/maven/scm/trunk",
77                  "username",
78                  "password'with'single'quotes and spaces");
79          clString = SvnCommandLineUtils.cryptPassword(
80                  SvnCommandLineUtils.getBaseSvnCommandLine(new File("."), repo, false));
81          expectedCmd = new Commandline("svn --username username --password ***** --no-auth-cache --non-interactive");
82          expectedCmd.setWorkingDirectory(new File(".").getAbsolutePath());
83          assertEquals(expectedCmd.toString(), clString);
84  
85          repo = new SvnScmProviderRepository(
86                  "https://svn.apache.org/repos/asf/maven/scm/trunk", "username", "password\"with\"double\"quotes");
87          clString = SvnCommandLineUtils.cryptPassword(
88                  SvnCommandLineUtils.getBaseSvnCommandLine(new File("."), repo, false));
89          expectedCmd = new Commandline("svn --username username --password ***** --no-auth-cache --non-interactive");
90          expectedCmd.setWorkingDirectory(new File(".").getAbsolutePath());
91          assertEquals(expectedCmd.toString(), clString);
92  
93          repo = new SvnScmProviderRepository(
94                  "https://svn.apache.org/repos/asf/maven/scm/trunk",
95                  "username",
96                  "password\"with\"double\"quotes and spaces");
97          clString = SvnCommandLineUtils.cryptPassword(
98                  SvnCommandLineUtils.getBaseSvnCommandLine(new File("."), repo, false));
99          expectedCmd = new Commandline("svn --username username --password ***** --no-auth-cache --non-interactive");
100         expectedCmd.setWorkingDirectory(new File(".").getAbsolutePath());
101         // FIXME https://github.com/codehaus-plexus/plexus-utils/issues/36
102         if (Os.isFamily(Os.FAMILY_WINDOWS)) {
103             assertNotEquals(expectedCmd.toString(), clString);
104         } else {
105             assertEquals(expectedCmd.toString(), clString);
106         }
107 
108         repo = new SvnScmProviderRepository(
109                 "https://svn.apache.org/repos/asf/maven/scm/trunk",
110                 "username",
111                 "password\"with\"double\"quotes'and'single'quotes");
112         clString = SvnCommandLineUtils.cryptPassword(
113                 SvnCommandLineUtils.getBaseSvnCommandLine(new File("."), repo, false));
114         expectedCmd = new Commandline("svn --username username --password ***** --no-auth-cache --non-interactive");
115         expectedCmd.setWorkingDirectory(new File(".").getAbsolutePath());
116         assertEquals(expectedCmd.toString(), clString);
117 
118         repo = new SvnScmProviderRepository(
119                 "https://svn.apache.org/repos/asf/maven/scm/trunk",
120                 "username",
121                 "password\"with\"double\"quotes'and'single'quotes and spaces");
122         clString = SvnCommandLineUtils.cryptPassword(
123                 SvnCommandLineUtils.getBaseSvnCommandLine(new File("."), repo, false));
124         expectedCmd = new Commandline("svn --username username --password ***** --no-auth-cache --non-interactive");
125         expectedCmd.setWorkingDirectory(new File(".").getAbsolutePath());
126         // FIXME https://github.com/codehaus-plexus/plexus-utils/issues/36
127         if (Os.isFamily(Os.FAMILY_WINDOWS)) {
128             assertNotEquals(expectedCmd.toString(), clString);
129         } else {
130             assertEquals(expectedCmd.toString(), clString);
131         }
132 
133         repo = new SvnScmProviderRepository("https://svn.apache.org/repos/asf/maven/scm/trunk", "username", null);
134         assertCommandLine(
135                 "svn --username username --no-auth-cache --non-interactive",
136                 new File("."),
137                 SvnCommandLineUtils.getBaseSvnCommandLine(new File("."), repo, false));
138     }
139 }