public void testWriteAccessToken() { string dir = rand8char(); string file = rand8char(); ObjectPath op = new ObjectPath("/" + dir + "/" + file); esu.CreateObjectOnPath(new ObjectPath("/" + dir + "/"), null, null, null, null); DateTime expiration = DateTime.UtcNow; expiration += TimeSpan.FromMinutes(5); SourceType source = new SourceType(); source.Allow = new string[] { "10.0.0.0/8", "128.0.0.0/8" }; source.Disallow = new string[] { "1.1.1.1" }; ContentLengthRangeType range = new ContentLengthRangeType(); range.From = 0; range.To = 1024; // 1KB FormFieldType formField1 = new FormFieldType(); formField1.Name = "x-emc-meta"; formField1.Optional = true; FormFieldType formField2 = new FormFieldType(); formField2.Name = "x-emc-listable-meta"; formField2.Optional = true; PolicyType policy = new PolicyType(); policy.Expiration = expiration; policy.Source = source; policy.MaxDownloads = 2; policy.MaxUploads = 1; policy.ContentLengthRange = range; policy.FormField = new FormFieldType[] { formField1, formField2 }; Uri tokenUri = esu.CreateAccessToken(op, policy, null); // create upload form manually (easiest since we're using HttpWebRequest) string content = "Form Upload Test"; string boundary = "BOUNDARY_1234567890"; string EOL = "\r\n"; string payload = "--" + boundary + EOL; payload += "Content-Type: text/plain" + EOL; payload += "Content-Disposition: form-data; name=\"x-emc-meta\"" + EOL + EOL; payload += "color=gray,size=3,foo=bar"; payload += EOL + "--" + boundary + EOL; payload += "Content-Type: text/plain" + EOL; payload += "Content-Disposition: form-data; name=\"x-emc-listable-meta\"" + EOL + EOL; payload += "listable="; payload += EOL + "--" + boundary + EOL; payload += "Content-Type: text/plain" + EOL; payload += "Content-Disposition: form-data; name=\"data\"; filename=\"foo.txt\"" + EOL + EOL; payload += content; payload += EOL + "--" + boundary + "--" + EOL; WebRequest request = WebRequest.Create(tokenUri); request.Method = "POST"; request.ContentType = "multipart/form-data; boundary=" + boundary; new MemoryStream(Encoding.UTF8.GetBytes(payload)).CopyTo(request.GetRequestStream()); WebResponse response = request.GetResponse(); Assert.AreEqual(201, (int) (response as HttpWebResponse).StatusCode, "Wrong status code"); string path = response.Headers["Location"]; ObjectId oid = new ObjectId(path.Split('/').Last()); cleanup.Add(oid); response.Close(); // read back object via token response = WebRequest.Create(tokenUri).GetResponse(); Assert.AreEqual(content, new StreamReader(response.GetResponseStream()).ReadToEnd(), "content from token not equal"); response.Close(); // read back object via namespace Assert.AreEqual(content, Encoding.UTF8.GetString(esu.ReadObject(op, null, null)), "content from namespace not equal"); esu.DeleteAccessToken(tokenUri); }
private void AssertTokenPolicy(AccessTokenType token, PolicyType policy) { if (token.ContentLengthRange != null) { Assert.AreEqual(policy.ContentLengthRange.From, token.ContentLengthRange.From, "policy differs"); Assert.AreEqual(policy.ContentLengthRange.To, token.ContentLengthRange.To, "policy differs"); } Assert.AreEqual(policy.Expiration, token.Expiration, "policy differs"); Assert.AreEqual(policy.MaxDownloads, token.MaxDownloads, "policy differs"); Assert.AreEqual(policy.MaxUploads, token.MaxUploads, "policy differs"); if (policy.Source != null) { Assert.IsTrue(policy.Source.Allow.SequenceEqual(token.Source.Allow), "policy differs"); Assert.IsTrue(policy.Source.Disallow.SequenceEqual(token.Source.Disallow), "policy differs"); } if (token.FormField != null) { for (int i = 0; i < token.FormField.Length; i++) { Assert.AreEqual(policy.FormField[i].Name, token.FormField[i].Name, "policy differs"); Assert.AreEqual(policy.FormField[i].Optional, token.FormField[i].Optional, "policy differs"); } } }
public void testReadAccessToken() { string dir = rand8char(); string file = rand8char(); ObjectPath op = new ObjectPath("/" + dir + "/" + file); string data = "hello"; ObjectId id = esu.CreateObjectOnPath(op, null, null, Encoding.UTF8.GetBytes(data), "text/plain"); DateTime expiration = DateTime.UtcNow; expiration += TimeSpan.FromMinutes(5); SourceType source = new SourceType(); source.Allow = new string[] {"10.0.0.0/8", "128.0.0.0/8"}; source.Disallow = new string[] {"1.1.1.1"}; ContentLengthRangeType range = new ContentLengthRangeType(); range.From = 0; range.To = 1024; // 1KB FormFieldType formField1 = new FormFieldType(); formField1.Name = "x-emc-meta"; formField1.Optional = true; FormFieldType formField2 = new FormFieldType(); formField2.Name = "x-emc-listable-meta"; formField2.Optional = true; PolicyType policy = new PolicyType(); policy.Expiration = expiration; policy.Source = source; policy.MaxDownloads = 2; policy.MaxUploads = 0; policy.ContentLengthRange = range; policy.FormField = new FormFieldType[] {formField1, formField2}; Uri tokenUri = esu.CreateAccessToken(id, policy, null); WebResponse response = WebRequest.Create(tokenUri).GetResponse(); string content = new StreamReader(response.GetResponseStream()).ReadToEnd(); response.Close(); Assert.AreEqual(data, content, "Token URL does not contain proper content"); esu.DeleteAccessToken(tokenUri); tokenUri = esu.CreateAccessToken(op, policy, null); response = WebRequest.Create(tokenUri).GetResponse(); content = new StreamReader(response.GetResponseStream()).ReadToEnd(); response.Close(); Assert.AreEqual(data, content, "Token URL does not contain proper content"); policy.MaxDownloads = policy.MaxDownloads - 1; // we already used one ListOptions options = new ListOptions(); List<AccessTokenType> tokens = esu.ListAccessTokens(options); Assert.AreEqual(1, tokens.Count, "ListTokens returns wrong count"); AssertTokenPolicy(tokens[0], policy); AccessTokenType token = esu.GetAccessToken(tokenUri); esu.DeleteAccessToken(token.AccessTokenId); string path = tokenUri.GetComponents(UriComponents.Path, UriFormat.Unescaped); Assert.AreEqual(path.Split('/').Last(), token.AccessTokenId, "token ID doesn't match"); AssertTokenPolicy(token, policy); }