diff options
-rw-r--r-- | java/org/direct_bt/DirectBTVersion.java | 83 | ||||
-rw-r--r-- | test/java/test/org/direct_bt/TestBringup00.java | 96 | ||||
-rw-r--r-- | test/java/test/org/direct_bt/TestVersionInfo.java | 76 | ||||
-rw-r--r-- | test/java/test/org/direct_bt/VersionInfo.java | 2 | ||||
-rw-r--r-- | trial/java/trial/org/direct_bt/DBTUtils.java | 40 | ||||
-rw-r--r-- | trial/java/trial/org/direct_bt/TestDBTClientServer00.java | 16 |
6 files changed, 266 insertions, 47 deletions
diff --git a/java/org/direct_bt/DirectBTVersion.java b/java/org/direct_bt/DirectBTVersion.java index 4554fbae..da1fec4a 100644 --- a/java/org/direct_bt/DirectBTVersion.java +++ b/java/org/direct_bt/DirectBTVersion.java @@ -24,8 +24,22 @@ package org.direct_bt; +import java.io.IOException; +import java.io.PrintStream; +import java.net.URISyntaxException; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Set; +import java.util.jar.Attributes; import java.util.jar.Manifest; +import java.util.regex.Pattern; +import org.jau.base.JaulibVersion; +import org.jau.pkg.TempJarSHASum; +import org.jau.sec.SHASum; import org.jau.util.JauVersion; import org.jau.util.VersionUtil; @@ -35,6 +49,47 @@ import org.jau.util.VersionUtil; */ public class DirectBTVersion extends JauVersion { + /** + * Print full Direct-BT version information. + * + * {@link BTFactory#initDirectBTLibrary()} is being called. + * + * @param out the output stream used + */ + public static final void printVersionInfo(final PrintStream out) { + BTFactory.initDirectBTLibrary(); + + BTUtils.println(out, "BTFactory: Jaulib: Available "+BTFactory.JAULIB_AVAILABLE+", JarCache in use "+BTFactory.JAULIB_JARCACHE_USED); + if( BTFactory.JAULIB_AVAILABLE ) { + out.println(VersionUtil.getPlatformInfo()); + BTUtils.println(out, "Version Info:"); + final DirectBTVersion v = DirectBTVersion.getInstance(); + out.println(v.toString()); + BTUtils.println(out, ""); + BTUtils.println(out, "Full Manifest:"); + out.println(v.getFullManifestInfo(null).toString()); + } else { + BTUtils.println(out, "Full Manifest:"); + final Manifest manifest = BTFactory.getManifest(BTFactory.class.getClassLoader(), new String[] { "org.direct_bt" } ); + final Attributes attr = manifest.getMainAttributes(); + final Set<Object> keys = attr.keySet(); + final StringBuilder sb = new StringBuilder(); + for(final Iterator<Object> iter=keys.iterator(); iter.hasNext(); ) { + final Attributes.Name key = (Attributes.Name) iter.next(); + final String val = attr.getValue(key); + sb.append(" "); + sb.append(key); + sb.append(" = "); + sb.append(val); + sb.append(System.lineSeparator()); + } + out.println(sb.toString()); + } + + BTUtils.println(out, "Direct-BT Native Version "+BTFactory.getNativeVersion()+" (API "+BTFactory.getNativeAPIVersion()+")"); + BTUtils.println(out, "Direct-BT Java Version "+BTFactory.getImplVersion()+" (API "+BTFactory.getAPIVersion()+")"); + } + protected DirectBTVersion(final String packageName, final Manifest mf) { super(packageName, mf); } @@ -54,6 +109,34 @@ public class DirectBTVersion extends JauVersion { } } + /** + * Direct-BT definition of {@link TempJarSHASum}'s specialization of {@link SHASum}. + * <p> + * Implementation uses {@link org.jau.pkg.cache.TempJarCache}. + * </p> + * <p> + * Constructor defines the includes and excludes as used for Direct-BT {@link SHASum} computation. + * </p> + */ + static public class JarSHASum extends TempJarSHASum { + /** + * See {@link DirectBTJarSHASum} + * @throws SecurityException + * @throws IllegalArgumentException + * @throws NoSuchAlgorithmException + * @throws IOException + * @throws URISyntaxException + */ + public JarSHASum() + throws SecurityException, IllegalArgumentException, NoSuchAlgorithmException, IOException, URISyntaxException + { + super(MessageDigest.getInstance("SHA-256"), JaulibVersion.class, new ArrayList<Pattern>(), new ArrayList<Pattern>()); + final List<Pattern> includes = getIncludes(); + final String origin = getOrigin(); + includes.add(Pattern.compile(origin+"/org/direct_bt/.*")); + includes.add(Pattern.compile(origin+"/jau/direct_bt/.*")); + } + } public static void main(final String args[]) { BTFactory.main(args); } diff --git a/test/java/test/org/direct_bt/TestBringup00.java b/test/java/test/org/direct_bt/TestBringup00.java new file mode 100644 index 00000000..76a57fcf --- /dev/null +++ b/test/java/test/org/direct_bt/TestBringup00.java @@ -0,0 +1,96 @@ +/** + * Author: Sven Gothel <[email protected]> + * Copyright (c) 2022 Gothel Software e.K. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package test.org.direct_bt; + +import java.lang.reflect.InvocationTargetException; +import java.util.List; + +import org.direct_bt.BTRole; +import org.direct_bt.BTAdapter; +import org.direct_bt.BTException; +import org.direct_bt.BTFactory; +import org.direct_bt.BTManager; +import org.direct_bt.BTUtils; +import org.direct_bt.DirectBTVersion; +import org.junit.Assert; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runners.MethodSorters; + +import jau.test.junit.util.SingletonJunitCase; + +/** + * Testing BTManager bring up using fat `Direct-BT Jaulib Fat Jar` and + * - test loading native libraries + * - show all installed adapter + * - no extra permissions required + */ +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class TestBringup00 extends SingletonJunitCase { + static final boolean DEBUG = false; + + @Test(timeout = 5000) + public final void test01_ManagerBringup() { + { + // System.setProperty("direct_bt.debug", "true"); // native code + // System.setProperty("direct_bt.debug", "true,gatt.data"); // native code + // System.setProperty("org.direct_bt.debug", "true"); // java + // System.setProperty("jau.debug", "true"); // java + // System.setProperty("jau.verbose", "true"); // java + } + BTFactory.initDirectBTLibrary(); + DirectBTVersion.printVersionInfo(System.err); + + BTManager manager = null; + try { + manager = BTFactory.getDirectBTManager(); + } catch (BTException | NoSuchMethodException | SecurityException + | IllegalAccessException | IllegalArgumentException + | InvocationTargetException | ClassNotFoundException e) { + e.printStackTrace(); + Assert.assertNull("Unable to instantiate Direct-BT BluetoothManager: "+e.getMessage(), e); + } + if( null == manager ) { + return; + } + final List<BTAdapter> adapters = manager.getAdapters(); + BTUtils.println(System.err, "Adapter: Count "+adapters.size()); + for(int i=0; i<adapters.size(); i++) { + BTUtils.println(System.err, i+": "+adapters.get(i).toString()); + } + for(final BTAdapter a : adapters) { + Assert.assertFalse( a.isInitialized() ); + Assert.assertFalse( a.isPowered() ); + Assert.assertEquals( BTRole.Master, a.getRole() ); // default role + Assert.assertTrue( 4 <= a.getBTMajorVersion() ); + } + // All implicit via destructor or shutdown hook! + manager.shutdown(); /* implies: adapter.close(); */ + } + + public static void main(final String args[]) { + org.junit.runner.JUnitCore.main(TestBringup00.class.getName()); + } +} diff --git a/test/java/test/org/direct_bt/TestVersionInfo.java b/test/java/test/org/direct_bt/TestVersionInfo.java new file mode 100644 index 00000000..e403c380 --- /dev/null +++ b/test/java/test/org/direct_bt/TestVersionInfo.java @@ -0,0 +1,76 @@ +/** + * Author: Sven Gothel <[email protected]> + * Copyright (c) 2022 Gothel Software e.K. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package test.org.direct_bt; + +import java.io.IOException; +import java.net.URISyntaxException; +import java.security.NoSuchAlgorithmException; + +import org.jau.sec.SHASum; +import org.jau.util.VersionUtil; +import org.junit.Assert; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runners.MethodSorters; +import org.direct_bt.DirectBTVersion; + +import jau.test.junit.util.SingletonJunitCase; + +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class TestVersionInfo extends SingletonJunitCase { + static boolean VERBOSE = false; + + @Test + public void test01Info() { + System.err.println(VersionUtil.getPlatformInfo()); + System.err.println("Version Info:"); + System.err.println(DirectBTVersion.getInstance()); + System.err.println(""); + System.err.println("Full Manifest:"); + System.err.println(DirectBTVersion.getInstance().getFullManifestInfo(null)); + } + + // @Test // FIXME: Add SHA signature in build system! + public void test02ValidateSHA() + throws IllegalArgumentException, IOException, URISyntaxException, SecurityException, NoSuchAlgorithmException + { + final DirectBTVersion info = DirectBTVersion.getInstance(); + final String shaClassesThis = info.getImplementationSHAClassesThis(); + System.err.println("SHA CLASSES.this (build-time): "+shaClassesThis); + + final DirectBTVersion.JarSHASum shaSum = new DirectBTVersion.JarSHASum(); + final byte[] shasum = shaSum.compute(VERBOSE); + final String shaClasses = SHASum.toHexString(shasum, null).toString(); + System.err.println("SHA CLASSES.this (now): "+shaClasses); + Assert.assertEquals("SHA not equal", shaClassesThis, shaClasses); + } + + public static void main(final String args[]) throws IOException { + // VERBOSE = true; + final String tstname = TestVersionInfo.class.getName(); + org.junit.runner.JUnitCore.main(tstname); + } + +} diff --git a/test/java/test/org/direct_bt/VersionInfo.java b/test/java/test/org/direct_bt/VersionInfo.java index 03f4ec7b..53b2a972 100644 --- a/test/java/test/org/direct_bt/VersionInfo.java +++ b/test/java/test/org/direct_bt/VersionInfo.java @@ -3,8 +3,6 @@ package test.org.direct_bt; import java.io.IOException; import org.direct_bt.BTFactory; -import org.direct_bt.DirectBTVersion; -import org.jau.util.VersionUtil; public class VersionInfo { public static void main(final String args[]) throws IOException { diff --git a/trial/java/trial/org/direct_bt/DBTUtils.java b/trial/java/trial/org/direct_bt/DBTUtils.java index 901307c1..fe55a52d 100644 --- a/trial/java/trial/org/direct_bt/DBTUtils.java +++ b/trial/java/trial/org/direct_bt/DBTUtils.java @@ -26,51 +26,11 @@ package trial.org.direct_bt; import java.io.File; import java.nio.file.Files; -import java.util.Iterator; -import java.util.Set; -import java.util.jar.Attributes; -import java.util.jar.Manifest; import org.direct_bt.BTFactory; import org.direct_bt.BTUtils; -import org.direct_bt.DirectBTVersion; -import org.jau.util.VersionUtil; public class DBTUtils { - public static final void printVersionInfo() { - BTFactory.initDirectBTLibrary(); - - BTUtils.println(System.err, "BTFactory: Jaulib: Available "+BTFactory.JAULIB_AVAILABLE+", JarCache in use "+BTFactory.JAULIB_JARCACHE_USED); - if( BTFactory.JAULIB_AVAILABLE ) { - System.err.println(VersionUtil.getPlatformInfo()); - BTUtils.println(System.err, "Version Info:"); - final DirectBTVersion v = DirectBTVersion.getInstance(); - System.err.println(v.toString()); - BTUtils.println(System.err, ""); - BTUtils.println(System.err, "Full Manifest:"); - System.err.println(v.getFullManifestInfo(null).toString()); - } else { - BTUtils.println(System.err, "Full Manifest:"); - final Manifest manifest = BTFactory.getManifest(BTFactory.class.getClassLoader(), new String[] { "org.direct_bt" } ); - final Attributes attr = manifest.getMainAttributes(); - final Set<Object> keys = attr.keySet(); - final StringBuilder sb = new StringBuilder(); - for(final Iterator<Object> iter=keys.iterator(); iter.hasNext(); ) { - final Attributes.Name key = (Attributes.Name) iter.next(); - final String val = attr.getValue(key); - sb.append(" "); - sb.append(key); - sb.append(" = "); - sb.append(val); - sb.append(System.lineSeparator()); - } - System.err.println(sb.toString()); - } - - BTUtils.println(System.err, "Direct-BT Native Version "+BTFactory.getNativeVersion()+" (API "+BTFactory.getNativeAPIVersion()+")"); - BTUtils.println(System.err, "Direct-BT Java Version "+BTFactory.getImplVersion()+" (API "+BTFactory.getAPIVersion()+")"); - } - public static final boolean mkdirKeyFolder() { BTFactory.initDirectBTLibrary(); boolean res = true; diff --git a/trial/java/trial/org/direct_bt/TestDBTClientServer00.java b/trial/java/trial/org/direct_bt/TestDBTClientServer00.java index 79482935..4a6e28c6 100644 --- a/trial/java/trial/org/direct_bt/TestDBTClientServer00.java +++ b/trial/java/trial/org/direct_bt/TestDBTClientServer00.java @@ -36,6 +36,7 @@ import org.direct_bt.BTException; import org.direct_bt.BTFactory; import org.direct_bt.BTManager; import org.direct_bt.BTUtils; +import org.direct_bt.DirectBTVersion; import org.jau.net.EUI48; import org.junit.Assert; import org.junit.FixMethodOrder; @@ -43,7 +44,8 @@ import org.junit.Test; import org.junit.runners.MethodSorters; /** - * Basic client and server Bluetooth tests, requiring one BT adapter: + * Basic client and server Bluetooth tests using non-fat `Direct-BT Jar`, requiring one BT adapter: + * - test loading native libraries * - start server advertising * - server stop advertising * - reuse server-adapter for client-mode discovery (just toggle on/off) @@ -53,13 +55,14 @@ public class TestDBTClientServer00 extends BaseDBTClientServer { static final boolean DEBUG = false; /** - * Testing BTManager bring up and - * - test that at least two adapter are present + * Testing BTManager bring up using non-fat `Direct-BT Jar` and + * - test loading native libraries + * - test that at least one adapter are present * - validating basic default adapter status */ @Test(timeout = 5000) public final void test01_ManagerBringup() { - DBTUtils.printVersionInfo(); + DirectBTVersion.printVersionInfo(System.err); BTManager manager = null; try { @@ -74,7 +77,10 @@ public class TestDBTClientServer00 extends BaseDBTClientServer { return; } final List<BTAdapter> adapters = manager.getAdapters(); - BTUtils.println(System.err, "Adapter: Count "+adapters.size()+": "+adapters.toString()); + BTUtils.println(System.err, "Adapter: Count "+adapters.size()); + for(int i=0; i<adapters.size(); i++) { + BTUtils.println(System.err, i+": "+adapters.get(i).toString()); + } Assert.assertTrue("Adapter count not >= 1 but "+adapters.size(), adapters.size() >= 1); for(final BTAdapter a : adapters) { |