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 22package org.apache.harmony.auth.tests.login; 23 24import java.io.File; 25import java.io.FileOutputStream; 26import java.io.OutputStream; 27import java.net.MalformedURLException; 28import java.net.URL; 29import java.util.HashMap; 30import java.util.Iterator; 31import java.util.List; 32import java.util.Map; 33import java.util.Properties; 34 35import javax.security.auth.login.AppConfigurationEntry; 36 37import junit.framework.TestCase; 38 39import org.apache.harmony.auth.login.DefaultConfigurationParser; 40import org.apache.harmony.auth.login.DefaultConfigurationParser.InvalidFormatException; 41 42import tests.support.resource.Support_Resources; 43 44/** 45 * Tests parser implementation for default configuration. 46 */ 47public class DefaultConfigParserTest extends TestCase { 48 49 static String fconf = Support_Resources 50 .getAbsoluteResourcePath("auth.conf"); 51 52 Properties p; 53 54 URL url; 55 56 List<AppConfigurationEntry> entriesList; 57 58 @Override 59 public void setUp() throws Exception { 60 p = System.getProperties(); 61 File f = new File(fconf); 62 try { 63 url = f.toURL(); 64 } catch (MalformedURLException e) { 65 } 66 } 67 68 /** 69 * test on existence of a application name 70 * @throws Exception 71 */ 72 public final void testConfigParser_01() throws Exception { 73 Map<String, List<AppConfigurationEntry>> config = null; 74 config = DefaultConfigurationParser.configParser(url, p, new HashMap<String, List<AppConfigurationEntry>>()); 75 entriesList = config.get("Login1"); 76 assertNotNull(entriesList); 77 entriesList = config.get("other"); 78 assertNotNull(entriesList); 79 entriesList = config.get("Login2"); 80 assertNotNull(entriesList); 81 entriesList = config.get("Login3"); 82 assertNotNull(entriesList); 83 entriesList = config.get("Login4"); 84 assertNotNull(entriesList); 85 entriesList = config.get("Login5"); 86 assertNotNull(entriesList); 87 entriesList = config.get("Login6"); 88 assertNotNull(entriesList); 89 entriesList = config.get("Login7"); 90 assertNotNull(entriesList); 91 entriesList = config.get("Login8"); 92 assertNull(entriesList); 93 } 94 95 /** 96 * test on correct initialization of a config file 97 * @throws Exception 98 */ 99 public final void testConfigParser_02() throws Exception { 100 101 Map<String, List<AppConfigurationEntry>> config = DefaultConfigurationParser.configParser(url, p, new HashMap<String, List<AppConfigurationEntry>>()); 102 entriesList = config.get("Login1"); 103 104 if (entriesList == null || entriesList.size() == 0) { 105 fail("Unable to read configuration"); 106 } 107 108 Iterator<AppConfigurationEntry> i = entriesList.iterator(); 109 while (i.hasNext()) { 110 AppConfigurationEntry e = i.next(); 111 assertEquals("com.intel.security.auth.module.LoginModule1", e 112 .getLoginModuleName()); 113 assertEquals("LoginModuleControlFlag: required", e 114 .getControlFlag().toString()); 115 assertEquals( 116 AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, 117 e.getControlFlag()); 118 Map<String, String> m = new HashMap<String, String>(); 119 m.put("debug1", "true"); 120 m.put("test1", "false"); 121 assertEquals(m, e.getOptions()); 122 } 123 } 124 125 /** 126 * test on invalid initialization of a config file 127 * @throws Exception 128 */ 129 public final void testNegConfig() throws Exception { 130 String[] str = { 131 132 "Login ERR {\n com.intel.security.auth.module.LoginModule required debug=\"true\" test=false;\n};", 133 "{\n com.intel.security.auth.module.LoginModule required debug=\"true\" test=false;\n};", 134 "Login \n com.intel.security.auth.module.LoginModule required debug=\"true\" test=false;\n};", 135 "Login {\n com.intel.security.auth.module.LoginModule required debug=\"true\" test=false;\n}", 136 "Login {\n com.intel.security.auth.module.LoginModule required debug=\"true\" test=false;\n;", 137 "Login {\n com.intel.security.auth.module.LoginModule required debug=\"true\" test=false\n};", 138 "Login {\n com.intel.security.auth.module.LoginModule required debug=\"true\" test=;\n};", 139 "Login {\n com.intel.security.auth.module.LoginModule required debug=\"true\" =false;\n};", 140 "Login {\n com.intel.security.auth.module.LoginModule required debug=\"true test=false;\n};", 141 "Login {\n com.intel.security.auth.module.LoginModule required debug=true\" test=false;\n};", 142 "Login {\n com.intel.security.auth.module.LoginModule required debug\n};", 143 "Login {\n com.intel.security.auth.module.LoginModule INVALID \n};", 144 "Login {\n required \n};", 145 "Login {\n com.intel.security.auth.module.LoginModule required\n}", 146 "Login {\n com.intel.security.auth.module.LoginModule1 required debug=\"true\";\n};\n" 147 + "Login {\n com.intel.security.auth.module.LoginModule2 required debug=\"true\" test=false;\n};\n", 148 }; 149 150 for (int i = 0; i < str.length; i++) { 151 File file = File.createTempFile("auth_neg_", ".conf"); 152 153 byte[] b = str[i].getBytes(); 154 OutputStream os = new FileOutputStream(file); 155 156 for (byte element : b) { 157 os.write(element); 158 } 159 160 os.flush(); 161 os.close(); 162 try { 163 DefaultConfigurationParser.configParser(file.toURL(), p, new HashMap<String, List<AppConfigurationEntry>>()); 164 fail("no expected InvalidFormatException" + i); 165 } catch (NullPointerException e) { 166 System.out.println(file.getCanonicalPath()); 167 } catch (DefaultConfigurationParser.InvalidFormatException e) { 168 } finally { 169 file.delete(); 170 } 171 } 172 } 173 174 public void testException() { 175 InvalidFormatException ife = new InvalidFormatException("message"); 176 assertEquals("message", ife.getMessage()); 177 try { 178 throw ife; 179 }catch (Exception e){ 180 assertTrue(ife.equals(e)); 181 } 182 DefaultConfigurationParser.InvalidFormatException ife1 = new DefaultConfigurationParser.InvalidFormatException("message"); 183 assertEquals("message", ife1.getMessage()); 184 } 185 186} 187