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 18package org.apache.harmony.security.tests.java.security; 19 20import dalvik.annotation.TestTargetClass; 21import dalvik.annotation.TestTargets; 22import dalvik.annotation.TestLevel; 23import dalvik.annotation.TestTargetNew; 24 25import java.security.MessageDigest; 26import java.security.NoSuchAlgorithmException; 27import java.security.Provider; 28import java.security.Security; 29 30@TestTargetClass(Provider.class) 31public class Provider2Test extends junit.framework.TestCase { 32 33 class TestProvider extends Provider { 34 TestProvider(String name, double version, String info) { 35 super(name, version, info); 36 } 37 } 38 39 class MyEntry implements java.util.Map.Entry { 40 public Object getKey() { 41 return null; 42 } 43 44 public Object getValue() { 45 return null; 46 } 47 48 public Object setValue(Object value) { 49 return null; 50 } 51 } 52 53 TestProvider provTest = new TestProvider("provTest", 1.2, 54 "contains nothings, purely for testing the class"); 55 56 57 /** 58 * @tests java.security.Provider#entrySet() 59 */ 60 @TestTargetNew( 61 level = TestLevel.PARTIAL_COMPLETE, 62 notes = "UnsupportedOperationException verification", 63 method = "entrySet", 64 args = {} 65 ) 66 public void test_entrySet() { 67 // test method of java.security.provider.entrySet 68 provTest.put("test.prop", "this is a test property"); 69 try { 70 //make it compilable on 1.5 71 provTest.entrySet().add(new MyEntry()); 72 fail("was able to modify the entrySet"); 73 } catch (UnsupportedOperationException e) { 74 // expected 75 } 76 } 77 78 /** 79 * @tests java.security.Provider#getInfo() 80 */ 81 @TestTargetNew( 82 level = TestLevel.COMPLETE, 83 notes = "", 84 method = "getInfo", 85 args = {} 86 ) 87 public void test_getInfo() { 88 // test method of java.security.provider.getInfo 89 assertEquals("the information of the provider is not stored properly", 90 "contains nothings, purely for testing the class", provTest 91 .getInfo()); 92 } 93 94 /** 95 * @tests java.security.Provider#getName() 96 */ 97 @TestTargetNew( 98 level = TestLevel.COMPLETE, 99 notes = "", 100 method = "getName", 101 args = {} 102 ) 103 public void test_getName() { 104 // test method of java.security.provider.getName 105 assertEquals("the name of the provider is not stored properly", 106 "provTest", provTest.getName()); 107 } 108 109 /** 110 * @tests java.security.Provider#getVersion() 111 */ 112 @TestTargetNew( 113 level = TestLevel.COMPLETE, 114 notes = "", 115 method = "getVersion", 116 args = {} 117 ) 118 public void test_getVersion() { 119 // test method of java.security.provider.getVersion 120 assertEquals("the version of the provider is not stored properly", 121 1.2, provTest.getVersion(), 0); 122 } 123 124 /** 125 * @tests java.security.Provider#keySet() 126 */ 127 @TestTargetNew( 128 level = TestLevel.PARTIAL, 129 notes = "UnsupportedOperationException verification", 130 method = "keySet", 131 args = {} 132 ) 133 public void test_keySet() { 134 // test method of java.security.provider.keySet 135 provTest.put("test.prop", "this is a test property"); 136 try { 137 provTest.keySet().add("another property key"); 138 fail("was able to modify the keySet"); 139 } catch (UnsupportedOperationException e) { 140 // expected 141 } 142 } 143 144 /** 145 * @tests java.security.Provider#values() 146 */ 147 @TestTargetNew( 148 level = TestLevel.PARTIAL_COMPLETE, 149 notes = "UnsupportedOperationException verification", 150 method = "values", 151 args = {} 152 ) 153 public void test_values() { 154 // test method of java.security.provider.values 155 provTest.put("test.prop", "this is a test property"); 156 try { 157 provTest.values().add("another property value"); 158 fail("was able to modify the values collection"); 159 } catch (UnsupportedOperationException e) { 160 // expected 161 } 162 } 163 164 165 /** 166 * @tests java.security.Provider#toString() 167 */ 168 @TestTargetNew( 169 level = TestLevel.COMPLETE, 170 notes = "Regression test", 171 method = "toString", 172 args = {} 173 ) 174 public void test_toString() { 175 // Regression for HARMONY-3734 176 assertEquals("provTest version 1.2", provTest.toString()); 177 } 178 179 // Regression Test for Provider.Service.getAlias(), which is an package 180 // private method but will lead to NPE thrown at 181 // Secure.SecurityDorr.getAliases 182 public void test_getAlias() throws Exception { 183 MockProvider mockProvider = new MockProvider("MOCKNAME", 1.0, 184 "MOCKINFO"); 185 Provider.Service service = new Provider.Service(mockProvider, 186 "MOCKTYPE", "MOCKALGORITHM", "TESTCLASSNAME", null, null); 187 mockProvider.putService(service); 188 Security.addProvider(mockProvider); 189 try { 190 MessageDigest messageDigest = MessageDigest 191 .getInstance("NOTEXISTS"); 192 } 193 194 catch (NoSuchAlgorithmException e) { 195 // expected 196 } finally { 197 Security.removeProvider("MOCKNAME"); 198 } 199 } 200 201 public static class MockProvider extends Provider { 202 203 private static final long serialVersionUID = 1L; 204 205 public MockProvider(String name, double version, String info) { 206 super(name, version, info); 207 208 } 209 210 public void putService(Provider.Service service) { 211 super.putService(service); 212 } 213 214 public void removeService(Provider.Service service) { 215 super.removeService(service); 216 } 217 } 218} 219