Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scionFTP #178

Open
wants to merge 569 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
569 commits
Select commit Hold shift + click to select a range
28c1041
ftp: fix OPTS UTF8 ON for Filezilla Server
mappu Mar 3, 2017
68918a8
Remove useless assignment
jlaffaye Mar 4, 2017
602886c
Do not export the scanner type
jlaffaye Mar 4, 2017
cb3924c
Merge pull request #80 from mappu/master
jlaffaye Mar 4, 2017
7b85eb4
Document ServerConn is not safe for concurrent use.
jlaffaye Mar 4, 2017
fcbae66
Test Go 1.7.5 and 1.8.1
corny Apr 15, 2017
569481f
Add SetDeadline to Response
corny Apr 15, 2017
07efa8a
Merge pull request #84 from digineo/go1.8
jlaffaye Apr 15, 2017
0895dc7
Merge pull request #83 from digineo/deadline
jlaffaye Apr 15, 2017
58864d8
Avoid forever lock
DAddYE May 2, 2017
7e3820b
Address comments
DAddYE May 4, 2017
cb362c4
Rename connClosed -> closed
DAddYE May 5, 2017
980e2e0
Document that closing two times doesn't do anything
DAddYE May 5, 2017
5c7b901
Merge pull request #87 from DAddYE/patch-1
jlaffaye May 7, 2017
e529ec1
Send back a proper multiline response (#28)
allenluce Jun 13, 2017
479d87b
added recursively deleting folders
Mojachieee Jun 5, 2017
6bccbfb
Merge pull request #93 from Mojachieee/master
jlaffaye Jun 16, 2017
4b29841
Add a case to catch Unix-like but 0 link count list line
Jul 6, 2017
a05056b
Merge pull request #96 from allenluce/zero-link-count
jlaffaye Jul 7, 2017
0445d85
Fix possible runtime error
corny Jul 20, 2017
769512c
Merge pull request #98 from digineo/fix_97
jlaffaye Jul 21, 2017
f5cdbb9
Use net.TCPAddr to extract remote IP address
corny Aug 11, 2017
090169a
#105 Fix parseRFC3659ListLine issue
Sep 26, 2017
299b7ff
Merge pull request #106 from laraoz/parseRFC3659ListLine-issue
jlaffaye Sep 27, 2017
3f23c1d
Accept PBSZ and PROT before authentication, some clients need that.
preng69 Oct 12, 2017
2e834c6
Use tlsConfig in passive socket
preng69 Oct 12, 2017
3b1b186
Require AUTH TLS before USER when server is configured for TLS.
preng69 Oct 12, 2017
c2fc7f0
Merge pull request #101 from digineo/tcpaddr
jlaffaye Dec 29, 2017
352a1d8
Fix time parsing and reduce syscalls
corny Jan 4, 2018
5bde7e9
Don't reduce year of date to two digits
corny Jan 4, 2018
3748fd9
Add another test case
corny Jan 4, 2018
9996d9d
Add golint
corny Jan 4, 2018
38ce066
Fix golint warning
corny Jan 4, 2018
1b2b72d
Add newTime helper to tests
corny Jan 4, 2018
2c34dfa
Merge pull request #111 from digineo/golint
jlaffaye Jan 5, 2018
83891db
Merge pull request #110 from digineo/time-parsing
jlaffaye Jan 5, 2018
db42e02
Fix connection failure in PASV mode. (#32)
Feb 1, 2018
8aeb589
Parse IP address returned with PASV to fix load balanced FTP servers
ncw Feb 4, 2018
4274679
Merge pull request #115 from ncw/fix-pasv
jlaffaye Feb 4, 2018
1bf7a78
fix bug when info is nil
lunny Feb 27, 2018
f100e25
Merge branch 'master' of github.com:goftp/server
lunny Feb 27, 2018
3bb68cb
Add test framework (#33)
lunny Feb 27, 2018
e814d4e
update README
lunny Feb 27, 2018
d032fb1
LICENSE
lunny Feb 27, 2018
d5afaa2
Merge branch 'master' into fix-explicit-tls
lunny Mar 1, 2018
b22a18d
Fix race bug and add race test (#35)
lunny Mar 3, 2018
4772add
Make it possible to specify what timezone to parse timestamps in
Mar 20, 2018
dab2053
Merge pull request #116 from freman/master
jlaffaye Mar 22, 2018
b481e9e
Fix documentation for driver interface (#37)
sapk Mar 25, 2018
2403248
Use last two Go version for travis
jlaffaye Apr 4, 2018
d4e78b2
Only open data connection if no error (#38)
sapk Apr 12, 2018
9f644f2
Refactor parse list param (#39)
sapk Apr 18, 2018
8217c2b
Add conn test (#40)
sapk Apr 19, 2018
fd25d82
add more tests and add badge on README
lunny Apr 19, 2018
ada2252
remove duplicated badge
lunny Apr 19, 2018
e2f3f73
more tests
lunny Apr 19, 2018
e79842b
more tests
lunny Apr 20, 2018
fe8e7ea
release v0.3.0
lunny Apr 20, 2018
c63d319
Implement Serve function to allow custom Listeners (#41)
folbricht Apr 23, 2018
114653a
Improve shutdown behaviour (#42)
folbricht Apr 25, 2018
51dfc8d
:bug: Fixes the problem of integer overflow when file size is too lar…
Mr-Dai May 2, 2018
6ce292d
:zap: Use sendfile syscall to send out file (#44)
Mr-Dai May 3, 2018
3f6433f
Workaround for FTP servers, that does not support UTF8 option. (#120)
larrabee Aug 8, 2018
0616c1e
Include UTF8 in FEAT list since the server supports UTF-8. (#30)
preng69 Aug 21, 2018
75862fd
merge
lunny Aug 21, 2018
b80db66
rebase
lunny Aug 21, 2018
ef4ed0d
Merge branch 'preng69-fix-explicit-tls'
lunny Aug 21, 2018
2fd9658
Fix socket leak on passive connections (#51)
ncw Sep 9, 2018
692e4bb
Fix "bind: address already in use" message when using passive mode (#50)
ncw Sep 9, 2018
c80f184
fix feats command bug (#53)
lunny Sep 9, 2018
bf3ca56
Fix race condition in GoListenAndServe() for passive sockets (#49)
ncw Sep 9, 2018
ceca2da
Make a new simpler example server (#52)
ncw Sep 11, 2018
3c330b4
Fix bug that hang on list a file (#54)
odknt Sep 12, 2018
e2ad872
remove trace log (#55)
lunny Sep 12, 2018
18b6436
Don't end error messages with \n as this confuses some clients. (#56)
ncw Sep 13, 2018
c472d79
Make sure we reset the append flag on RETR (#57)
ncw Sep 13, 2018
ec84fce
Fix formating for fmt.Sprint() messages (#58)
ncw Sep 13, 2018
1fd52c8
Fix race condition for passive sockets (#59)
ncw Sep 14, 2018
a6cab19
Update Go version in Travis build.
xperimental Oct 6, 2018
89749c6
Run tests with Go 1.11
corny Oct 10, 2018
fe78734
Merge pull request #126 from digineo/go-1.11
jlaffaye Oct 11, 2018
47f21d1
Merge pull request #124 from xperimental/upgrade-go
jlaffaye Nov 1, 2018
88de73f
Fix socket leak for never used PASV connections (#62)
ncw Jan 11, 2019
509a294
Add missing error check in RemoveDirRecur
jlaffaye Jan 22, 2019
f75dead
error strings should not be capitalized (ST1005)
jlaffaye Jan 22, 2019
72f5c01
StatusText returns a text for the FTP status code
jlaffaye Jan 22, 2019
b218223
Use error values in tests
jlaffaye Jan 22, 2019
8019e67
Add tests for StatusText
jlaffaye Jan 26, 2019
eabccc5
Report errors to client when sending the RETR data (#63)
ncw Mar 4, 2019
5554648
Use mock for all tests
jlaffaye Apr 10, 2019
04b1878
Add DialWithOptions
jlaffaye Apr 10, 2019
73b56ee
Rework README
jlaffaye Apr 10, 2019
52d3001
Specify that a server conn is not concurrent safe.
jlaffaye Apr 11, 2019
75b9ebe
Fix errors in example code
jlaffaye Apr 15, 2019
8b7b512
Add DialWithDebugOutput to log commands.
jlaffaye Apr 17, 2019
9b5a3ad
Update issue templates
jlaffaye Apr 17, 2019
e6de3d3
Replace DialWithOptions with Dial
jlaffaye Apr 23, 2019
6a014d5
Add DialWithDialFunc to specify dial function used for both control a…
alexpevzner Apr 27, 2019
8f5b34c
Complete Implicit FTPS support (#143)
gary-kim May 19, 2019
b7acaf1
Fix the usage of example (#71)
wzshiming Jul 11, 2019
8751113
Use constant-time compare for SimpleAuth (#68)
titanous Jul 11, 2019
ef16055
add drone support (#74)
lunny Jul 12, 2019
189ad87
Add appe command support (#73)
lunny Jul 12, 2019
6f2be9e
Fix for `passiveListenIP`. Currently the function returns different (…
xetorthio Jul 12, 2019
dea12ec
add xmkd support (#75)
lunny Jul 12, 2019
2ddf435
added LPRT command (#48)
discobean Jul 12, 2019
c46bdf8
compitable previous public ip setting (#76)
lunny Jul 12, 2019
1149070
Update conn.go (#77)
lichao2014 Jul 12, 2019
b61bdec
update projects new package name
lunny Jul 12, 2019
dee2229
Added client implementation
elwin Jul 18, 2019
fd3021f
Fixed import paths
elwin Jul 18, 2019
cb9866d
Initial commit
elwin Jul 18, 2019
df2eabd
Added client
elwin Jul 18, 2019
367aa9d
Added server
elwin Jul 18, 2019
ce5bc65
Removed TLS
elwin Jul 18, 2019
f7c7413
Now removed all tsl
elwin Jul 18, 2019
f5248df
Refactored cmd in client
elwin Jul 19, 2019
ce1e241
Renamed exampleftp to scionftp
elwin Jul 19, 2019
a0449bb
Expanded Client
elwin Jul 19, 2019
3322378
Refactoring
elwin Jul 19, 2019
47d7ef8
renamed client to response
elwin Jul 19, 2019
4399526
Switched to new socket
elwin Jul 19, 2019
4997e67
Added Multisocket
elwin Jul 19, 2019
22ea52b
Striping working!
elwin Jul 19, 2019
a220f2a
Removed logging
elwin Jul 19, 2019
6a3e5f1
Removed Connect Alias
elwin Jul 20, 2019
b037cbb
Added scion helpers
elwin Jul 21, 2019
7f6841e
Replaced net SCION
elwin Jul 21, 2019
24edd34
Closed listener
elwin Jul 21, 2019
a93b37e
made host flag mandatory
elwin Jul 23, 2019
04ce602
Fixed nasty bug (messed up remote & local in init)
elwin Aug 21, 2019
da94a71
Reordered client
elwin Aug 22, 2019
0f9f724
Added ERET
elwin Aug 22, 2019
61737f9
Working ERET
elwin Aug 22, 2019
3727d41
small cleanup
elwin Aug 23, 2019
5486224
Fixed single path bug: don't close listener when still listening
elwin Aug 23, 2019
babbc87
Added my own address struct, finally
elwin Aug 23, 2019
44e5143
cleaaaaning
elwin Aug 23, 2019
6245a83
Added Testbench
elwin Aug 24, 2019
8d772dc
Added vendor file
elwin Aug 25, 2019
64ffaf9
updateded venodr
elwin Aug 25, 2019
837497a
Added all vendor packages cuz have trouble syncing
elwin Aug 25, 2019
cdefbec
modified queue with channels
elwin Aug 25, 2019
4893554
Can now add PathSelector
elwin Aug 25, 2019
8e8f409
Added rotating path selector to testbench
elwin Aug 25, 2019
f1222b3
Removed wrong deps
elwin Aug 26, 2019
68d7c3a
Added flag parse
elwin Aug 26, 2019
2a0cde3
printing
elwin Aug 26, 2019
80ac85a
asdf
elwin Aug 26, 2019
db13b4a
Renamed path selector
elwin Aug 26, 2019
4d0e0ba
Augmented scion with address helpers
elwin Aug 28, 2019
a5a7a8c
Prallelized dialling, added :0 port
elwin Aug 28, 2019
f984f1e
Added csv to path selector
elwin Aug 28, 2019
6610d3f
added config option to scion
elwin Aug 29, 2019
16fa9d9
reverted speedtest_client refactor
elwin Aug 29, 2019
146e860
fixed client thing
elwin Aug 29, 2019
ac0053e
new tests
elwin Aug 29, 2019
b7dd7cb
updated vendor (?)
elwin Aug 29, 2019
45379bd
fixed vendoor stuff?
elwin Aug 29, 2019
170a628
removed mode
elwin Aug 29, 2019
c6ff1f2
added bandwith measurement
elwin Aug 29, 2019
eac12a6
interactive selector?
elwin Aug 29, 2019
77b9fd4
added selector
elwin Aug 29, 2019
b8818f4
added new path selectors
elwin Aug 29, 2019
07e7926
fixed rotating path selecor
elwin Aug 29, 2019
d1ee083
Small changes to test
elwin Sep 2, 2019
95d09b6
Fixed / Workound for large block size
elwin Sep 8, 2019
5b0e27b
Set new block sizes
elwin Sep 8, 2019
ce0ce93
reverted to previous with bug
elwin Sep 8, 2019
871660a
Merge branch 'scion' of https://github.com/elwin/transmit2 into scion
elwin Sep 8, 2019
083a2b6
Revert "Small changes to test"
elwin Sep 8, 2019
09144fd
Reverted changes (hopefully)
elwin Sep 8, 2019
6df3a2f
added proper labels
elwin Sep 8, 2019
509e575
added new tests
elwin Sep 8, 2019
1ce9e01
changed size unit to 1000x1000
elwin Sep 8, 2019
43caa78
Multiple repetitions
elwin Sep 11, 2019
0bacefb
Removed repetitions
elwin Sep 11, 2019
865ead3
warmup with mulitple paths
elwin Sep 11, 2019
630b8b1
fixed previous comit
elwin Sep 11, 2019
a24a92c
Sleep before etst start
elwin Sep 11, 2019
0d0c4db
added max paths to ratator
elwin Sep 11, 2019
613a4e9
fixed getPathsCount
elwin Sep 11, 2019
27019aa
Added static selcetor
elwin Sep 14, 2019
a79d10c
print used path
elwin Sep 14, 2019
e982e10
remove log .
elwin Sep 14, 2019
6acdb75
Added log
elwin Sep 14, 2019
220c024
Fixed rotating path selector
elwin Sep 14, 2019
11360f0
increased maxpaths
elwin Sep 14, 2019
4b7a2cc
Moved repo ScionFTP and added doc
elwin Sep 19, 2019
9fbe413
Noted cur directory in interactive
elwin Sep 19, 2019
3136577
 Cleanup
elwin Sep 19, 2019
30040a7
Updated Docs for Libraries
elwin Sep 19, 2019
916f37c
Can start now from everywhere
elwin Oct 15, 2019
ebe38da
Make scionftp work with newest scion libraries.
cneukom Oct 22, 2020
6a8e4a2
Remove leftovers
cneukom Oct 22, 2020
f2d57cf
Add build files to .gitignore
cneukom Oct 22, 2020
9bd425c
Proof of concept implementation of RETR_HERCULES command
cneukom Oct 23, 2020
7a22b9c
Remove -key and -cert in client
cneukom Oct 23, 2020
924b35a
Add -hercules flag to specify hercules binary on both server and client.
cneukom Oct 23, 2020
bca35ae
Add config param for hercules with geth
cneukom Oct 27, 2020
43dca3a
Add separate Quic stream for keep alive packets to prevent interleavi…
cneukom Nov 3, 2020
1815005
Fix for 43dca3: keep alive stream is now optional.
cneukom Nov 3, 2020
c9eaf2f
Locking for Hercules, currently only one concurrent transfer supported
cneukom Nov 5, 2020
f7e21ee
Use sudo to start hercules, so that scionftp itself does not need to …
cneukom Nov 5, 2020
faf1c21
check if file can be read, before starting hercules in RETR_HERCULES
cneukom Nov 5, 2020
a13fbbb
Advertise RETR_HERCULES in the serer, print a hint on the client, if …
cneukom Nov 5, 2020
2d003cd
Add 'scionftp/' from commit 'a13fbbb82e1ba47dc400c4aac2dfff46c4449136'
cneukom Nov 12, 2020
b5dba1e
Integrate scion-ftp into scion-apps (lint, build)
cneukom Nov 12, 2020
3b2e70c
Update scionftp README
cneukom Nov 12, 2020
93f317f
Rename scionftp package to ftp
cneukom Nov 12, 2020
c1d27f0
Set the correct user on the received file after RETR_HERCULES
cneukom Nov 12, 2020
b6a7650
Fix typo
cneukom Nov 13, 2020
a18d004
Add RealPath to the ftpserver's Driver interface to make use of it in…
cneukom Nov 13, 2020
e9cf93a
Implement STOR_HERCULES
cneukom Nov 13, 2020
fd4692b
Use PASV for Hercules transfers
cneukom Nov 13, 2020
713c9c9
Minor refactoring
cneukom Nov 17, 2020
d4ba02f
Merge branch 'master' into cneukom/scionftp
cneukom Dec 22, 2020
1261314
Add copyright note
cneukom Dec 22, 2020
4a3fc58
remove obsolete .gitignore
cneukom Dec 22, 2020
07f83c9
Clean up scionFTP directory structure
cneukom Jan 7, 2021
2f4d1ad
Remove -local from scion-ftp
cneukom Jan 7, 2021
dae75de
Correct Copyright notes
cneukom Jan 13, 2021
cace751
Reorganize scion-ftp(d) files to move towards the Golang Standard Pro…
cneukom Jan 13, 2021
92d6000
Rename ftpd/internal/ftp to core to be consistent with goftp
cneukom Jan 13, 2021
5f16283
Add support for anonymous FTP
cneukom Jan 13, 2021
523891a
Remove -cert and -key in scionFTP and use dummy certificate.
cneukom Jan 13, 2021
7f4cca7
Remove 3rd argument for geth/puth, instead resolve Hercules config fi…
cneukom Jan 14, 2021
12e7559
Replace Hercules specific commands with Hercules mode
cneukom Jan 19, 2021
831c560
Remove duplicate address parsing logic in scion ftp
cneukom Jan 20, 2021
b8b0024
Remove obsolete debug output
cneukom Jan 20, 2021
f46648e
Refactor internal/ftp/scion to internal/ftp/sockquic, remove -host fr…
cneukom Jan 21, 2021
e2c61e9
Remove redundancies and dead code
cneukom Feb 9, 2021
41e37e8
Update documentation
cneukom Feb 9, 2021
0a84d4d
Remove obsolete go.sum in ftpd/internal/core
cneukom Feb 9, 2021
19b90cb
Rename import aliases (xy2 -> libxy)
cneukom Feb 11, 2021
12616b1
Replace custom queue with container/heap in GridFTP striping
cneukom Feb 11, 2021
7169bd8
In mode E: make server return SCION address again
cneukom Feb 11, 2021
bcda2d2
Move multisocket to striping package
cneukom Feb 11, 2021
4327f4c
Drop DataSocket in favour of net.Conn
cneukom Feb 16, 2021
b93cf31
Remove pointer noise in segment queue
cneukom Mar 17, 2021
7f77714
Refactor sockquic into SingleSocket
cneukom Mar 17, 2021
e05b394
Remove unused isClosed and typo in README.
cneukom Mar 17, 2021
deee607
Use a single listener for striped data connections and close that aft…
cneukom Mar 17, 2021
721eb4b
Refactor "close later" into DelayedCloserSocket and use it in EPSV too.
cneukom Mar 17, 2021
113260d
Add note regarding Hercules mode to openDataConn()
cneukom Mar 18, 2021
c47777b
Remove superfluous reply from "MODE S"
cneukom Mar 18, 2021
7b3ee70
Reorganize imports
cneukom Mar 23, 2021
7754131
Adapt license of delayedclosersocket.go
cneukom Mar 23, 2021
845ce45
Use concrete type in SegmentQueue
cneukom Mar 23, 2021
e8c2715
move listener to internal/ftp/socket
cneukom Mar 23, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ build: scion-bat \
scion-netcat \
scion-sensorfetcher scion-sensorserver \
scion-ssh scion-sshd \
scion-ftp scion-ftpd \
example-helloworld \
example-shttp-client example-shttp-server example-shttp-fileserver example-shttp-proxy

Expand Down Expand Up @@ -90,6 +91,14 @@ scion-sshd:
scion-webapp:
go build -tags=$(TAGS) -o $(BIN)/$@ ./webapp/

.PHONY: scion-ftp
scion-ftp:
go build -tags=$(TAGS) -o $(BIN)/$@ ./ftp/

.PHONY: scion-ftpd
scion-ftpd:
go build -tags=$(TAGS) -o $(BIN)/$@ ./ftpd/

.PHONY: example-helloworld
example-helloworld:
go build -tags=$(TAGS) -o $(BIN)/$@ ./_examples/helloworld/
Expand Down
14 changes: 14 additions & 0 deletions ftp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# scion-ftp

This is an interactive FTP client for testing and demonstrating usage of FTP on the SCION network. Build this
application from [scion-apps](../../) using the command `make scion-ftp`

```
$ scion-ftp
Usage of scion-ftp:
-hercules string
Enable RETR_HERCULES using the Hercules binary specified
In Hercules mode, scionFTP checks the following directories for Hercules config files: ., /etc, /etc/scion-ftp
```

After starting the application run `help` to see all available commands.
13 changes: 13 additions & 0 deletions ftp/internal/ftp/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright (c) 2011-2013, Julien Laffaye <[email protected]>

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 changes: 24 additions & 0 deletions ftp/internal/ftp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# scionftp client library

Client package for FTP + GridFTP extension, adapted to the SCION network.
Forked from [jlaffaye/ftp](https://github.com/jlaffaye/ftp), with SCION support originally added in [elwin/scionFTP](https://github.com/elwin/scionFTP).

## Example ##

```go
c, err := ftp.Dial("1-ff00:0:110,[127.0.0.1]:4000, 1-ff00:0:110,[127.0.0.1]:2121", ftp.DialWithTimeout(5*time.Second))
if err != nil {
log.Fatal(err)
}

err = c.Login("admin", "123456")
if err != nil {
log.Fatal(err)
}

// Do something with the FTP connection

if err := c.Quit(); err != nil {
log.Fatal(err)
}
```
281 changes: 281 additions & 0 deletions ftp/internal/ftp/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,281 @@
// Copyright (c) 2011-2013, Julien Laffaye <[email protected]>
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
// Copyright 2020-2021 ETH Zurich modifications to add support for SCION

package ftp

import (
"bytes"
"io/ioutil"
"net/textproto"
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

const (
testData = "Just some text"
testDir = "mydir"
)

func TestConnPASV(t *testing.T) {
testConn(t, true)
}

func TestConnEPSV(t *testing.T) {
testConn(t, false)
}

func testConn(t *testing.T, disableEPSV bool) {

mock, c := openConn(t, "127.0.0.1", DialWithTimeout(5*time.Second), DialWithDisabledEPSV(disableEPSV))

err := c.Login("anonymous", "anonymous")
if err != nil {
t.Fatal(err)
}

err = c.NoOp()
if err != nil {
t.Error(err)
}

err = c.ChangeDir("incoming")
if err != nil {
t.Error(err)
}

dir, err := c.CurrentDir()
if err != nil {
t.Error(err)
} else {
if dir != "/incoming" {
t.Error("Wrong dir: " + dir)
}
}

data := bytes.NewBufferString(testData)
err = c.Stor("test", data)
if err != nil {
t.Error(err)
}

_, err = c.List(".")
if err != nil {
t.Error(err)
}

err = c.Rename("test", "tset")
if err != nil {
t.Error(err)
}

// Read without deadline
r, err := c.Retr("tset")
if err != nil {
t.Error(err)
} else {
buf, err := ioutil.ReadAll(r)
if err != nil {
t.Error(err)
}
if string(buf) != testData {
t.Errorf("'%s'", buf)
}
assert.NoError(t, r.Close())
assert.NoError(t, r.Close()) // test we can close two times
}

// Read with deadline
r, err = c.Retr("tset")
if err != nil {
t.Error(err)
} else {
assert.NoError(t, r.SetDeadline(time.Now()))
_, err := ioutil.ReadAll(r)
if err == nil {
t.Error("deadline should have caused error")
} else if !strings.HasSuffix(err.Error(), "i/o timeout") {
t.Error(err)
}
assert.NoError(t, r.Close())
}

// Read with offset
r, err = c.RetrFrom("tset", 5)
if err != nil {
t.Error(err)
} else {
buf, err := ioutil.ReadAll(r)
if err != nil {
t.Error(err)
}
expected := testData[5:]
if string(buf) != expected {
t.Errorf("read %q, expected %q", buf, expected)
}
assert.NoError(t, r.Close())
}

fileSize, err := c.FileSize("magic-file")
if err != nil {
t.Error(err)
}
if fileSize != 42 {
t.Errorf("file size %q, expected %q", fileSize, 42)
}

_, err = c.FileSize("not-found")
if err == nil {
t.Fatal("expected error, got nil")
}

err = c.Delete("tset")
if err != nil {
t.Error(err)
}

err = c.MakeDir(testDir)
if err != nil {
t.Error(err)
}

err = c.ChangeDir(testDir)
if err != nil {
t.Error(err)
}

err = c.ChangeDirToParent()
if err != nil {
t.Error(err)
}

entries, err := c.NameList("/")
if err != nil {
t.Error(err)
}
if len(entries) != 1 || entries[0] != "/incoming" {
t.Errorf("Unexpected entries: %v", entries)
}

err = c.RemoveDir(testDir)
if err != nil {
t.Error(err)
}

err = c.Logout()
if err != nil {
if protoErr := err.(*textproto.Error); protoErr != nil {
if protoErr.Code != StatusNotImplemented {
t.Error(err)
}
} else {
t.Error(err)
}
}

if err := c.Quit(); err != nil {
t.Fatal(err)
}

// Wait for the connection to close
mock.Wait()

err = c.NoOp()
if err == nil {
t.Error("Expected error")
}
}

func TestTimeout(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}

c, err := DialTimeout("localhost:2121", 1*time.Second)
if err == nil {
t.Fatal("expected timeout, got nil error")
_ = c.Quit()
}
}

func TestWrongLogin(t *testing.T) {
mock, err := newFtpMock(t, "127.0.0.1")
if err != nil {
t.Fatal(err)
}
defer mock.Close()

c, err := DialTimeout(mock.Addr(), 5*time.Second)
if err != nil {
t.Fatal(err)
}
defer func() { assert.NoError(t, c.Quit()) }()

err = c.Login("zoo2Shia", "fei5Yix9")
if err == nil {
t.Fatal("expected error, got nil")
}
}

func TestDeleteDirRecur(t *testing.T) {
mock, c := openConn(t, "127.0.0.1")

err := c.RemoveDirRecur("testDir")
if err != nil {
t.Error(err)
}

if err := c.Quit(); err != nil {
t.Fatal(err)
}

// Wait for the connection to close
mock.Wait()
}

// func TestFileDeleteDirRecur(t *testing.T) {
// mock, c := openConn(t, "127.0.0.1")

// err := c.RemoveDirRecur("testFile")
// if err == nil {
// t.Fatal("expected error got nil")
// }

// if err := c.Quit(); err != nil {
// t.Fatal(err)
// }

// // Wait for the connection to close
// mock.Wait()
// }

func TestMissingFolderDeleteDirRecur(t *testing.T) {
mock, c := openConn(t, "127.0.0.1")

err := c.RemoveDirRecur("missing-dir")
if err == nil {
t.Fatal("expected error got nil")
}

if err := c.Quit(); err != nil {
t.Fatal(err)
}

// Wait for the connection to close
mock.Wait()
}
Loading