Statistics
| Branch: | Revision:

root / trunk / Pithos.Core.Test / NetworkAgentTest.cs @ a27aa447

History | View | Annotate | Download (3.3 kB)

1
using System;
2
using System.Collections.Generic;
3
using System.IO;
4
using System.Linq;
5
using System.Text;
6
using NUnit.Framework;
7
using Pithos.Core.Agents;
8
using Pithos.Network;
9

    
10
namespace Pithos.Core.Test
11
{
12
    [TestFixture]
13
    class NetworkAgentTest
14
    {
15
        [Test]
16
        public void TestUpload()
17
        {
18
            var agent = new NetworkAgent
19
            {
20
                PithosContainer = "pithos",
21
                BlockSize = 4194304,
22
                BlockHash="sha256",
23
                CloudClient = new CloudFilesClient
24
                                {
25
                                    AuthenticationUrl = @"https://pithos.dev.grnet.gr",
26
                                    UsePithos = true
27
                                }
28
            };
29

    
30
            agent.CloudClient.Authenticate("890329@vho.grnet.gr", "24989dce4e0fcb072f8cb60c8922be19");
31

    
32
            var fileName = "012345.dump";
33
            var filePath = Path.Combine(@"e:\pithos\", fileName);
34

    
35
            var random = new Random();
36
            var buffer = new byte[4096];
37
            random.NextBytes(buffer);
38

    
39
            if (File.Exists(filePath))
40
                File.Delete(filePath);
41

    
42
            using (var stream = File.OpenWrite(filePath))
43
            {
44
                for (var i = 0; i < 2* 1024;i++ )
45
                    stream.Write(buffer, 0, buffer.Length);
46
            }
47

    
48

    
49
            agent.CloudClient.DeleteObject(agent.PithosContainer, fileName);
50

    
51
            var task = Signature.CalculateTreeHashAsync(filePath, agent.BlockSize, agent.BlockHash);
52
            agent.UploadWithHashMap(new FileInfo(filePath),fileName,task);
53

    
54
            var newHash = agent.CloudClient.GetHashMap(agent.PithosContainer, fileName).Result;
55

    
56

    
57
            var treeHash = task.Result;
58
            Assert.AreEqual(treeHash.TopHash, newHash.TopHash);
59

    
60
            Assert.AreEqual(treeHash.Hashes, newHash.Hashes);
61

    
62
        }
63

    
64
        [Test]
65
        public void TestDownload()
66
        {
67
            var fileAgent = new FileAgent {FragmentsPath = @"e:\pithos\Fragments"};
68

    
69
            var agent = new NetworkAgent
70
            {
71
                PithosContainer = "pithos",
72
                BlockSize = 4194304,
73
                BlockHash="sha256",
74
                CloudClient = new CloudFilesClient
75
                                {
76
                                    AuthenticationUrl = @"https://pithos.dev.grnet.gr",
77
                                    UsePithos = true
78
                                },
79
                FileAgent=fileAgent
80
            };
81

    
82
            agent.CloudClient.Authenticate("890329@vho.grnet.gr", "24989dce4e0fcb072f8cb60c8922be19");
83

    
84
            var fileName = @"vlc-1.1.11-win32.exe";
85

    
86
            var filePath = Path.Combine(@"e:\pithos\", fileName);
87
            if (File.Exists(filePath))
88
                File.Delete(filePath);            
89
            
90
            var newHash = agent.CloudClient.GetHashMap(agent.PithosContainer, fileName).Result;
91
            agent.DownloadWithBlocks(agent.PithosContainer,new Uri(fileName,UriKind.Relative),filePath,newHash)
92
                .Wait();
93

    
94
            Assert.IsTrue(File.Exists(filePath));
95
            var treeHash = Signature.CalculateTreeHashAsync(filePath, agent.BlockSize, agent.BlockHash).Result;
96

    
97
            Assert.AreEqual(treeHash.TopHash, newHash.TopHash);
98

    
99
            Assert.AreEqual(treeHash.Hashes, newHash.Hashes);
100

    
101
        }
102

    
103
    }
104
}