diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/PostPolicyV4.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/PostPolicyV4.java index 96afca06e..4abcb4fd3 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/PostPolicyV4.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/PostPolicyV4.java @@ -443,13 +443,21 @@ public String toJson() { StringBuilder escapedJson = new StringBuilder(); // Certain characters in a policy must be escaped - for (char c : json.toCharArray()) { + char[] jsonArray = json.toCharArray(); + for (int i = 0; i < jsonArray.length; i++) { + char c = jsonArray[i]; if (c >= 128) { // is a unicode character escapedJson.append(String.format("\\u%04x", (int) c)); } else { switch (c) { case '\\': - escapedJson.append("\\\\"); + // The JsonObject/JsonArray operations above handle quote escapes, so leave any "\"" + // found alone + if (jsonArray[i + 1] == '"') { + escapedJson.append("\\"); + } else { + escapedJson.append("\\\\"); + } break; case '\b': escapedJson.append("\\b"); diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/V4PostPolicyTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/V4PostPolicyTest.java index 161fb3640..fc7f59f92 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/V4PostPolicyTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/V4PostPolicyTest.java @@ -28,7 +28,6 @@ import com.google.cloud.conformance.storage.v1.UrlStyle; import com.google.cloud.storage.testing.RemoteStorageHelper; import com.google.common.base.Charsets; -import com.google.common.base.Strings; import com.google.common.io.BaseEncoding; import com.google.protobuf.Timestamp; import com.google.protobuf.util.JsonFormat; @@ -117,17 +116,6 @@ public void test() { PolicyConditions conditions = policyInput.getConditions(); - if (!Strings.isNullOrEmpty(fields.get("success_action_redirect"))) { - builder.addSuccessActionRedirectUrlCondition( - PostPolicyV4.ConditionV4Type.MATCHES, fields.get("success_action_redirect")); - } - - if (!Strings.isNullOrEmpty(fields.get("success_action_status"))) { - builder.addSuccessActionStatusCondition( - PostPolicyV4.ConditionV4Type.MATCHES, - Integer.parseInt(fields.get("success_action_status"))); - } - if (conditions != null) { if (!conditions.getStartsWithList().isEmpty()) { builder.addCustomCondition( @@ -166,17 +154,25 @@ public void test() { style); String expectedPolicy = testData.getPolicyOutput().getExpectedDecodedPolicy(); + StringBuilder escapedPolicy = new StringBuilder(); // Java automatically unescapes the unicode escapes in the conformance tests, so we need to // manually re-escape them - for (char c : expectedPolicy.toCharArray()) { + char[] expectedPolicyArray = expectedPolicy.toCharArray(); + for (int i = 0; i < expectedPolicyArray.length; i++) { + char c = expectedPolicyArray[i]; if (c >= 128) { escapedPolicy.append(String.format("\\u%04x", (int) c)); } else { switch (c) { case '\\': - escapedPolicy.append("\\\\"); + // quotes aren't unescaped, so leave any "\"" found alone + if (expectedPolicyArray[i + 1] == '"') { + escapedPolicy.append("\\"); + } else { + escapedPolicy.append("\\\\"); + } break; case '\b': escapedPolicy.append("\\b"); @@ -202,6 +198,7 @@ public void test() { } } assertEquals(testData.getPolicyOutput().getFieldsMap(), policy.getFields()); + assertEquals( escapedPolicy.toString(), new String(BaseEncoding.base64().decode(policy.getFields().get("policy")))); diff --git a/pom.xml b/pom.xml index 910e68190..3e539687b 100644 --- a/pom.xml +++ b/pom.xml @@ -108,7 +108,7 @@ com.google.cloud google-cloud-conformance-tests - 0.0.11 + 0.1.1 test