forked from mcardosos/azure-storage-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblobsasuri_test.go
185 lines (154 loc) · 5.53 KB
/
blobsasuri_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package storage
import (
"io/ioutil"
"net/http"
"net/url"
"time"
chk "gopkg.in/check.v1"
)
type BlobSASURISuite struct{}
var _ = chk.Suite(&BlobSASURISuite{})
var oldAPIVer = "2013-08-15"
var newerAPIVer = "2015-04-05"
func (s *BlobSASURISuite) TestGetBlobSASURI(c *chk.C) {
api, err := NewClient("foo", dummyMiniStorageKey, DefaultBaseURL, oldAPIVer, true)
c.Assert(err, chk.IsNil)
cli := api.GetBlobService()
cnt := cli.GetContainerReference("container")
b := cnt.GetBlobReference("name")
expiry := time.Time{}
expectedParts := url.URL{
Scheme: "https",
Host: "foo.blob.core.windows.net",
Path: "container/name",
RawQuery: url.Values{
"sv": {oldAPIVer},
"sig": {"/OXG7rWh08jYwtU03GzJM0DHZtidRGpC6g69rSGm3I0="},
"sr": {"b"},
"sp": {"r"},
"se": {"0001-01-01T00:00:00Z"},
}.Encode()}
u, err := b.GetSASURI(expiry, "r")
c.Assert(err, chk.IsNil)
sasParts, err := url.Parse(u)
c.Assert(err, chk.IsNil)
c.Assert(expectedParts.String(), chk.Equals, sasParts.String())
c.Assert(expectedParts.Query(), chk.DeepEquals, sasParts.Query())
}
//Gets a SASURI for the entire container
func (s *BlobSASURISuite) TestGetBlobSASURIContainer(c *chk.C) {
api, err := NewClient("foo", dummyMiniStorageKey, DefaultBaseURL, oldAPIVer, true)
c.Assert(err, chk.IsNil)
cli := api.GetBlobService()
cnt := cli.GetContainerReference("container")
b := cnt.GetBlobReference("")
expiry := time.Time{}
expectedParts := url.URL{
Scheme: "https",
Host: "foo.blob.core.windows.net",
Path: "container",
RawQuery: url.Values{
"sv": {oldAPIVer},
"sig": {"KMjYyQODKp6uK9EKR3yGhO2M84e1LfoztypU32kHj4s="},
"sr": {"c"},
"sp": {"r"},
"se": {"0001-01-01T00:00:00Z"},
}.Encode()}
u, err := b.GetSASURI(expiry, "r")
c.Assert(err, chk.IsNil)
sasParts, err := url.Parse(u)
c.Assert(err, chk.IsNil)
c.Assert(expectedParts.String(), chk.Equals, sasParts.String())
c.Assert(expectedParts.Query(), chk.DeepEquals, sasParts.Query())
}
func (s *BlobSASURISuite) TestGetBlobSASURIWithSignedIPAndProtocolValidAPIVersionPassed(c *chk.C) {
api, err := NewClient("foo", dummyMiniStorageKey, DefaultBaseURL, newerAPIVer, true)
c.Assert(err, chk.IsNil)
cli := api.GetBlobService()
cnt := cli.GetContainerReference("container")
b := cnt.GetBlobReference("name")
expiry := time.Time{}
expectedParts := url.URL{
Scheme: "https",
Host: "foo.blob.core.windows.net",
Path: "/container/name",
RawQuery: url.Values{
"sv": {newerAPIVer},
"sig": {"VBOYJmt89UuBRXrxNzmsCMoC+8PXX2yklV71QcL1BfM="},
"sr": {"b"},
"sip": {"127.0.0.1"},
"sp": {"r"},
"se": {"0001-01-01T00:00:00Z"},
"spr": {"https"},
}.Encode()}
u, err := b.GetSASURIWithSignedIPAndProtocol(expiry, "r", "127.0.0.1", true)
c.Assert(err, chk.IsNil)
sasParts, err := url.Parse(u)
c.Assert(err, chk.IsNil)
c.Assert(sasParts.Query(), chk.DeepEquals, expectedParts.Query())
}
// Trying to use SignedIP and Protocol but using an older version of the API.
// Should ignore the signedIP/protocol and just use what the older version requires.
func (s *BlobSASURISuite) TestGetBlobSASURIWithSignedIPAndProtocolUsingOldAPIVersion(c *chk.C) {
api, err := NewClient("foo", dummyMiniStorageKey, DefaultBaseURL, oldAPIVer, true)
c.Assert(err, chk.IsNil)
cli := api.GetBlobService()
cnt := cli.GetContainerReference("container")
b := cnt.GetBlobReference("name")
expiry := time.Time{}
expectedParts := url.URL{
Scheme: "https",
Host: "foo.blob.core.windows.net",
Path: "/container/name",
RawQuery: url.Values{
"sv": {oldAPIVer},
"sig": {"/OXG7rWh08jYwtU03GzJM0DHZtidRGpC6g69rSGm3I0="},
"sr": {"b"},
"sp": {"r"},
"se": {"0001-01-01T00:00:00Z"},
}.Encode()}
u, err := b.GetSASURIWithSignedIPAndProtocol(expiry, "r", "", true)
c.Assert(err, chk.IsNil)
sasParts, err := url.Parse(u)
c.Assert(err, chk.IsNil)
c.Assert(expectedParts.String(), chk.Equals, sasParts.String())
c.Assert(expectedParts.Query(), chk.DeepEquals, sasParts.Query())
}
func (s *BlobSASURISuite) TestBlobSASURICorrectness(c *chk.C) {
cli := getBlobClient(c)
if cli.client.usesDummies() {
c.Skip("As GetSASURI result depends on the account key, it is not practical to test it with a dummy key.")
}
simpleClient := &http.Client{}
rec := cli.client.appendRecorder(c)
simpleClient.Transport = rec
defer rec.Stop()
cnt := cli.GetContainerReference(containerName(c))
c.Assert(cnt.Create(nil), chk.IsNil)
b := cnt.GetBlobReference(contentWithSpecialChars(5))
defer cnt.Delete(nil)
body := content(100)
expiry := fixedTime.UTC().Add(time.Hour)
permissions := "r"
c.Assert(b.putSingleBlockBlob(body), chk.IsNil)
sasURI, err := b.GetSASURI(expiry, permissions)
c.Assert(err, chk.IsNil)
resp, err := simpleClient.Get(sasURI)
c.Assert(err, chk.IsNil)
blobResp, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
c.Assert(err, chk.IsNil)
c.Assert(resp.StatusCode, chk.Equals, http.StatusOK)
c.Assert(string(blobResp), chk.Equals, string(body))
}
func (s *BlobSASURISuite) Test_blobSASStringToSign(c *chk.C) {
_, err := blobSASStringToSign("2012-02-12", "CS", "SE", "SP", "", "")
c.Assert(err, chk.NotNil) // not implemented SAS for versions earlier than 2013-08-15
out, err := blobSASStringToSign(oldAPIVer, "CS", "SE", "SP", "", "")
c.Assert(err, chk.IsNil)
c.Assert(out, chk.Equals, "SP\n\nSE\nCS\n\n2013-08-15\n\n\n\n\n")
// check format for 2015-04-05 version
out, err = blobSASStringToSign(newerAPIVer, "CS", "SE", "SP", "127.0.0.1", "https,http")
c.Assert(err, chk.IsNil)
c.Assert(out, chk.Equals, "SP\n\nSE\n/blobCS\n\n127.0.0.1\nhttps,http\n2015-04-05\n\n\n\n\n")
}