Skip to content

Commit

Permalink
properties set from asses files
Browse files Browse the repository at this point in the history
  • Loading branch information
sbSteveK committed Sep 6, 2023
1 parent e52cc47 commit 225b610
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,71 +19,107 @@

// Overrides just for testing
public class CrtPlatformImpl extends software.amazon.awssdk.crt.android.CrtPlatformImpl {
public void jvmInit() {
System.out.println("Android TEST: CrtPlatformImpl.jvmInit() src.test.android.testapp.src.androidTest.java Started");
// Ensure that android JUnitTestRunner test arguments get turned into system properties
Bundle testArgs = InstrumentationRegistry.getArguments();
if (testArgs != null) {
System.out.println("Android TEST: CrtPlatformImpl.jvmInit() testArgs != null");
final Set<String> keys = testArgs.keySet();
for (final String key : keys) {
if (key.startsWith("aws.crt.")) {
System.setProperty(key, testArgs.getString(key));
System.out.println("Android TEST: CrtPlatformImpl.jvmInit() setting key:" + key + " to value:" + testArgs.getString(key));
}
}
}
else {
System.out.println("Android TEST: CrtPlatformImpl.jvmInit() testArgs == null");
}
System.out.println("Android TEST: CrtPlatformImpl.jvmInit() Completed");
}

public PackageInfo.Version getVersion() {
System.out.println("Android TEST: CrtPlatformImpl.getVersion() from testapp->androidTest hit");
return new PackageInfo.Version("0.0.0-UNITTEST");
}

private static byte[] assetContents(String filename) {
System.out.println("Android TEST: CrtPlatformImpl.AssetContents(" + filename +") Started");
AssetManager assets = InstrumentationRegistry.getInstrumentation().getContext().getResources().getAssets();
try (InputStream assetStream = assets.open(filename);) {
byte[] contents = new byte[assetStream.available()];
assetStream.read(contents);
System.out.println("Android TEST: CrtPlatformImpl.AssetContents() byte[] being returned");
return contents;
} catch (IOException ex) {
System.out.println("Android TEST: CrtPlatformImpl.AssetContents() null being returned");
return null;
}
}

public void testSetup(Object context) {
public void jvmInit() {
System.out.println("Android TEST: CrtPlatformImpl.jvmInit() src.test.android.testapp.src.androidTest.java Started");
// Ensure that android JUnitTestRunner test arguments get turned into system properties
Bundle testArgs = InstrumentationRegistry.getArguments();
if (testArgs != null) {
System.out.println("Android TEST: CrtPlatformImpl.jvmInit() testArgs != null");
final Set<String> keys = testArgs.keySet();
for (final String key : keys) {
if (key.startsWith("aws.crt.")) {
System.setProperty(key, testArgs.getString(key));
System.out.println("Android TEST: CrtPlatformImpl.jvmInit() setting key:" + key + " to value:" + testArgs.getString(key));
}
}
}
else {
System.out.println("Android TEST: CrtPlatformImpl.jvmInit() testArgs == null");
}
System.out.println("Android TEST: CrtPlatformImpl.jvmInit() Completed");
}
public PackageInfo.Version getVersion() {
System.out.println("Android TEST: CrtPlatformImpl.getVersion() from testapp->androidTest hit");
return new PackageInfo.Version("0.0.0-UNITTEST");
}
private static byte[] assetContents(String filename) {
System.out.println("Android TEST: CrtPlatformImpl.AssetContents(" + filename +") Started");
AssetManager assets = InstrumentationRegistry.getInstrumentation().getContext().getResources().getAssets();
try (InputStream assetStream = assets.open(filename);) {
byte[] contents = new byte[assetStream.available()];
assetStream.read(contents);
System.out.println("Android TEST: CrtPlatformImpl.AssetContents() byte[] being returned");
return contents;
} catch (IOException ex) {
System.out.println("Android TEST: CrtPlatformImpl.AssetContents() null being returned");
return null;
}
}

// Attempts to set a System property to the contents of a file in the assets folder
private void SetPropertyFromFile(String propertyName, String fileName){
AssetManager assets = InstrumentationRegistry.getInstrumentation().getContext().getResources().getAssets();
try (InputStream assetStream = assets.open(fileName);) {
byte[] contents = new byte[assetStream.available()];
assetStream.read(contents);
System.setProperty(propertyName, new String(contents).trim());
} catch (IOException ex){
// File didn't exist or was unreadable
}
}

// Attempts to create a cached file from a file in the assets folder and set a System property pointing to the created file
private void SetPropertyToFileLocation(String name, String fileName){
AssetManager assets = InstrumentationRegistry.getInstrumentation().getContext().getResources().getAssets();
try (InputStream assetStream = assets.open(fileName);){
byte[] contents = new byte[assetStream.available()];
assetStream.read(contents);

// Files in the assets folder are compressed and need to be saved to a cache before being used
File file = new File(InstrumentationRegistry.getInstrumentation().getContext().getCacheDir(), fileName);
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(new String(contents).trim());
bw.close();
System.setProperty(propertyName, file.getAbsolutePath() + fileName);
} catch (IOException ex){
// File didn't exist or was unreadable
}
}

public void testSetup(Object context) {
System.out.println("Android TEST: CrtPlatformImpl.testSetup() Started");
// TODO These should not be setting this context object but should instead be setting environment variables if available for tests.

// SetPropertyFromByteArray("AWS_TEST_MQTT311_IOT_CORE_RSA_CERT", assetContents("pubSubCertificate.pem"));
// SetPropertyFromByteArray("AWS_TEST_MQTT311_IOT_CORE_RSA_KEY", assetContents("pubSubPrivatekey.pem"));
// SetPropertyFromByteArray("AWS_TEST_MQTT311_IOT_CORE_HOST", assetContents("endpoint.txt"));

CrtTestContext ctx = (CrtTestContext) context;
ctx.trustStore = assetContents("ca-certificates.crt");
ctx.iotClientCertificate = assetContents("pubSubCertificate.pem");
System.setProperty("AWS_TEST_MQTT311_IOT_CORE_RSA_CERT", new String(ctx.iotClientCertificate).trim());
ctx.iotClientPrivateKey = assetContents("pubSubPrivatekey.pem");
System.setProperty("AWS_TEST_MQTT311_IOT_CORE_RSA_KEY", new String(ctx.iotClientPrivateKey).trim());
ctx.iotClientECCPrivateKey = assetContents("ecc_privatekey.pem");
ctx.iotClientECCCertificate = assetContents("ecc_certificate.pem");
byte[] endpoint = assetContents("endpoint.txt");
if (endpoint != null) {
ctx.iotEndpoint = new String(endpoint).trim();
System.setProperty("AWS_TEST_MQTT311_IOT_CORE_HOST", ctx.iotEndpoint);
}
ctx.iotCARoot = assetContents("AmazonRootCA1.pem");
SetPropertyToFileLocation("AWS_TEST_MQTT311_IOT_CORE_RSA_CERT", "pubSubCertificate.pem");
SetPropertyToFileLocation("AWS_TEST_MQTT311_IOT_CORE_RSA_KEY", "pubSubPrivatekey.pem");
SetPropertyFromFile("AWS_TEST_MQTT311_IOT_CORE_HOST", "endpoint.txt");

// CrtTestContext ctx = (CrtTestContext) context;
// ctx.trustStore = assetContents("ca-certificates.crt");
// ctx.iotClientCertificate = assetContents("pubSubCertificate.pem");
// System.setProperty("AWS_TEST_MQTT311_IOT_CORE_RSA_CERT", new String(ctx.iotClientCertificate).trim());
// ctx.iotClientPrivateKey = assetContents("pubSubPrivatekey.pem");
// System.setProperty("AWS_TEST_MQTT311_IOT_CORE_RSA_KEY", new String(ctx.iotClientPrivateKey).trim());
// ctx.iotClientECCPrivateKey = assetContents("ecc_privatekey.pem");
// ctx.iotClientECCCertificate = assetContents("ecc_certificate.pem");
// byte[] endpoint = assetContents("endpoint.txt");
// if (endpoint != null) {
// ctx.iotEndpoint = new String(endpoint).trim();
// System.setProperty("AWS_TEST_MQTT311_IOT_CORE_HOST", ctx.iotEndpoint);
// }
// ctx.iotCARoot = assetContents("AmazonRootCA1.pem");

System.out.println("Android TEST: CrtPlatformImpl.testSetup() Completed");
}
}

public void testTearDown(Object context) {
public void testTearDown(Object context) {
System.out.println("Android TEST: CrtPlatformImpl.testTearDown() Started");
System.out.println("Android TEST: CrtPlatformImpl.testTearDown() Completed");
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package software.amazon.awssdk.crt.test;

// Encapsulates any platform-specific configuration for tests
// TODO remove this. We are going to be using System Properties for tests
public class CrtTestContext {
// Trust store PEM blob
public byte[] trustStore = null;
Expand Down

0 comments on commit 225b610

Please sign in to comment.