예제 #1
0
 private bool containsId(List<ObjectResult> objects, ObjectId id)
 {
     foreach (ObjectResult res in objects)
     {
         if (res.Id.Equals(id))
         {
             return true;
         }
     }
     return false;
 }
예제 #2
0
        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);
        }