Home | History | Annotate | Download | only in login
      1 /*
      2  *  Licensed to the Apache Software Foundation (ASF) under one or more
      3  *  contributor license agreements.  See the NOTICE file distributed with
      4  *  this work for additional information regarding copyright ownership.
      5  *  The ASF licenses this file to You under the Apache License, Version 2.0
      6  *  (the "License"); you may not use this file except in compliance with
      7  *  the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  */
     17 
     18 /**
     19 * @author Maxim V. Makarov
     20 */
     21 
     22 package org.apache.harmony.auth.tests.javax.security.auth.login;
     23 
     24 import java.io.File;
     25 import java.io.FileOutputStream;
     26 import java.security.NoSuchAlgorithmException;
     27 import java.security.NoSuchProviderException;
     28 import java.security.Provider;
     29 import java.security.Security;
     30 import java.util.HashMap;
     31 import java.util.Map;
     32 import java.util.Properties;
     33 
     34 import javax.security.auth.AuthPermission;
     35 import javax.security.auth.login.AppConfigurationEntry;
     36 import javax.security.auth.login.Configuration;
     37 import javax.security.auth.login.ConfigurationSpi;
     38 
     39 import junit.framework.TestCase;
     40 
     41 import org.apache.harmony.auth.tests.support.TestUtils;
     42 
     43 import tests.support.Support_Exec;
     44 import tests.support.resource.Support_Resources;
     45 
     46 /**
     47  * Tests Configuration class
     48  */
     49 public class ConfigurationTest extends TestCase {
     50 
     51     // system property to specify another login configuration file
     52     private static final String AUTH_LOGIN_CONFIG = "java.security.auth.login.config";
     53 
     54     // security property to specify default configuration implementation
     55     private static final String LOGIN_CONFIG_PROVIDER = "login.configuration.provider";
     56 
     57     // testing config file
     58     private static String testConfFile = Support_Resources
     59             .getAbsoluteResourcePath("auth.conf");
     60 
     61 	/**
     62 	 * Ease the configuration class
     63 	 */
     64 	public static class ConfTestProvider extends Configuration {
     65 
     66 		@Override
     67         public AppConfigurationEntry[] getAppConfigurationEntry(
     68 				String applicationName) {
     69 			return null;
     70 		}
     71 
     72 		@Override
     73         public void refresh() {
     74 		}
     75 	}
     76 
     77     // default implementation of Configuration class
     78     Configuration defaultConfig;
     79 
     80     // value of java.security.auth.login.config system property
     81     private String oldAuthConfig;
     82 
     83     @Override
     84     protected void setUp() {
     85 
     86         // point to some existing file to be read
     87         oldAuthConfig = System.setProperty(AUTH_LOGIN_CONFIG, "="
     88                 + testConfFile);
     89 
     90         defaultConfig = Configuration.getConfiguration();
     91     }
     92 
     93     @Override
     94     protected void tearDown() {
     95 
     96         TestUtils.setSystemProperty(AUTH_LOGIN_CONFIG, oldAuthConfig);
     97 
     98         Configuration.setConfiguration(defaultConfig);
     99     }
    100 
    101 	/**
    102 	 * Tests loading of a default provider, both valid and invalid class
    103 	 * references.
    104 	 */
    105     public void test_loadDefaultProvider() {
    106 
    107         String oldProvider = Security.getProperty(LOGIN_CONFIG_PROVIDER);
    108         try {
    109             // test: loading custom test provider
    110             Security.setProperty(LOGIN_CONFIG_PROVIDER, ConfTestProvider.class
    111                     .getName());
    112             Configuration.setConfiguration(null); // reset default config
    113             assertEquals(ConfTestProvider.class, Configuration
    114                     .getConfiguration().getClass());
    115 
    116             // test: loading absent class as test provider
    117             Security.setProperty(LOGIN_CONFIG_PROVIDER, "ThereIsNoSuchClass");
    118             Configuration.setConfiguration(null); // reset default config
    119             try {
    120                 Configuration.getConfiguration();
    121                 fail("No SecurityException on failed provider");
    122             } catch (SecurityException ok) {
    123                 assertTrue(ok.getCause() instanceof ClassNotFoundException);
    124             }
    125 
    126             // test: loading wrong class as test provider
    127             // a name of this unit test is used as config provider
    128             Security.setProperty(LOGIN_CONFIG_PROVIDER, this.getClass()
    129                     .getName());
    130             Configuration.setConfiguration(null);// reset default config
    131             try {
    132                 Configuration.getConfiguration();
    133                 fail("No expected ClassCastException");
    134             } catch (ClassCastException ok) {
    135             }
    136 
    137         } finally {
    138             //TODO reset security property if oldProvider==null
    139             Security.setProperty(LOGIN_CONFIG_PROVIDER,
    140                     (oldProvider == null) ? "" : oldProvider);
    141         }
    142     }
    143 
    144     /**
    145      * Tests reading config files by default provider
    146      */
    147     public void test_defaultProvider() throws Exception {
    148 
    149         // test: there are no config files to be read
    150         // Regression for HARMONY-1715
    151 
    152         // no login.config.url.N security properties should be set
    153         String javaSecurityFile = TestUtils
    154                 .createJavaPropertiesFile(new Properties());
    155 
    156         // tmp user home to avoid presence of ${user.home}/.java.login.config
    157         String tmpUserHome = System.getProperty("java.io.tmpdir")
    158                 + File.separatorChar + "tmpUserHomeForConfigurationTest";
    159         File dir = new File(tmpUserHome);
    160         if (!dir.exists()) {
    161             dir.mkdirs();
    162             dir.deleteOnExit();
    163         }
    164         String javaLoginConfig = tmpUserHome + File.separatorChar
    165                 + ".java.login.config";
    166         assertFalse("There should be no login config file: " + javaLoginConfig,
    167                 new File(javaLoginConfig).exists());
    168 
    169         String[] arg = new String[] { "-Duser.home=" + tmpUserHome,
    170                 "-Djava.security.properties=" + javaSecurityFile,
    171                 NoConfigFileToBeRead.class.getName() };
    172 
    173         Support_Exec.execJava(arg, null, true);
    174     }
    175 
    176     public static class NoConfigFileToBeRead {
    177 
    178         // the test is based on assumption that security properties
    179         // login.config.url.N are unset and there is no file
    180         // ${user.home}/.java.login.config
    181         public static void main(String[] args) {
    182 
    183             //reset path to alternative configuration file
    184             TestUtils.setSystemProperty(AUTH_LOGIN_CONFIG, null);
    185 
    186             Configuration.setConfiguration(null); // reset default config
    187             try {
    188                 Configuration.getConfiguration();
    189                 fail("No expected SecurityException");
    190             } catch (SecurityException e) {
    191             }
    192         }
    193     }
    194 
    195     /**
    196      * Tests loading config files specified with the security properties
    197      * login.config.url.N
    198      *
    199      * TODO create test for loading a default config file:
    200      * ${user.home}/.java.login.config
    201      */
    202     public void test_defaultProvider_securityProperties() throws Exception {
    203 
    204         // create tmp config file
    205         File tmpConfFile = File.createTempFile("login", "config");
    206         tmpConfFile.deleteOnExit();
    207 
    208         String newConfFile = "LoginNew {\n org.apache.harmony.auth.module.LoginModule1 optional"
    209                 + " debug=\"true\" test=false;\n};";
    210 
    211         FileOutputStream out = new FileOutputStream(tmpConfFile);
    212         out.write(newConfFile.getBytes());
    213         out.close();
    214 
    215         // set up security properties
    216         Properties props = new Properties();
    217         props.setProperty("login.config.url.1", "file:"
    218                 + tmpConfFile.getCanonicalPath());
    219         props.setProperty("login.config.url.2", "file:"
    220                 + new File(testConfFile).getCanonicalPath());
    221         String javaSecurityFile = TestUtils
    222                 .createJavaPropertiesFile(props);
    223 
    224         // run test
    225         String[] arg = new String[] {
    226                 "-Djava.security.properties=" + javaSecurityFile,
    227                 SecurityPropertiesToBeRead.class.getName() };
    228 
    229         Support_Exec.execJava(arg, null, true);
    230     }
    231 
    232     /**
    233      * @tests javax.security.auth.login.Configuration#getInstance(java.lang.String, javax.security.auth.login.Configuration.Parameters, java.security.Provider)
    234      */
    235     public void test_getInstance_String_Parameters_Provider() throws NoSuchAlgorithmException{
    236         MockConfigurationParameters mcp = new MockConfigurationParameters();
    237         MockProvider mp = new MockProvider();
    238         Configuration cf = Configuration.getInstance("MockType", mcp, mp);
    239         assertEquals("Configuration parameters got should be equals to parameters provided",cf.getParameters(),mcp);
    240         assertEquals("Configuration provider got should be equals to provider provided",cf.getProvider(),mp);
    241         assertEquals("Configuration type got should be equals to type provided",cf.getType(),"MockType");
    242         try{
    243             Configuration.getInstance(null, mcp, mp);
    244             fail("Should throw NullPointerException here");
    245         }
    246         catch(NullPointerException e){
    247             //expect to catch NullPointerException here
    248         }
    249 
    250         try{
    251             Configuration.getInstance("MockType2", mcp, mp);
    252             fail("Should throw NoSuchAlgorithmException here");
    253         }
    254         catch(NoSuchAlgorithmException e){
    255             //expect to catch NoSuchAlgorithmException here
    256         }
    257 
    258         try{
    259             Configuration.getInstance("MockType2", mcp, (Provider)null);
    260             fail("Should throw IllegalArgumentException here");
    261         }
    262         catch(IllegalArgumentException e){
    263             //expect to catch NoSuchAlgorithmException here
    264         }
    265 
    266         cf = Configuration.getInstance("MockType", null, mp);
    267         assertEquals("MockType", cf.getType());
    268         assertNull(cf.getParameters());
    269     }
    270 
    271     /**
    272      * @tests javax.security.auth.login.Configuration#getInstance(java.lang.String, javax.security.auth.login.Configuration.Parameters, java.lang.String)
    273      */
    274     public void test_getInstance_String_Parameters_String() throws NoSuchAlgorithmException, NoSuchProviderException{
    275         MockConfigurationParameters mcp = new MockConfigurationParameters();
    276         MockProvider mp = new MockProvider();
    277         Security.addProvider(mp);
    278         Configuration cf = Configuration.getInstance("MockType", mcp, "MockProvider");
    279 
    280         assertEquals("Configuration parameters got should be equals to parameters provided",cf.getParameters(),mcp);
    281         assertEquals("Configuration provider got should be equals to provider provided",cf.getProvider(),mp);
    282         assertEquals("Configuration type got should be equals to type provided",cf.getType(),"MockType");
    283         try{
    284             Configuration.getInstance(null, mcp, "MockProvider");
    285             fail("Should throw NullPointerException here");
    286         }
    287         catch(NullPointerException e){
    288             //expect to catch NullPointerException here
    289         }
    290 
    291         try{
    292             Configuration.getInstance("MockType2", mcp, "MockProvider");
    293             fail("Should throw NoSuchAlgorithmException here");
    294         }
    295         catch(NoSuchAlgorithmException e){
    296             //expect to catch NoSuchAlgorithmException here
    297         }
    298 
    299         try{
    300             Configuration.getInstance("MockType2", mcp, (String)null);
    301             fail("Should throw IllegalArgumentException here");
    302         }
    303         catch(IllegalArgumentException e){
    304             //expect to catch NoSuchAlgorithmException here
    305         }
    306 
    307         try{
    308             Configuration.getInstance("MockType2", mcp, "");
    309             fail("Should throw IllegalArgumentException here");
    310         }
    311         catch(IllegalArgumentException e){
    312             //expect to catch NoSuchAlgorithmException here
    313         }
    314 
    315         try{
    316             Configuration.getInstance("MockType2", mcp, "not_exist_provider");
    317             fail("Should throw NoSuchProviderException here");
    318         }
    319         catch(NoSuchProviderException e){
    320             //expect to catch NoSuchAlgorithmException here
    321         }
    322 
    323         Security.removeProvider("MockProvider");
    324     }
    325 
    326     /**
    327      * @tests javax.security.auth.login.Configuration#getInstance(java.lang.String, javax.security.auth.login.Configuration.Parameters)
    328      */
    329     public void test_getInstance_String_Parameters() throws NoSuchAlgorithmException{
    330         MockConfigurationParameters mcp = new MockConfigurationParameters();
    331         MockProvider mp = new MockProvider();
    332         Security.addProvider(mp);
    333         Configuration cf = Configuration.getInstance("MockType", mcp);
    334 
    335         assertEquals("Configuration parameters got should be equals to parameters provided",cf.getParameters(),mcp);
    336         assertEquals("Configuration provider got should be equals to provider provided",cf.getProvider(),mp);
    337         assertEquals("Configuration type got should be equals to type provided",cf.getType(),"MockType");
    338 
    339         try{
    340             Configuration.getInstance(null, mcp);
    341             fail("Should throw NullPointerException here");
    342         }
    343         catch(NullPointerException e){
    344             //expect to catch NullPointerException here
    345         }
    346 
    347         try{
    348             Configuration.getInstance("MockType2", mcp);
    349             fail("Should throw NoSuchAlgorithmException here");
    350         }
    351         catch(NoSuchAlgorithmException e){
    352             //expect to catch NoSuchAlgorithmException here
    353         }
    354 
    355         Security.removeProvider("MockProvider");
    356     }
    357 
    358     /**
    359      * @throws NoSuchAlgorithmException
    360      * @tests javax.security.auth.login.Configuration#getProvider()
    361      */
    362     public void test_getProvider() throws NoSuchAlgorithmException{
    363         MockConfigurationParameters mcp = new MockConfigurationParameters();
    364         MockProvider mp = new MockProvider();
    365         Configuration cf = Configuration.getInstance("MockType", mcp, mp);
    366         assertEquals("Configuration provider got should be equals to provider provided",cf.getProvider(),mp);
    367     }
    368 
    369     /**
    370      * @throws NoSuchAlgorithmException
    371      * @tests javax.security.auth.login.Configuration#getProvider()
    372      */
    373     public void test_getParameter() throws NoSuchAlgorithmException{
    374         MockConfigurationParameters mcp = new MockConfigurationParameters();
    375         MockProvider mp = new MockProvider();
    376         Configuration cf = Configuration.getInstance("MockType", mcp, mp);
    377         assertEquals("Configuration parameters got should be equals to parameters provided",cf.getParameters(),mcp);
    378     }
    379 
    380     /**
    381      * @throws NoSuchAlgorithmException
    382      * @tests javax.security.auth.login.Configuration#getProvider()
    383      */
    384     public void test_getType() throws NoSuchAlgorithmException{
    385         MockConfigurationParameters mcp = new MockConfigurationParameters();
    386         MockProvider mp = new MockProvider();
    387         Configuration cf = Configuration.getInstance("MockType", mcp, mp);
    388         assertEquals("Configuration type got should be equals to type provided",cf.getType(),"MockType");
    389     }
    390 
    391     private static class MockConfigurationParameters implements Configuration.Parameters{
    392 
    393     }
    394 
    395     public static class MockConfiguration extends ConfigurationSpi {
    396 
    397         public MockConfiguration(Configuration.Parameters params) {
    398 
    399         }
    400 
    401         @Override
    402         protected AppConfigurationEntry[] engineGetAppConfigurationEntry(
    403                 String name) {
    404             return null;
    405         }
    406     }
    407 
    408     private static class MockProvider extends Provider{
    409         /**
    410          *
    411          */
    412         private static final long serialVersionUID = 1L;
    413 
    414         public MockProvider(){
    415             super("MockProvider",1.0,"MokeProvider for configuration test");
    416             put("Configuration.MockType", MockConfiguration.class.getName());
    417         }
    418     }
    419 
    420 
    421 
    422     public static class SecurityPropertiesToBeRead {
    423 
    424         // the test is based on assumption that security properties
    425         // login.config.url.1 and login.config.url.1 are set
    426         public static void main(String[] args) {
    427 
    428             //reset path to alternative configuration file
    429             TestUtils.setSystemProperty(AUTH_LOGIN_CONFIG, null);
    430 
    431             Configuration.setConfiguration(null); // reset default config
    432 
    433             Configuration config = Configuration.getConfiguration();
    434 
    435             AppConfigurationEntry[] ents = config
    436                     .getAppConfigurationEntry("LoginNew");
    437             assertNotNull(ents);
    438             assertEquals("org.apache.harmony.auth.module.LoginModule1", ents[0]
    439                     .getLoginModuleName());
    440             Map<String, String> m = new HashMap<String, String>();
    441             m.put("debug", "true");
    442             m.put("test", "false");
    443             assertEquals(m, ents[0].getOptions());
    444             assertEquals("LoginModuleControlFlag: optional", ents[0]
    445                     .getControlFlag().toString());
    446 
    447             ents = config.getAppConfigurationEntry("Login1");
    448             assertNotNull(ents);
    449             for (AppConfigurationEntry element : ents) {
    450                 assertEquals("com.intel.security.auth.module.LoginModule1",
    451                         element.getLoginModuleName());
    452                 m.clear();
    453                 m.put("debug1", "true");
    454                 m.put("test1", "false");
    455                 assertEquals(m, element.getOptions());
    456                 assertEquals("LoginModuleControlFlag: required", element
    457                         .getControlFlag().toString());
    458             }
    459         }
    460     }
    461 }
    462