1515
1616package com.microsoft.azure.storage
1717
18+ import com.microsoft.azure.storage.blob.AccountSASResourceType
19+ import com.microsoft.azure.storage.blob.AccountSASService
20+ import com.microsoft.azure.storage.blob.AccountSASSignatureValues
21+ import com.microsoft.azure.storage.blob.BlobSASPermission
22+ import com.microsoft.azure.storage.blob.ContainerSASPermission
23+ import com.microsoft.azure.storage.blob.IPRange
1824import com.microsoft.azure.storage.blob.RequestRetryFactory
1925import com.microsoft.azure.storage.blob.RequestRetryOptions
2026import com.microsoft.azure.storage.blob.RequestRetryTestFactory
2127import com.microsoft.azure.storage.blob.RetryPolicyType
28+ import com.microsoft.azure.storage.blob.SASProtocol
29+ import com.microsoft.azure.storage.blob.ServiceSASSignatureValues
2230import com.microsoft.azure.storage.blob.StorageException
31+ import com.microsoft.azure.storage.blob.Utility
2332import com.microsoft.azure.storage.blob.models.StorageErrorCode
2433import com.microsoft.azure.storage.blob.models.StorageErrorException
2534import com.microsoft.rest.v2.http.HttpHeaders
@@ -30,6 +39,10 @@ import com.microsoft.rest.v2.http.HttpResponse
3039import io.reactivex.Flowable
3140import spock.lang.Unroll
3241
42+ import java.time.OffsetDateTime
43+ import java.time.ZoneOffset
44+ import java.time.ZonedDateTime
45+
3346class HelperTest extends APISpec {
3447
3548 def " responseError" () {
@@ -43,4 +56,48 @@ class HelperTest extends APISpec {
4356 e. message(). contains(" Value for one of the query parameters specified in the request URI is invalid." )
4457 e. getMessage(). contains(" <?xml" ) // Ensure that the details in the payload are printable
4558 }
59+
60+ def " serviceSasSignatures" () {
61+ when :
62+ def v = new ServiceSASSignatureValues ()
63+ v. blobName = " foo"
64+ v. containerName = " bar"
65+ v. expiryTime = OffsetDateTime . of(2017 , 1 , 1 , 0 , 0 , 0 , 0 , ZoneOffset . UTC )
66+ v. protocol = SASProtocol . HTTPS_ONLY
67+ def p = new BlobSASPermission ()
68+ p. read = true
69+ v. permissions = p. toString();
70+ def expectedCanonicalName = " /blob/${ primaryCreds.accountName} /${ v.containerName} /${ v.blobName} "
71+ def expectedStringToSign = " ${ v.permissions} \n\n ${ Utility.ISO8601UTCDateFormatter.format(v.expiryTime)} \n ${ expectedCanonicalName} \n\n ${ IPRange.DEFAULT.toString()} \n ${ v.protocol} \n ${ v.version} \n\n\n\n\n "
72+
73+ def token = v. generateSASQueryParameters(primaryCreds)
74+
75+ then :
76+ token. signature == primaryCreds. computeHmac256(expectedStringToSign)
77+ }
78+
79+ def " accountSasSignatures" () {
80+ when :
81+ def v = new AccountSASSignatureValues ()
82+ v. expiryTime = OffsetDateTime . of(2017 , 1 , 1 , 0 , 0 , 0 , 0 , ZoneOffset . UTC )
83+ v. protocol = SASProtocol . HTTPS_ONLY
84+ def p = new ContainerSASPermission ()
85+ p. read = true
86+ v. permissions = p. toString()
87+ def s = new AccountSASService ()
88+ s. blob = true
89+ v. services = s. toString()
90+ def t = new AccountSASResourceType ()
91+ t. container = true
92+ t. object = true
93+ v. resourceTypes = t. toString()
94+ def expectedStringToSign = " ${ primaryCreds.accountName} \n ${ p.toString()} \n ${ v.services} \n ${ v.resourceTypes} \n\n ${ Utility.ISO8601UTCDateFormatter.format(v.expiryTime)} \n ${ IPRange.DEFAULT.toString()} \n ${ SASProtocol.HTTPS_ONLY} \n ${ v.version} \n "
95+ def token = v. generateSASQueryParameters(primaryCreds)
96+
97+ then :
98+ token. signature == primaryCreds. computeHmac256(expectedStringToSign)
99+
100+ }
101+
102+
46103}
0 commit comments