1 package ch
.cyberduck
.core
.s3
;
3 import ch
.cyberduck
.core
.AbstractTestCase
;
4 import ch
.cyberduck
.core
.Credentials
;
5 import ch
.cyberduck
.core
.DescriptiveUrl
;
6 import ch
.cyberduck
.core
.DisabledPasswordStore
;
7 import ch
.cyberduck
.core
.Host
;
8 import ch
.cyberduck
.core
.Path
;
10 import org
.junit
.Test
;
13 import java
.util
.EnumSet
;
14 import java
.util
.Iterator
;
16 import static org
.junit
.Assert
.*;
21 public class S3UrlProviderTest
extends AbstractTestCase
{
24 public void testToHttpURL() throws Exception
{
25 final S3Session session
= new S3Session(new Host(new S3Protocol(), new S3Protocol().getDefaultHostname()));
26 Path p
= new Path("/bucket/f/key f", EnumSet
.of(Path
.Type
.file
));
27 assertEquals("https://bucket.s3.amazonaws.com/f/key%20f",
28 new S3UrlProvider(session
).toUrl(p
).find(DescriptiveUrl
.Type
.http
).getUrl());
29 assertTrue(new S3UrlProvider(session
).toUrl(p
).filter(DescriptiveUrl
.Type
.http
).contains(
30 new DescriptiveUrl(URI
.create("http://bucket.s3.amazonaws.com/f/key%20f"))
32 assertEquals(5, new S3UrlProvider(session
, new DisabledPasswordStore() {
34 public String
find(final Host host
) {
37 }).toUrl(p
).filter(DescriptiveUrl
.Type
.signed
).size());
41 public void testProviderUriWithKey() throws Exception
{
42 final S3Session session
= new S3Session(new Host(new S3Protocol(), new S3Protocol().getDefaultHostname()));
43 final Iterator
<DescriptiveUrl
> provider
= new S3UrlProvider(session
).toUrl(new Path("/test.cyberduck.ch/key",
44 EnumSet
.of(Path
.Type
.file
))).filter(DescriptiveUrl
.Type
.provider
).iterator();
45 assertEquals("https://s3.amazonaws.com/test.cyberduck.ch/key", provider
.next().getUrl());
46 assertEquals("s3://test.cyberduck.ch/key", provider
.next().getUrl());
50 public void testProviderUriRoot() throws Exception
{
51 final S3Session session
= new S3Session(new Host(new S3Protocol(), new S3Protocol().getDefaultHostname()));
52 final Iterator
<DescriptiveUrl
> provider
= new S3UrlProvider(session
).toUrl(new Path("/test.cyberduck.ch",
53 EnumSet
.of(Path
.Type
.directory
))).filter(DescriptiveUrl
.Type
.provider
).iterator();
54 assertEquals("https://s3.amazonaws.com/test.cyberduck.ch", provider
.next().getUrl());
55 assertEquals("s3://test.cyberduck.ch/", provider
.next().getUrl());
59 public void testHttpUri() throws Exception
{
60 final S3Session session
= new S3Session(new Host(new S3Protocol(), new S3Protocol().getDefaultHostname()));
61 assertEquals("https://test.cyberduck.ch.s3.amazonaws.com/key",
62 new S3UrlProvider(session
).toUrl(new Path("/test.cyberduck.ch/key", EnumSet
.of(Path
.Type
.file
))).find(DescriptiveUrl
.Type
.http
).getUrl());
66 public void testToSignedUrlAnonymous() throws Exception
{
67 final S3Session session
= new S3Session(new Host(new S3Protocol(), new S3Protocol().getDefaultHostname(),
68 new Credentials("anonymous", null)));
69 assertEquals(DescriptiveUrl
.EMPTY
,
70 new S3UrlProvider(session
, new DisabledPasswordStore() {
72 public String
find(final Host host
) {
75 }).toUrl(new Path("/test.cyberduck.ch/test f", EnumSet
.of(Path
.Type
.file
))).find(DescriptiveUrl
.Type
.signed
)
80 public void testToSignedUrlThirdparty() throws Exception
{
81 final S3Session session
= new S3Session(new Host(new S3Protocol(), "s.greenqloud.com",
82 new Credentials("k", "s")));
83 final S3UrlProvider provider
= new S3UrlProvider(session
, new DisabledPasswordStore() {
85 public String
find(final Host host
) {
90 provider
.toUrl(new Path("/test.cyberduck.ch/test", EnumSet
.of(Path
.Type
.file
))).find(DescriptiveUrl
.Type
.signed
)
95 public void testToSignedUrl() throws Exception
{
96 final S3Session session
= new S3Session(new Host(new S3Protocol(), new S3Protocol().getDefaultHostname(), new Credentials(
97 properties
.getProperty("s3.key"), null
99 final S3UrlProvider provider
= new S3UrlProvider(session
, new DisabledPasswordStore() {
101 public String
find(final Host host
) {
105 assertTrue(provider
.sign(new Path("/test.cyberduck.ch/test", EnumSet
.of(Path
.Type
.file
)), 30).getUrl().startsWith(
106 "https://test.cyberduck.ch.s3.amazonaws.com/test?AWSAccessKeyId=AKIAIGNLFZ2PXC6H2UPQ&Expires="));
110 public void testToTorrentUrl() throws Exception
{
111 final S3Session session
= new S3Session(new Host(new S3Protocol(), new S3Protocol().getDefaultHostname(),
112 new Credentials("anonymous", null)));
113 assertEquals(new DescriptiveUrl(URI
.create("http://test.cyberduck.ch.s3.amazonaws.com/test%20f?torrent"), DescriptiveUrl
.Type
.torrent
),
114 new S3UrlProvider(session
).toUrl(new Path("/test.cyberduck.ch/test f", EnumSet
.of(Path
.Type
.file
))).find(DescriptiveUrl
.Type
.torrent
));
118 public void testToTorrentUrlThirdparty() throws Exception
{
119 final S3Session session
= new S3Session(new Host(new S3Protocol(), "test.cyberduck.ch",
120 new Credentials("anonymous", null)));
121 assertEquals(new DescriptiveUrl(URI
.create("http://test.cyberduck.ch/c/test%20f?torrent"), DescriptiveUrl
.Type
.torrent
),
122 new S3UrlProvider(session
).toUrl(new Path("/c/test f", EnumSet
.of(Path
.Type
.file
))).find(DescriptiveUrl
.Type
.torrent
));
126 public void testPlaceholder() throws Exception
{
127 final S3Session session
= new S3Session(new Host(new S3Protocol(), new S3Protocol().getDefaultHostname(), new Credentials(
128 properties
.getProperty("s3.key"), null
131 new S3UrlProvider(session
).toUrl(new Path("/test.cyberduck.ch/test", EnumSet
.of(Path
.Type
.directory
))).filter(DescriptiveUrl
.Type
.signed
).isEmpty());