1 /*
  2  * Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /**
 25  * @test
 26  * @library /test/lib
 27  * @modules jdk.compiler
 28  * @enablePreview
 29  * @build jdk.test.lib.compiler.CompilerUtils
 30  * @run testng/othervm BadProvidersTest
 31  * @summary Basic test of ServiceLoader with bad provider and bad provider
 32  *          factories deployed on the module path
 33  */
 34 
 35 import java.lang.classfile.ClassFile;
 36 import java.lang.constant.ClassDesc;
 37 import java.lang.constant.MethodTypeDesc;
 38 import java.lang.module.Configuration;
 39 import java.lang.module.ModuleFinder;
 40 import java.lang.reflect.AccessFlag;
 41 import java.nio.file.Files;
 42 import java.nio.file.Path;
 43 import java.nio.file.Paths;
 44 import java.nio.file.StandardCopyOption;
 45 import java.util.List;
 46 import java.util.ServiceConfigurationError;
 47 import java.util.ServiceLoader;
 48 import java.util.ServiceLoader.Provider;
 49 import java.util.Set;
 50 import java.util.stream.Collectors;
 51 
 52 import jdk.test.lib.compiler.CompilerUtils;
 53 
 54 import org.testng.annotations.Test;
 55 import org.testng.annotations.DataProvider;
 56 
 57 import static java.lang.classfile.ClassFile.ACC_PUBLIC;
 58 import static java.lang.classfile.ClassFile.ACC_STATIC;
 59 import static java.lang.constant.ConstantDescs.CD_Object;
 60 import static java.lang.constant.ConstantDescs.INIT_NAME;
 61 import static java.lang.constant.ConstantDescs.MTD_void;
 62 
 63 import static org.testng.Assert.*;
 64 
 65 /**
 66  * Basic test of `provides S with PF` and `provides S with P` where the provider
 67  * factory or provider
 68  */
 69 
 70 public class BadProvidersTest {
 71 
 72     private static final String TEST_SRC = System.getProperty("test.src");
 73 
 74     private static final Path USER_DIR   = Paths.get(System.getProperty("user.dir"));
 75     private static final Path SRC_DIR    = Paths.get(TEST_SRC, "modules");
 76 
 77     private static final Path BADFACTORIES_DIR = Paths.get(TEST_SRC, "badfactories");
 78     private static final Path BADPROVIDERS_DIR = Paths.get(TEST_SRC, "badproviders");
 79 
 80     private static final String TEST1_MODULE = "test1";
 81     private static final String TEST2_MODULE = "test2";
 82 
 83     private static final String TEST_SERVICE = "p.Service";
 84 
 85     /**
 86      * Compiles a module, returning a module path with the compiled module.
 87      */
 88     private Path compileTest(String moduleName) throws Exception {
 89         Path dir = Files.createTempDirectory(USER_DIR, "mods");
 90         Path output = Files.createDirectory(dir.resolve(moduleName));
 91         boolean compiled = CompilerUtils.compile(SRC_DIR.resolve(moduleName), output);
 92         assertTrue(compiled);
 93         return dir;
 94     }
 95 
 96     /**
 97      * Resolves a test module and loads it into its own layer. ServiceLoader
 98      * is then used to load all providers.
 99      */
100     private List<Provider> loadProviders(Path mp, String moduleName) throws Exception {
101         ModuleFinder finder = ModuleFinder.of(mp);
102 
103         ModuleLayer bootLayer = ModuleLayer.boot();
104 
105         Configuration cf = bootLayer.configuration()
106                 .resolveAndBind(finder, ModuleFinder.of(), Set.of(moduleName));
107 
108         ClassLoader scl = ClassLoader.getSystemClassLoader();
109 
110         ModuleLayer layer = ModuleLayer.boot().defineModulesWithOneLoader(cf, scl);
111 
112         Class<?> service = layer.findLoader(moduleName).loadClass(TEST_SERVICE);
113 
114         return ServiceLoader.load(layer, service)
115                 .stream()
116                 .collect(Collectors.toList());
117     }
118 
119     @Test
120     public void sanityTest1() throws Exception {
121         Path mods = compileTest(TEST1_MODULE);
122         List<Provider> list = loadProviders(mods, TEST1_MODULE);
123         assertTrue(list.size() == 1);
124 
125         // the provider is a singleton, enforced by the provider factory
126         Object p1 = list.get(0).get();
127         Object p2 = list.get(0).get();
128         assertTrue(p1 != null);
129         assertTrue(p1 == p2);
130     }
131 
132     @Test
133     public void sanityTest2() throws Exception {
134         Path mods = compileTest(TEST2_MODULE);
135         List<Provider> list = loadProviders(mods, TEST2_MODULE);
136         assertTrue(list.size() == 1);
137         Object p = list.get(0).get();
138         assertTrue(p != null);
139     }
140 
141 
142     @DataProvider(name = "badfactories")
143     public Object[][] createBadFactories() {
144         return new Object[][] {
145                 { "classnotpublic",     null },
146                 { "methodnotpublic",    null },
147                 { "badreturntype",      null },
148                 { "returnsnull",        null },
149                 { "throwsexception",    null },
150         };
151     }
152 
153 
154     @Test(dataProvider = "badfactories",
155           expectedExceptions = ServiceConfigurationError.class)
156     public void testBadFactory(String testName, String ignore) throws Exception {
157         Path mods = compileTest(TEST1_MODULE);
158 
159         // compile the bad factory
160         Path source = BADFACTORIES_DIR.resolve(testName);
161         Path output = Files.createTempDirectory(USER_DIR, "tmp");
162         boolean compiled = CompilerUtils.compile(source, output);
163         assertTrue(compiled);
164 
165         // copy the compiled class into the module
166         Path classFile = Paths.get("p", "ProviderFactory.class");
167         Files.copy(output.resolve(classFile),
168                    mods.resolve(TEST1_MODULE).resolve(classFile),
169                    StandardCopyOption.REPLACE_EXISTING);
170 
171         // load providers and instantiate each one
172         loadProviders(mods, TEST1_MODULE).forEach(Provider::get);
173     }
174 
175 
176     @DataProvider(name = "badproviders")
177     public Object[][] createBadProviders() {
178         return new Object[][] {
179                 { "notpublic",          null },
180                 { "ctornotpublic",      null },
181                 { "notasubtype",        null },
182                 { "throwsexception",    null }
183         };
184     }
185 
186 
187     @Test(dataProvider = "badproviders",
188           expectedExceptions = ServiceConfigurationError.class)
189     public void testBadProvider(String testName, String ignore) throws Exception {
190         Path mods = compileTest(TEST2_MODULE);
191 
192         // compile the bad provider
193         Path source = BADPROVIDERS_DIR.resolve(testName);
194         Path output = Files.createTempDirectory(USER_DIR, "tmp");
195         boolean compiled = CompilerUtils.compile(source, output);
196         assertTrue(compiled);
197 
198         // copy the compiled class into the module
199         Path classFile = Paths.get("p", "Provider.class");
200         Files.copy(output.resolve(classFile),
201                    mods.resolve(TEST2_MODULE).resolve(classFile),
202                    StandardCopyOption.REPLACE_EXISTING);
203 
204         // load providers and instantiate each one
205         loadProviders(mods, TEST2_MODULE).forEach(Provider::get);
206     }
207 
208 
209     /**
210      * Test a service provider that defines more than one no-args
211      * public static "provider" method.
212      */
213     @Test(expectedExceptions = ServiceConfigurationError.class)
214     public void testWithTwoFactoryMethods() throws Exception {
215         Path mods = compileTest(TEST1_MODULE);
216 
217         var bytes = ClassFile.of().build(ClassDesc.of("p", "ProviderFactory"), clb -> {
218             clb.withSuperclass(CD_Object);
219             clb.withFlags(AccessFlag.PUBLIC, AccessFlag.SUPER);
220 
221             var providerFactory$1 = ClassDesc.of("p", "ProviderFactory$1");
222 
223             // public static p.Service provider()
224             clb.withMethodBody("provider", MethodTypeDesc.of(ClassDesc.of("p", "Service")),
225                     ACC_PUBLIC | ACC_STATIC, cob -> {
226                         cob.new_(providerFactory$1);
227                         cob.dup();
228                         cob.invokespecial(providerFactory$1, INIT_NAME, MTD_void);
229                         cob.areturn();
230                     });
231 
232             // public static p.ProviderFactory$1 provider()
233             clb.withMethodBody("provider", MethodTypeDesc.of(providerFactory$1),
234                     ACC_PUBLIC | ACC_STATIC, cob -> {
235                         cob.new_(providerFactory$1);
236                         cob.dup();
237                         cob.invokespecial(providerFactory$1, INIT_NAME, MTD_void);
238                         cob.areturn();
239                     });
240         });
241 
242         // write the class bytes into the compiled module directory
243         Path classFile = mods.resolve(TEST1_MODULE)
244                 .resolve("p")
245                 .resolve("ProviderFactory.class");
246         Files.write(classFile, bytes);
247 
248         // load providers and instantiate each one
249         loadProviders(mods, TEST1_MODULE).forEach(Provider::get);
250     }
251 
252 }
253