1 package org.apache.maven.it;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.it.util.ResourceExtractor;
23 import org.eclipse.jetty.security.ConstraintMapping;
24 import org.eclipse.jetty.security.ConstraintSecurityHandler;
25 import org.eclipse.jetty.security.HashLoginService;
26 import org.eclipse.jetty.server.NetworkConnector;
27 import org.eclipse.jetty.server.Server;
28 import org.eclipse.jetty.server.handler.DefaultHandler;
29 import org.eclipse.jetty.server.handler.HandlerList;
30 import org.eclipse.jetty.server.handler.ResourceHandler;
31 import org.eclipse.jetty.servlet.ServletContextHandler;
32 import org.eclipse.jetty.util.security.Constraint;
33 import org.eclipse.jetty.util.security.Password;
34
35 import java.io.File;
36 import java.util.Properties;
37
38 import static org.eclipse.jetty.servlet.ServletContextHandler.SECURITY;
39 import static org.eclipse.jetty.servlet.ServletContextHandler.SESSIONS;
40 import static org.eclipse.jetty.util.security.Constraint.__BASIC_AUTH;
41
42
43
44
45
46
47
48 public class MavenITmng4068AuthenticatedMirrorTest
49 extends AbstractMavenIntegrationTestCase
50 {
51
52 private File testDir;
53
54 private Server server;
55
56 private int port;
57
58 public MavenITmng4068AuthenticatedMirrorTest()
59 {
60 super( ALL_MAVEN_VERSIONS );
61 }
62
63 @Override
64 protected void setUp()
65 throws Exception
66 {
67 testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-4068" );
68
69 Constraint constraint = new Constraint();
70 constraint.setName( Constraint.__BASIC_AUTH );
71 constraint.setRoles( new String[] { "user" } );
72 constraint.setAuthenticate( true );
73
74 ConstraintMapping constraintMapping = new ConstraintMapping();
75 constraintMapping.setConstraint( constraint );
76 constraintMapping.setPathSpec( "/*" );
77
78 HashLoginService userRealm = new HashLoginService( "TestRealm" );
79 userRealm.putUser( "testuser", new Password( "testtest" ), new String[] { "user" } );
80
81 server = new Server( 0 );
82 ServletContextHandler ctx = new ServletContextHandler( server, "/", SESSIONS | SECURITY );
83 ConstraintSecurityHandler securityHandler = (ConstraintSecurityHandler) ctx.getSecurityHandler();
84 securityHandler.setLoginService( userRealm );
85 securityHandler.setAuthMethod( __BASIC_AUTH );
86 securityHandler.setConstraintMappings( new ConstraintMapping[] { constraintMapping } );
87
88 ResourceHandler repoHandler = new ResourceHandler();
89 repoHandler.setResourceBase( new File( testDir, "repo" ).getAbsolutePath() );
90
91 HandlerList handlerList = new HandlerList();
92 handlerList.addHandler( securityHandler );
93 handlerList.addHandler( repoHandler );
94 handlerList.addHandler( new DefaultHandler() );
95
96 server.setHandler( handlerList );
97 server.start();
98 if ( server.isFailed() )
99 {
100 fail( "Couldn't bind the server socket to a free port!" );
101 }
102 port = ( (NetworkConnector) server.getConnectors()[0] ).getLocalPort();
103 System.out.println( "Bound server socket to the port " + port );
104 }
105
106 @Override
107 protected void tearDown()
108 throws Exception
109 {
110 if ( server != null )
111 {
112 server.stop();
113 server.join();
114 }
115 }
116
117
118
119
120
121
122
123 public void testit()
124 throws Exception
125 {
126 Properties filterProps = new Properties();
127 filterProps.setProperty( "@mirrorPort@", Integer.toString( port ) );
128
129 Verifier verifier = newVerifier( testDir.getAbsolutePath() );
130 verifier.filterFile( "settings-template.xml", "settings.xml", "UTF-8", filterProps );
131 verifier.setAutoclean( false );
132 verifier.deleteArtifacts( "org.apache.maven.its.mng4068" );
133 verifier.addCliOption( "--settings" );
134 verifier.addCliOption( "settings.xml" );
135 verifier.executeGoal( "validate" );
136 verifier.verifyErrorFreeLog();
137 verifier.resetStreams();
138
139 verifier.assertArtifactPresent( "org.apache.maven.its.mng4068", "a", "0.1", "jar" );
140 verifier.assertArtifactPresent( "org.apache.maven.its.mng4068", "b", "0.1-SNAPSHOT", "jar" );
141 }
142
143 }