-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Overload to Sanitize AzureSasCredential Signature (#23033)
Add Signature Encoding Overload to AzureSasCredential
- Loading branch information
1 parent
fdb0e62
commit 7c9ac27
Showing
3 changed files
with
103 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
sdk/core/azure-core/src/test/java/com/azure/core/credential/AzureSasCredentialTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.core.credential; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
/** | ||
* Tests {@link AzureSasCredential}. | ||
*/ | ||
public class AzureSasCredentialTests { | ||
@ParameterizedTest | ||
@MethodSource("invalidConstructorParametersSupplier") | ||
public void invalidConstructorParameters(String signature, Class<? extends Throwable> expectedException) { | ||
assertThrows(expectedException, () -> new AzureSasCredential(signature)); | ||
assertThrows(expectedException, () -> new AzureSasCredential(signature, String::toString)); | ||
} | ||
|
||
private static Stream<Arguments> invalidConstructorParametersSupplier() { | ||
return Stream.of( | ||
Arguments.of(null, NullPointerException.class), | ||
Arguments.of("", IllegalArgumentException.class) | ||
); | ||
} | ||
|
||
@Test | ||
public void baseConstructorReturnsSignatureAsIs() { | ||
final String signature = "sas=a bc"; | ||
|
||
AzureSasCredential credential = new AzureSasCredential(signature); | ||
assertEquals(signature, credential.getSignature()); | ||
} | ||
|
||
@Test | ||
public void constructorWithoutEncodingFunctionReturnsSignatureAsIs() { | ||
final String signature = "sas=a bc"; | ||
|
||
AzureSasCredential credential = new AzureSasCredential(signature, null); | ||
assertEquals(signature, credential.getSignature()); | ||
} | ||
|
||
@Test | ||
public void constructorEncodesSignature() { | ||
final String signature = "sas=a bc"; | ||
final String expectedSignature = "sas=a%20bc"; | ||
|
||
AzureSasCredential credential = new AzureSasCredential(signature, sig -> sig.replaceAll(" ", "%20")); | ||
assertEquals(expectedSignature, credential.getSignature()); | ||
} | ||
|
||
@Test | ||
public void credentialWithoutEncodingFunctionDoesNotEncodeUpdates() { | ||
final String signature = "sas=a bc"; | ||
final String updatedSignature = "sas=a b c"; | ||
|
||
AzureSasCredential credential = new AzureSasCredential(signature).update(updatedSignature); | ||
assertEquals(updatedSignature, credential.getSignature()); | ||
} | ||
|
||
@Test | ||
public void credentialWithEncodingFunctionEncodesSignatureUpdates() { | ||
final String signature = "sas=a bc"; | ||
final String updatedSignature = "sas=a b c"; | ||
final String expectedSignature = "sas=a%20b%20c"; | ||
|
||
AzureSasCredential credential = new AzureSasCredential(signature, sig -> sig.replaceAll(" ", "%20")) | ||
.update(updatedSignature); | ||
assertEquals(expectedSignature, credential.getSignature()); | ||
} | ||
} |