| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055 |
- package sftpd_test
- import (
- "bufio"
- "bytes"
- "crypto/rand"
- "crypto/sha256"
- "crypto/sha512"
- "fmt"
- "hash"
- "io"
- "io/ioutil"
- "math"
- "net"
- "net/http"
- "os"
- "os/exec"
- "path"
- "path/filepath"
- "runtime"
- "strings"
- "testing"
- "time"
- _ "github.com/go-sql-driver/mysql"
- _ "github.com/lib/pq"
- _ "github.com/mattn/go-sqlite3"
- "golang.org/x/crypto/ssh"
- "github.com/drakkan/sftpgo/config"
- "github.com/drakkan/sftpgo/dataprovider"
- "github.com/drakkan/sftpgo/httpd"
- "github.com/drakkan/sftpgo/logger"
- "github.com/drakkan/sftpgo/sftpd"
- "github.com/drakkan/sftpgo/utils"
- "github.com/pkg/sftp"
- "github.com/rs/zerolog"
- )
- const (
- logSender = "sftpdTesting"
- sftpServerAddr = "127.0.0.1:2022"
- defaultUsername = "test_user_sftp"
- defaultPassword = "test_password"
- testPubKey = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC03jj0D+djk7pxIf/0OhrxrchJTRZklofJ1NoIu4752Sq02mdXmarMVsqJ1cAjV5LBVy3D1F5U6XW4rppkXeVtd04Pxb09ehtH0pRRPaoHHlALiJt8CoMpbKYMA8b3KXPPriGxgGomvtU2T2RMURSwOZbMtpsugfjYSWenyYX+VORYhylWnSXL961LTyC21ehd6d6QnW9G7E5hYMITMY9TuQZz3bROYzXiTsgN0+g6Hn7exFQp50p45StUMfV/SftCMdCxlxuyGny2CrN/vfjO7xxOo2uv7q1qm10Q46KPWJQv+pgZ/OfL+EDjy07n5QVSKHlbx+2nT4Q0EgOSQaCTYwn3YjtABfIxWwgAFdyj6YlPulCL22qU4MYhDcA6PSBwDdf8hvxBfvsiHdM+JcSHvv8/VeJhk6CmnZxGY0fxBupov27z3yEO8nAg8k+6PaUiW1MSUfuGMF/ktB8LOstXsEPXSszuyXiOv4DaryOXUiSn7bmRqKcEFlJusO6aZP0= nicola@p1"
- testPubKey1 = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCd60+/j+y8f0tLftihWV1YN9RSahMI9btQMDIMqts/jeNbD8jgoogM3nhF7KxfcaMKURuD47KC4Ey6iAJUJ0sWkSNNxOcIYuvA+5MlspfZDsa8Ag76Fe1vyz72WeHMHMeh/hwFo2TeIeIXg480T1VI6mzfDrVp2GzUx0SS0dMsQBjftXkuVR8YOiOwMCAH2a//M1OrvV7d/NBk6kBN0WnuIBb2jKm15PAA7+jQQG7tzwk2HedNH3jeL5GH31xkSRwlBczRK0xsCQXehAlx6cT/e/s44iJcJTHfpPKoSk6UAhPJYe7Z1QnuoawY9P9jQaxpyeImBZxxUEowhjpj2avBxKdRGBVK8R7EL8tSOeLbhdyWe5Mwc1+foEbq9Zz5j5Kd+hn3Wm1UnsGCrXUUUoZp1jnlNl0NakCto+5KmqnT9cHxaY+ix2RLUWAZyVFlRq71OYux1UHJnEJPiEI1/tr4jFBSL46qhQZv/TfpkfVW8FLz0lErfqu0gQEZnNHr3Fc= nicola@p1"
- testPrivateKey = `-----BEGIN OPENSSH PRIVATE KEY-----
- b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABlwAAAAdzc2gtcn
- NhAAAAAwEAAQAAAYEAtN449A/nY5O6cSH/9Doa8a3ISU0WZJaHydTaCLuO+dkqtNpnV5mq
- zFbKidXAI1eSwVctw9ReVOl1uK6aZF3lbXdOD8W9PXobR9KUUT2qBx5QC4ibfAqDKWymDA
- PG9ylzz64hsYBqJr7VNk9kTFEUsDmWzLabLoH42Elnp8mF/lTkWIcpVp0ly/etS08gttXo
- XenekJ1vRuxOYWDCEzGPU7kGc920TmM14k7IDdPoOh5+3sRUKedKeOUrVDH1f0n7QjHQsZ
- cbshp8tgqzf734zu8cTqNrr+6taptdEOOij1iUL/qYGfzny/hA48tO5+UFUih5W8ftp0+E
- NBIDkkGgk2MJ92I7QAXyMVsIABXco+mJT7pQi9tqlODGIQ3AOj0gcA3X/Ib8QX77Ih3TPi
- XEh77/P1XiYZOgpp2cRmNH8QbqaL9u898hDvJwIPJPuj2lIltTElH7hjBf5LQfCzrLV7BD
- 10rM7sl4jr+A2q8jl1Ikp+25kainBBZSbrDummT9AAAFgDU/VLk1P1S5AAAAB3NzaC1yc2
- EAAAGBALTeOPQP52OTunEh//Q6GvGtyElNFmSWh8nU2gi7jvnZKrTaZ1eZqsxWyonVwCNX
- ksFXLcPUXlTpdbiummRd5W13Tg/FvT16G0fSlFE9qgceUAuIm3wKgylspgwDxvcpc8+uIb
- GAaia+1TZPZExRFLA5lsy2my6B+NhJZ6fJhf5U5FiHKVadJcv3rUtPILbV6F3p3pCdb0bs
- TmFgwhMxj1O5BnPdtE5jNeJOyA3T6Doeft7EVCnnSnjlK1Qx9X9J+0Ix0LGXG7IafLYKs3
- +9+M7vHE6ja6/urWqbXRDjoo9YlC/6mBn858v4QOPLTuflBVIoeVvH7adPhDQSA5JBoJNj
- CfdiO0AF8jFbCAAV3KPpiU+6UIvbapTgxiENwDo9IHAN1/yG/EF++yId0z4lxIe+/z9V4m
- GToKadnEZjR/EG6mi/bvPfIQ7ycCDyT7o9pSJbUxJR+4YwX+S0Hws6y1ewQ9dKzO7JeI6/
- gNqvI5dSJKftuZGopwQWUm6w7ppk/QAAAAMBAAEAAAGAHKnC+Nq0XtGAkIFE4N18e6SAwy
- 0WSWaZqmCzFQM0S2AhJnweOIG/0ZZHjsRzKKauOTmppQk40dgVsejpytIek9R+aH172gxJ
- 2n4Cx0UwduRU5x8FFQlNc/kl722B0JWfJuB/snOZXv6LJ4o5aObIkozt2w9tVFeAqjYn2S
- 1UsNOfRHBXGsTYwpRDwFWP56nKo2d2wBBTHDhCy6fb2dLW1fvSi/YspueOGIlHpvlYKi2/
- CWqvs9xVrwcScMtiDoQYq0khhO0efLCxvg/o+W9CLMVM2ms4G1zoSUQKN0oYWWQJyW4+VI
- YneWO8UpN0J3ElXKi7bhgAat7dBaM1g9IrAzk153DiEFZNsPxGOgL/+YdQN7zUBx/z7EkI
- jyv80RV7fpUXvcq2p+qNl6UVig3VSzRrnsaJkUWu/A0u59ha7ocv6NxDIXjxpIDJme16GF
- quiGVBQNnYJymS/vFEbGf6bgf7iRmMCRUMG4nqLA6fPYP9uAtch+CmDfVLZC/fIdC5AAAA
- wQCDissV4zH6bfqgxJSuYNk8Vbb+19cF3b7gH1rVlB3zxpCAgcRgMHC+dP1z2NRx7UW9MR
- nye6kjpkzZZ0OigLqo7TtEq8uTglD9o6W7mRXqhy5A/ySOmqPL3ernHHQhGuoNODYAHkOU
- u2Rh8HXi+VLwKZcLInPOYJvcuLG4DxN8WfeVvlMHwhAOaTNNOtL4XZDHQeIPc4qHmJymmv
- sV7GuyQ6yW5C10uoGdxRPd90Bh4z4h2bKfZFjvEBbSBVkqrlAAAADBAN/zNtNayd/dX7Cr
- Nb4sZuzCh+CW4BH8GOePZWNCATwBbNXBVb5cR+dmuTqYm+Ekz0VxVQRA1TvKncluJOQpoa
- Xj8r0xdIgqkehnfDPMKtYVor06B9Fl1jrXtXU0Vrr6QcBWruSVyK1ZxqcmcNK/+KolVepe
- A6vcl/iKaG4U7su166nxLST06M2EgcSVsFJHpKn5+WAXC+X0Gx8kNjWIIb3GpiChdc0xZD
- mq02xZthVJrTCVw/e7gfDoB2QRsNV8HwAAAMEAzsCghZVp+0YsYg9oOrw4tEqcbEXEMhwY
- 0jW8JNL8Spr1Ibp5Dw6bRSk5azARjmJtnMJhJ3oeHfF0eoISqcNuQXGndGQbVM9YzzAzc1
- NbbCNsVroqKlChT5wyPNGS+phi2bPARBno7WSDvshTZ7dAVEP2c9MJW0XwoSevwKlhgSdt
- RLFFQ/5nclJSdzPBOmQouC0OBcMFSrYtMeknJ4VvueVvve5HcHFaEsaMc7ABAGaLYaBQOm
- iixITGvaNZh/tjAAAACW5pY29sYUBwMQE=
- -----END OPENSSH PRIVATE KEY-----`
- configDir = ".."
- )
- var (
- allPerms = []string{dataprovider.PermAny}
- homeBasePath string
- scpPath string
- gitPath string
- sshPath string
- pubKeyPath string
- privateKeyPath string
- gitWrapPath string
- logFilePath string
- )
- func TestMain(m *testing.M) {
- logFilePath = filepath.Join(configDir, "sftpgo_sftpd_test.log")
- loginBannerFileName := "login_banner"
- loginBannerFile := filepath.Join(configDir, loginBannerFileName)
- ioutil.WriteFile(loginBannerFile, []byte("simple login banner\n"), 0777)
- logger.InitLogger(logFilePath, 5, 1, 28, false, zerolog.DebugLevel)
- config.LoadConfig(configDir, "")
- providerConf := config.GetProviderConf()
- err := dataprovider.Initialize(providerConf, configDir)
- if err != nil {
- logger.Warn(logSender, "", "error initializing data provider: %v", err)
- os.Exit(1)
- }
- dataProvider := dataprovider.GetProvider()
- sftpdConf := config.GetSFTPDConfig()
- httpdConf := config.GetHTTPDConfig()
- sftpdConf.BindPort = 2022
- sftpdConf.KexAlgorithms = []string{"[email protected]", "ecdh-sha2-nistp256",
- "ecdh-sha2-nistp384"}
- sftpdConf.Ciphers = []string{"[email protected]", "[email protected]",
- "aes256-ctr"}
- sftpdConf.MACs = []string{"[email protected]", "hmac-sha2-256"}
- sftpdConf.LoginBannerFile = loginBannerFileName
- // we need to test all supported ssh commands
- sftpdConf.EnabledSSHCommands = []string{"*"}
- // we run the test cases with UploadMode atomic and resume support. The non atomic code path
- // simply does not execute some code so if it works in atomic mode will
- // work in non atomic mode too
- sftpdConf.UploadMode = 2
- homeBasePath = os.TempDir()
- var scriptArgs string
- if runtime.GOOS == "windows" {
- scriptArgs = "%*"
- } else {
- sftpdConf.Actions.ExecuteOn = []string{"download", "upload", "rename", "delete", "ssh_cmd"}
- sftpdConf.Actions.Command = "/usr/bin/true"
- sftpdConf.Actions.HTTPNotificationURL = "http://127.0.0.1:8080/"
- scriptArgs = "$@"
- }
- scpPath, err = exec.LookPath("scp")
- if err != nil {
- logger.Warn(logSender, "", "unable to get scp command. SCP tests will be skipped, err: %v", err)
- logger.WarnToConsole("unable to get scp command. SCP tests will be skipped, err: %v", err)
- scpPath = ""
- }
- gitPath, err = exec.LookPath("git")
- if err != nil {
- logger.Warn(logSender, "", "unable to get git command. GIT tests will be skipped, err: %v", err)
- logger.WarnToConsole("unable to get git command. GIT tests will be skipped, err: %v", err)
- gitPath = ""
- }
- sshPath, err = exec.LookPath("ssh")
- if err != nil {
- logger.Warn(logSender, "", "unable to get ssh command. GIT tests will be skipped, err: %v", err)
- logger.WarnToConsole("unable to get ssh command. GIT tests will be skipped, err: %v", err)
- gitPath = ""
- }
- pubKeyPath = filepath.Join(homeBasePath, "ssh_key.pub")
- privateKeyPath = filepath.Join(homeBasePath, "ssh_key")
- gitWrapPath = filepath.Join(homeBasePath, "gitwrap.sh")
- err = ioutil.WriteFile(pubKeyPath, []byte(testPubKey+"\n"), 0600)
- if err != nil {
- logger.WarnToConsole("unable to save public key to file: %v", err)
- }
- err = ioutil.WriteFile(privateKeyPath, []byte(testPrivateKey+"\n"), 0600)
- if err != nil {
- logger.WarnToConsole("unable to save private key to file: %v", err)
- }
- err = ioutil.WriteFile(gitWrapPath, []byte(fmt.Sprintf("%v -i %v -oStrictHostKeyChecking=no %v\n",
- sshPath, privateKeyPath, scriptArgs)), 0755)
- if err != nil {
- logger.WarnToConsole("unable to save gitwrap shell script: %v", err)
- }
- sftpd.SetDataProvider(dataProvider)
- httpd.SetDataProvider(dataProvider)
- go func() {
- logger.Debug(logSender, "", "initializing SFTP server with config %+v", sftpdConf)
- if err := sftpdConf.Initialize(configDir); err != nil {
- logger.Error(logSender, "", "could not start SFTP server: %v", err)
- }
- }()
- go func() {
- if err := httpdConf.Initialize(configDir); err != nil {
- logger.Error(logSender, "", "could not start HTTP server: %v", err)
- }
- }()
- waitTCPListening(fmt.Sprintf("%s:%d", sftpdConf.BindAddress, sftpdConf.BindPort))
- waitTCPListening(fmt.Sprintf("%s:%d", httpdConf.BindAddress, httpdConf.BindPort))
- exitCode := m.Run()
- os.Remove(logFilePath)
- os.Remove(loginBannerFile)
- os.Remove(pubKeyPath)
- os.Remove(privateKeyPath)
- os.Remove(gitWrapPath)
- os.Exit(exitCode)
- }
- func TestInitialization(t *testing.T) {
- config.LoadConfig(configDir, "")
- sftpdConf := config.GetSFTPDConfig()
- sftpdConf.Umask = "invalid umask"
- sftpdConf.BindPort = 2022
- sftpdConf.LoginBannerFile = "invalid_file"
- sftpdConf.IsSCPEnabled = true
- sftpdConf.EnabledSSHCommands = append(sftpdConf.EnabledSSHCommands, "ls")
- err := sftpdConf.Initialize(configDir)
- if err == nil {
- t.Errorf("Inizialize must fail, a SFTP server should be already running")
- }
- }
- func TestBasicSFTPHandling(t *testing.T) {
- usePubKey := false
- u := getTestUser(usePubKey)
- u.QuotaSize = 6553600
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- testFileSize := int64(65535)
- expectedQuotaSize := user.UsedQuotaSize + testFileSize
- expectedQuotaFiles := user.UsedQuotaFiles + 1
- createTestFile(testFilePath, testFileSize)
- err = sftpUploadFile(testFilePath, path.Join("/missing_dir", testFileName), testFileSize, client)
- if err == nil {
- t.Errorf("upload a file to a missing dir must fail")
- }
- err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
- if err != nil {
- t.Errorf("file upload error: %v", err)
- }
- localDownloadPath := filepath.Join(homeBasePath, "test_download.dat")
- err = sftpDownloadFile(testFileName, localDownloadPath, testFileSize, client)
- if err != nil {
- t.Errorf("file download error: %v", err)
- }
- user, _, err = httpd.GetUserByID(user.ID, http.StatusOK)
- if err != nil {
- t.Errorf("error getting user: %v", err)
- }
- if expectedQuotaFiles != user.UsedQuotaFiles {
- t.Errorf("quota files does not match, expected: %v, actual: %v", expectedQuotaFiles, user.UsedQuotaFiles)
- }
- if expectedQuotaSize != user.UsedQuotaSize {
- t.Errorf("quota size does not match, expected: %v, actual: %v", expectedQuotaSize, user.UsedQuotaSize)
- }
- err = client.Remove(testFileName)
- if err != nil {
- t.Errorf("error removing uploaded file: %v", err)
- }
- _, err = client.Lstat(testFileName)
- if err == nil {
- t.Errorf("stat for deleted file must not succeed")
- }
- user, _, err = httpd.GetUserByID(user.ID, http.StatusOK)
- if err != nil {
- t.Errorf("error getting user: %v", err)
- }
- if (expectedQuotaFiles - 1) != user.UsedQuotaFiles {
- t.Errorf("quota files does not match after delete, expected: %v, actual: %v", expectedQuotaFiles-1, user.UsedQuotaFiles)
- }
- if (expectedQuotaSize - testFileSize) != user.UsedQuotaSize {
- t.Errorf("quota size does not match, expected: %v, actual: %v", expectedQuotaSize-testFileSize, user.UsedQuotaSize)
- }
- os.Remove(testFilePath)
- os.Remove(localDownloadPath)
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- }
- func TestUploadResume(t *testing.T) {
- usePubKey := false
- u := getTestUser(usePubKey)
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- testFileSize := int64(65535)
- appendDataSize := int64(65535)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
- if err != nil {
- t.Errorf("file upload error: %v", err)
- }
- err = appendToTestFile(testFilePath, appendDataSize)
- if err != nil {
- t.Errorf("unable to append to test file: %v", err)
- }
- err = sftpUploadResumeFile(testFilePath, testFileName, testFileSize+appendDataSize, false, client)
- if err != nil {
- t.Errorf("file upload resume error: %v", err)
- }
- localDownloadPath := filepath.Join(homeBasePath, "test_download.dat")
- err = sftpDownloadFile(testFileName, localDownloadPath, testFileSize+appendDataSize, client)
- if err != nil {
- t.Errorf("file download error: %v", err)
- }
- initialHash, err := computeHashForFile(sha256.New(), testFilePath)
- if err != nil {
- t.Errorf("error computing file hash: %v", err)
- }
- donwloadedFileHash, err := computeHashForFile(sha256.New(), localDownloadPath)
- if err != nil {
- t.Errorf("error computing downloaded file hash: %v", err)
- }
- if donwloadedFileHash != initialHash {
- t.Errorf("resume failed: file hash does not match")
- }
- err = sftpUploadResumeFile(testFilePath, testFileName, testFileSize+appendDataSize, true, client)
- if err == nil {
- t.Errorf("file upload resume with invalid offset must fail")
- }
- os.Remove(testFilePath)
- os.Remove(localDownloadPath)
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- }
- func TestDirCommands(t *testing.T) {
- usePubKey := false
- user, _, err := httpd.AddUser(getTestUser(usePubKey), http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- // remove the home dir to test auto creation
- os.RemoveAll(user.HomeDir)
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- err = client.Mkdir("test1")
- if err != nil {
- t.Errorf("error mkdir: %v", err)
- }
- err = client.Rename("test1", "test")
- if err != nil {
- t.Errorf("error rename: %v", err)
- }
- _, err = client.Lstat("/test1")
- if err == nil {
- t.Errorf("stat for renamed dir must not succeed")
- }
- err = client.PosixRename("test", "test1")
- if err != nil {
- t.Errorf("error posix rename: %v", err)
- }
- err = client.Remove("test1")
- if err != nil {
- t.Errorf("error rmdir: %v", err)
- }
- err = client.Mkdir("/test/test1")
- if err == nil {
- t.Errorf("recursive mkdir must fail")
- }
- client.Mkdir("/test")
- err = client.Mkdir("/test/test1")
- if err != nil {
- t.Errorf("mkdir dir error: %v", err)
- }
- _, err = client.ReadDir("/this/dir/does/not/exist")
- if err == nil {
- t.Errorf("reading a missing dir must fail")
- }
- err = client.RemoveDirectory("/test/test1")
- if err != nil {
- t.Errorf("remove dir error: %v", err)
- }
- err = client.RemoveDirectory("/test")
- if err != nil {
- t.Errorf("remove dir error: %v", err)
- }
- _, err = client.Lstat("/test")
- if err == nil {
- t.Errorf("stat for deleted dir must not succeed")
- }
- err = client.RemoveDirectory("/test")
- if err == nil {
- t.Errorf("remove missing path must fail")
- }
- }
- httpd.RemoveUser(user, http.StatusOK)
- os.RemoveAll(user.GetHomeDir())
- }
- func TestRemove(t *testing.T) {
- usePubKey := true
- user, _, err := httpd.AddUser(getTestUser(usePubKey), http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- err = client.Mkdir("test")
- if err != nil {
- t.Errorf("error mkdir: %v", err)
- }
- err = client.Mkdir("/test/test1")
- if err != nil {
- t.Errorf("error mkdir subdir: %v", err)
- }
- testFileName := "/test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- testFileSize := int64(65535)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- err = sftpUploadFile(testFilePath, path.Join("/test", testFileName), testFileSize, client)
- if err != nil {
- t.Errorf("file upload error: %v", err)
- }
- err = client.Remove("/test")
- if err == nil {
- t.Errorf("remove non empty dir must fail")
- }
- err = client.RemoveDirectory(path.Join("/test", testFileName))
- if err == nil {
- t.Errorf("remove a file with rmdir must fail")
- }
- err = client.Remove(path.Join("/test", testFileName))
- if err != nil {
- t.Errorf("remove file error: %v", err)
- }
- err = client.Remove(path.Join("/test", testFileName))
- if err == nil {
- t.Errorf("remove missing file must fail")
- }
- err = client.Remove("/test/test1")
- if err != nil {
- t.Errorf("remove dir error: %v", err)
- }
- err = client.Remove("/test")
- if err != nil {
- t.Errorf("remove dir error: %v", err)
- }
- os.Remove(testFilePath)
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- }
- func TestLink(t *testing.T) {
- usePubKey := false
- user, _, err := httpd.AddUser(getTestUser(usePubKey), http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- testFileSize := int64(65535)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
- if err != nil {
- t.Errorf("file upload error: %v", err)
- }
- err = client.Symlink(testFileName, testFileName+".link")
- if err != nil {
- t.Errorf("error creating symlink: %v", err)
- }
- _, err = client.ReadLink(testFileName + ".link")
- if err == nil {
- t.Errorf("readlink is currently not implemented so must fail")
- }
- err = client.Symlink(testFileName, testFileName+".link")
- if err == nil {
- t.Errorf("creating a symlink to an existing one must fail")
- }
- err = client.Link(testFileName, testFileName+".hlink")
- if err == nil {
- t.Errorf("hard link is not supported and must fail")
- }
- err = client.Remove(testFileName + ".link")
- if err != nil {
- t.Errorf("error removing symlink: %v", err)
- }
- err = client.Remove(testFileName)
- if err != nil {
- t.Errorf("error removing uploaded file: %v", err)
- }
- os.Remove(testFilePath)
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- }
- func TestStat(t *testing.T) {
- usePubKey := false
- user, _, err := httpd.AddUser(getTestUser(usePubKey), http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- testFileSize := int64(65535)
- createTestFile(testFilePath, testFileSize)
- err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
- if err != nil {
- t.Errorf("file upload error: %v", err)
- }
- _, err := client.Lstat(testFileName)
- if err != nil {
- t.Errorf("stat error: %v", err)
- }
- // mode 0666 and 0444 works on Windows too
- newPerm := os.FileMode(0666)
- err = client.Chmod(testFileName, newPerm)
- if err != nil {
- t.Errorf("chmod error: %v", err)
- }
- newFi, err := client.Lstat(testFileName)
- if err != nil {
- t.Errorf("stat error: %v", err)
- }
- if newPerm != newFi.Mode().Perm() {
- t.Errorf("chmod failed expected: %v, actual: %v", newPerm, newFi.Mode().Perm())
- }
- newPerm = os.FileMode(0444)
- err = client.Chmod(testFileName, newPerm)
- if err != nil {
- t.Errorf("chmod error: %v", err)
- }
- newFi, err = client.Lstat(testFileName)
- if err != nil {
- t.Errorf("stat error: %v", err)
- }
- if newPerm != newFi.Mode().Perm() {
- t.Errorf("chmod failed expected: %v, actual: %v", newPerm, newFi.Mode().Perm())
- }
- _, err = client.ReadLink(testFileName)
- if err == nil {
- t.Errorf("readlink is not supported and must fail")
- }
- err = client.Truncate(testFileName, 0)
- if err != nil {
- t.Errorf("truncate must be silently ignored: %v", err)
- }
- os.Remove(testFilePath)
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- }
- func TestStatChownChmod(t *testing.T) {
- if runtime.GOOS == "windows" {
- t.Skip("chown is not supported on Windows, chmod is partially supported")
- }
- usePubKey := true
- user, _, err := httpd.AddUser(getTestUser(usePubKey), http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- testFileSize := int64(65535)
- createTestFile(testFilePath, testFileSize)
- err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
- if err != nil {
- t.Errorf("file upload error: %v", err)
- }
- err = client.Chown(testFileName, os.Getuid(), os.Getgid())
- if err != nil {
- t.Errorf("chown error: %v", err)
- }
- newPerm := os.FileMode(0600)
- err = client.Chmod(testFileName, newPerm)
- if err != nil {
- t.Errorf("chmod error: %v", err)
- }
- newFi, err := client.Lstat(testFileName)
- if err != nil {
- t.Errorf("stat error: %v", err)
- }
- if newPerm != newFi.Mode().Perm() {
- t.Errorf("chown failed expected: %v, actual: %v", newPerm, newFi.Mode().Perm())
- }
- err = client.Remove(testFileName)
- if err != nil {
- t.Errorf("error removing uploaded file: %v", err)
- }
- // l'errore viene riconvertito da sftp.ErrSSHFxNoSuchFile in os.ErrNotExist
- err = client.Chmod(testFileName, newPerm)
- if err != os.ErrNotExist {
- t.Errorf("unexpected chmod error: %v expected: %v", err, os.ErrNotExist)
- }
- err = client.Chown(testFileName, os.Getuid(), os.Getgid())
- if err != os.ErrNotExist {
- t.Errorf("unexpected chown error: %v expected: %v", err, os.ErrNotExist)
- }
- os.Remove(testFilePath)
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- }
- func TestChtimes(t *testing.T) {
- usePubKey := false
- user, _, err := httpd.AddUser(getTestUser(usePubKey), http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- testFileSize := int64(65535)
- testDir := "test"
- createTestFile(testFilePath, testFileSize)
- err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
- if err != nil {
- t.Errorf("file upload error: %v", err)
- }
- acmodTime := time.Now()
- err = client.Chtimes(testFileName, acmodTime, acmodTime)
- if err != nil {
- t.Errorf("error changing file times")
- }
- newFi, err := client.Lstat(testFileName)
- if err != nil {
- t.Errorf("file stat error: %v", err)
- }
- diff := math.Abs(newFi.ModTime().Sub(acmodTime).Seconds())
- if diff > 1 {
- t.Errorf("diff between wanted and real modification time too big: %v", diff)
- }
- err = client.Chtimes("invalidFile", acmodTime, acmodTime)
- if !os.IsNotExist(err) {
- t.Errorf("unexpected error: %v", err)
- }
- err = client.Mkdir(testDir)
- if err != nil {
- t.Errorf("unable to create dir: %v", err)
- }
- err = client.Chtimes(testDir, acmodTime, acmodTime)
- if err != nil {
- t.Errorf("error changing dir times")
- }
- newFi, err = client.Lstat(testDir)
- if err != nil {
- t.Errorf("dir stat error: %v", err)
- }
- diff = math.Abs(newFi.ModTime().Sub(acmodTime).Seconds())
- if diff > 1 {
- t.Errorf("diff between wanted and real modification time too big: %v", diff)
- }
- os.Remove(testFilePath)
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- }
- // basic tests to verify virtual chroot, should be improved to cover more cases ...
- func TestEscapeHomeDir(t *testing.T) {
- usePubKey := true
- user, _, err := httpd.AddUser(getTestUser(usePubKey), http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- _, err := client.Getwd()
- if err != nil {
- t.Errorf("unable to get working dir: %v", err)
- }
- testDir := "testDir"
- linkPath := filepath.Join(homeBasePath, defaultUsername, testDir)
- err = os.Symlink(homeBasePath, linkPath)
- if err != nil {
- t.Errorf("error making local symlink: %v", err)
- }
- _, err = client.ReadDir(testDir)
- if err == nil {
- t.Errorf("reading a symbolic link outside home dir should not succeeded")
- }
- os.Remove(linkPath)
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- testFileSize := int64(65535)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- remoteDestPath := path.Join("..", "..", testFileName)
- err = sftpUploadFile(testFilePath, remoteDestPath, testFileSize, client)
- if err != nil {
- t.Errorf("file upload error: %v", err)
- }
- _, err = client.Lstat(testFileName)
- if err != nil {
- t.Errorf("file stat error: %v the file was created outside the user dir!", err)
- }
- err = client.Remove(testFileName)
- if err != nil {
- t.Errorf("error removing uploaded file: %v", err)
- }
- linkPath = filepath.Join(homeBasePath, defaultUsername, testFileName)
- err = os.Symlink(homeBasePath, linkPath)
- if err != nil {
- t.Errorf("error making local symlink: %v", err)
- }
- err = sftpDownloadFile(testFileName, testFilePath, 0, client)
- if err == nil {
- t.Errorf("download file outside home dir must fail")
- }
- err = sftpUploadFile(testFilePath, remoteDestPath, testFileSize, client)
- if err == nil {
- t.Errorf("overwrite a file outside home dir must fail")
- }
- err = client.Chmod(remoteDestPath, 0644)
- if err == nil {
- t.Errorf("setstat on a file outside home dir must fail")
- }
- os.Remove(linkPath)
- os.Remove(testFilePath)
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- }
- func TestHomeSpecialChars(t *testing.T) {
- usePubKey := true
- u := getTestUser(usePubKey)
- u.HomeDir = filepath.Join(homeBasePath, "abc açà#&%lk")
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- _, err := client.Getwd()
- if err != nil {
- t.Errorf("unable to get working dir: %v", err)
- }
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- testFileSize := int64(65535)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
- if err != nil {
- t.Errorf("file upload error: %v", err)
- }
- files, err := client.ReadDir(".")
- if err != nil {
- t.Errorf("unable to read remote dir: %v", err)
- }
- if len(files) < 1 {
- t.Errorf("expected at least 1 file in this dir")
- }
- err = client.Remove(testFileName)
- if err != nil {
- t.Errorf("error removing uploaded file: %v", err)
- }
- os.Remove(testFilePath)
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- }
- func TestLogin(t *testing.T) {
- u := getTestUser(false)
- u.PublicKeys = []string{testPubKey}
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, false)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- _, err := client.Getwd()
- if err != nil {
- t.Errorf("sftp client with valid password must work")
- }
- user, _, err = httpd.GetUserByID(user.ID, http.StatusOK)
- if err != nil {
- t.Errorf("error getting user: %v", err)
- }
- if user.LastLogin <= 0 {
- t.Errorf("last login must be updated after a successful login: %v", user.LastLogin)
- }
- }
- client, err = getSftpClient(user, true)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- _, err := client.Getwd()
- if err != nil {
- t.Errorf("sftp client with valid public key must work")
- }
- }
- user.Password = "invalid password"
- client, err = getSftpClient(user, false)
- if err == nil {
- t.Errorf("login with invalid password must fail")
- defer client.Close()
- }
- // testPubKey1 is not authorized
- user.PublicKeys = []string{testPubKey1}
- user.Password = ""
- _, _, err = httpd.UpdateUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to update user: %v", err)
- }
- client, err = getSftpClient(user, true)
- if err == nil {
- t.Errorf("login with invalid public key must fail")
- defer client.Close()
- }
- // login a user with multiple public keys, only the second one is valid
- user.PublicKeys = []string{testPubKey1, testPubKey}
- user.Password = ""
- _, _, err = httpd.UpdateUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to update user: %v", err)
- }
- client, err = getSftpClient(user, true)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- _, err := client.Getwd()
- if err != nil {
- t.Errorf("sftp client with multiple public key must work if at least one public key is valid")
- }
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- }
- func TestLoginUserStatus(t *testing.T) {
- usePubKey := true
- user, _, err := httpd.AddUser(getTestUser(usePubKey), http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- _, err := client.Getwd()
- if err != nil {
- t.Errorf("sftp client with valid credentials must work")
- }
- user, _, err = httpd.GetUserByID(user.ID, http.StatusOK)
- if err != nil {
- t.Errorf("error getting user: %v", err)
- }
- if user.LastLogin <= 0 {
- t.Errorf("last login must be updated after a successful login: %v", user.LastLogin)
- }
- }
- user.Status = 0
- user, _, err = httpd.UpdateUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to update user: %v", err)
- }
- client, err = getSftpClient(user, usePubKey)
- if err == nil {
- t.Errorf("login for a disabled user must fail")
- defer client.Close()
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- }
- func TestLoginUserExpiration(t *testing.T) {
- usePubKey := true
- user, _, err := httpd.AddUser(getTestUser(usePubKey), http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- _, err := client.Getwd()
- if err != nil {
- t.Errorf("sftp client with valid credentials must work")
- }
- user, _, err = httpd.GetUserByID(user.ID, http.StatusOK)
- if err != nil {
- t.Errorf("error getting user: %v", err)
- }
- if user.LastLogin <= 0 {
- t.Errorf("last login must be updated after a successful login: %v", user.LastLogin)
- }
- }
- user.ExpirationDate = utils.GetTimeAsMsSinceEpoch(time.Now()) - 120000
- user, _, err = httpd.UpdateUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to update user: %v", err)
- }
- client, err = getSftpClient(user, usePubKey)
- if err == nil {
- t.Errorf("login for an expired user must fail")
- defer client.Close()
- }
- user.ExpirationDate = utils.GetTimeAsMsSinceEpoch(time.Now()) + 120000
- _, _, err = httpd.UpdateUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to update user: %v", err)
- }
- client, err = getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("login for a non expired user must succeed: %v", err)
- } else {
- defer client.Close()
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- }
- func TestLoginWithIPFilters(t *testing.T) {
- usePubKey := true
- u := getTestUser(usePubKey)
- u.Filters.DeniedIP = []string{"192.167.0.0/24", "172.18.0.0/16"}
- u.Filters.AllowedIP = []string{}
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- _, err := client.Getwd()
- if err != nil {
- t.Errorf("sftp client with valid credentials must work")
- }
- user, _, err = httpd.GetUserByID(user.ID, http.StatusOK)
- if err != nil {
- t.Errorf("error getting user: %v", err)
- }
- if user.LastLogin <= 0 {
- t.Errorf("last login must be updated after a successful login: %v", user.LastLogin)
- }
- }
- user.Filters.AllowedIP = []string{"127.0.0.0/8"}
- _, _, err = httpd.UpdateUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to update user: %v", err)
- }
- client, err = getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("login from an allowed IP must succeed: %v", err)
- } else {
- defer client.Close()
- }
- user.Filters.AllowedIP = []string{"172.19.0.0/16"}
- _, _, err = httpd.UpdateUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to update user: %v", err)
- }
- client, err = getSftpClient(user, usePubKey)
- if err == nil {
- t.Errorf("login from an not allowed IP must fail")
- client.Close()
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- }
- func TestLoginAfterUserUpdateEmptyPwd(t *testing.T) {
- usePubKey := false
- user, _, err := httpd.AddUser(getTestUser(usePubKey), http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- user.Password = ""
- user.PublicKeys = []string{}
- // password and public key should remain unchanged
- _, _, err = httpd.UpdateUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to update user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- _, err := client.Getwd()
- if err != nil {
- t.Errorf("unable to get working dir: %v", err)
- }
- _, err = client.ReadDir(".")
- if err != nil {
- t.Errorf("unable to read remote dir: %v", err)
- }
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- }
- func TestLoginAfterUserUpdateEmptyPubKey(t *testing.T) {
- usePubKey := true
- user, _, err := httpd.AddUser(getTestUser(usePubKey), http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- user.Password = ""
- user.PublicKeys = []string{}
- // password and public key should remain unchanged
- _, _, err = httpd.UpdateUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to update user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- _, err := client.Getwd()
- if err != nil {
- t.Errorf("unable to get working dir: %v", err)
- }
- _, err = client.ReadDir(".")
- if err != nil {
- t.Errorf("unable to read remote dir: %v", err)
- }
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- }
- func TestMaxSessions(t *testing.T) {
- usePubKey := false
- u := getTestUser(usePubKey)
- u.MaxSessions = 1
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- _, err := client.Getwd()
- if err != nil {
- t.Errorf("unable to get working dir: %v", err)
- }
- _, err = client.ReadDir(".")
- if err != nil {
- t.Errorf("unable to read remote dir: %v", err)
- }
- _, err = getSftpClient(user, usePubKey)
- if err == nil {
- t.Errorf("max sessions exceeded, new login should not succeed")
- }
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- }
- func TestQuotaFileReplace(t *testing.T) {
- usePubKey := false
- u := getTestUser(usePubKey)
- u.QuotaFiles = 1000
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- testFileSize := int64(65535)
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- expectedQuotaSize := user.UsedQuotaSize + testFileSize
- expectedQuotaFiles := user.UsedQuotaFiles + 1
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
- if err != nil {
- t.Errorf("file upload error: %v", err)
- }
- user, _, err = httpd.GetUserByID(user.ID, http.StatusOK)
- if err != nil {
- t.Errorf("error getting user: %v", err)
- }
- // now replace the same file, the quota must not change
- err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
- if err != nil {
- t.Errorf("file upload error: %v", err)
- }
- user, _, err = httpd.GetUserByID(user.ID, http.StatusOK)
- if err != nil {
- t.Errorf("error getting user: %v", err)
- }
- if expectedQuotaFiles != user.UsedQuotaFiles {
- t.Errorf("quota files does not match, expected: %v, actual: %v", expectedQuotaFiles, user.UsedQuotaFiles)
- }
- if expectedQuotaSize != user.UsedQuotaSize {
- t.Errorf("quota size does not match, expected: %v, actual: %v", expectedQuotaSize, user.UsedQuotaSize)
- }
- }
- // now set a quota size restriction and upload the same file, upload should fail for space limit exceeded
- user.QuotaSize = testFileSize - 1
- user, _, err = httpd.UpdateUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("error updating user: %v", err)
- }
- client, err = getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
- if err == nil {
- t.Errorf("quota size exceeded, file upload must fail")
- }
- err = client.Remove(testFileName)
- if err != nil {
- t.Errorf("error removing uploaded file: %v", err)
- }
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.Remove(testFilePath)
- os.RemoveAll(user.GetHomeDir())
- }
- func TestQuotaScan(t *testing.T) {
- usePubKey := false
- user, _, err := httpd.AddUser(getTestUser(usePubKey), http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- testFileSize := int64(65535)
- expectedQuotaSize := user.UsedQuotaSize + testFileSize
- expectedQuotaFiles := user.UsedQuotaFiles + 1
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
- if err != nil {
- t.Errorf("file upload error: %v", err)
- }
- os.Remove(testFilePath)
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- // create user with the same home dir, so there is at least an untracked file
- user, _, err = httpd.AddUser(getTestUser(usePubKey), http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- _, err = httpd.StartQuotaScan(user, http.StatusCreated)
- if err != nil {
- t.Errorf("error starting quota scan: %v", err)
- }
- err = waitQuotaScans()
- if err != nil {
- t.Errorf("error waiting for active quota scans: %v", err)
- }
- user, _, err = httpd.GetUserByID(user.ID, http.StatusOK)
- if err != nil {
- t.Errorf("error getting user: %v", err)
- }
- if expectedQuotaFiles != user.UsedQuotaFiles {
- t.Errorf("quota files does not match after scan, expected: %v, actual: %v", expectedQuotaFiles, user.UsedQuotaFiles)
- }
- if expectedQuotaSize != user.UsedQuotaSize {
- t.Errorf("quota size does not match after scan, expected: %v, actual: %v", expectedQuotaSize, user.UsedQuotaSize)
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- }
- func TestMultipleQuotaScans(t *testing.T) {
- if !sftpd.AddQuotaScan(defaultUsername) {
- t.Errorf("add quota failed")
- }
- if sftpd.AddQuotaScan(defaultUsername) {
- t.Errorf("add quota must fail if another scan is already active")
- }
- sftpd.RemoveQuotaScan(defaultUsername)
- activeScans := sftpd.GetQuotaScans()
- if len(activeScans) > 0 {
- t.Errorf("no quota scan must be active: %v", len(activeScans))
- }
- }
- func TestQuotaSize(t *testing.T) {
- usePubKey := false
- testFileSize := int64(65535)
- u := getTestUser(usePubKey)
- u.QuotaFiles = 1
- u.QuotaSize = testFileSize - 1
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- err = sftpUploadFile(testFilePath, testFileName+".quota", testFileSize, client)
- if err != nil {
- t.Errorf("file upload error: %v", err)
- }
- err = sftpUploadFile(testFilePath, testFileName+".quota.1", testFileSize, client)
- if err == nil {
- t.Errorf("user is over quota file upload must fail")
- }
- err = client.Remove(testFileName + ".quota")
- if err != nil {
- t.Errorf("error removing uploaded file: %v", err)
- }
- os.Remove(testFilePath)
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- }
- func TestBandwidthAndConnections(t *testing.T) {
- usePubKey := false
- testFileSize := int64(131072)
- u := getTestUser(usePubKey)
- u.UploadBandwidth = 30
- u.DownloadBandwidth = 25
- wantedUploadElapsed := 1000 * (testFileSize / 1000) / u.UploadBandwidth
- wantedDownloadElapsed := 1000 * (testFileSize / 1000) / u.DownloadBandwidth
- // 100 ms tolerance
- wantedUploadElapsed -= 100
- wantedDownloadElapsed -= 100
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- startTime := time.Now()
- err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
- if err != nil {
- t.Errorf("file upload error: %v", err)
- }
- elapsed := time.Since(startTime).Nanoseconds() / 1000000
- if elapsed < (wantedUploadElapsed) {
- t.Errorf("upload bandwidth throttling not respected, elapsed: %v, wanted: %v", elapsed, wantedUploadElapsed)
- }
- startTime = time.Now()
- localDownloadPath := filepath.Join(homeBasePath, "test_download.dat")
- c := sftpDownloadNonBlocking(testFileName, localDownloadPath, testFileSize, client)
- waitForActiveTransfer()
- // wait some additional arbitrary time to wait for transfer activity to happen
- // it is need to reach all the code in CheckIdleConnections
- time.Sleep(100 * time.Millisecond)
- sftpd.CheckIdleConnections()
- err = <-c
- if err != nil {
- t.Errorf("file download error: %v", err)
- }
- elapsed = time.Since(startTime).Nanoseconds() / 1000000
- if elapsed < (wantedDownloadElapsed) {
- t.Errorf("download bandwidth throttling not respected, elapsed: %v, wanted: %v", elapsed, wantedDownloadElapsed)
- }
- // test disconnection
- c = sftpUploadNonBlocking(testFilePath, testFileName+"_partial", testFileSize, client)
- waitForActiveTransfer()
- time.Sleep(100 * time.Millisecond)
- sftpd.CheckIdleConnections()
- stats := sftpd.GetConnectionsStats()
- for _, stat := range stats {
- sftpd.CloseActiveConnection(stat.ConnectionID)
- }
- err = <-c
- if err == nil {
- t.Errorf("connection closed upload must fail")
- }
- os.Remove(testFilePath)
- os.Remove(localDownloadPath)
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- }
- func TestMissingFile(t *testing.T) {
- usePubKey := false
- u := getTestUser(usePubKey)
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- localDownloadPath := filepath.Join(homeBasePath, "test_download.dat")
- err = sftpDownloadFile("missing_file", localDownloadPath, 0, client)
- if err == nil {
- t.Errorf("download missing file must fail")
- }
- os.Remove(localDownloadPath)
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- }
- func TestOpenError(t *testing.T) {
- if runtime.GOOS == "windows" {
- t.Skip("this test is not available on Windows")
- }
- usePubKey := false
- u := getTestUser(usePubKey)
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- os.Chmod(user.GetHomeDir(), 0001)
- _, err = client.ReadDir(".")
- if err == nil {
- t.Errorf("read dir must fail if we have no filesystem read permissions")
- }
- os.Chmod(user.GetHomeDir(), 0755)
- testFileSize := int64(65535)
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(user.GetHomeDir(), testFileName)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- _, err = client.Stat(testFileName)
- if err != nil {
- t.Errorf("file stat error: %v", err)
- }
- localDownloadPath := filepath.Join(homeBasePath, "test_download.dat")
- err = sftpDownloadFile(testFileName, localDownloadPath, testFileSize, client)
- if err != nil {
- t.Errorf("file download error: %v", err)
- }
- os.Chmod(testFilePath, 0001)
- err = sftpDownloadFile(testFileName, localDownloadPath, testFileSize, client)
- if err == nil {
- t.Errorf("file download must fail if we have no filesystem read permissions")
- }
- err = sftpUploadFile(localDownloadPath, testFileName, testFileSize, client)
- if err == nil {
- t.Errorf("upload must fail if we have no filesystem write permissions")
- }
- err = client.Mkdir("test")
- if err != nil {
- t.Errorf("error making dir: %v", err)
- }
- os.Chmod(user.GetHomeDir(), 0000)
- _, err = client.Lstat(testFileName)
- if err == nil {
- t.Errorf("file stat must fail if we have no filesystem read permissions")
- }
- os.Chmod(user.GetHomeDir(), 0755)
- os.Chmod(filepath.Join(user.GetHomeDir(), "test"), 0000)
- err = client.Rename(testFileName, path.Join("test", testFileName))
- if err == nil || !strings.Contains(err.Error(), sftp.ErrSSHFxPermissionDenied.Error()) {
- t.Errorf("unexpected error: %v expected: %v", err, sftp.ErrSSHFxPermissionDenied)
- }
- os.Chmod(filepath.Join(user.GetHomeDir(), "test"), 0755)
- os.Remove(testFilePath)
- os.Remove(localDownloadPath)
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- }
- func TestOverwriteDirWithFile(t *testing.T) {
- usePubKey := false
- u := getTestUser(usePubKey)
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- testFileSize := int64(65535)
- testFileName := "test_file.dat"
- testDirName := "test_dir"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- err = client.Mkdir(testDirName)
- if err != nil {
- t.Errorf("mkdir error: %v", err)
- }
- err = sftpUploadFile(testFilePath, testDirName, testFileSize, client)
- if err == nil {
- t.Errorf("copying a file over an existing dir must fail")
- }
- err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
- if err != nil {
- t.Errorf("file upload error: %v", err)
- }
- err = client.Rename(testFileName, testDirName)
- if err == nil {
- t.Errorf("rename a file over an existing dir must fail")
- }
- err = client.RemoveDirectory(testDirName)
- if err != nil {
- t.Errorf("dir remove error: %v", err)
- }
- err = client.Remove(testFileName)
- if err != nil {
- t.Errorf("error removing uploaded file: %v", err)
- }
- os.Remove(testFilePath)
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- }
- func TestPasswordsHashPbkdf2Sha1(t *testing.T) {
- pbkdf2Pwd := "$pbkdf2-sha1$150000$DveVjgYUD05R$X6ydQZdyMeOvpgND2nqGR/0GGic="
- pbkdf2ClearPwd := "password"
- usePubKey := false
- u := getTestUser(usePubKey)
- u.Password = pbkdf2Pwd
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- user.Password = pbkdf2ClearPwd
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to login with pkkdf2 sha1 password: %v", err)
- } else {
- defer client.Close()
- _, err = client.Getwd()
- if err != nil {
- t.Errorf("unable to get working dir with pkkdf2 sha1 password: %v", err)
- }
- }
- user.Password = pbkdf2Pwd
- _, err = getSftpClient(user, usePubKey)
- if err == nil {
- t.Errorf("login with wrong password must fail")
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- }
- func TestPasswordsHashPbkdf2Sha256(t *testing.T) {
- pbkdf2Pwd := "$pbkdf2-sha256$150000$E86a9YMX3zC7$R5J62hsSq+pYw00hLLPKBbcGXmq7fj5+/M0IFoYtZbo="
- pbkdf2ClearPwd := "password"
- usePubKey := false
- u := getTestUser(usePubKey)
- u.Password = pbkdf2Pwd
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- user.Password = pbkdf2ClearPwd
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to login with pkkdf2 sha1 password: %v", err)
- } else {
- defer client.Close()
- _, err = client.Getwd()
- if err != nil {
- t.Errorf("unable to get working dir with pkkdf2 sha1 password: %v", err)
- }
- }
- user.Password = pbkdf2Pwd
- _, err = getSftpClient(user, usePubKey)
- if err == nil {
- t.Errorf("login with wrong password must fail")
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- }
- func TestPasswordsHashPbkdf2Sha512(t *testing.T) {
- pbkdf2Pwd := "$pbkdf2-sha512$150000$dsu7T5R3IaVQ$1hFXPO1ntRBcoWkSLKw+s4sAP09Xtu4Ya7CyxFq64jM9zdUg8eRJVr3NcR2vQgb0W9HHvZaILHsL4Q/Vr6arCg=="
- pbkdf2ClearPwd := "password"
- usePubKey := false
- u := getTestUser(usePubKey)
- u.Password = pbkdf2Pwd
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- user.Password = pbkdf2ClearPwd
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to login with pkkdf2 sha1 password: %v", err)
- } else {
- defer client.Close()
- _, err = client.Getwd()
- if err != nil {
- t.Errorf("unable to get working dir with pkkdf2 sha1 password: %v", err)
- }
- }
- user.Password = pbkdf2Pwd
- _, err = getSftpClient(user, usePubKey)
- if err == nil {
- t.Errorf("login with wrong password must fail")
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- }
- func TestPasswordsHashBcrypt(t *testing.T) {
- bcryptPwd := "$2a$14$ajq8Q7fbtFRQvXpdCq7Jcuy.Rx1h/L4J60Otx.gyNLbAYctGMJ9tK"
- bcryptClearPwd := "secret"
- usePubKey := false
- u := getTestUser(usePubKey)
- u.Password = bcryptPwd
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- user.Password = bcryptClearPwd
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to login with bcrypt password: %v", err)
- } else {
- defer client.Close()
- _, err = client.Getwd()
- if err != nil {
- t.Errorf("unable to get working dir with bcrypt password: %v", err)
- }
- }
- user.Password = bcryptPwd
- _, err = getSftpClient(user, usePubKey)
- if err == nil {
- t.Errorf("login with wrong password must fail")
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- }
- func TestPasswordsHashSHA512Crypt(t *testing.T) {
- sha512CryptPwd := "$6$459ead56b72e44bc$uog86fUxscjt28BZxqFBE2pp2QD8P/1e98MNF75Z9xJfQvOckZnQ/1YJqiq1XeytPuDieHZvDAMoP7352ELkO1"
- clearPwd := "secret"
- usePubKey := false
- u := getTestUser(usePubKey)
- u.Password = sha512CryptPwd
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- user.Password = clearPwd
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to login with sha512 crypt password: %v", err)
- } else {
- defer client.Close()
- _, err = client.Getwd()
- if err != nil {
- t.Errorf("unable to get working dir with sha512 crypt password: %v", err)
- }
- }
- user.Password = sha512CryptPwd
- _, err = getSftpClient(user, usePubKey)
- if err == nil {
- t.Errorf("login with wrong password must fail")
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- }
- func TestPasswordsHashMD5Crypt(t *testing.T) {
- md5CryptPwd := "$1$b5caebda$VODr/nyhGWgZaY8sJ4x05."
- clearPwd := "password"
- usePubKey := false
- u := getTestUser(usePubKey)
- u.Password = md5CryptPwd
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- user.Password = clearPwd
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to login with md5 crypt password: %v", err)
- } else {
- defer client.Close()
- _, err = client.Getwd()
- if err != nil {
- t.Errorf("unable to get working dir with md5 crypt password: %v", err)
- }
- }
- user.Password = md5CryptPwd
- _, err = getSftpClient(user, usePubKey)
- if err == nil {
- t.Errorf("login with wrong password must fail")
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- }
- func TestPermList(t *testing.T) {
- usePubKey := true
- u := getTestUser(usePubKey)
- u.Permissions["/"] = []string{dataprovider.PermDownload, dataprovider.PermUpload, dataprovider.PermDelete, dataprovider.PermRename,
- dataprovider.PermCreateDirs, dataprovider.PermCreateSymlinks, dataprovider.PermOverwrite, dataprovider.PermChmod,
- dataprovider.PermChown, dataprovider.PermChtimes}
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- _, err = client.ReadDir(".")
- if err == nil {
- t.Errorf("read remote dir without permission should not succeed")
- }
- _, err = client.Stat("test_file")
- if err == nil {
- t.Errorf("stat remote file without permission should not succeed")
- }
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- }
- func TestPermDownload(t *testing.T) {
- usePubKey := true
- u := getTestUser(usePubKey)
- u.Permissions["/"] = []string{dataprovider.PermListItems, dataprovider.PermUpload, dataprovider.PermDelete, dataprovider.PermRename,
- dataprovider.PermCreateDirs, dataprovider.PermCreateSymlinks, dataprovider.PermOverwrite, dataprovider.PermChmod,
- dataprovider.PermChown, dataprovider.PermChtimes}
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- testFileSize := int64(65535)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
- if err != nil {
- t.Errorf("file upload error: %v", err)
- }
- localDownloadPath := filepath.Join(homeBasePath, "test_download.dat")
- err = sftpDownloadFile(testFileName, localDownloadPath, testFileSize, client)
- if err == nil {
- t.Errorf("file download without permission should not succeed")
- }
- err = client.Remove(testFileName)
- if err != nil {
- t.Errorf("error removing uploaded file: %v", err)
- }
- os.Remove(testFilePath)
- os.Remove(localDownloadPath)
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- }
- func TestPermUpload(t *testing.T) {
- usePubKey := false
- u := getTestUser(usePubKey)
- u.Permissions["/"] = []string{dataprovider.PermListItems, dataprovider.PermDownload, dataprovider.PermDelete, dataprovider.PermRename,
- dataprovider.PermCreateDirs, dataprovider.PermCreateSymlinks, dataprovider.PermOverwrite, dataprovider.PermChmod,
- dataprovider.PermChown, dataprovider.PermChtimes}
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- testFileSize := int64(65535)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
- if err == nil {
- t.Errorf("file upload without permission should not succeed")
- }
- os.Remove(testFilePath)
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- }
- func TestPermOverwrite(t *testing.T) {
- usePubKey := false
- u := getTestUser(usePubKey)
- u.Permissions["/"] = []string{dataprovider.PermListItems, dataprovider.PermDownload, dataprovider.PermUpload, dataprovider.PermDelete,
- dataprovider.PermRename, dataprovider.PermCreateDirs, dataprovider.PermCreateSymlinks, dataprovider.PermChmod,
- dataprovider.PermChown, dataprovider.PermChtimes}
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- testFileSize := int64(65535)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
- if err != nil {
- t.Errorf("error uploading file: %v", err)
- }
- err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
- if err == nil {
- t.Errorf("file overwrite without permission should not succeed")
- }
- os.Remove(testFilePath)
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- }
- func TestPermDelete(t *testing.T) {
- usePubKey := false
- u := getTestUser(usePubKey)
- u.Permissions["/"] = []string{dataprovider.PermListItems, dataprovider.PermDownload, dataprovider.PermUpload, dataprovider.PermRename,
- dataprovider.PermCreateDirs, dataprovider.PermCreateSymlinks, dataprovider.PermOverwrite, dataprovider.PermChmod,
- dataprovider.PermChown, dataprovider.PermChtimes}
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- testFileSize := int64(65535)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
- if err != nil {
- t.Errorf("file upload error: %v", err)
- }
- err = client.Remove(testFileName)
- if err == nil {
- t.Errorf("delete without permission should not succeed")
- }
- os.Remove(testFilePath)
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- }
- func TestPermRename(t *testing.T) {
- usePubKey := false
- u := getTestUser(usePubKey)
- u.Permissions["/"] = []string{dataprovider.PermListItems, dataprovider.PermDownload, dataprovider.PermUpload, dataprovider.PermDelete,
- dataprovider.PermCreateDirs, dataprovider.PermCreateSymlinks, dataprovider.PermOverwrite, dataprovider.PermChmod,
- dataprovider.PermChown, dataprovider.PermChtimes}
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- testFileSize := int64(65535)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
- if err != nil {
- t.Errorf("file upload error: %v", err)
- }
- err = client.Rename(testFileName, testFileName+".rename")
- if err == nil {
- t.Errorf("rename without permission should not succeed")
- }
- err = client.Remove(testFileName)
- if err != nil {
- t.Errorf("error removing uploaded file: %v", err)
- }
- os.Remove(testFilePath)
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- }
- func TestPermCreateDirs(t *testing.T) {
- usePubKey := false
- u := getTestUser(usePubKey)
- u.Permissions["/"] = []string{dataprovider.PermListItems, dataprovider.PermDownload, dataprovider.PermUpload, dataprovider.PermDelete,
- dataprovider.PermRename, dataprovider.PermCreateSymlinks, dataprovider.PermOverwrite, dataprovider.PermChmod,
- dataprovider.PermChown, dataprovider.PermChtimes}
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- err = client.Mkdir("testdir")
- if err == nil {
- t.Errorf("mkdir without permission should not succeed")
- }
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- }
- func TestPermSymlink(t *testing.T) {
- usePubKey := false
- u := getTestUser(usePubKey)
- u.Permissions["/"] = []string{dataprovider.PermListItems, dataprovider.PermDownload, dataprovider.PermUpload, dataprovider.PermDelete,
- dataprovider.PermRename, dataprovider.PermCreateDirs, dataprovider.PermOverwrite, dataprovider.PermChmod, dataprovider.PermChown,
- dataprovider.PermChtimes}
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- testFileSize := int64(65535)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
- if err != nil {
- t.Errorf("file upload error: %v", err)
- }
- err = client.Symlink(testFilePath, testFilePath+".symlink")
- if err == nil {
- t.Errorf("symlink without permission should not succeed")
- }
- err = client.Remove(testFileName)
- if err != nil {
- t.Errorf("error removing uploaded file: %v", err)
- }
- os.Remove(testFilePath)
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- }
- func TestPermChmod(t *testing.T) {
- usePubKey := false
- u := getTestUser(usePubKey)
- u.Permissions["/"] = []string{dataprovider.PermListItems, dataprovider.PermDownload, dataprovider.PermUpload, dataprovider.PermDelete,
- dataprovider.PermRename, dataprovider.PermCreateDirs, dataprovider.PermCreateSymlinks, dataprovider.PermOverwrite,
- dataprovider.PermChown, dataprovider.PermChtimes}
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- testFileSize := int64(65535)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
- if err != nil {
- t.Errorf("file upload error: %v", err)
- }
- err = client.Chmod(testFileName, 0666)
- if err == nil {
- t.Errorf("chmod without permission should not succeed")
- }
- err = client.Remove(testFileName)
- if err != nil {
- t.Errorf("error removing uploaded file: %v", err)
- }
- os.Remove(testFilePath)
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- }
- func TestPermChown(t *testing.T) {
- usePubKey := false
- u := getTestUser(usePubKey)
- u.Permissions["/"] = []string{dataprovider.PermListItems, dataprovider.PermDownload, dataprovider.PermUpload, dataprovider.PermDelete,
- dataprovider.PermRename, dataprovider.PermCreateDirs, dataprovider.PermCreateSymlinks, dataprovider.PermOverwrite,
- dataprovider.PermChmod, dataprovider.PermChtimes}
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- testFileSize := int64(65535)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
- if err != nil {
- t.Errorf("file upload error: %v", err)
- }
- err = client.Chown(testFileName, os.Getuid(), os.Getgid())
- if err == nil {
- t.Errorf("chown without permission should not succeed")
- }
- err = client.Remove(testFileName)
- if err != nil {
- t.Errorf("error removing uploaded file: %v", err)
- }
- os.Remove(testFilePath)
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- }
- func TestPermChtimes(t *testing.T) {
- usePubKey := false
- u := getTestUser(usePubKey)
- u.Permissions["/"] = []string{dataprovider.PermListItems, dataprovider.PermDownload, dataprovider.PermUpload, dataprovider.PermDelete,
- dataprovider.PermRename, dataprovider.PermCreateDirs, dataprovider.PermCreateSymlinks, dataprovider.PermOverwrite,
- dataprovider.PermChmod, dataprovider.PermChown}
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- testFileSize := int64(65535)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
- if err != nil {
- t.Errorf("file upload error: %v", err)
- }
- err = client.Chtimes(testFileName, time.Now(), time.Now())
- if err == nil {
- t.Errorf("chtimes without permission should not succeed")
- }
- err = client.Remove(testFileName)
- if err != nil {
- t.Errorf("error removing uploaded file: %v", err)
- }
- os.Remove(testFilePath)
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- }
- func TestSubDirsUploads(t *testing.T) {
- usePubKey := true
- u := getTestUser(usePubKey)
- u.Permissions["/"] = []string{dataprovider.PermAny}
- u.Permissions["/subdir"] = []string{dataprovider.PermChtimes, dataprovider.PermDownload}
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- err = client.Mkdir("subdir")
- if err != nil {
- t.Errorf("unexpected mkdir error: %v", err)
- }
- testFileName := "test_file.dat"
- testFileNameSub := "/subdir/test_file_dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- testFileSize := int64(65535)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
- if err != nil {
- t.Errorf("file upload error: %v", err)
- }
- err = sftpUploadFile(testFilePath, testFileNameSub, testFileSize, client)
- if !strings.Contains(err.Error(), "Permission Denied") {
- t.Errorf("unexpected upload error: %v", err)
- }
- err = client.Symlink(testFileName, testFileNameSub+".link")
- if !strings.Contains(err.Error(), "Permission Denied") {
- t.Errorf("unexpected upload error: %v", err)
- }
- err = client.Symlink(testFileName, testFileName+".link")
- if err != nil {
- t.Errorf("symlink error: %v", err)
- }
- err = client.Rename(testFileName, testFileNameSub+".rename")
- if !strings.Contains(err.Error(), "Permission Denied") {
- t.Errorf("unexpected rename error: %v", err)
- }
- err = client.Rename(testFileName, testFileName+".rename")
- if err != nil {
- t.Errorf("rename error: %v", err)
- }
- err = client.Remove(testFileNameSub)
- if !strings.Contains(err.Error(), "Permission Denied") {
- t.Errorf("unexpected upload error: %v", err)
- }
- err = client.Remove(testFileName + ".rename")
- if err != nil {
- t.Errorf("remove error: %v", err)
- }
- os.Remove(testFilePath)
- }
- httpd.RemoveUser(user, http.StatusOK)
- os.RemoveAll(user.GetHomeDir())
- }
- func TestSubDirsOverwrite(t *testing.T) {
- usePubKey := true
- u := getTestUser(usePubKey)
- u.Permissions["/"] = []string{dataprovider.PermAny}
- u.Permissions["/subdir"] = []string{dataprovider.PermOverwrite, dataprovider.PermListItems}
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- testFileName := "/subdir/test_file.dat"
- testFilePath := filepath.Join(homeBasePath, "test_file.dat")
- testFileSFTPPath := filepath.Join(u.GetHomeDir(), "subdir", "test_file.dat")
- testFileSize := int64(65535)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- err = createTestFile(testFileSFTPPath, 16384)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- err = sftpUploadFile(testFilePath, testFileName+".new", testFileSize, client)
- if !strings.Contains(err.Error(), "Permission Denied") {
- t.Errorf("unexpected upload error: %v", err)
- }
- err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
- if err != nil {
- t.Errorf("unexpected overwrite error: %v", err)
- }
- os.Remove(testFilePath)
- }
- httpd.RemoveUser(user, http.StatusOK)
- os.RemoveAll(user.GetHomeDir())
- }
- func TestSubDirsDownloads(t *testing.T) {
- usePubKey := true
- u := getTestUser(usePubKey)
- u.Permissions["/"] = []string{dataprovider.PermAny}
- u.Permissions["/subdir"] = []string{dataprovider.PermChmod, dataprovider.PermUpload, dataprovider.PermListItems}
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- err = client.Mkdir("subdir")
- if err != nil {
- t.Errorf("unexpected mkdir error: %v", err)
- }
- testFileName := "/subdir/test_file.dat"
- testFilePath := filepath.Join(homeBasePath, "test_file.dat")
- testFileSize := int64(65535)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
- if err != nil {
- t.Errorf("file upload error: %v", err)
- }
- localDownloadPath := filepath.Join(homeBasePath, "test_download.dat")
- err = sftpDownloadFile(testFileName, localDownloadPath, testFileSize, client)
- if !strings.Contains(err.Error(), "Permission Denied") {
- t.Errorf("unexpected upload error: %v", err)
- }
- err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
- if !strings.Contains(err.Error(), "Permission Denied") {
- t.Errorf("unexpected overwrite error: %v", err)
- }
- err = client.Chtimes(testFileName, time.Now(), time.Now())
- if !strings.Contains(err.Error(), "Permission Denied") {
- t.Errorf("unexpected chtimes error: %v", err)
- }
- err = client.Rename(testFileName, testFileName+".rename")
- if !strings.Contains(err.Error(), "Permission Denied") {
- t.Errorf("unexpected rename error: %v", err)
- }
- err = client.Symlink(testFileName, testFileName+".link")
- if !strings.Contains(err.Error(), "Permission Denied") {
- t.Errorf("unexpected symlink error: %v", err)
- }
- err = client.Remove(testFileName)
- if !strings.Contains(err.Error(), "Permission Denied") {
- t.Errorf("unexpected remove error: %v", err)
- }
- os.Remove(localDownloadPath)
- os.Remove(testFilePath)
- }
- httpd.RemoveUser(user, http.StatusOK)
- os.RemoveAll(user.GetHomeDir())
- }
- func TestPermsSubDirsSetstat(t *testing.T) {
- // for setstat we check the parent dir permission if the requested path is a dir
- // otherwise the path permission
- usePubKey := true
- u := getTestUser(usePubKey)
- u.Permissions["/"] = []string{dataprovider.PermListItems, dataprovider.PermCreateDirs}
- u.Permissions["/subdir"] = []string{dataprovider.PermAny}
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- err = client.Mkdir("subdir")
- if err != nil {
- t.Errorf("unexpected mkdir error: %v", err)
- }
- testFileName := "/subdir/test_file.dat"
- testFilePath := filepath.Join(homeBasePath, "test_file.dat")
- testFileSize := int64(65535)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
- if err != nil {
- t.Errorf("file upload error: %v", err)
- }
- err = client.Chtimes("/subdir/", time.Now(), time.Now())
- if !strings.Contains(err.Error(), "Permission Denied") {
- t.Errorf("unexpected chtimes error: %v", err)
- }
- err = client.Chtimes("subdir/", time.Now(), time.Now())
- if !strings.Contains(err.Error(), "Permission Denied") {
- t.Errorf("unexpected chtimes error: %v", err)
- }
- err = client.Chtimes(testFileName, time.Now(), time.Now())
- if err != nil {
- t.Errorf("unexpected chtimes error: %v", err)
- }
- os.Remove(testFilePath)
- }
- httpd.RemoveUser(user, http.StatusOK)
- os.RemoveAll(user.GetHomeDir())
- }
- func TestPermsSubDirsCommands(t *testing.T) {
- usePubKey := true
- u := getTestUser(usePubKey)
- u.Permissions["/"] = []string{dataprovider.PermAny}
- u.Permissions["/subdir"] = []string{dataprovider.PermDownload, dataprovider.PermUpload}
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- client.Mkdir("subdir")
- acmodTime := time.Now()
- err = client.Chtimes("/subdir", acmodTime, acmodTime)
- if err != nil {
- t.Errorf("unexpected chtimes error: %v", err)
- }
- _, err = client.Stat("/subdir")
- if err != nil {
- t.Errorf("unexpected stat error: %v", err)
- }
- _, err = client.ReadDir("/")
- if err != nil {
- t.Errorf("unexpected readdir error: %v", err)
- }
- _, err = client.ReadDir("/subdir")
- if !strings.Contains(err.Error(), "Permission Denied") {
- t.Errorf("unexpected error: %v", err)
- }
- err = client.RemoveDirectory("/subdir/dir")
- if !strings.Contains(err.Error(), "Permission Denied") {
- t.Errorf("unexpected error: %v", err)
- }
- err = client.Mkdir("/subdir/dir")
- if !strings.Contains(err.Error(), "Permission Denied") {
- t.Errorf("unexpected error: %v", err)
- }
- client.Mkdir("/otherdir")
- err = client.Rename("/otherdir", "/subdir/otherdir")
- if !strings.Contains(err.Error(), "Permission Denied") {
- t.Errorf("unexpected error: %v", err)
- }
- err = client.Symlink("/otherdir", "/subdir/otherdir")
- if !strings.Contains(err.Error(), "Permission Denied") {
- t.Errorf("unexpected error: %v", err)
- }
- err = client.Symlink("/otherdir", "/otherdir_link")
- if err != nil {
- t.Errorf("unexpected rename dir error: %v", err)
- }
- err = client.Rename("/otherdir", "/otherdir1")
- if err != nil {
- t.Errorf("unexpected rename dir error: %v", err)
- }
- err = client.RemoveDirectory("/subdir")
- if err != nil {
- t.Errorf("unexpected remove dir error: %v", err)
- }
- }
- httpd.RemoveUser(user, http.StatusOK)
- os.RemoveAll(user.GetHomeDir())
- }
- func TestRootDirCommands(t *testing.T) {
- usePubKey := true
- u := getTestUser(usePubKey)
- u.Permissions["/"] = []string{dataprovider.PermAny}
- u.Permissions["/subdir"] = []string{dataprovider.PermDownload, dataprovider.PermUpload}
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- err = client.Rename("/", "rootdir")
- if !strings.Contains(err.Error(), "Permission Denied") {
- t.Errorf("unexpected error renaming root dir: %v", err)
- }
- err = client.Symlink("/", "rootdir")
- if !strings.Contains(err.Error(), "Permission Denied") {
- t.Errorf("unexpected error symlinking root dir: %v", err)
- }
- err = client.RemoveDirectory("/")
- if !strings.Contains(err.Error(), "Permission Denied") {
- t.Errorf("unexpected error removing root dir: %v", err)
- }
- }
- httpd.RemoveUser(user, http.StatusOK)
- os.RemoveAll(user.GetHomeDir())
- }
- func TestRelativePaths(t *testing.T) {
- user := getTestUser(true)
- path := filepath.Join(user.HomeDir, "/")
- rel := user.GetRelativePath(path)
- if rel != "/" {
- t.Errorf("Unexpected relative path: %v", rel)
- }
- path = filepath.Join(user.HomeDir, "//")
- rel = user.GetRelativePath(path)
- if rel != "/" {
- t.Errorf("Unexpected relative path: %v", rel)
- }
- path = filepath.Join(user.HomeDir, "../..")
- rel = user.GetRelativePath(path)
- if rel != "/" {
- t.Errorf("Unexpected relative path: %v", rel)
- }
- path = filepath.Join(user.HomeDir, "../../../../../")
- rel = user.GetRelativePath(path)
- if rel != "/" {
- t.Errorf("Unexpected relative path: %v", rel)
- }
- path = filepath.Join(user.HomeDir, "/..")
- rel = user.GetRelativePath(path)
- if rel != "/" {
- t.Errorf("Unexpected relative path: %v", rel)
- }
- path = filepath.Join(user.HomeDir, "/../../../..")
- rel = user.GetRelativePath(path)
- if rel != "/" {
- t.Errorf("Unexpected relative path: %v", rel)
- }
- path = filepath.Join(user.HomeDir, "")
- rel = user.GetRelativePath(path)
- if rel != "/" {
- t.Errorf("Unexpected relative path: %v", rel)
- }
- path = filepath.Join(user.HomeDir, ".")
- rel = user.GetRelativePath(path)
- if rel != "/" {
- t.Errorf("Unexpected relative path: %v", rel)
- }
- path = filepath.Join(user.HomeDir, "somedir")
- rel = user.GetRelativePath(path)
- if rel != "/somedir" {
- t.Errorf("Unexpected relative path: %v", rel)
- }
- path = filepath.Join(user.HomeDir, "/somedir/subdir")
- rel = user.GetRelativePath(path)
- if rel != "/somedir/subdir" {
- t.Errorf("Unexpected relative path: %v", rel)
- }
- }
- func TestUserPerms(t *testing.T) {
- user := getTestUser(true)
- user.Permissions = make(map[string][]string)
- user.Permissions["/"] = []string{dataprovider.PermListItems}
- user.Permissions["/p"] = []string{dataprovider.PermDelete}
- user.Permissions["/p/1"] = []string{dataprovider.PermDownload, dataprovider.PermUpload}
- user.Permissions["/p/2"] = []string{dataprovider.PermCreateDirs}
- user.Permissions["/p/3"] = []string{dataprovider.PermChmod}
- user.Permissions["/p/3/4"] = []string{dataprovider.PermChtimes}
- user.Permissions["/tmp"] = []string{dataprovider.PermRename}
- if !user.HasPerm(dataprovider.PermListItems, filepath.Join(user.HomeDir, "/")) {
- t.Error("expected permission not found")
- }
- if !user.HasPerm(dataprovider.PermListItems, filepath.Join(user.HomeDir, ".")) {
- t.Error("expected permission not found")
- }
- if !user.HasPerm(dataprovider.PermListItems, filepath.Join(user.HomeDir, "")) {
- t.Error("expected permission not found")
- }
- if !user.HasPerm(dataprovider.PermListItems, filepath.Join(user.HomeDir, "../")) {
- t.Error("expected permission not found")
- }
- // path p and /p are the same
- if !user.HasPerm(dataprovider.PermDelete, filepath.Join(user.HomeDir, "/p")) {
- t.Error("expected permission not found")
- }
- if !user.HasPerm(dataprovider.PermDownload, filepath.Join(user.HomeDir, "/p/1")) {
- t.Error("expected permission not found")
- }
- if !user.HasPerm(dataprovider.PermCreateDirs, filepath.Join(user.HomeDir, "p/2")) {
- t.Error("expected permission not found")
- }
- if !user.HasPerm(dataprovider.PermChmod, filepath.Join(user.HomeDir, "/p/3")) {
- t.Error("expected permission not found")
- }
- if !user.HasPerm(dataprovider.PermChtimes, filepath.Join(user.HomeDir, "p/3/4")) {
- t.Error("expected permission not found")
- }
- if !user.HasPerm(dataprovider.PermChtimes, filepath.Join(user.HomeDir, "p/3/4/../4")) {
- t.Error("expected permission not found")
- }
- // undefined paths have permissions of the nearest path
- if !user.HasPerm(dataprovider.PermListItems, filepath.Join(user.HomeDir, "/p34")) {
- t.Error("expected permission not found")
- }
- if !user.HasPerm(dataprovider.PermListItems, filepath.Join(user.HomeDir, "/p34/p1/file.dat")) {
- t.Error("expected permission not found")
- }
- if !user.HasPerm(dataprovider.PermChtimes, filepath.Join(user.HomeDir, "/p/3/4/5/6")) {
- t.Error("expected permission not found")
- }
- if !user.HasPerm(dataprovider.PermDownload, filepath.Join(user.HomeDir, "/p/1/test/file.dat")) {
- t.Error("expected permission not found")
- }
- }
- func TestUserFiltersIPMaskConditions(t *testing.T) {
- user := getTestUser(true)
- // with no filter login must be allowed even if the remoteIP is invalid
- if !user.IsLoginAllowed("192.168.1.5") {
- t.Error("unexpected login denied")
- }
- if !user.IsLoginAllowed("invalid") {
- t.Error("unexpected login denied")
- }
- user.Filters.DeniedIP = append(user.Filters.DeniedIP, "192.168.1.0/24")
- if user.IsLoginAllowed("192.168.1.5") {
- t.Error("unexpected login allowed")
- }
- if !user.IsLoginAllowed("192.168.2.6") {
- t.Error("unexpected login denied")
- }
- user.Filters.AllowedIP = append(user.Filters.AllowedIP, "192.168.1.5/32")
- // if the same ip/mask is both denied and allowed then login must be denied
- if user.IsLoginAllowed("192.168.1.5") {
- t.Error("unexpected login allowed")
- }
- if user.IsLoginAllowed("192.168.3.6") {
- t.Error("unexpected login allowed")
- }
- user.Filters.DeniedIP = []string{}
- if !user.IsLoginAllowed("192.168.1.5") {
- t.Error("unexpected login denied")
- }
- if user.IsLoginAllowed("192.168.1.6") {
- t.Error("unexpected login allowed")
- }
- user.Filters.DeniedIP = []string{"192.168.0.0/16", "172.16.0.0/16"}
- user.Filters.AllowedIP = []string{}
- if user.IsLoginAllowed("192.168.5.255") {
- t.Error("unexpected login allowed")
- }
- if user.IsLoginAllowed("172.16.1.2") {
- t.Error("unexpected login allowed")
- }
- if !user.IsLoginAllowed("172.18.2.1") {
- t.Error("unexpected login denied")
- }
- user.Filters.AllowedIP = []string{"10.4.4.0/24"}
- if user.IsLoginAllowed("10.5.4.2") {
- t.Error("unexpected login allowed")
- }
- if !user.IsLoginAllowed("10.4.4.2") {
- t.Error("unexpected login denied")
- }
- if !user.IsLoginAllowed("invalid") {
- t.Error("unexpected login denied")
- }
- }
- func TestSSHCommands(t *testing.T) {
- usePubKey := false
- user, _, err := httpd.AddUser(getTestUser(usePubKey), http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- _, err = runSSHCommand("ls", user, usePubKey)
- if err == nil {
- t.Errorf("unsupported ssh command must fail")
- }
- _, err = runSSHCommand("cd", user, usePubKey)
- if err != nil {
- t.Errorf("unexpected error for ssh cd command: %v", err)
- }
- out, err := runSSHCommand("pwd", user, usePubKey)
- if err != nil {
- t.Errorf("unexpected error: %v", err)
- t.Fail()
- }
- if string(out) != "/\n" {
- t.Errorf("invalid response for ssh pwd command: %v", string(out))
- }
- out, err = runSSHCommand("md5sum", user, usePubKey)
- if err != nil {
- t.Errorf("unexpected error: %v", err)
- t.Fail()
- }
- // echo -n '' | md5sum
- if !strings.Contains(string(out), "d41d8cd98f00b204e9800998ecf8427e") {
- t.Errorf("invalid md5sum: %v", string(out))
- }
- out, err = runSSHCommand("sha1sum", user, usePubKey)
- if err != nil {
- t.Errorf("unexpected error: %v", err)
- t.Fail()
- }
- if !strings.Contains(string(out), "da39a3ee5e6b4b0d3255bfef95601890afd80709") {
- t.Errorf("invalid sha1sum: %v", string(out))
- }
- out, err = runSSHCommand("sha256sum", user, usePubKey)
- if err != nil {
- t.Errorf("unexpected error: %v", err)
- t.Fail()
- }
- if !strings.Contains(string(out), "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855") {
- t.Errorf("invalid sha256sum: %v", string(out))
- }
- out, err = runSSHCommand("sha384sum", user, usePubKey)
- if err != nil {
- t.Errorf("unexpected error: %v", err)
- t.Fail()
- }
- if !strings.Contains(string(out), "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b") {
- t.Errorf("invalid sha384sum: %v", string(out))
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- }
- func TestSSHFileHash(t *testing.T) {
- usePubKey := true
- user, _, err := httpd.AddUser(getTestUser(usePubKey), http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(user, usePubKey)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- testFileSize := int64(65535)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
- if err != nil {
- t.Errorf("file upload error: %v", err)
- }
- user.Permissions = make(map[string][]string)
- user.Permissions["/"] = []string{dataprovider.PermUpload}
- _, _, err = httpd.UpdateUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to update user: %v", err)
- }
- _, err = runSSHCommand("sha512sum "+testFileName, user, usePubKey)
- if err == nil {
- t.Errorf("hash command with no list permission must fail")
- }
- user.Permissions["/"] = []string{dataprovider.PermAny}
- _, _, err = httpd.UpdateUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to update user: %v", err)
- }
- initialHash, err := computeHashForFile(sha512.New(), testFilePath)
- if err != nil {
- t.Errorf("error computing file hash: %v", err)
- }
- out, err := runSSHCommand("sha512sum "+testFileName, user, usePubKey)
- if err != nil {
- t.Errorf("unexpected error: %v", err)
- t.Fail()
- }
- if !strings.Contains(string(out), initialHash) {
- t.Errorf("invalid sha512sum: %v", string(out))
- }
- _, err = runSSHCommand("sha512sum invalid_path", user, usePubKey)
- if err == nil {
- t.Errorf("hash for an invalid path must fail")
- }
- os.Remove(testFilePath)
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- }
- func TestBasicGitCommands(t *testing.T) {
- if len(gitPath) == 0 || len(sshPath) == 0 {
- t.Skip("git and/or ssh command not found, unable to execute this test")
- }
- usePubKey := true
- u := getTestUser(usePubKey)
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- repoName := "testrepo"
- clonePath := filepath.Join(homeBasePath, repoName)
- os.RemoveAll(user.GetHomeDir())
- os.RemoveAll(filepath.Join(homeBasePath, repoName))
- out, err := initGitRepo(filepath.Join(user.HomeDir, repoName))
- if err != nil {
- t.Errorf("unexpected error: %v out: %v", err, string(out))
- }
- out, err = cloneGitRepo(homeBasePath, "/"+repoName, user.Username)
- if err != nil {
- t.Errorf("unexpected error: %v out: %v", err, string(out))
- }
- out, err = addFileToGitRepo(clonePath, 128)
- if err != nil {
- t.Errorf("unexpected error: %v out: %v", err, string(out))
- }
- user.QuotaFiles = 100000
- _, _, err = httpd.UpdateUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to update user: %v", err)
- }
- out, err = pushToGitRepo(clonePath)
- if err != nil {
- t.Errorf("unexpected error: %v out: %v", err, string(out))
- printLatestLogs(10)
- }
- err = waitQuotaScans()
- if err != nil {
- t.Errorf("error waiting for active quota scans: %v", err)
- }
- user, _, err = httpd.GetUserByID(user.ID, http.StatusOK)
- if err != nil {
- t.Errorf("unable to get user: %v", err)
- }
- user.QuotaSize = user.UsedQuotaSize - 1
- _, _, err = httpd.UpdateUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to update user: %v", err)
- }
- out, err = pushToGitRepo(clonePath)
- if err == nil {
- t.Errorf("git push must fail if quota is exceeded, out: %v", string(out))
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- os.RemoveAll(clonePath)
- }
- func TestGitErrors(t *testing.T) {
- if len(gitPath) == 0 || len(sshPath) == 0 {
- t.Skip("git and/or ssh command not found, unable to execute this test")
- }
- usePubKey := true
- u := getTestUser(usePubKey)
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- repoName := "testrepo"
- clonePath := filepath.Join(homeBasePath, repoName)
- os.RemoveAll(user.GetHomeDir())
- os.RemoveAll(filepath.Join(homeBasePath, repoName))
- out, err := cloneGitRepo(homeBasePath, "/"+repoName, user.Username)
- if err == nil {
- t.Errorf("cloning a missing repo must fail, out: %v", string(out))
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- os.RemoveAll(clonePath)
- }
- // Start SCP tests
- func TestSCPBasicHandling(t *testing.T) {
- if len(scpPath) == 0 {
- t.Skip("scp command not found, unable to execute this test")
- }
- usePubKey := true
- u := getTestUser(usePubKey)
- u.QuotaSize = 6553600
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- testFileSize := int64(131074)
- expectedQuotaSize := user.UsedQuotaSize + testFileSize
- expectedQuotaFiles := user.UsedQuotaFiles + 1
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- remoteUpPath := fmt.Sprintf("%[email protected]:%v", user.Username, "/")
- remoteDownPath := fmt.Sprintf("%[email protected]:%v", user.Username, path.Join("/", testFileName))
- localPath := filepath.Join(homeBasePath, "scp_download.dat")
- // test to download a missing file
- err = scpDownload(localPath, remoteDownPath, false, false)
- if err == nil {
- t.Errorf("downloading a missing file via scp must fail")
- }
- err = scpUpload(testFilePath, remoteUpPath, false, false)
- if err != nil {
- t.Errorf("error uploading file via scp: %v", err)
- }
- err = scpDownload(localPath, remoteDownPath, false, false)
- if err != nil {
- t.Errorf("error downloading file via scp: %v", err)
- }
- fi, err := os.Stat(localPath)
- if err != nil {
- t.Errorf("stat for the downloaded file must succeed")
- } else {
- if fi.Size() != testFileSize {
- t.Errorf("size of the file downloaded via SCP does not match the expected one")
- }
- }
- os.Remove(localPath)
- user, _, err = httpd.GetUserByID(user.ID, http.StatusOK)
- if err != nil {
- t.Errorf("error getting user: %v", err)
- }
- if expectedQuotaFiles != user.UsedQuotaFiles {
- t.Errorf("quota files does not match, expected: %v, actual: %v", expectedQuotaFiles, user.UsedQuotaFiles)
- }
- if expectedQuotaSize != user.UsedQuotaSize {
- t.Errorf("quota size does not match, expected: %v, actual: %v", expectedQuotaSize, user.UsedQuotaSize)
- }
- err = os.RemoveAll(user.GetHomeDir())
- if err != nil {
- t.Errorf("error removing uploaded files")
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.Remove(testFilePath)
- }
- func TestSCPUploadFileOverwrite(t *testing.T) {
- if len(scpPath) == 0 {
- t.Skip("scp command not found, unable to execute this test")
- }
- usePubKey := true
- u := getTestUser(usePubKey)
- u.QuotaFiles = 1000
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- testFileSize := int64(32760)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- remoteUpPath := fmt.Sprintf("%[email protected]:%v", user.Username, path.Join("/", testFileName))
- err = scpUpload(testFilePath, remoteUpPath, true, false)
- if err != nil {
- t.Errorf("error uploading file via scp: %v", err)
- }
- // test a new upload that must overwrite the existing file
- err = scpUpload(testFilePath, remoteUpPath, true, false)
- if err != nil {
- t.Errorf("error uploading existing file via scp: %v", err)
- }
- user, _, err = httpd.GetUserByID(user.ID, http.StatusOK)
- if err != nil {
- t.Errorf("error getting user: %v", err)
- }
- if user.UsedQuotaSize != testFileSize || user.UsedQuotaFiles != 1 {
- t.Errorf("update quota error on file overwrite, actual size: %v, expected: %v actual files: %v, expected: 1",
- user.UsedQuotaSize, testFileSize, user.UsedQuotaFiles)
- }
- remoteDownPath := fmt.Sprintf("%[email protected]:%v", user.Username, path.Join("/", testFileName))
- localPath := filepath.Join(homeBasePath, "scp_download.dat")
- err = scpDownload(localPath, remoteDownPath, false, false)
- if err != nil {
- t.Errorf("error downloading file via scp: %v", err)
- }
- fi, err := os.Stat(localPath)
- if err != nil {
- t.Errorf("stat for the downloaded file must succeed")
- } else {
- if fi.Size() != testFileSize {
- t.Errorf("size of the file downloaded via SCP does not match the expected one")
- }
- }
- os.Remove(localPath)
- os.Remove(testFilePath)
- err = os.RemoveAll(user.GetHomeDir())
- if err != nil {
- t.Errorf("error removing uploaded files")
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- }
- func TestSCPRecursive(t *testing.T) {
- if len(scpPath) == 0 {
- t.Skip("scp command not found, unable to execute this test")
- }
- usePubKey := true
- u := getTestUser(usePubKey)
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- testFileName := "test_file.dat"
- testBaseDirName := "test_dir"
- testBaseDirPath := filepath.Join(homeBasePath, testBaseDirName)
- testBaseDirDownName := "test_dir_down"
- testBaseDirDownPath := filepath.Join(homeBasePath, testBaseDirDownName)
- testFilePath := filepath.Join(homeBasePath, testBaseDirName, testFileName)
- testFilePath1 := filepath.Join(homeBasePath, testBaseDirName, testBaseDirName, testFileName)
- testFileSize := int64(131074)
- createTestFile(testFilePath, testFileSize)
- createTestFile(testFilePath1, testFileSize)
- remoteDownPath := fmt.Sprintf("%[email protected]:%v", user.Username, path.Join("/", testBaseDirName))
- // test to download a missing dir
- err = scpDownload(testBaseDirDownPath, remoteDownPath, true, true)
- if err == nil {
- t.Errorf("downloading a missing dir via scp must fail")
- }
- remoteUpPath := fmt.Sprintf("%[email protected]:%v", user.Username, "/")
- err = scpUpload(testBaseDirPath, remoteUpPath, true, false)
- if err != nil {
- t.Errorf("error uploading dir via scp: %v", err)
- }
- err = scpDownload(testBaseDirDownPath, remoteDownPath, true, true)
- if err != nil {
- t.Errorf("error downloading dir via scp: %v", err)
- }
- // test download without passing -r
- err = scpDownload(testBaseDirDownPath, remoteDownPath, true, false)
- if err == nil {
- t.Errorf("recursive download without -r must fail")
- }
- fi, err := os.Stat(filepath.Join(testBaseDirDownPath, testFileName))
- if err != nil {
- t.Errorf("error downloading file using scp recursive: %v", err)
- } else {
- if fi.Size() != testFileSize {
- t.Errorf("size for file downloaded using recursive scp does not match, actual: %v, expected: %v", fi.Size(), testFileSize)
- }
- }
- fi, err = os.Stat(filepath.Join(testBaseDirDownPath, testBaseDirName, testFileName))
- if err != nil {
- t.Errorf("error downloading file using scp recursive: %v", err)
- } else {
- if fi.Size() != testFileSize {
- t.Errorf("size for file downloaded using recursive scp does not match, actual: %v, expected: %v", fi.Size(), testFileSize)
- }
- }
- // upload to a non existent dir
- remoteUpPath = fmt.Sprintf("%[email protected]:%v", user.Username, "/non_existent_dir")
- err = scpUpload(testBaseDirPath, remoteUpPath, true, false)
- if err == nil {
- t.Errorf("uploading via scp to a non existent dir must fail")
- }
- os.RemoveAll(testBaseDirPath)
- os.RemoveAll(testBaseDirDownPath)
- err = os.RemoveAll(user.GetHomeDir())
- if err != nil {
- t.Errorf("error removing uploaded files")
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- }
- func TestSCPPermsSubDirs(t *testing.T) {
- if len(scpPath) == 0 {
- t.Skip("scp command not found, unable to execute this test")
- }
- usePubKey := true
- u := getTestUser(usePubKey)
- u.Permissions["/"] = []string{dataprovider.PermAny}
- u.Permissions["/somedir"] = []string{dataprovider.PermListItems, dataprovider.PermUpload}
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- localPath := filepath.Join(homeBasePath, "scp_download.dat")
- subPath := filepath.Join(user.GetHomeDir(), "somedir")
- testFileSize := int64(65535)
- os.MkdirAll(subPath, 0777)
- remoteDownPath := fmt.Sprintf("%[email protected]:%v", user.Username, "/somedir")
- err = scpDownload(localPath, remoteDownPath, false, true)
- if err == nil {
- t.Error("download a dir with no permissions must fail")
- }
- os.Remove(subPath)
- err = createTestFile(subPath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- err = scpDownload(localPath, remoteDownPath, false, false)
- if err != nil {
- t.Errorf("unexpected download error: %v", err)
- }
- os.Chmod(subPath, 0001)
- err = scpDownload(localPath, remoteDownPath, false, false)
- if err == nil {
- t.Error("download a file with no system permissions must fail")
- }
- os.Chmod(subPath, 0755)
- os.Remove(localPath)
- os.RemoveAll(user.GetHomeDir())
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- }
- func TestSCPPermCreateDirs(t *testing.T) {
- if len(scpPath) == 0 {
- t.Skip("scp command not found, unable to execute this test")
- }
- usePubKey := true
- u := getTestUser(usePubKey)
- u.Permissions["/"] = []string{dataprovider.PermDownload, dataprovider.PermUpload}
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- testFileSize := int64(32760)
- testBaseDirName := "test_dir"
- testBaseDirPath := filepath.Join(homeBasePath, testBaseDirName)
- testFilePath1 := filepath.Join(homeBasePath, testBaseDirName, testFileName)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- err = createTestFile(testFilePath1, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- remoteUpPath := fmt.Sprintf("%[email protected]:%v", user.Username, "/tmp/")
- err = scpUpload(testFilePath, remoteUpPath, true, false)
- if err == nil {
- t.Errorf("scp upload must fail, the user cannot create files in a missing dir")
- }
- err = scpUpload(testBaseDirPath, remoteUpPath, true, false)
- if err == nil {
- t.Errorf("scp upload must fail, the user cannot create new dirs")
- }
- err = os.Remove(testFilePath)
- if err != nil {
- t.Errorf("error removing test file")
- }
- os.RemoveAll(testBaseDirPath)
- err = os.RemoveAll(user.GetHomeDir())
- if err != nil {
- t.Errorf("error removing uploaded files")
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- }
- func TestSCPPermUpload(t *testing.T) {
- if len(scpPath) == 0 {
- t.Skip("scp command not found, unable to execute this test")
- }
- usePubKey := true
- u := getTestUser(usePubKey)
- u.Permissions["/"] = []string{dataprovider.PermDownload, dataprovider.PermCreateDirs}
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- testFileSize := int64(65536)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- remoteUpPath := fmt.Sprintf("%[email protected]:%v", user.Username, "/tmp")
- err = scpUpload(testFilePath, remoteUpPath, true, false)
- if err == nil {
- t.Errorf("scp upload must fail, the user cannot upload")
- }
- err = os.Remove(testFilePath)
- if err != nil {
- t.Errorf("error removing test file")
- }
- err = os.RemoveAll(user.GetHomeDir())
- if err != nil {
- t.Errorf("error removing uploaded files")
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- }
- func TestSCPPermOverwrite(t *testing.T) {
- if len(scpPath) == 0 {
- t.Skip("scp command not found, unable to execute this test")
- }
- usePubKey := true
- u := getTestUser(usePubKey)
- u.Permissions["/"] = []string{dataprovider.PermUpload, dataprovider.PermCreateDirs}
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- testFileSize := int64(65536)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- remoteUpPath := fmt.Sprintf("%[email protected]:%v", user.Username, "/tmp")
- err = scpUpload(testFilePath, remoteUpPath, true, false)
- if err != nil {
- t.Errorf("scp upload error: %v", err)
- }
- err = scpUpload(testFilePath, remoteUpPath, true, false)
- if err == nil {
- t.Errorf("scp upload must fail, the user cannot ovewrite existing files")
- }
- err = os.Remove(testFilePath)
- if err != nil {
- t.Errorf("error removing test file")
- }
- err = os.RemoveAll(user.GetHomeDir())
- if err != nil {
- t.Errorf("error removing uploaded files")
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- }
- func TestSCPPermDownload(t *testing.T) {
- if len(scpPath) == 0 {
- t.Skip("scp command not found, unable to execute this test")
- }
- usePubKey := true
- u := getTestUser(usePubKey)
- u.Permissions["/"] = []string{dataprovider.PermUpload, dataprovider.PermCreateDirs}
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- testFileSize := int64(65537)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- remoteUpPath := fmt.Sprintf("%[email protected]:%v", user.Username, "/")
- err = scpUpload(testFilePath, remoteUpPath, true, false)
- if err != nil {
- t.Errorf("error uploading existing file via scp: %v", err)
- }
- remoteDownPath := fmt.Sprintf("%[email protected]:%v", user.Username, path.Join("/", testFileName))
- localPath := filepath.Join(homeBasePath, "scp_download.dat")
- err = scpDownload(localPath, remoteDownPath, false, false)
- if err == nil {
- t.Errorf("scp download must fail, the user cannot download")
- }
- err = os.Remove(testFilePath)
- if err != nil {
- t.Errorf("error removing test file")
- }
- err = os.RemoveAll(user.GetHomeDir())
- if err != nil {
- t.Errorf("error removing uploaded files")
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- }
- func TestSCPQuotaSize(t *testing.T) {
- if len(scpPath) == 0 {
- t.Skip("scp command not found, unable to execute this test")
- }
- usePubKey := true
- testFileSize := int64(65535)
- u := getTestUser(usePubKey)
- u.QuotaFiles = 1
- u.QuotaSize = testFileSize - 1
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- remoteUpPath := fmt.Sprintf("%[email protected]:%v", user.Username, path.Join("/", testFileName))
- err = scpUpload(testFilePath, remoteUpPath, true, false)
- if err != nil {
- t.Errorf("error uploading existing file via scp: %v", err)
- }
- err = scpUpload(testFilePath, remoteUpPath+".quota", true, false)
- if err == nil {
- t.Errorf("user is over quota scp upload must fail")
- }
- err = os.Remove(testFilePath)
- if err != nil {
- t.Errorf("error removing test file")
- }
- err = os.RemoveAll(user.GetHomeDir())
- if err != nil {
- t.Errorf("error removing uploaded files")
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- }
- func TestSCPEscapeHomeDir(t *testing.T) {
- if len(scpPath) == 0 {
- t.Skip("scp command not found, unable to execute this test")
- }
- usePubKey := true
- user, _, err := httpd.AddUser(getTestUser(usePubKey), http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- os.MkdirAll(user.GetHomeDir(), 0777)
- testDir := "testDir"
- linkPath := filepath.Join(homeBasePath, defaultUsername, testDir)
- err = os.Symlink(homeBasePath, linkPath)
- if err != nil {
- t.Errorf("error making local symlink: %v", err)
- }
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- testFileSize := int64(65535)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- remoteUpPath := fmt.Sprintf("%[email protected]:%v", user.Username, path.Join(testDir, testDir))
- err = scpUpload(testFilePath, remoteUpPath, false, false)
- if err == nil {
- t.Errorf("uploading to a dir with a symlink outside home dir must fail")
- }
- remoteDownPath := fmt.Sprintf("%[email protected]:%v", user.Username, path.Join("/", testDir, testFileName))
- localPath := filepath.Join(homeBasePath, "scp_download.dat")
- err = scpDownload(localPath, remoteDownPath, false, false)
- if err == nil {
- t.Errorf("scp download must fail, the requested file has a symlink outside user home")
- }
- remoteDownPath = fmt.Sprintf("%[email protected]:%v", user.Username, path.Join("/", testDir))
- err = scpDownload(homeBasePath, remoteDownPath, false, true)
- if err == nil {
- t.Errorf("scp download must fail, the requested dir is a symlink outside user home")
- }
- err = os.Remove(testFilePath)
- if err != nil {
- t.Errorf("error removing test file")
- }
- err = os.RemoveAll(user.GetHomeDir())
- if err != nil {
- t.Errorf("error removing uploaded files")
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- }
- func TestSCPUploadPaths(t *testing.T) {
- if len(scpPath) == 0 {
- t.Skip("scp command not found, unable to execute this test")
- }
- usePubKey := true
- user, _, err := httpd.AddUser(getTestUser(usePubKey), http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- testFileSize := int64(65535)
- testDirName := "testDir"
- testDirPath := filepath.Join(user.GetHomeDir(), testDirName)
- os.MkdirAll(testDirPath, 0777)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- remoteUpPath := fmt.Sprintf("%[email protected]:%v", user.Username, testDirName)
- remoteDownPath := fmt.Sprintf("%[email protected]:%v", user.Username, path.Join(testDirName, testFileName))
- localPath := filepath.Join(homeBasePath, "scp_download.dat")
- err = scpUpload(testFilePath, remoteUpPath, false, false)
- if err != nil {
- t.Errorf("scp upload error: %v", err)
- }
- err = scpDownload(localPath, remoteDownPath, false, false)
- if err != nil {
- t.Errorf("scp download error: %v", err)
- }
- // upload a file to a missing dir
- remoteUpPath = fmt.Sprintf("%[email protected]:%v", user.Username, path.Join(testDirName, testDirName, testFileName))
- err = scpUpload(testFilePath, remoteUpPath, false, false)
- if err == nil {
- t.Errorf("scp upload to a missing dir must fail")
- }
- err = os.RemoveAll(user.GetHomeDir())
- if err != nil {
- t.Errorf("error removing uploaded files")
- }
- os.Remove(localPath)
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- }
- func TestSCPOverwriteDirWithFile(t *testing.T) {
- if len(scpPath) == 0 {
- t.Skip("scp command not found, unable to execute this test")
- }
- usePubKey := true
- user, _, err := httpd.AddUser(getTestUser(usePubKey), http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- testFileSize := int64(65535)
- testDirPath := filepath.Join(user.GetHomeDir(), testFileName)
- os.MkdirAll(testDirPath, 0777)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- remoteUpPath := fmt.Sprintf("%[email protected]:%v", user.Username, "/")
- err = scpUpload(testFilePath, remoteUpPath, false, false)
- if err == nil {
- t.Errorf("copying a file over an existing dir must fail")
- }
- err = os.RemoveAll(user.GetHomeDir())
- if err != nil {
- t.Errorf("error removing uploaded files")
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- }
- func TestSCPRemoteToRemote(t *testing.T) {
- if len(scpPath) == 0 {
- t.Skip("scp command not found, unable to execute this test")
- }
- usePubKey := true
- user, _, err := httpd.AddUser(getTestUser(usePubKey), http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- u := getTestUser(usePubKey)
- u.Username += "1"
- u.HomeDir += "1"
- user1, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- testFileSize := int64(65535)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- remoteUpPath := fmt.Sprintf("%[email protected]:%v", user.Username, path.Join("/", testFileName))
- remote1UpPath := fmt.Sprintf("%[email protected]:%v", user1.Username, path.Join("/", testFileName))
- err = scpUpload(testFilePath, remoteUpPath, false, false)
- if err != nil {
- t.Errorf("scp upload error: %v", err)
- }
- err = scpUpload(remoteUpPath, remote1UpPath, false, true)
- if err != nil {
- t.Errorf("scp upload remote to remote error: %v", err)
- }
- err = os.RemoveAll(user.GetHomeDir())
- if err != nil {
- t.Errorf("error removing uploaded files")
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- err = os.RemoveAll(user1.GetHomeDir())
- if err != nil {
- t.Errorf("error removing uploaded files for user1")
- }
- _, err = httpd.RemoveUser(user1, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user1: %v", err)
- }
- }
- func TestSCPErrors(t *testing.T) {
- if len(scpPath) == 0 {
- t.Skip("scp command not found, unable to execute this test")
- }
- u := getTestUser(true)
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- testFileSize := int64(524288)
- testFileName := "test_file.dat"
- testFilePath := filepath.Join(homeBasePath, testFileName)
- err = createTestFile(testFilePath, testFileSize)
- if err != nil {
- t.Errorf("unable to create test file: %v", err)
- }
- remoteUpPath := fmt.Sprintf("%[email protected]:%v", user.Username, "/")
- remoteDownPath := fmt.Sprintf("%[email protected]:%v", user.Username, path.Join("/", testFileName))
- localPath := filepath.Join(homeBasePath, "scp_download.dat")
- err = scpUpload(testFilePath, remoteUpPath, false, false)
- if err != nil {
- t.Errorf("error uploading file via scp: %v", err)
- }
- user.UploadBandwidth = 512
- user.DownloadBandwidth = 512
- _, _, err = httpd.UpdateUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to update user: %v", err)
- }
- cmd := getScpDownloadCommand(localPath, remoteDownPath, false, false)
- go func() {
- if cmd.Run() == nil {
- t.Errorf("SCP download must fail")
- }
- }()
- waitForActiveTransfer()
- // wait some additional arbitrary time to wait for transfer activity to happen
- // it is need to reach all the code in CheckIdleConnections
- time.Sleep(100 * time.Millisecond)
- cmd.Process.Kill()
- waitForNoActiveTransfer()
- cmd = getScpUploadCommand(testFilePath, remoteUpPath, false, false)
- go func() {
- if cmd.Run() == nil {
- t.Errorf("SCP upload must fail")
- }
- }()
- waitForActiveTransfer()
- // wait some additional arbitrary time to wait for transfer activity to happen
- // it is need to reach all the code in CheckIdleConnections
- time.Sleep(100 * time.Millisecond)
- cmd.Process.Kill()
- waitForNoActiveTransfer()
- err = os.Remove(testFilePath)
- if err != nil {
- t.Errorf("error removing test file")
- }
- os.Remove(localPath)
- err = os.RemoveAll(user.GetHomeDir())
- if err != nil {
- t.Errorf("error removing uploaded files")
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- }
- // End SCP tests
- func waitTCPListening(address string) {
- for {
- conn, err := net.Dial("tcp", address)
- if err != nil {
- logger.WarnToConsole("tcp server %v not listening: %v\n", address, err)
- time.Sleep(100 * time.Millisecond)
- continue
- }
- logger.InfoToConsole("tcp server %v now listening\n", address)
- defer conn.Close()
- break
- }
- }
- func getTestUser(usePubKey bool) dataprovider.User {
- user := dataprovider.User{
- Username: defaultUsername,
- Password: defaultPassword,
- HomeDir: filepath.Join(homeBasePath, defaultUsername),
- Status: 1,
- ExpirationDate: 0,
- }
- user.Permissions = make(map[string][]string)
- user.Permissions["/"] = allPerms
- if usePubKey {
- user.PublicKeys = []string{testPubKey}
- user.Password = ""
- }
- return user
- }
- func runSSHCommand(command string, user dataprovider.User, usePubKey bool) ([]byte, error) {
- var sshSession *ssh.Session
- var output []byte
- config := &ssh.ClientConfig{
- User: defaultUsername,
- HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
- return nil
- },
- }
- if usePubKey {
- key, err := ssh.ParsePrivateKey([]byte(testPrivateKey))
- if err != nil {
- return output, err
- }
- config.Auth = []ssh.AuthMethod{ssh.PublicKeys(key)}
- } else {
- config.Auth = []ssh.AuthMethod{ssh.Password(defaultPassword)}
- }
- conn, err := ssh.Dial("tcp", sftpServerAddr, config)
- if err != nil {
- return output, err
- }
- defer conn.Close()
- sshSession, err = conn.NewSession()
- if err != nil {
- return output, err
- }
- var stdout, stderr bytes.Buffer
- sshSession.Stdout = &stdout
- sshSession.Stderr = &stderr
- err = sshSession.Run(command)
- if err != nil {
- return nil, fmt.Errorf("failed to run command %v: %v", command, stderr.Bytes())
- }
- return stdout.Bytes(), err
- }
- func getSftpClient(user dataprovider.User, usePubKey bool) (*sftp.Client, error) {
- var sftpClient *sftp.Client
- config := &ssh.ClientConfig{
- User: user.Username,
- HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
- return nil
- },
- }
- if usePubKey {
- key, err := ssh.ParsePrivateKey([]byte(testPrivateKey))
- if err != nil {
- return nil, err
- }
- config.Auth = []ssh.AuthMethod{ssh.PublicKeys(key)}
- } else {
- if len(user.Password) > 0 {
- config.Auth = []ssh.AuthMethod{ssh.Password(user.Password)}
- } else {
- config.Auth = []ssh.AuthMethod{ssh.Password(defaultPassword)}
- }
- }
- conn, err := ssh.Dial("tcp", sftpServerAddr, config)
- if err != nil {
- return sftpClient, err
- }
- sftpClient, err = sftp.NewClient(conn)
- return sftpClient, err
- }
- func createTestFile(path string, size int64) error {
- baseDir := filepath.Dir(path)
- if _, err := os.Stat(baseDir); os.IsNotExist(err) {
- os.MkdirAll(baseDir, 0777)
- }
- content := make([]byte, size)
- _, err := rand.Read(content)
- if err != nil {
- return err
- }
- return ioutil.WriteFile(path, content, 0666)
- }
- func appendToTestFile(path string, size int64) error {
- content := make([]byte, size)
- _, err := rand.Read(content)
- if err != nil {
- return err
- }
- f, err := os.OpenFile(path, os.O_APPEND|os.O_WRONLY, 0666)
- if err != nil {
- return err
- }
- written, err := io.Copy(f, bytes.NewReader(content))
- if err != nil {
- return err
- }
- if int64(written) != size {
- return fmt.Errorf("write error, written: %v/%v", written, size)
- }
- return nil
- }
- func sftpUploadFile(localSourcePath string, remoteDestPath string, expectedSize int64, client *sftp.Client) error {
- srcFile, err := os.Open(localSourcePath)
- if err != nil {
- return err
- }
- defer srcFile.Close()
- destFile, err := client.Create(remoteDestPath)
- if err != nil {
- return err
- }
- _, err = io.Copy(destFile, srcFile)
- if err != nil {
- destFile.Close()
- return err
- }
- // we need to close the file to trigger the close method on server
- // we cannot defer closing or Lstat will fail for uploads in atomic mode
- destFile.Close()
- if expectedSize > 0 {
- fi, err := client.Stat(remoteDestPath)
- if err != nil {
- return err
- }
- if fi.Size() != expectedSize {
- return fmt.Errorf("uploaded file size does not match, actual: %v, expected: %v", fi.Size(), expectedSize)
- }
- }
- return err
- }
- func sftpUploadResumeFile(localSourcePath string, remoteDestPath string, expectedSize int64, invalidOffset bool,
- client *sftp.Client) error {
- srcFile, err := os.Open(localSourcePath)
- if err != nil {
- return err
- }
- defer srcFile.Close()
- fi, err := client.Lstat(remoteDestPath)
- if err != nil {
- return err
- }
- if !invalidOffset {
- _, err = srcFile.Seek(fi.Size(), 0)
- if err != nil {
- return err
- }
- }
- destFile, err := client.OpenFile(remoteDestPath, os.O_WRONLY|os.O_APPEND)
- if err != nil {
- return err
- }
- if !invalidOffset {
- _, err = destFile.Seek(fi.Size(), 0)
- if err != nil {
- return err
- }
- }
- _, err = io.Copy(destFile, srcFile)
- if err != nil {
- destFile.Close()
- return err
- }
- // we need to close the file to trigger the close method on server
- // we cannot defer closing or Lstat will fail for upload atomic mode
- destFile.Close()
- if expectedSize > 0 {
- fi, err := client.Lstat(remoteDestPath)
- if err != nil {
- return err
- }
- if fi.Size() != expectedSize {
- return fmt.Errorf("uploaded file size does not match, actual: %v, expected: %v", fi.Size(), expectedSize)
- }
- }
- return err
- }
- func sftpDownloadFile(remoteSourcePath string, localDestPath string, expectedSize int64, client *sftp.Client) error {
- downloadDest, err := os.Create(localDestPath)
- if err != nil {
- return err
- }
- defer downloadDest.Close()
- sftpSrcFile, err := client.Open(remoteSourcePath)
- if err != nil {
- return err
- }
- defer sftpSrcFile.Close()
- _, err = io.Copy(downloadDest, sftpSrcFile)
- if err != nil {
- return err
- }
- err = downloadDest.Sync()
- if err != nil {
- return err
- }
- if expectedSize > 0 {
- fi, err := downloadDest.Stat()
- if err != nil {
- return err
- }
- if fi.Size() != expectedSize {
- return fmt.Errorf("downloaded file size does not match, actual: %v, expected: %v", fi.Size(), expectedSize)
- }
- }
- return err
- }
- func sftpUploadNonBlocking(localSourcePath string, remoteDestPath string, expectedSize int64, client *sftp.Client) <-chan error {
- c := make(chan error)
- go func() {
- c <- sftpUploadFile(localSourcePath, remoteDestPath, expectedSize, client)
- }()
- return c
- }
- func sftpDownloadNonBlocking(remoteSourcePath string, localDestPath string, expectedSize int64, client *sftp.Client) <-chan error {
- c := make(chan error)
- go func() {
- c <- sftpDownloadFile(remoteSourcePath, localDestPath, expectedSize, client)
- }()
- return c
- }
- func scpUpload(localPath, remotePath string, preserveTime, remoteToRemote bool) error {
- cmd := getScpUploadCommand(localPath, remotePath, preserveTime, remoteToRemote)
- return cmd.Run()
- }
- func scpDownload(localPath, remotePath string, preserveTime, recursive bool) error {
- cmd := getScpDownloadCommand(localPath, remotePath, preserveTime, recursive)
- return cmd.Run()
- }
- func getScpDownloadCommand(localPath, remotePath string, preserveTime, recursive bool) *exec.Cmd {
- var args []string
- if preserveTime {
- args = append(args, "-p")
- }
- if recursive {
- args = append(args, "-r")
- }
- args = append(args, "-P")
- args = append(args, "2022")
- args = append(args, "-o")
- args = append(args, "StrictHostKeyChecking=no")
- args = append(args, "-i")
- args = append(args, privateKeyPath)
- args = append(args, remotePath)
- args = append(args, localPath)
- return exec.Command(scpPath, args...)
- }
- func getScpUploadCommand(localPath, remotePath string, preserveTime, remoteToRemote bool) *exec.Cmd {
- var args []string
- if remoteToRemote {
- args = append(args, "-3")
- }
- if preserveTime {
- args = append(args, "-p")
- }
- fi, err := os.Stat(localPath)
- if err == nil {
- if fi.IsDir() {
- args = append(args, "-r")
- }
- }
- args = append(args, "-P")
- args = append(args, "2022")
- args = append(args, "-o")
- args = append(args, "StrictHostKeyChecking=no")
- args = append(args, "-i")
- args = append(args, privateKeyPath)
- args = append(args, localPath)
- args = append(args, remotePath)
- return exec.Command(scpPath, args...)
- }
- func computeHashForFile(hasher hash.Hash, path string) (string, error) {
- hash := ""
- f, err := os.Open(path)
- if err != nil {
- return hash, err
- }
- defer f.Close()
- _, err = io.Copy(hasher, f)
- if err == nil {
- hash = fmt.Sprintf("%x", hasher.Sum(nil))
- }
- return hash, err
- }
- func waitForNoActiveTransfer() {
- for len(sftpd.GetConnectionsStats()) > 0 {
- time.Sleep(100 * time.Millisecond)
- }
- }
- func waitForActiveTransfer() {
- stats := sftpd.GetConnectionsStats()
- for len(stats) < 1 {
- stats = sftpd.GetConnectionsStats()
- }
- activeTransferFound := false
- for !activeTransferFound {
- stats = sftpd.GetConnectionsStats()
- if len(stats) == 0 {
- break
- }
- for _, stat := range stats {
- if len(stat.Transfers) > 0 {
- activeTransferFound = true
- }
- }
- }
- }
- func waitQuotaScans() error {
- time.Sleep(100 * time.Millisecond)
- scans, _, err := httpd.GetQuotaScans(http.StatusOK)
- if err != nil {
- return err
- }
- for len(scans) > 0 {
- time.Sleep(100 * time.Millisecond)
- scans, _, err = httpd.GetQuotaScans(http.StatusOK)
- if err != nil {
- return err
- }
- }
- return nil
- }
- func initGitRepo(path string) ([]byte, error) {
- os.MkdirAll(path, 0777)
- args := []string{"init", "--bare"}
- cmd := exec.Command(gitPath, args...)
- cmd.Dir = path
- return cmd.CombinedOutput()
- }
- func pushToGitRepo(repoPath string) ([]byte, error) {
- cmd := exec.Command(gitPath, "push")
- cmd.Dir = repoPath
- cmd.Env = append(os.Environ(),
- fmt.Sprintf("GIT_SSH=%v", gitWrapPath))
- return cmd.CombinedOutput()
- }
- func cloneGitRepo(basePath, remotePath, username string) ([]byte, error) {
- remoteUrl := fmt.Sprintf("ssh://%[email protected]:2022%v", username, remotePath)
- args := []string{"clone", remoteUrl}
- cmd := exec.Command(gitPath, args...)
- cmd.Dir = basePath
- cmd.Env = append(os.Environ(),
- fmt.Sprintf("GIT_SSH=%v", gitWrapPath))
- return cmd.CombinedOutput()
- }
- func addFileToGitRepo(repoPath string, fileSize int64) ([]byte, error) {
- path := filepath.Join(repoPath, "test")
- err := createTestFile(path, fileSize)
- if err != nil {
- return []byte(""), err
- }
- cmd := exec.Command(gitPath, "config", "user.email", "[email protected]")
- cmd.Dir = repoPath
- out, err := cmd.CombinedOutput()
- if err != nil {
- return out, err
- }
- cmd = exec.Command(gitPath, "config", "user.name", "testuser")
- cmd.Dir = repoPath
- out, err = cmd.CombinedOutput()
- if err != nil {
- return out, err
- }
- cmd = exec.Command(gitPath, "add", "test")
- cmd.Dir = repoPath
- out, err = cmd.CombinedOutput()
- if err != nil {
- return out, err
- }
- cmd = exec.Command(gitPath, "commit", "-am", "test")
- cmd.Dir = repoPath
- return cmd.CombinedOutput()
- }
- func printLatestLogs(maxNumberOfLines int) {
- var lines []string
- f, err := os.Open(logFilePath)
- if err != nil {
- return
- }
- defer f.Close()
- scanner := bufio.NewScanner(f)
- for scanner.Scan() {
- lines = append(lines, scanner.Text()+"\r\n")
- for len(lines) > maxNumberOfLines {
- lines = lines[1:]
- }
- }
- if scanner.Err() != nil {
- logger.WarnToConsole("Unable to print latest logs: %v", scanner.Err())
- return
- }
- for _, line := range lines {
- logger.DebugToConsole(line)
- }
- }
|