Issue 433: don't accept empty userdata

This commit is contained in:
Adrian Cole 2010-12-20 21:56:53 +01:00
parent 0423f08a87
commit 892bc85d4e
2 changed files with 28 additions and 34 deletions

View File

@ -121,8 +121,9 @@ public class RunInstancesOptions extends BaseEC2RequestOptions {
* Unencoded data
*/
public RunInstancesOptions withUserData(byte[] unencodedData) {
checkArgument(checkNotNull(unencodedData, "unencodedData").length <= 16 * 1024,
"userData cannot be larger than 16kb");
int length = checkNotNull(unencodedData, "unencodedData").length;
checkArgument(length > 0, "userData cannot be empty");
checkArgument(length <= 16 * 1024, "userData cannot be larger than 16kb");
formParameters.put("UserData", Base64.encodeBytes(unencodedData));
return this;
}

View File

@ -79,8 +79,7 @@ public class RunInstancesOptionsTest {
public void testWithSecurityGroup() {
RunInstancesOptions options = new RunInstancesOptions();
options.withSecurityGroup("test");
assertEquals(options.buildFormParameters().get("SecurityGroup.1"), Collections
.singletonList("test"));
assertEquals(options.buildFormParameters().get("SecurityGroup.1"), Collections.singletonList("test"));
}
@Test
@ -92,8 +91,7 @@ public class RunInstancesOptionsTest {
@Test
public void testWithSecurityGroupStatic() {
RunInstancesOptions options = withSecurityGroup("test");
assertEquals(options.buildFormParameters().get("SecurityGroup.1"), Collections
.singletonList("test"));
assertEquals(options.buildFormParameters().get("SecurityGroup.1"), Collections.singletonList("test"));
}
@Test(expectedExceptions = NullPointerException.class)
@ -105,8 +103,7 @@ public class RunInstancesOptionsTest {
public void testWithAdditionalInfo() {
RunInstancesOptions options = new RunInstancesOptions();
options.withAdditionalInfo("test");
assertEquals(options.buildFormParameters().get("AdditionalInfo"), Collections
.singletonList("test"));
assertEquals(options.buildFormParameters().get("AdditionalInfo"), Collections.singletonList("test"));
}
@Test
@ -118,8 +115,7 @@ public class RunInstancesOptionsTest {
@Test
public void testWithAdditionalInfoStatic() {
RunInstancesOptions options = withAdditionalInfo("test");
assertEquals(options.buildFormParameters().get("AdditionalInfo"), Collections
.singletonList("test"));
assertEquals(options.buildFormParameters().get("AdditionalInfo"), Collections.singletonList("test"));
}
@Test(expectedExceptions = NullPointerException.class)
@ -151,12 +147,16 @@ public class RunInstancesOptionsTest {
withUserData(null);
}
@Test(expectedExceptions = IllegalArgumentException.class)
public void testWithUserDataEmpty() {
withUserData("".getBytes());
}
@Test
public void testWithInstanceType() {
RunInstancesOptions options = new RunInstancesOptions();
options.asType(InstanceType.C1_XLARGE);
assertEquals(options.buildFormParameters().get("InstanceType"), Collections
.singletonList("c1.xlarge"));
assertEquals(options.buildFormParameters().get("InstanceType"), Collections.singletonList("c1.xlarge"));
}
@Test
@ -168,8 +168,7 @@ public class RunInstancesOptionsTest {
@Test
public void testWithInstanceTypeStatic() {
RunInstancesOptions options = asType(InstanceType.C1_XLARGE);
assertEquals(options.buildFormParameters().get("InstanceType"), Collections
.singletonList("c1.xlarge"));
assertEquals(options.buildFormParameters().get("InstanceType"), Collections.singletonList("c1.xlarge"));
}
@Test(expectedExceptions = NullPointerException.class)
@ -205,22 +204,21 @@ public class RunInstancesOptionsTest {
public void testWithDeviceName() {
RunInstancesOptions options = new RunInstancesOptions();
options.withDeviceName("test");
assertEquals(options.buildFormParameters().get("BlockDeviceMapping.DeviceName"), Collections
.singletonList("test"));
assertEquals(options.buildFormParameters().get("BlockDeviceMapping.DeviceName"),
Collections.singletonList("test"));
}
@Test
public void testNullWithDeviceName() {
RunInstancesOptions options = new RunInstancesOptions();
assertEquals(options.buildFormParameters().get("BlockDeviceMapping.DeviceName"),
Collections.EMPTY_LIST);
assertEquals(options.buildFormParameters().get("BlockDeviceMapping.DeviceName"), Collections.EMPTY_LIST);
}
@Test
public void testWithDeviceNameStatic() {
RunInstancesOptions options = withDeviceName("test");
assertEquals(options.buildFormParameters().get("BlockDeviceMapping.DeviceName"), Collections
.singletonList("test"));
assertEquals(options.buildFormParameters().get("BlockDeviceMapping.DeviceName"),
Collections.singletonList("test"));
}
@Test(expectedExceptions = NullPointerException.class)
@ -232,8 +230,7 @@ public class RunInstancesOptionsTest {
public void testWithMonitoringEnabled() {
RunInstancesOptions options = new RunInstancesOptions();
options.enableMonitoring();
assertEquals(options.buildFormParameters().get("Monitoring.Enabled"), Collections
.singletonList("true"));
assertEquals(options.buildFormParameters().get("Monitoring.Enabled"), Collections.singletonList("true"));
}
@Test
@ -245,8 +242,7 @@ public class RunInstancesOptionsTest {
@Test
public void testWithMonitoringEnabledStatic() {
RunInstancesOptions options = enableMonitoring();
assertEquals(options.buildFormParameters().get("Monitoring.Enabled"), Collections
.singletonList("true"));
assertEquals(options.buildFormParameters().get("Monitoring.Enabled"), Collections.singletonList("true"));
}
@Test
@ -277,8 +273,7 @@ public class RunInstancesOptionsTest {
public void testWithRamdisk() {
RunInstancesOptions options = new RunInstancesOptions();
options.withRamdisk("test");
assertEquals(options.buildFormParameters().get("RamdiskId"), Collections
.singletonList("test"));
assertEquals(options.buildFormParameters().get("RamdiskId"), Collections.singletonList("test"));
}
@Test
@ -290,8 +285,7 @@ public class RunInstancesOptionsTest {
@Test
public void testWithRamdiskStatic() {
RunInstancesOptions options = withRamdisk("test");
assertEquals(options.buildFormParameters().get("RamdiskId"), Collections
.singletonList("test"));
assertEquals(options.buildFormParameters().get("RamdiskId"), Collections.singletonList("test"));
}
@Test(expectedExceptions = NullPointerException.class)
@ -303,22 +297,21 @@ public class RunInstancesOptionsTest {
public void testWithVirtualName() {
RunInstancesOptions options = new RunInstancesOptions();
options.withVirtualName("test");
assertEquals(options.buildFormParameters().get("BlockDeviceMapping.VirtualName"), Collections
.singletonList("test"));
assertEquals(options.buildFormParameters().get("BlockDeviceMapping.VirtualName"),
Collections.singletonList("test"));
}
@Test
public void testNullWithVirtualName() {
RunInstancesOptions options = new RunInstancesOptions();
assertEquals(options.buildFormParameters().get("BlockDeviceMapping.VirtualName"),
Collections.EMPTY_LIST);
assertEquals(options.buildFormParameters().get("BlockDeviceMapping.VirtualName"), Collections.EMPTY_LIST);
}
@Test
public void testWithVirtualNameStatic() {
RunInstancesOptions options = withVirtualName("test");
assertEquals(options.buildFormParameters().get("BlockDeviceMapping.VirtualName"), Collections
.singletonList("test"));
assertEquals(options.buildFormParameters().get("BlockDeviceMapping.VirtualName"),
Collections.singletonList("test"));
}
@Test(expectedExceptions = NullPointerException.class)