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.shared.release.exec;
20  
21  import java.io.File;
22  import java.io.Writer;
23  
24  import org.apache.maven.settings.Proxy;
25  import org.apache.maven.settings.Server;
26  import org.apache.maven.settings.Settings;
27  import org.apache.maven.settings.io.xpp3.SettingsXpp3Writer;
28  import org.apache.maven.shared.release.PlexusJUnit4TestCase;
29  import org.apache.maven.shared.release.ReleaseResult;
30  import org.apache.maven.shared.release.env.DefaultReleaseEnvironment;
31  import org.apache.maven.shared.release.util.MavenCrypto;
32  import org.mockito.ArgumentCaptor;
33  import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher;
34  
35  import static org.junit.Assert.assertEquals;
36  import static org.junit.Assert.assertNotEquals;
37  import static org.junit.Assert.assertNotSame;
38  import static org.mockito.Matchers.isA;
39  import static org.mockito.Mockito.mock;
40  import static org.mockito.Mockito.spy;
41  import static org.mockito.Mockito.verify;
42  import static org.mockito.Mockito.when;
43  
44  public class InvokerMavenExecutorTest extends PlexusJUnit4TestCase {
45  
46      private MavenCrypto mavenCrypto;
47  
48      private SecDispatcher secDispatcher;
49  
50      @Override
51      public void setUp() throws Exception {
52          super.setUp();
53  
54          mavenCrypto = lookup(MavenCrypto.class);
55          secDispatcher = lookup(SecDispatcher.class);
56      }
57  
58      public void testEncryptSettings() throws Exception {
59          InvokerMavenExecutor executor = new InvokerMavenExecutor(mavenCrypto);
60  
61          // prepare
62          File workingDirectory = getTestFile("target/working-directory");
63          workingDirectory.mkdirs();
64  
65          Settings settings = new Settings();
66          Server server = new Server();
67          server.setPassphrase("server_passphrase");
68          server.setPassword("server_password");
69          settings.addServer(server);
70          Proxy proxy = new Proxy();
71          proxy.setPassword("proxy_password");
72          settings.addProxy(proxy);
73  
74          DefaultReleaseEnvironment releaseEnvironment = new DefaultReleaseEnvironment();
75          releaseEnvironment.setSettings(settings);
76          releaseEnvironment.setMavenHome(new File(System.getProperty("injectedMavenHome")));
77  
78          InvokerMavenExecutor executorSpy = spy(executor);
79          SettingsXpp3Writer settingsWriter = mock(SettingsXpp3Writer.class);
80  
81          ArgumentCaptor<Settings> encryptedSettings = ArgumentCaptor.forClass(Settings.class);
82  
83          when(executorSpy.getSettingsWriter()).thenReturn(settingsWriter);
84  
85          try {
86              executorSpy.executeGoals(
87                      workingDirectory, "validate", releaseEnvironment, false, null, null, new ReleaseResult());
88          } catch (MavenExecutorException e) {
89          }
90  
91          verify(settingsWriter).write(isA(Writer.class), encryptedSettings.capture());
92  
93          assertNotSame(settings, encryptedSettings.getValue());
94  
95          Server encryptedServer = encryptedSettings.getValue().getServers().get(0);
96          assertEquals("server_passphrase", secDispatcher.decrypt(encryptedServer.getPassphrase()));
97          assertEquals("server_password", secDispatcher.decrypt(encryptedServer.getPassword()));
98  
99          Proxy encryptedProxy = encryptedSettings.getValue().getProxies().get(0);
100         assertEquals("proxy_password", secDispatcher.decrypt(encryptedProxy.getPassword()));
101 
102         File settingsSecurity = new File(System.getProperty("user.home"), ".m2/settings-security.xml");
103         if (settingsSecurity.exists()) {
104             assertNotEquals("server_passphrase", encryptedServer.getPassphrase());
105             assertNotEquals("server_password", encryptedServer.getPassword());
106             assertNotEquals("proxy_password", encryptedProxy.getPassword());
107         }
108     }
109 }