From 892bc85d4e9a3e1e37e3e805939f63524cf94855 Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Mon, 20 Dec 2010 21:56:53 +0100 Subject: [PATCH] Issue 433: don't accept empty userdata --- .../aws/ec2/options/RunInstancesOptions.java | 5 +- .../ec2/options/RunInstancesOptionsTest.java | 57 ++++++++----------- 2 files changed, 28 insertions(+), 34 deletions(-) diff --git a/aws/core/src/main/java/org/jclouds/aws/ec2/options/RunInstancesOptions.java b/aws/core/src/main/java/org/jclouds/aws/ec2/options/RunInstancesOptions.java index b9bd70cb42..1775dc1bb3 100644 --- a/aws/core/src/main/java/org/jclouds/aws/ec2/options/RunInstancesOptions.java +++ b/aws/core/src/main/java/org/jclouds/aws/ec2/options/RunInstancesOptions.java @@ -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; } diff --git a/aws/core/src/test/java/org/jclouds/aws/ec2/options/RunInstancesOptionsTest.java b/aws/core/src/test/java/org/jclouds/aws/ec2/options/RunInstancesOptionsTest.java index 945ab9f88b..fa51852597 100644 --- a/aws/core/src/test/java/org/jclouds/aws/ec2/options/RunInstancesOptionsTest.java +++ b/aws/core/src/test/java/org/jclouds/aws/ec2/options/RunInstancesOptionsTest.java @@ -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)