Statistics
| Branch: | Revision:

root / trunk / Pithos.Network.Test / ChecksumTest.cs @ 1cc1e8c5

History | View | Annotate | Download (1.8 kB)

1
using System;
2
using System.Collections.Generic;
3
using System.IO;
4
using System.Linq;
5
using System.Security.Cryptography;
6
using System.Text;
7
using NUnit.Framework;
8
using Pithos.Interfaces;
9

    
10

    
11
namespace Pithos.Network.Test
12
{
13
    [TestFixture]
14
    class ChecksumTest
15
    {
16

    
17
        private ICloudClient client;
18
        [SetUp]
19
        public void Setup()
20
        {
21
            var account = "890329@vho.grnet.gr";
22
            var apiKey = "24989dce4e0fcb072f8cb60c8922be19";
23
            client = new CloudFilesClient(account, apiKey);
24
            client.Authenticate();
25
            
26
        }
27

    
28
        [Test]
29
        public void TestChecksum()
30
        {
31
            Assert.DoesNotThrow(() =>
32
            {
33

    
34
                var filePath = "devguide.pdf";
35
                var info=new FileInfo(filePath);
36

    
37
               
38
                    var hash = CalculateHash(filePath);
39
                    
40
                    client.PutObject(null, "Shares", info.Name, filePath);
41

    
42

    
43
                    var meta = client.GetObjectInfo(null, "Shares", "DeveloperGuide.pdf");
44
                    Assert.IsNotEmpty(meta.X_Object_Hash);
45

    
46

    
47
                    Assert.AreEqual(hash, meta.X_Object_Hash, String.Format("The hashes don't match, expected {0} but got {1}", hash, meta.X_Object_Hash));
48
                
49

    
50
            });
51

    
52
        
53
        }
54

    
55
        private static string CalculateHash(string fileName)
56
        {
57
            string hash;
58
            using (var hasher = MD5.Create())
59
            using(var stream=File.OpenRead(fileName))
60
            {
61
                var hashBuilder = new StringBuilder();
62
                foreach (byte b in hasher.ComputeHash(stream))
63
                    hashBuilder.Append(b.ToString("x2").ToLower());
64
                hash = hashBuilder.ToString();
65
            }
66
            return hash;
67
        }
68
    }
69
}