Statistics
| Branch: | Revision:

root / trunk / Pithos.Network.Test / ChecksumTest.cs @ c53aa229

History | View | Annotate | Download (1.9 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 string _apiKey = "9d3cb7b231e96f72ebe96af1c6cd5112";
18
        private string _userName = "pkanavos";
19
        private bool _usePithos = true;
20

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

    
32
        [Test]
33
        public void TestChecksum()
34
        {
35
            Assert.DoesNotThrow(() =>
36
            {
37

    
38
                var filePath = "devguide.pdf";
39
                var info=new FileInfo(filePath);
40

    
41
               
42
                    var hash = CalculateHash(filePath);
43
                    
44
                    client.PutObject(null, "Shares", info.Name, filePath);
45

    
46

    
47
                    var meta = client.GetObjectInfo(null, "Shares", "DeveloperGuide.pdf");
48
                    Assert.IsNotEmpty(meta.Hash);
49
                    
50

    
51
                    Assert.AreEqual(hash,meta.Hash,String.Format("The hashes don't match, expected {0} but got {1}",hash,meta.Hash));
52
                
53

    
54
            });
55

    
56
        
57
        }
58

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