Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/awslabs/aws-crt-java into m…
Browse files Browse the repository at this point in the history
…qtt5to3adapter
  • Loading branch information
xiazhvera committed Sep 7, 2023
2 parents e3808a2 + ee4f6a3 commit 83efd8f
Show file tree
Hide file tree
Showing 12 changed files with 327 additions and 192 deletions.
28 changes: 15 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,25 +159,27 @@ static: certificate has an existing certificate-key pair that was previously imp
```

## Testing
Many tests require custom arguments. These tests will be quietly skipped if their arguments are not set.
Arguments can be passed like so:
Many tests require environment variables to be set. These environment variables are translated at runtime to system properties for use by the tests. These tests will be quietly skipped if the properties they require are not set.

Environment variables can be set like so:
```
mvn test -Dcertificate=path/to/cert -Dprivatekey=path/to/key ...
export ENV_VARIABLE_NAME="<variable value>"
```
Many tests require that you have [set up](https://console.aws.amazon.com/iot) an AWS IoT Thing.

Full list of test arguments:
- `endpoint`: AWS IoT service endpoint hostname
- `certificate`: Path to the IoT thing certificate
- `privatekey`: Path to the IoT thing private key
- `privatekey_p8`: Path to the IoT thing private key in PKCS#8 format
- `ecc_certificate`: Path to the IoT thing with EC-based certificate
- `ecc_privatekey`: Path to the IoT thing with ECC private key (The ECC key file should only contains the ECC Private Key section to working on MacOS.)
- `rootca`: Path to the root certificate
- `proxyhost`: Hostname of proxy
- `proxyport`: Port of proxy
Partial list of environment variables:
- `AWS_TEST_MQTT311_IOT_CORE_HOST`: AWS IoT service endpoint hostname for MQTT3
- `AWS_TEST_MQTT311_IOT_CORE_RSA_CERT`: Path to the IoT thing certificate for MQTT3
- `AWS_TEST_MQTT311_IOT_CORE_RSA_KEY`: Path to the IoT thing private key for MQTT3
- `AWS_TEST_MQTT311_IOT_CORE_ECC_CERT`: Path to the IoT thing with EC-based certificate for MQTT3
- `AWS_TEST_MQTT311_IOT_CORE_ECC_KEY`: Path to the IoT thing with ECC private key for MQTT3 (The ECC key file should only contains the ECC Private Key section to working on MacOS.)
- `AWS_TEST_MQTT311_ROOT_CA`: Path to the root certificate
- `AWS_TEST_HTTP_PROXY_HOST`: Hostname of proxy
- `AWS_TEST_HTTP_PROXY_PORT`: Port of proxy
- `NETWORK_TESTS_DISABLED`: Set this if tests are running in a constrained environment where network access is not guaranteed/allowed.

Other Environment Variables that can be set can be found in the `SetupTestProperties()` function in [CrtTestFixture.java](https://github.com/awslabs/aws-crt-java/blob/main/src/test/java/software/amazon/awssdk/crt/test/CrtTestFixture.java)

These can be set persistently via Maven settings (usually in `~/.m2/settings.xml`):
```xml
<settings>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/software/amazon/awssdk/crt/CRT.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public static String getArchIdentifier() throws UnknownPlatformException {
return systemPropertyOverride;
}

String environmentOverride = System.getenv(CRT_ARCH_OVERRIDE_ENVIRONMENT_VARIABLE);
String environmentOverride = System.getProperty(CRT_ARCH_OVERRIDE_ENVIRONMENT_VARIABLE);
if (environmentOverride != null && environmentOverride.length() > 0) {
return environmentOverride;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ public class CredentialsProviderTest extends CrtTestFixture {
static private String SECRET_ACCESS_KEY = "secret_access_key";
static private String SESSION_TOKEN = "session_token";

private static String COGNITO_ENDPOINT = System.getenv("AWS_TEST_MQTT311_COGNITO_ENDPOINT");
private static String COGNITO_IDENTITY = System.getenv("AWS_TEST_MQTT311_COGNITO_IDENTITY");
private static String TEST_HTTP_PROXY_HOST = System.getenv("AWS_TEST_HTTP_PROXY_HOST");
private static String TEST_HTTP_PROXY_PORT = System.getenv("AWS_TEST_HTTP_PROXY_PORT");
private static String COGNITO_ENDPOINT = System.getProperty("AWS_TEST_MQTT311_COGNITO_ENDPOINT");
private static String COGNITO_IDENTITY = System.getProperty("AWS_TEST_MQTT311_COGNITO_IDENTITY");
private static String TEST_HTTP_PROXY_HOST = System.getProperty("AWS_TEST_HTTP_PROXY_HOST");
private static String TEST_HTTP_PROXY_PORT = System.getProperty("AWS_TEST_HTTP_PROXY_PORT");

private boolean isCIEnvironmentSetUp() {
if (COGNITO_IDENTITY == null ) {
Expand Down
173 changes: 173 additions & 0 deletions src/test/java/software/amazon/awssdk/crt/test/CrtTestFixture.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,175 @@ public final CrtTestContext getContext() {
return context;
}

private void SetPropertyFromEnv(String name){
String propertyValue = System.getenv(name);
if (propertyValue != null){
System.setProperty(name, propertyValue);
}
}

// Setup System properties from environment variables set by builder for use by unit tests.
private void SetupTestProperties(){
SetPropertyFromEnv("AWS_TEST_IS_CI");
SetPropertyFromEnv("AWS_TEST_MQTT311_ROOT_CA");
SetPropertyFromEnv("ENDPOINT");
SetPropertyFromEnv("REGION");

// Cognito
SetPropertyFromEnv("AWS_TEST_MQTT311_COGNITO_ENDPOINT");
SetPropertyFromEnv("AWS_TEST_MQTT311_COGNITO_IDENTITY");

//Proxy
SetPropertyFromEnv("AWS_TEST_HTTP_PROXY_HOST");
SetPropertyFromEnv("AWS_TEST_HTTP_PROXY_PORT");
SetPropertyFromEnv("AWS_TEST_HTTPS_PROXY_HOST");
SetPropertyFromEnv("AWS_TEST_HTTPS_PROXY_PORT");
SetPropertyFromEnv("AWS_TEST_HTTP_PROXY_BASIC_HOST");
SetPropertyFromEnv("AWS_TEST_HTTP_PROXY_BASIC_PORT");

// Static credential related
SetPropertyFromEnv("AWS_TEST_MQTT311_ROLE_CREDENTIAL_ACCESS_KEY");
SetPropertyFromEnv("AWS_TEST_MQTT311_ROLE_CREDENTIAL_SECRET_ACCESS_KEY");
SetPropertyFromEnv("AWS_TEST_MQTT311_ROLE_CREDENTIAL_SESSION_TOKEN");

// Custom Key Ops
SetPropertyFromEnv("AWS_TEST_MQTT311_CUSTOM_KEY_OPS_KEY");
SetPropertyFromEnv("AWS_TEST_MQTT311_CUSTOM_KEY_OPS_CERT");

// MQTT311 Codebuild/Direct connections data
SetPropertyFromEnv("AWS_TEST_MQTT311_DIRECT_MQTT_HOST");
SetPropertyFromEnv("AWS_TEST_MQTT311_DIRECT_MQTT_PORT");
SetPropertyFromEnv("AWS_TEST_MQTT311_DIRECT_MQTT_BASIC_AUTH_HOST");
SetPropertyFromEnv("AWS_TEST_MQTT311_DIRECT_MQTT_BASIC_AUTH_PORT");
SetPropertyFromEnv("AWS_TEST_MQTT311_DIRECT_MQTT_TLS_HOST");
SetPropertyFromEnv("AWS_TEST_MQTT311_DIRECT_MQTT_TLS_PORT");

// MQTT311 Codebuild/Websocket connections data
SetPropertyFromEnv("AWS_TEST_MQTT311_WS_MQTT_HOST");
SetPropertyFromEnv("AWS_TEST_MQTT311_WS_MQTT_PORT");
SetPropertyFromEnv("AWS_TEST_MQTT311_WS_MQTT_BASIC_AUTH_HOST");
SetPropertyFromEnv("AWS_TEST_MQTT311_WS_MQTT_BASIC_AUTH_PORT");
SetPropertyFromEnv("AWS_TEST_MQTT311_WS_MQTT_TLS_HOST");
SetPropertyFromEnv("AWS_TEST_MQTT311_WS_MQTT_TLS_PORT");

// MQTT311 Codebuild misc connections data
SetPropertyFromEnv("AWS_TEST_MQTT311_BASIC_AUTH_USERNAME");
SetPropertyFromEnv("AWS_TEST_MQTT311_BASIC_AUTH_PASSWORD");
SetPropertyFromEnv("AWS_TEST_MQTT311_CERTIFICATE_FILE");
SetPropertyFromEnv("AWS_TEST_MQTT311_KEY_FILE");

// MQTT311 IoT Endpoint, Key, Cert
SetPropertyFromEnv("AWS_TEST_MQTT311_IOT_CORE_HOST");
SetPropertyFromEnv("AWS_TEST_MQTT311_IOT_CORE_RSA_CERT");
SetPropertyFromEnv("AWS_TEST_MQTT311_IOT_CORE_RSA_KEY");
SetPropertyFromEnv("AWS_TEST_MQTT311_IOT_CORE_ECC_CERT");
SetPropertyFromEnv("AWS_TEST_MQTT311_IOT_CORE_ECC_KEY");

// MQTT311 Proxy
SetPropertyFromEnv("AWS_TEST_MQTT311_PROXY_HOST");
SetPropertyFromEnv("AWS_TEST_MQTT311_PROXY_PORT");

// MQTT311 Keystore
SetPropertyFromEnv("AWS_TEST_MQTT311_IOT_CORE_KEYSTORE_FORMAT");
SetPropertyFromEnv("AWS_TEST_MQTT311_IOT_CORE_KEYSTORE_FILE");
SetPropertyFromEnv("AWS_TEST_MQTT311_IOT_CORE_KEYSTORE_PASSWORD");
SetPropertyFromEnv("AWS_TEST_MQTT311_IOT_CORE_KEYSTORE_CERT_ALIAS");
SetPropertyFromEnv("AWS_TEST_MQTT311_IOT_CORE_KEYSTORE_CERT_PASSWORD");

// MQTT311 PKCS12
SetPropertyFromEnv("AWS_TEST_MQTT311_IOT_CORE_PKCS12_KEY");
SetPropertyFromEnv("AWS_TEST_MQTT311_IOT_CORE_PKCS12_KEY_PASSWORD");

// PKCS11
SetPropertyFromEnv("AWS_TEST_PKCS11_LIB");
SetPropertyFromEnv("AWS_TEST_PKCS11_TOKEN_LABEL");
SetPropertyFromEnv("AWS_TEST_PKCS11_PIN");
SetPropertyFromEnv("AWS_TEST_PKCS11_PKEY_LABEL");
SetPropertyFromEnv("AWS_TEST_PKCS11_CERT_FILE");
SetPropertyFromEnv("AWS_TEST_PKCS11_CA_FILE");

// MQTT311 X509
SetPropertyFromEnv("AWS_TEST_MQTT311_IOT_CORE_X509_CERT");
SetPropertyFromEnv("AWS_TEST_MQTT311_IOT_CORE_X509_KEY");
SetPropertyFromEnv("AWS_TEST_MQTT311_IOT_CORE_X509_ENDPOINT");
SetPropertyFromEnv("AWS_TEST_MQTT311_IOT_CORE_X509_ROLE_ALIAS");
SetPropertyFromEnv("AWS_TEST_MQTT311_IOT_CORE_X509_THING_NAME");

// MQTT311 Windows Cert Store
SetPropertyFromEnv("AWS_TEST_MQTT311_IOT_CORE_WINDOWS_PFX_CERT_NO_PASS");
SetPropertyFromEnv("AWS_TEST_MQTT311_IOT_CORE_WINDOWS_CERT_STORE");

// MQTT5 Codebuild/Direct connections data
SetPropertyFromEnv("AWS_TEST_MQTT5_DIRECT_MQTT_HOST");
SetPropertyFromEnv("AWS_TEST_MQTT5_DIRECT_MQTT_PORT");
SetPropertyFromEnv("AWS_TEST_MQTT5_DIRECT_MQTT_BASIC_AUTH_HOST");
SetPropertyFromEnv("AWS_TEST_MQTT5_DIRECT_MQTT_BASIC_AUTH_PORT");
SetPropertyFromEnv("AWS_TEST_MQTT5_DIRECT_MQTT_TLS_HOST");
SetPropertyFromEnv("AWS_TEST_MQTT5_DIRECT_MQTT_TLS_PORT");

// MQTT5 Codebuild/Websocket connections data
SetPropertyFromEnv("AWS_TEST_MQTT5_WS_MQTT_HOST");
SetPropertyFromEnv("AWS_TEST_MQTT5_WS_MQTT_PORT");
SetPropertyFromEnv("AWS_TEST_MQTT5_WS_MQTT_BASIC_AUTH_HOST");
SetPropertyFromEnv("AWS_TEST_MQTT5_WS_MQTT_BASIC_AUTH_PORT");
SetPropertyFromEnv("AWS_TEST_MQTT5_WS_MQTT_TLS_HOST");
SetPropertyFromEnv("AWS_TEST_MQTT5_WS_MQTT_TLS_PORT");

// MQTT5 Codebuild misc connections data
SetPropertyFromEnv("AWS_TEST_MQTT5_BASIC_AUTH_USERNAME");
SetPropertyFromEnv("AWS_TEST_MQTT5_BASIC_AUTH_PASSWORD");
SetPropertyFromEnv("AWS_TEST_MQTT5_CERTIFICATE_FILE");
SetPropertyFromEnv("AWS_TEST_MQTT5_KEY_FILE");

// MQTT5 Proxy
SetPropertyFromEnv("AWS_TEST_MQTT5_PROXY_HOST");
SetPropertyFromEnv("AWS_TEST_MQTT5_PROXY_PORT");

// MQTT5 Endpoint/Host credential
SetPropertyFromEnv("AWS_TEST_MQTT5_IOT_CORE_HOST");
SetPropertyFromEnv("AWS_TEST_MQTT5_IOT_CORE_REGION");
SetPropertyFromEnv("AWS_TEST_MQTT5_IOT_CORE_RSA_CERT");
SetPropertyFromEnv("AWS_TEST_MQTT5_IOT_CORE_RSA_KEY");

// MQTT5 Static credential related
SetPropertyFromEnv("AWS_TEST_MQTT5_ROLE_CREDENTIAL_ACCESS_KEY");
SetPropertyFromEnv("AWS_TEST_MQTT5_ROLE_CREDENTIAL_SECRET_ACCESS_KEY");
SetPropertyFromEnv("AWS_TEST_MQTT5_ROLE_CREDENTIAL_SESSION_TOKEN");

// MQTT5 Cognito
SetPropertyFromEnv("AWS_TEST_MQTT5_COGNITO_ENDPOINT");
SetPropertyFromEnv("AWS_TEST_MQTT5_COGNITO_IDENTITY");

// MQTT5 Keystore
SetPropertyFromEnv("AWS_TEST_MQTT5_IOT_CORE_KEYSTORE_FORMAT");
SetPropertyFromEnv("AWS_TEST_MQTT5_IOT_CORE_KEYSTORE_FILE");
SetPropertyFromEnv("AWS_TEST_MQTT5_IOT_CORE_KEYSTORE_PASSWORD");
SetPropertyFromEnv("AWS_TEST_MQTT5_IOT_CORE_KEYSTORE_CERT_ALIAS");
SetPropertyFromEnv("AWS_TEST_MQTT5_IOT_CORE_KEYSTORE_CERT_PASSWORD");

// MQTT5 PKCS12
SetPropertyFromEnv("AWS_TEST_MQTT5_IOT_CORE_PKCS12_KEY");
SetPropertyFromEnv("AWS_TEST_MQTT5_IOT_CORE_PKCS12_KEY_PASSWORD");

// MQTT5 X509
SetPropertyFromEnv("AWS_TEST_MQTT5_IOT_CORE_X509_CERT");
SetPropertyFromEnv("AWS_TEST_MQTT5_IOT_CORE_X509_KEY");
SetPropertyFromEnv("AWS_TEST_MQTT5_IOT_CORE_X509_ENDPOINT");
SetPropertyFromEnv("AWS_TEST_MQTT5_IOT_CORE_X509_ROLE_ALIAS");
SetPropertyFromEnv("AWS_TEST_MQTT5_IOT_CORE_X509_THING_NAME");

// MQTT5 Windows Cert Store
SetPropertyFromEnv("AWS_TEST_MQTT5_IOT_CORE_WINDOWS_PFX_CERT_NO_PASS");
SetPropertyFromEnv("AWS_TEST_MQTT5_IOT_CORE_WINDOWS_CERT_STORE");

// MQTT5 Custom Key Ops (so we don't have to make a new file just for a single test)
SetPropertyFromEnv("AWS_TEST_MQTT5_CUSTOM_KEY_OPS_CERT");
SetPropertyFromEnv("AWS_TEST_MQTT5_CUSTOM_KEY_OPS_KEY");

SetPropertyFromEnv("AWS_TEST_BASIC_AUTH_USERNAME");
SetPropertyFromEnv("AWS_TEST_BASIC_AUTH_PASSWORD");
}

@Before
public void setup() {
// We only want to see the CRT logs if the test fails.
Expand All @@ -46,11 +215,15 @@ public void setup() {
Log.initLoggingToFile(Log.LogLevel.Trace, "log.txt");
}
Log.log(Log.LogLevel.Debug, LogSubject.JavaCrtGeneral, "CrtTestFixture setup begin");

context = new CrtTestContext();
CrtPlatform platform = CRT.getPlatformImpl();
if (platform != null) {
platform.testSetup(context);
} else {
SetupTestProperties();
}

Log.log(Log.LogLevel.Debug, LogSubject.JavaCrtGeneral, "CrtTestFixture setup end");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.junit.Test;

import software.amazon.awssdk.crt.CrtRuntimeException;
import software.amazon.awssdk.crt.http.HttpClientConnection;
import software.amazon.awssdk.crt.http.HttpVersion;
import software.amazon.awssdk.crt.http.HttpHeader;
import software.amazon.awssdk.crt.http.Http2Request;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import software.amazon.awssdk.crt.mqtt5.packets.SubscribePacket.RetainHandlingType;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
Expand Down
Loading

0 comments on commit 83efd8f

Please sign in to comment.