Skip to content

Commit

Permalink
fix(tests): upgrade jmockit version in device client to avoid known r…
Browse files Browse the repository at this point in the history
…andom jmockit bug

jmockit/jmockit1#263
  • Loading branch information
timtay-microsoft committed Oct 18, 2018
1 parent 3f7d92b commit ac80003
Show file tree
Hide file tree
Showing 17 changed files with 544 additions and 801 deletions.
9 changes: 1 addition & 8 deletions device/iot-device-client/pom.xml
Expand Up @@ -65,7 +65,7 @@
<dependency>
<groupId>org.jmockit</groupId>
<artifactId>jmockit</artifactId>
<version>1.22</version>
<version>1.24</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -146,13 +146,6 @@
</goals>
</execution>
</executions>
<configuration>
<forkCount>1</forkCount>
<reuseForks>true</reuseForks>
<argLine>
-javaagent:${settings.localRepository}/org/jmockit/jmockit/1.22/jmockit-1.22.jar
</argLine>
</configuration>
</plugin>
</plugins>
</build>
Expand Down
Expand Up @@ -716,9 +716,6 @@ public void securityProviderConstructorWithX509Success() throws SecurityProvider
mockSecurityProviderX509.getSSLContext();
result = mockSSLContext;

new IotHubX509HardwareAuthenticationProvider("hostname", "gatewayHostname", "deviceId", "moduleId", mockSecurityProviderX509);
result = mockX509HardwareAuthentication;

mockIotHubConnectionString.getHostName();
result = "hostname";

Expand Down Expand Up @@ -751,12 +748,9 @@ public void securityProviderConstructorWithX509Success() throws SecurityProvider
public void securityClientConstructorWithTPMSuccess() throws SecurityProviderException, IOException
{
//arrange
new NonStrictExpectations()
new Expectations()
{
{
mockSecurityProviderSAS.getSSLContext();
result = mockSSLContext;

mockIotHubConnectionString.getHostName();
result = expectedHostname;

Expand All @@ -765,9 +759,6 @@ public void securityClientConstructorWithTPMSuccess() throws SecurityProviderExc

mockIotHubConnectionString.getModuleId();
result = expectedModuleId;

new IotHubSasTokenHardwareAuthenticationProvider(expectedHostname, null, expectedDeviceId, expectedModuleId, mockSecurityProviderSAS);
result = mockSasTokenHardwareAuthentication;
}
};

Expand Down
Expand Up @@ -11,10 +11,7 @@
import com.microsoft.azure.sdk.iot.device.fileupload.FileUpload;
import com.microsoft.azure.sdk.iot.device.transport.amqps.IoTHubConnectionType;
import com.microsoft.azure.sdk.iot.provisioning.security.SecurityProvider;
import mockit.Deencapsulation;
import mockit.Mocked;
import mockit.NonStrictExpectations;
import mockit.Verifications;
import mockit.*;
import org.junit.Test;

import java.io.IOError;
Expand Down Expand Up @@ -221,7 +218,9 @@ public void constructorBadConnectionStringThrows() throws URISyntaxException, IO
final String connString =
"HostName=iothub.device.com;CredentialType=SharedAccessKey;CredentialScope=Device;deviceId=testdevice;SharedAccessKey=adjkl234j52=;";
final IotHubClientProtocol protocol = IotHubClientProtocol.AMQPS;
new NonStrictExpectations()

//assert
new Expectations()
{
{
Deencapsulation.newInstance(IotHubConnectionString.class, connString);
Expand All @@ -243,9 +242,7 @@ public void constructorBadConnectionStringThrows() throws URISyntaxException, IO
new Verifications()
{
{
IotHubConnectionString iotHubConnectionString = Deencapsulation.newInstance(IotHubConnectionString.class, connString);
times = 1;
Deencapsulation.newInstance(DeviceClientConfig.class, iotHubConnectionString);
Deencapsulation.newInstance(DeviceClientConfig.class, new Class[] {IotHubConnectionString.class}, (IotHubConnectionString) any);
times = 0;
Deencapsulation.newInstance("com.microsoft.azure.sdk.iot.device.DeviceIO",
new Class[] {DeviceClientConfig.class, long.class, long.class},
Expand Down Expand Up @@ -1584,13 +1581,15 @@ public void startFileUploadOneFileUploadInstanceSucceeds(@Mocked final FileUploa
final String destinationBlobName = "valid/blob/name.txt";
final long streamLength = 100;

new NonStrictExpectations()
//assert
new Expectations()
{
{
Deencapsulation.newInstance(FileUpload.class, mockConfig);
result = mockedFileUpload;
Deencapsulation.invoke(mockedFileUpload, "uploadToBlobAsync",
destinationBlobName, mockInputStream, streamLength, mockedStatusCB, mockedPropertyCB);
times = 2;
}
};
DeviceClient client = Deencapsulation.newInstance(DeviceClient.class, new Class[] {String.class, IotHubClientProtocol.class}, "some conn string", protocol);
Expand All @@ -1600,19 +1599,6 @@ public void startFileUploadOneFileUploadInstanceSucceeds(@Mocked final FileUploa

// act
Deencapsulation.invoke(client, "uploadToBlobAsync", destinationBlobName, mockInputStream, streamLength, mockedStatusCB, mockedPropertyCB);

// assert
new Verifications()
{
{
Deencapsulation.newInstance(FileUpload.class, mockConfig);
times = 1;
Deencapsulation.invoke(mockedFileUpload, "uploadToBlobAsync",
destinationBlobName, mockInputStream, streamLength, mockedStatusCB, mockedPropertyCB);
times = 2;

}
};
}

/* Tests_SRS_INTERNALCLIENT_21_045: [If the `callback` is null, the uploadToBlobAsync shall throw IllegalArgumentException.] */
Expand Down Expand Up @@ -1760,7 +1746,8 @@ public void startFileUploadSucceeds(@Mocked final FileUpload mockedFileUpload,
final String destinationBlobName = "valid/blob/name.txt";
final long streamLength = 100;

new NonStrictExpectations()
// assert
new Expectations()
{
{
Deencapsulation.newInstance(FileUpload.class, new Class[] {DeviceClientConfig.class}, (DeviceClientConfig) any);
Expand All @@ -1773,18 +1760,6 @@ public void startFileUploadSucceeds(@Mocked final FileUpload mockedFileUpload,

// act
Deencapsulation.invoke(client, "uploadToBlobAsync", destinationBlobName, mockInputStream, streamLength, mockedStatusCB, mockedPropertyCB);

// assert
new Verifications()
{
{
Deencapsulation.newInstance(FileUpload.class, new Class[] {DeviceClientConfig.class}, (DeviceClientConfig) any);
times = 1;
Deencapsulation.invoke(mockedFileUpload, "uploadToBlobAsync",
destinationBlobName, mockInputStream, streamLength, mockedStatusCB, mockedPropertyCB);
times = 1;
}
};
}

/* Tests_SRS_INTERNALCLIENT_21_054: [If the fileUpload is not null, the closeNow shall call closeNow on fileUpload.] */
Expand Down
Expand Up @@ -1700,7 +1700,7 @@ public void startDeviceTwinSuccess(final @Mocked DeviceTwin mockedDeviceTwin) th
Deencapsulation.setField(client, "deviceIO", mockDeviceIO);
Deencapsulation.setField(client, "config", mockConfig);
Deencapsulation.setField(client, "twin", null);
new NonStrictExpectations()
new Expectations()
{
{
mockDeviceIO.isOpen();
Expand All @@ -1719,9 +1719,6 @@ public void startDeviceTwinSuccess(final @Mocked DeviceTwin mockedDeviceTwin) th
new Verifications()
{
{
new DeviceTwin(mockDeviceIO, mockConfig, mockedIotHubEventCallback, any, mockedTwinPropertyCallback, any);
times = 1;

mockedDeviceTwin.getDeviceTwin();
times = 1;
}
Expand Down
Expand Up @@ -580,7 +580,7 @@ public void signatureProvider(final @Mocked System mockedSystem, @Mocked final I
mockedSystemVariables.put(Deencapsulation.getField(ModuleClient.class, "ModuleGenerationIdVariableName").toString(), expectedGenerationId);
mockedSystemVariables.put(Deencapsulation.getField(ModuleClient.class, "GatewayHostnameVariableName").toString(), expectedGatewayHostname);

new NonStrictExpectations()
new Expectations()
{
{
mockedSystem.getenv();
Expand All @@ -597,28 +597,15 @@ public void signatureProvider(final @Mocked System mockedSystem, @Mocked final I

mockedHttpsHsmTrustBundleProvider.getTrustBundleCerts(expectedIotEdgedUri, expectedApiVersion);
result = expectedTrustedCerts;
}
};

//act
ModuleClient.createFromEnvironment(protocol);

//assert
new Verifications()
{
{
new HttpHsmSignatureProvider(expectedIotEdgedUri, expectedApiVersion);
times = 1;

IotHubSasTokenHsmAuthenticationProvider.create((SignatureProvider) any, expectedDeviceId, expectedModuleId, expectedHostname, expectedGatewayHostname, anyString, anyInt, anyInt);
times = 1;

Deencapsulation.newInstance(ModuleClient.class,
new Class[] {IotHubAuthenticationProvider.class, IotHubClientProtocol.class, long.class, long.class},
mockedModuleAuthenticationWithHsm, protocol, anyLong, anyLong);
times = 1;
mockedModuleAuthenticationWithHsm, (IotHubClientProtocol) any, anyLong, anyLong);
}
};

//act
ModuleClient.createFromEnvironment(protocol);
}

//Tests_SRS_MODULECLIENT_34_014: [This function shall check for environment variables for edgedUri, deviceId, moduleId,
Expand Down
Expand Up @@ -58,7 +58,7 @@ public void securityProviderConstructorSavesNeededInfo() throws IOException, Inv
//arrange
final String someToken = "someToken";
final byte[] tokenBytes= someToken.getBytes();
new NonStrictExpectations()
new Expectations()
{
{
URLEncoder.encode(anyString, encodingName);
Expand Down Expand Up @@ -93,13 +93,6 @@ public void securityProviderConstructorSavesNeededInfo() throws IOException, Inv
assertEquals(expectedDeviceId, actualDeviceId);
assertEquals(expectedModuleId, actualModuleId);
assertEquals(mockSecurityProviderTpm, actualSecurityProvider);
new Verifications()
{
{
Deencapsulation.newInstance(IotHubSSLContext.class, new Class[] {SSLContext.class}, mockSSLContext);
times = 1;
}
};
}

//Tests_SRS_IOTHUBSASTOKENHARDWAREAUTHENTICATION_34_003: [If the provided security provider is not an instance of SecurityProviderTpm, this function shall throw an IllegalArgumentException.]
Expand Down Expand Up @@ -148,6 +141,8 @@ public void getRenewedSasTokenAutoRenewsFromSecurityProvider(@Mocked final Syste
//arrange
final String someToken = "someToken";
final byte[] tokenBytes= someToken.getBytes();

//assert
new Expectations()
{
{
Expand All @@ -159,6 +154,7 @@ public void getRenewedSasTokenAutoRenewsFromSecurityProvider(@Mocked final Syste

mockSecurityProviderTpm.signWithIdentity((byte[]) any);
result = tokenBytes;
times = 2;

Base64.encodeBase64Local((byte[]) any);
result = tokenBytes;
Expand All @@ -175,15 +171,6 @@ public void getRenewedSasTokenAutoRenewsFromSecurityProvider(@Mocked final Syste

//act
Deencapsulation.invoke(sasAuth, "getRenewedSasToken");

//assert
new Verifications()
{
{
mockSecurityProviderTpm.signWithIdentity((byte[]) any);
times = 2;
}
};
}

//Tests_SRS_IOTHUBSASTOKENHARDWAREAUTHENTICATION_34_005: [This function shall return the saved sas token.]
Expand Down
Expand Up @@ -114,14 +114,12 @@ public void overloadedConstructorSavesArguments()
@Test
public void getRenewedSasTokenAutoRenews(@Mocked final System mockSystem) throws IOException, TransportException
{
//arrange
new NonStrictExpectations()
//assert
new Expectations()
{
{
System.currentTimeMillis();
result = 0;
Deencapsulation.newInstance(IotHubSasToken.class, new Class[] {String.class, String.class, String.class, String.class, String.class, long.class}, expectedHostname, expectedDeviceId, expectedDeviceKey, null, expectedModuleId, expectedExpiryTime);
result = mockSasToken;
Deencapsulation.invoke(mockSasToken, "isExpired");
result = true;
System.currentTimeMillis();
Expand All @@ -131,19 +129,12 @@ public void getRenewedSasTokenAutoRenews(@Mocked final System mockSystem) throws
}
};

//arrange
IotHubSasTokenAuthenticationProvider sasAuth = new IotHubSasTokenSoftwareAuthenticationProvider(expectedHostname, expectedGatewayHostname, expectedDeviceId, expectedModuleId, expectedDeviceKey, expectedSasToken);

//act
sasAuth.getRenewedSasToken();

//assert
new Verifications()
{
{
Deencapsulation.newInstance(IotHubSasToken.class, new Class[] {String.class, String.class, String.class, String.class, String.class, long.class}, expectedHostname, expectedDeviceId, expectedDeviceKey, null, expectedModuleId, expectedExpiryTime);
times = 1;
}
};
}

//Tests_SRS_IOTHUBSASTOKENSOFTWAREAUTHENTICATION_34_005: [This function shall return the saved sas token.]
Expand Down

0 comments on commit ac80003

Please sign in to comment.