root / trunk / Pithos.Network.Test / ChecksumTest.cs @ cfed7823
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 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 |
client = new CloudFilesClient(); |
26 |
client.Authenticate(_userName, _apiKey); |
27 |
|
28 |
} |
29 |
|
30 |
[Test] |
31 |
public void TestChecksum() |
32 |
{ |
33 |
Assert.DoesNotThrow(() => |
34 |
{ |
35 |
|
36 |
var filePath = "devguide.pdf"; |
37 |
var info=new FileInfo(filePath); |
38 |
|
39 |
|
40 |
var hash = CalculateHash(filePath); |
41 |
|
42 |
client.PutObject(null, "Shares", info.Name, filePath); |
43 |
|
44 |
|
45 |
var meta = client.GetObjectInfo(null, "Shares", "DeveloperGuide.pdf"); |
46 |
Assert.IsNotEmpty(meta.Hash); |
47 |
|
48 |
|
49 |
Assert.AreEqual(hash,meta.Hash,String.Format("The hashes don't match, expected {0} but got {1}",hash,meta.Hash)); |
50 |
|
51 |
|
52 |
}); |
53 |
|
54 |
|
55 |
} |
56 |
|
57 |
private static string CalculateHash(string fileName) |
58 |
{ |
59 |
string hash; |
60 |
using (var hasher = MD5.Create()) |
61 |
using(var stream=File.OpenRead(fileName)) |
62 |
{ |
63 |
var hashBuilder = new StringBuilder(); |
64 |
foreach (byte b in hasher.ComputeHash(stream)) |
65 |
hashBuilder.Append(b.ToString("x2").ToLower()); |
66 |
hash = hashBuilder.ToString(); |
67 |
} |
68 |
return hash; |
69 |
} |
70 |
} |
71 |
} |