| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116 |
- package sftpd_test
- import (
- "bufio"
- "bytes"
- "crypto/rand"
- "crypto/sha256"
- "crypto/sha512"
- "encoding/json"
- "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/drakkan/sftpgo/vfs"
- "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
- extAuthPath string
- keyIntAuthPath string
- preLoginPath 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:8083/"
- scriptArgs = "$@"
- }
- keyIntAuthPath = filepath.Join(homeBasePath, "keyintauth.sh")
- ioutil.WriteFile(keyIntAuthPath, getKeyboardInteractiveScriptContent([]string{"1", "2"}, 0, false, 1), 0755)
- sftpdConf.KeyboardInteractiveProgram = keyIntAuthPath
- 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")
- extAuthPath = filepath.Join(homeBasePath, "extauth.sh")
- preLoginPath = filepath.Join(homeBasePath, "prelogin.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))
- sftpdConf.BindPort = 2222
- sftpdConf.ProxyProtocol = 1
- 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)
- }
- }()
- waitTCPListening(fmt.Sprintf("%s:%d", sftpdConf.BindAddress, sftpdConf.BindPort))
- exitCode := m.Run()
- os.Remove(logFilePath)
- os.Remove(loginBannerFile)
- os.Remove(pubKeyPath)
- os.Remove(privateKeyPath)
- os.Remove(gitWrapPath)
- os.Remove(extAuthPath)
- os.Remove(preLoginPath)
- os.Remove(keyIntAuthPath)
- 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.Error("Inizialize must fail, a SFTP server should be already running")
- }
- sftpdConf.KeyboardInteractiveProgram = "invalid_file"
- err = sftpdConf.Initialize(configDir)
- if err == nil {
- t.Error("Inizialize must fail, a SFTP server should be already running")
- }
- sftpdConf.KeyboardInteractiveProgram = filepath.Join(homeBasePath, "invalid_file")
- err = sftpdConf.Initialize(configDir)
- if err == nil {
- t.Error("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 TestProxyProtocol(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 := getSftpClientWithAddr(user, usePubKey, "127.0.0.1:2222")
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- defer client.Close()
- _, err = client.Getwd()
- if err != nil {
- t.Errorf("error mkdir: %v", err)
- }
- }
- httpd.RemoveUser(user, http.StatusOK)
- 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 TestLoginInvalidFs(t *testing.T) {
- if runtime.GOOS == "windows" {
- t.Skip("this test is not available on Windows")
- }
- config.LoadConfig(configDir, "")
- providerConf := config.GetProviderConf()
- if providerConf.Driver != dataprovider.SQLiteDataProviderName {
- t.Skip("this test require sqlite provider")
- }
- dbPath := providerConf.Name
- if !filepath.IsAbs(dbPath) {
- dbPath = filepath.Join(configDir, dbPath)
- }
- usePubKey := true
- u := getTestUser(usePubKey)
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- // we update the database using sqlite3 CLI since we cannot add an user with an invalid config
- time.Sleep(200 * time.Millisecond)
- updateUserQuery := fmt.Sprintf("UPDATE users SET filesystem='{\"provider\":1}' WHERE id=%v", user.ID)
- cmd := exec.Command("sqlite3", dbPath, updateUserQuery)
- out, err := cmd.CombinedOutput()
- if err != nil {
- t.Errorf("unexpected error: %v, cmd out: %v", err, string(out))
- }
- time.Sleep(200 * time.Millisecond)
- _, err = getSftpClient(user, usePubKey)
- if err == nil {
- t.Error("login must fail, the user has an invalid filesystem config")
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- }
- func TestDeniedLoginMethods(t *testing.T) {
- u := getTestUser(true)
- u.Filters.DeniedLoginMethods = []string{dataprovider.SSHLoginMethodPublicKey, dataprovider.SSHLoginMethodPassword}
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- _, err = getSftpClient(user, true)
- if err == nil {
- t.Error("public key login is disabled, authentication must fail")
- }
- user.Filters.DeniedLoginMethods = []string{dataprovider.SSHLoginMethodKeyboardInteractive, dataprovider.SSHLoginMethodPassword}
- user, _, 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 {
- client.Close()
- }
- user.Password = defaultPassword
- user, _, err = httpd.UpdateUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to update user: %v", err)
- }
- _, err = getSftpClient(user, false)
- if err == nil {
- t.Error("password login is disabled, authentication must fail")
- }
- user.Filters.DeniedLoginMethods = []string{dataprovider.SSHLoginMethodKeyboardInteractive, dataprovider.SSHLoginMethodPublicKey}
- user, _, err = httpd.UpdateUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to update user: %v", err)
- }
- client, err = getSftpClient(user, false)
- if err != nil {
- t.Errorf("unable to create sftp client: %v", err)
- } else {
- 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 TestLoginKeyboardInteractiveAuth(t *testing.T) {
- if runtime.GOOS == "windows" {
- t.Skip("this test is not available on Windows")
- }
- user, _, err := httpd.AddUser(getTestUser(false), http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- ioutil.WriteFile(keyIntAuthPath, getKeyboardInteractiveScriptContent([]string{"1", "2"}, 0, false, 1), 0755)
- client, err := getKeyboardInteractiveSftpClient(user, []string{"1", "2"})
- 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)
- }
- }
- user.Status = 0
- user, _, err = httpd.UpdateUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("error updating user: %v", err)
- }
- client, err = getKeyboardInteractiveSftpClient(user, []string{"1", "2"})
- if err == nil {
- t.Error("keyboard interactive auth must fail the user is disabled")
- }
- user.Status = 1
- user, _, err = httpd.UpdateUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("error updating user: %v", err)
- }
- ioutil.WriteFile(keyIntAuthPath, getKeyboardInteractiveScriptContent([]string{"1", "2"}, 0, false, -1), 0755)
- client, err = getKeyboardInteractiveSftpClient(user, []string{"1", "2"})
- if err == nil {
- t.Error("keyboard interactive auth must fail the script returned -1")
- }
- ioutil.WriteFile(keyIntAuthPath, getKeyboardInteractiveScriptContent([]string{"1", "2"}, 0, true, 1), 0755)
- client, err = getKeyboardInteractiveSftpClient(user, []string{"1", "2"})
- if err == nil {
- t.Error("keyboard interactive auth must fail the script returned bad json")
- }
- ioutil.WriteFile(keyIntAuthPath, getKeyboardInteractiveScriptContent([]string{"1", "2"}, 5, true, 1), 0755)
- client, err = getKeyboardInteractiveSftpClient(user, []string{"1", "2"})
- if err == nil {
- t.Error("keyboard interactive auth must fail the script returned bad json")
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- }
- func TestPreLoginScript(t *testing.T) {
- if runtime.GOOS == "windows" {
- t.Skip("this test is not available on Windows")
- }
- usePubKey := true
- u := getTestUser(usePubKey)
- dataProvider := dataprovider.GetProvider()
- dataprovider.Close(dataProvider)
- config.LoadConfig(configDir, "")
- providerConf := config.GetProviderConf()
- ioutil.WriteFile(preLoginPath, getPreLoginScriptContent(u, false), 0755)
- providerConf.PreLoginProgram = preLoginPath
- err := dataprovider.Initialize(providerConf, configDir)
- if err != nil {
- t.Errorf("error initializing data provider")
- }
- httpd.SetDataProvider(dataprovider.GetProvider())
- sftpd.SetDataProvider(dataprovider.GetProvider())
- user, _, err := httpd.AddUser(u, http.StatusOK)
- if err != nil {
- t.Errorf("unable to add user: %v", err)
- }
- client, err := getSftpClient(u, 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)
- }
- }
- ioutil.WriteFile(preLoginPath, getPreLoginScriptContent(user, true), 0755)
- _, err = getSftpClient(u, usePubKey)
- if err == nil {
- t.Error("pre login script returned a non json response, login must fail")
- }
- user.Status = 0
- ioutil.WriteFile(preLoginPath, getPreLoginScriptContent(user, false), 0755)
- _, err = getSftpClient(u, usePubKey)
- if err == nil {
- t.Error("pre login script returned a disabled user, login must fail")
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- dataProvider = dataprovider.GetProvider()
- dataprovider.Close(dataProvider)
- config.LoadConfig(configDir, "")
- providerConf = config.GetProviderConf()
- err = dataprovider.Initialize(providerConf, configDir)
- if err != nil {
- t.Errorf("error initializing data provider")
- }
- httpd.SetDataProvider(dataprovider.GetProvider())
- sftpd.SetDataProvider(dataprovider.GetProvider())
- os.Remove(preLoginPath)
- }
- func TestLoginExternalAuthPwdAndPubKey(t *testing.T) {
- if runtime.GOOS == "windows" {
- t.Skip("this test is not available on Windows")
- }
- usePubKey := true
- u := getTestUser(usePubKey)
- u.QuotaFiles = 1000
- dataProvider := dataprovider.GetProvider()
- dataprovider.Close(dataProvider)
- config.LoadConfig(configDir, "")
- providerConf := config.GetProviderConf()
- ioutil.WriteFile(extAuthPath, getExtAuthScriptContent(u, 0, false), 0755)
- providerConf.ExternalAuthProgram = extAuthPath
- providerConf.ExternalAuthScope = 0
- err := dataprovider.Initialize(providerConf, configDir)
- if err != nil {
- t.Errorf("error initializing data provider")
- }
- httpd.SetDataProvider(dataprovider.GetProvider())
- sftpd.SetDataProvider(dataprovider.GetProvider())
- client, err := getSftpClient(u, 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)
- }
- os.Remove(testFilePath)
- }
- u.Username = defaultUsername + "1"
- client, err = getSftpClient(u, usePubKey)
- if err == nil {
- t.Error("external auth login with invalid user must fail")
- }
- usePubKey = false
- u = getTestUser(usePubKey)
- u.PublicKeys = []string{}
- ioutil.WriteFile(extAuthPath, getExtAuthScriptContent(u, 0, false), 0755)
- client, err = getSftpClient(u, 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)
- }
- }
- users, out, err := httpd.GetUsers(0, 0, defaultUsername, http.StatusOK)
- if err != nil {
- t.Errorf("unable to get users: %v, out: %v", err, string(out))
- }
- if len(users) != 1 {
- t.Errorf("number of users mismatch, expected: 1, actual: %v", len(users))
- }
- user := users[0]
- if len(user.PublicKeys) != 0 {
- t.Errorf("number of public keys mismatch, expected: 0, actual: %v", len(user.PublicKeys))
- }
- if user.UsedQuotaSize == 0 {
- t.Error("quota size must be > 0")
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- dataProvider = dataprovider.GetProvider()
- dataprovider.Close(dataProvider)
- config.LoadConfig(configDir, "")
- providerConf = config.GetProviderConf()
- err = dataprovider.Initialize(providerConf, configDir)
- if err != nil {
- t.Errorf("error initializing data provider")
- }
- httpd.SetDataProvider(dataprovider.GetProvider())
- sftpd.SetDataProvider(dataprovider.GetProvider())
- os.Remove(extAuthPath)
- }
- func TestLoginExternalAuthPwd(t *testing.T) {
- if runtime.GOOS == "windows" {
- t.Skip("this test is not available on Windows")
- }
- usePubKey := false
- u := getTestUser(usePubKey)
- dataProvider := dataprovider.GetProvider()
- dataprovider.Close(dataProvider)
- config.LoadConfig(configDir, "")
- providerConf := config.GetProviderConf()
- ioutil.WriteFile(extAuthPath, getExtAuthScriptContent(u, 0, false), 0755)
- providerConf.ExternalAuthProgram = extAuthPath
- providerConf.ExternalAuthScope = 1
- err := dataprovider.Initialize(providerConf, configDir)
- if err != nil {
- t.Errorf("error initializing data provider")
- }
- httpd.SetDataProvider(dataprovider.GetProvider())
- sftpd.SetDataProvider(dataprovider.GetProvider())
- client, err := getSftpClient(u, 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)
- }
- }
- u.Username = defaultUsername + "1"
- client, err = getSftpClient(u, usePubKey)
- if err == nil {
- t.Error("external auth login with invalid user must fail")
- }
- usePubKey = true
- u = getTestUser(usePubKey)
- client, err = getSftpClient(u, usePubKey)
- if err == nil {
- t.Error("external auth login with valid user but invalid auth scope must fail")
- }
- users, out, err := httpd.GetUsers(0, 0, defaultUsername, http.StatusOK)
- if err != nil {
- t.Errorf("unable to get users: %v, out: %v", err, string(out))
- }
- if len(users) != 1 {
- t.Errorf("number of users mismatch, expected: 1, actual: %v", len(users))
- }
- user := users[0]
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- dataProvider = dataprovider.GetProvider()
- dataprovider.Close(dataProvider)
- config.LoadConfig(configDir, "")
- providerConf = config.GetProviderConf()
- err = dataprovider.Initialize(providerConf, configDir)
- if err != nil {
- t.Errorf("error initializing data provider")
- }
- httpd.SetDataProvider(dataprovider.GetProvider())
- sftpd.SetDataProvider(dataprovider.GetProvider())
- os.Remove(extAuthPath)
- }
- func TestLoginExternalAuthPubKey(t *testing.T) {
- if runtime.GOOS == "windows" {
- t.Skip("this test is not available on Windows")
- }
- usePubKey := true
- u := getTestUser(usePubKey)
- dataProvider := dataprovider.GetProvider()
- dataprovider.Close(dataProvider)
- config.LoadConfig(configDir, "")
- providerConf := config.GetProviderConf()
- ioutil.WriteFile(extAuthPath, getExtAuthScriptContent(u, 0, false), 0755)
- providerConf.ExternalAuthProgram = extAuthPath
- providerConf.ExternalAuthScope = 2
- err := dataprovider.Initialize(providerConf, configDir)
- if err != nil {
- t.Errorf("error initializing data provider")
- }
- httpd.SetDataProvider(dataprovider.GetProvider())
- sftpd.SetDataProvider(dataprovider.GetProvider())
- client, err := getSftpClient(u, 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)
- }
- }
- u.Username = defaultUsername + "1"
- client, err = getSftpClient(u, usePubKey)
- if err == nil {
- t.Error("external auth login with invalid user must fail")
- }
- usePubKey = false
- u = getTestUser(usePubKey)
- client, err = getSftpClient(u, usePubKey)
- if err == nil {
- t.Error("external auth login with valid user but invalid auth scope must fail")
- }
- users, out, err := httpd.GetUsers(0, 0, defaultUsername, http.StatusOK)
- if err != nil {
- t.Errorf("unable to get users: %v, out: %v", err, string(out))
- }
- if len(users) != 1 {
- t.Errorf("number of users mismatch, expected: 1, actual: %v", len(users))
- }
- user := users[0]
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- dataProvider = dataprovider.GetProvider()
- dataprovider.Close(dataProvider)
- config.LoadConfig(configDir, "")
- providerConf = config.GetProviderConf()
- err = dataprovider.Initialize(providerConf, configDir)
- if err != nil {
- t.Errorf("error initializing data provider")
- }
- httpd.SetDataProvider(dataprovider.GetProvider())
- sftpd.SetDataProvider(dataprovider.GetProvider())
- os.Remove(extAuthPath)
- }
- func TestLoginExternalAuthInteractive(t *testing.T) {
- if runtime.GOOS == "windows" {
- t.Skip("this test is not available on Windows")
- }
- usePubKey := false
- u := getTestUser(usePubKey)
- dataProvider := dataprovider.GetProvider()
- dataprovider.Close(dataProvider)
- config.LoadConfig(configDir, "")
- providerConf := config.GetProviderConf()
- ioutil.WriteFile(extAuthPath, getExtAuthScriptContent(u, 0, false), 0755)
- providerConf.ExternalAuthProgram = extAuthPath
- providerConf.ExternalAuthScope = 4
- err := dataprovider.Initialize(providerConf, configDir)
- if err != nil {
- t.Errorf("error initializing data provider")
- }
- httpd.SetDataProvider(dataprovider.GetProvider())
- sftpd.SetDataProvider(dataprovider.GetProvider())
- ioutil.WriteFile(keyIntAuthPath, getKeyboardInteractiveScriptContent([]string{"1", "2"}, 0, false, 1), 0755)
- client, err := getKeyboardInteractiveSftpClient(u, []string{"1", "2"})
- 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)
- }
- }
- u.Username = defaultUsername + "1"
- client, err = getKeyboardInteractiveSftpClient(u, []string{"1", "2"})
- if err == nil {
- t.Error("external auth login with invalid user must fail")
- }
- usePubKey = true
- u = getTestUser(usePubKey)
- client, err = getSftpClient(u, usePubKey)
- if err == nil {
- t.Error("external auth login with valid user but invalid auth scope must fail")
- }
- users, out, err := httpd.GetUsers(0, 0, defaultUsername, http.StatusOK)
- if err != nil {
- t.Errorf("unable to get users: %v, out: %v", err, string(out))
- }
- if len(users) != 1 {
- t.Errorf("number of users mismatch, expected: 1, actual: %v", len(users))
- }
- user := users[0]
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- dataProvider = dataprovider.GetProvider()
- dataprovider.Close(dataProvider)
- config.LoadConfig(configDir, "")
- providerConf = config.GetProviderConf()
- err = dataprovider.Initialize(providerConf, configDir)
- if err != nil {
- t.Errorf("error initializing data provider")
- }
- httpd.SetDataProvider(dataprovider.GetProvider())
- sftpd.SetDataProvider(dataprovider.GetProvider())
- os.Remove(extAuthPath)
- }
- func TestLoginExternalAuthErrors(t *testing.T) {
- if runtime.GOOS == "windows" {
- t.Skip("this test is not available on Windows")
- }
- usePubKey := true
- u := getTestUser(usePubKey)
- dataProvider := dataprovider.GetProvider()
- dataprovider.Close(dataProvider)
- config.LoadConfig(configDir, "")
- providerConf := config.GetProviderConf()
- ioutil.WriteFile(extAuthPath, getExtAuthScriptContent(u, 0, true), 0755)
- providerConf.ExternalAuthProgram = extAuthPath
- providerConf.ExternalAuthScope = 0
- err := dataprovider.Initialize(providerConf, configDir)
- if err != nil {
- t.Errorf("error initializing data provider")
- }
- httpd.SetDataProvider(dataprovider.GetProvider())
- sftpd.SetDataProvider(dataprovider.GetProvider())
- _, err = getSftpClient(u, usePubKey)
- if err == nil {
- t.Error("login must fail, external auth returns a non json response")
- }
- usePubKey = false
- u = getTestUser(usePubKey)
- _, err = getSftpClient(u, usePubKey)
- if err == nil {
- t.Error("login must fail, external auth returns a non json response")
- }
- users, out, err := httpd.GetUsers(0, 0, defaultUsername, http.StatusOK)
- if err != nil {
- t.Errorf("unable to get users: %v, out: %v", err, string(out))
- }
- if len(users) != 0 {
- t.Errorf("number of users mismatch, expected: 0, actual: %v", len(users))
- }
- dataProvider = dataprovider.GetProvider()
- dataprovider.Close(dataProvider)
- config.LoadConfig(configDir, "")
- providerConf = config.GetProviderConf()
- err = dataprovider.Initialize(providerConf, configDir)
- if err != nil {
- t.Errorf("error initializing data provider")
- }
- httpd.SetDataProvider(dataprovider.GetProvider())
- sftpd.SetDataProvider(dataprovider.GetProvider())
- os.Remove(extAuthPath)
- }
- func TestQuotaDisabledError(t *testing.T) {
- dataProvider := dataprovider.GetProvider()
- dataprovider.Close(dataProvider)
- config.LoadConfig(configDir, "")
- providerConf := config.GetProviderConf()
- providerConf.TrackQuota = 0
- err := dataprovider.Initialize(providerConf, configDir)
- if err != nil {
- t.Errorf("error initializing data provider")
- }
- httpd.SetDataProvider(dataprovider.GetProvider())
- sftpd.SetDataProvider(dataprovider.GetProvider())
- usePubKey := false
- u := getTestUser(usePubKey)
- u.QuotaFiles = 10
- 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)
- }
- os.Remove(testFilePath)
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove: %v", err)
- }
- os.RemoveAll(user.GetHomeDir())
- dataProvider = dataprovider.GetProvider()
- dataprovider.Close(dataProvider)
- config.LoadConfig(configDir, "")
- providerConf = config.GetProviderConf()
- err = dataprovider.Initialize(providerConf, configDir)
- if err != nil {
- t.Errorf("error initializing data provider")
- }
- httpd.SetDataProvider(dataprovider.GetProvider())
- sftpd.SetDataProvider(dataprovider.GetProvider())
- }
- func TestMaxSessions(t *testing.T) {
- usePubKey := false
- u := getTestUser(usePubKey)
- u.Username += "1"
- 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 TestVirtualFolders(t *testing.T) {
- usePubKey := true
- u := getTestUser(usePubKey)
- mappedPath := filepath.Join(os.TempDir(), "vdir")
- vdirPath := "/vdir"
- u.VirtualFolders = append(u.VirtualFolders, vfs.VirtualFolder{
- VirtualPath: vdirPath,
- MappedPath: mappedPath,
- })
- os.MkdirAll(mappedPath, 0777)
- 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(131072)
- 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)
- }
- localDownloadPath := filepath.Join(homeBasePath, "test_download.dat")
- err = sftpUploadFile(testFilePath, path.Join(vdirPath, testFileName), testFileSize, client)
- if err != nil {
- t.Errorf("file upload error: %v", err)
- }
- err = sftpDownloadFile(path.Join(vdirPath, testFileName), localDownloadPath, testFileSize, client)
- if err != nil {
- t.Errorf("file download error: %v", err)
- }
- err = client.Rename(vdirPath, "new_name")
- if err == nil {
- t.Error("renaming a virtual folder must fail")
- }
- err = client.RemoveDirectory(vdirPath)
- if err == nil {
- t.Error("removing a virtual folder must fail")
- }
- err = client.Mkdir(vdirPath)
- if err == nil {
- t.Error("creating a virtual folder must fail")
- }
- err = client.Symlink(path.Join(vdirPath, testFileName), vdirPath)
- if err == nil {
- t.Error("symlink to a virtual folder 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())
- os.RemoveAll(mappedPath)
- }
- 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 TestPasswordsHashMD5CryptApr1(t *testing.T) {
- md5CryptPwd := "$apr1$OBWLeSme$WoJbB736e7kKxMBIAqilb1"
- 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)
- var path, rel string
- filesystems := []vfs.Fs{vfs.NewOsFs("", user.GetHomeDir(), user.VirtualFolders)}
- keyPrefix := strings.TrimPrefix(user.GetHomeDir(), "/") + "/"
- s3config := vfs.S3FsConfig{
- KeyPrefix: keyPrefix,
- }
- s3fs, _ := vfs.NewS3Fs("", user.GetHomeDir(), s3config)
- gcsConfig := vfs.GCSFsConfig{
- KeyPrefix: keyPrefix,
- }
- gcsfs, _ := vfs.NewGCSFs("", user.GetHomeDir(), gcsConfig)
- if runtime.GOOS != "windows" {
- filesystems = append(filesystems, s3fs, gcsfs)
- }
- for _, fs := range filesystems {
- path = filepath.Join(user.HomeDir, "/")
- rel = fs.GetRelativePath(path)
- if rel != "/" {
- t.Errorf("Unexpected relative path: %v fs: %v", rel, fs.Name())
- }
- path = filepath.Join(user.HomeDir, "//")
- rel = fs.GetRelativePath(path)
- if rel != "/" {
- t.Errorf("Unexpected relative path: %v fs: %v", rel, fs.Name())
- }
- path = filepath.Join(user.HomeDir, "../..")
- rel = fs.GetRelativePath(path)
- if rel != "/" {
- t.Errorf("Unexpected relative path: %v path: %v fs: %v", rel, path, fs.Name())
- }
- path = filepath.Join(user.HomeDir, "../../../../../")
- rel = fs.GetRelativePath(path)
- if rel != "/" {
- t.Errorf("Unexpected relative path: %v fs: %v", rel, fs.Name())
- }
- path = filepath.Join(user.HomeDir, "/..")
- rel = fs.GetRelativePath(path)
- if rel != "/" {
- t.Errorf("Unexpected relative path: %v path: %v fs: %v", rel, path, fs.Name())
- }
- path = filepath.Join(user.HomeDir, "/../../../..")
- rel = fs.GetRelativePath(path)
- if rel != "/" {
- t.Errorf("Unexpected relative path: %v fs: %v", rel, fs.Name())
- }
- path = filepath.Join(user.HomeDir, "")
- rel = fs.GetRelativePath(path)
- if rel != "/" {
- t.Errorf("Unexpected relative path: %v fs: %v", rel, fs.Name())
- }
- path = filepath.Join(user.HomeDir, ".")
- rel = fs.GetRelativePath(path)
- if rel != "/" {
- t.Errorf("Unexpected relative path: %v fs: %v", rel, fs.Name())
- }
- path = filepath.Join(user.HomeDir, "somedir")
- rel = fs.GetRelativePath(path)
- if rel != "/somedir" {
- t.Errorf("Unexpected relative path: %v fs: %v", rel, fs.Name())
- }
- path = filepath.Join(user.HomeDir, "/somedir/subdir")
- rel = fs.GetRelativePath(path)
- if rel != "/somedir/subdir" {
- t.Errorf("Unexpected relative path: %v fs: %v", rel, fs.Name())
- }
- }
- }
- func TestResolvePaths(t *testing.T) {
- user := getTestUser(true)
- var path, resolved string
- var err error
- filesystems := []vfs.Fs{vfs.NewOsFs("", user.GetHomeDir(), user.VirtualFolders)}
- keyPrefix := strings.TrimPrefix(user.GetHomeDir(), "/") + "/"
- s3config := vfs.S3FsConfig{
- KeyPrefix: keyPrefix,
- }
- os.MkdirAll(user.GetHomeDir(), 0777)
- s3fs, _ := vfs.NewS3Fs("", user.GetHomeDir(), s3config)
- gcsConfig := vfs.GCSFsConfig{
- KeyPrefix: keyPrefix,
- }
- gcsfs, _ := vfs.NewGCSFs("", user.GetHomeDir(), gcsConfig)
- if runtime.GOOS != "windows" {
- filesystems = append(filesystems, s3fs, gcsfs)
- }
- for _, fs := range filesystems {
- path = "/"
- resolved, _ = fs.ResolvePath(filepath.ToSlash(path))
- if resolved != fs.Join(user.GetHomeDir(), "/") {
- t.Errorf("Unexpected resolved path: %v for: %v, fs: %v", resolved, path, fs.Name())
- }
- path = "."
- resolved, _ = fs.ResolvePath(filepath.ToSlash(path))
- if resolved != fs.Join(user.GetHomeDir(), "/") {
- t.Errorf("Unexpected resolved path: %v for: %v, fs: %v", resolved, path, fs.Name())
- }
- path = "test/sub"
- resolved, _ = fs.ResolvePath(filepath.ToSlash(path))
- if resolved != fs.Join(user.GetHomeDir(), "/test/sub") {
- t.Errorf("Unexpected resolved path: %v for: %v, fs: %v", resolved, path, fs.Name())
- }
- path = "../test/sub"
- resolved, err = fs.ResolvePath(filepath.ToSlash(path))
- if vfs.IsLocalOsFs(fs) {
- if err == nil {
- t.Errorf("Unexpected resolved path: %v for: %v, fs: %v", resolved, path, fs.Name())
- }
- } else {
- if resolved != fs.Join(user.GetHomeDir(), "/test/sub") && err == nil {
- t.Errorf("Unexpected resolved path: %v for: %v, fs: %v", resolved, path, fs.Name())
- }
- }
- path = "../../../test/../sub"
- resolved, err = fs.ResolvePath(filepath.ToSlash(path))
- if vfs.IsLocalOsFs(fs) {
- if err == nil {
- t.Errorf("Unexpected resolved path: %v for: %v, fs: %v", resolved, path, fs.Name())
- }
- } else {
- if resolved != fs.Join(user.GetHomeDir(), "/sub") && err == nil {
- t.Errorf("Unexpected resolved path: %v for: %v, fs: %v", resolved, path, fs.Name())
- }
- }
- }
- os.RemoveAll(user.GetHomeDir())
- }
- func TestVirtualRelativePaths(t *testing.T) {
- user := getTestUser(true)
- mappedPath := filepath.Join(os.TempDir(), "vdir")
- vdirPath := "/vdir"
- user.VirtualFolders = append(user.VirtualFolders, vfs.VirtualFolder{
- VirtualPath: vdirPath,
- MappedPath: mappedPath,
- })
- os.MkdirAll(mappedPath, 0777)
- fs := vfs.NewOsFs("", user.GetHomeDir(), user.VirtualFolders)
- rel := fs.GetRelativePath(mappedPath)
- if rel != vdirPath {
- t.Errorf("Unexpected relative path: %v", rel)
- }
- rel = fs.GetRelativePath(filepath.Join(mappedPath, ".."))
- if rel != "/" {
- t.Errorf("Unexpected relative path: %v", rel)
- }
- // path outside home and virtual dir
- rel = fs.GetRelativePath(filepath.Join(mappedPath, "../vdir1"))
- if rel != "/" {
- t.Errorf("Unexpected relative path: %v", rel)
- }
- rel = fs.GetRelativePath(filepath.Join(mappedPath, "../vdir/file.txt"))
- if rel != "/vdir/file.txt" {
- t.Errorf("Unexpected relative path: %v", rel)
- }
- rel = fs.GetRelativePath(filepath.Join(user.HomeDir, "vdir1/file.txt"))
- if rel != "/vdir1/file.txt" {
- t.Errorf("Unexpected relative path: %v", rel)
- }
- }
- func TestResolveVirtualPaths(t *testing.T) {
- user := getTestUser(true)
- mappedPath := filepath.Join(os.TempDir(), "vdir")
- vdirPath := "/vdir"
- user.VirtualFolders = append(user.VirtualFolders, vfs.VirtualFolder{
- VirtualPath: vdirPath,
- MappedPath: mappedPath,
- })
- os.MkdirAll(mappedPath, 0777)
- fs := vfs.NewOsFs("", user.GetHomeDir(), user.VirtualFolders)
- osFs := fs.(*vfs.OsFs)
- b, f := osFs.GetFsPaths("/vdir/a.txt")
- if b != mappedPath {
- t.Errorf("unexpected base path: %#v expected: %#v", b, mappedPath)
- }
- if f != filepath.Join(mappedPath, "a.txt") {
- t.Errorf("unexpected fs path: %#v expected: %#v", f, filepath.Join(mappedPath, "a.txt"))
- }
- b, f = osFs.GetFsPaths("/vdir/sub with space & spécial chars/a.txt")
- if b != mappedPath {
- t.Errorf("unexpected base path: %#v expected: %#v", b, mappedPath)
- }
- if f != filepath.Join(mappedPath, "sub with space & spécial chars/a.txt") {
- t.Errorf("unexpected fs path: %#v expected: %#v", f, filepath.Join(mappedPath, "sub with space & spécial chars/a.txt"))
- }
- b, f = osFs.GetFsPaths("/vdir/../a.txt")
- if b != user.GetHomeDir() {
- t.Errorf("unexpected base path: %#v expected: %#v", b, user.GetHomeDir())
- }
- if f != filepath.Join(user.GetHomeDir(), "a.txt") {
- t.Errorf("unexpected fs path: %#v expected: %#v", f, filepath.Join(user.GetHomeDir(), "a.txt"))
- }
- b, f = osFs.GetFsPaths("/vdir1/a.txt")
- if b != user.GetHomeDir() {
- t.Errorf("unexpected base path: %#v expected: %#v", b, user.GetHomeDir())
- }
- if f != filepath.Join(user.GetHomeDir(), "/vdir1/a.txt") {
- t.Errorf("unexpected fs path: %#v expected: %#v", f, filepath.Join(user.GetHomeDir(), "/vdir1/a.txt"))
- }
- }
- 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, "/") {
- t.Error("expected permission not found")
- }
- if !user.HasPerm(dataprovider.PermListItems, ".") {
- t.Error("expected permission not found")
- }
- if !user.HasPerm(dataprovider.PermListItems, "") {
- t.Error("expected permission not found")
- }
- if !user.HasPerm(dataprovider.PermListItems, "../") {
- t.Error("expected permission not found")
- }
- // path p and /p are the same
- if !user.HasPerm(dataprovider.PermDelete, "/p") {
- t.Error("expected permission not found")
- }
- if !user.HasPerm(dataprovider.PermDownload, "/p/1") {
- t.Error("expected permission not found")
- }
- if !user.HasPerm(dataprovider.PermCreateDirs, "p/2") {
- t.Error("expected permission not found")
- }
- if !user.HasPerm(dataprovider.PermChmod, "/p/3") {
- t.Error("expected permission not found")
- }
- if !user.HasPerm(dataprovider.PermChtimes, "p/3/4/") {
- t.Error("expected permission not found")
- }
- if !user.HasPerm(dataprovider.PermChtimes, "p/3/4/../4") {
- t.Error("expected permission not found")
- }
- // undefined paths have permissions of the nearest path
- if !user.HasPerm(dataprovider.PermListItems, "/p34") {
- t.Error("expected permission not found")
- }
- if !user.HasPerm(dataprovider.PermListItems, "/p34/p1/file.dat") {
- t.Error("expected permission not found")
- }
- if !user.HasPerm(dataprovider.PermChtimes, "/p/3/4/5/6") {
- t.Error("expected permission not found")
- }
- if !user.HasPerm(dataprovider.PermDownload, "/p/1/test/file.dat") {
- t.Error("expected permission not found")
- }
- }
- func TestUserEmptySubDirPerms(t *testing.T) {
- user := getTestUser(true)
- user.Permissions = make(map[string][]string)
- user.Permissions["/emptyperms"] = []string{}
- for _, p := range dataprovider.ValidPerms {
- if user.HasPerm(p, "/emptyperms") {
- t.Errorf("unexpected permission %#v for dir /emptyperms", p)
- }
- }
- }
- func TestUserFiltersIPMaskConditions(t *testing.T) {
- user := getTestUser(true)
- // with no filter login must be allowed even if the remoteIP is invalid
- if !user.IsLoginFromAddrAllowed("192.168.1.5") {
- t.Error("unexpected login denied")
- }
- if !user.IsLoginFromAddrAllowed("invalid") {
- t.Error("unexpected login denied")
- }
- user.Filters.DeniedIP = append(user.Filters.DeniedIP, "192.168.1.0/24")
- if user.IsLoginFromAddrAllowed("192.168.1.5") {
- t.Error("unexpected login allowed")
- }
- if !user.IsLoginFromAddrAllowed("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.IsLoginFromAddrAllowed("192.168.1.5") {
- t.Error("unexpected login allowed")
- }
- if user.IsLoginFromAddrAllowed("192.168.3.6") {
- t.Error("unexpected login allowed")
- }
- user.Filters.DeniedIP = []string{}
- if !user.IsLoginFromAddrAllowed("192.168.1.5") {
- t.Error("unexpected login denied")
- }
- if user.IsLoginFromAddrAllowed("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.IsLoginFromAddrAllowed("192.168.5.255") {
- t.Error("unexpected login allowed")
- }
- if user.IsLoginFromAddrAllowed("172.16.1.2") {
- t.Error("unexpected login allowed")
- }
- if !user.IsLoginFromAddrAllowed("172.18.2.1") {
- t.Error("unexpected login denied")
- }
- user.Filters.AllowedIP = []string{"10.4.4.0/24"}
- if user.IsLoginFromAddrAllowed("10.5.4.2") {
- t.Error("unexpected login allowed")
- }
- if !user.IsLoginFromAddrAllowed("10.4.4.2") {
- t.Error("unexpected login denied")
- }
- if !user.IsLoginFromAddrAllowed("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: %v/%v",
- fi.Size(), testFileSize)
- }
- }
- 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: %v/%v",
- fi.Size(), testFileSize)
- }
- }
- 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)
- }
- // overwrite existing dir
- 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 TestSCPVirtualFolders(t *testing.T) {
- if len(scpPath) == 0 {
- t.Skip("scp command not found, unable to execute this test")
- }
- usePubKey := true
- u := getTestUser(usePubKey)
- mappedPath := filepath.Join(os.TempDir(), "vdir")
- vdirPath := "/vdir"
- u.VirtualFolders = append(u.VirtualFolders, vfs.VirtualFolder{
- VirtualPath: vdirPath,
- MappedPath: mappedPath,
- })
- os.MkdirAll(mappedPath, 0777)
- 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("/", vdirPath))
- remoteUpPath := fmt.Sprintf("%[email protected]:%v", user.Username, vdirPath)
- 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)
- }
- _, err = httpd.RemoveUser(user, http.StatusOK)
- if err != nil {
- t.Errorf("unable to remove user: %v", err)
- }
- os.RemoveAll(testBaseDirPath)
- os.RemoveAll(testBaseDirDownPath)
- os.RemoveAll(user.GetHomeDir())
- os.RemoveAll(mappedPath)
- }
- 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 getSftpClientWithAddr(user dataprovider.User, usePubKey bool, addr string) (*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", addr, config)
- if err != nil {
- return sftpClient, err
- }
- sftpClient, err = sftp.NewClient(conn)
- return sftpClient, err
- }
- func getSftpClient(user dataprovider.User, usePubKey bool) (*sftp.Client, error) {
- return getSftpClientWithAddr(user, usePubKey, sftpServerAddr)
- }
- func getKeyboardInteractiveSftpClient(user dataprovider.User, answers []string) (*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
- },
- Auth: []ssh.AuthMethod{
- ssh.KeyboardInteractive(func(user, instruction string, questions []string, echos []bool) ([]string, error) {
- return answers, nil
- }),
- },
- }
- 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 getKeyboardInteractiveScriptContent(questions []string, sleepTime int, nonJsonResponse bool, result int) []byte {
- content := []byte("#!/bin/sh\n\n")
- q, _ := json.Marshal(questions)
- echos := []bool{}
- for index := range questions {
- echos = append(echos, index%2 == 0)
- }
- e, _ := json.Marshal(echos)
- if nonJsonResponse {
- content = append(content, []byte(fmt.Sprintf("echo 'questions: %v echos: %v\n", string(q), string(e)))...)
- } else {
- content = append(content, []byte(fmt.Sprintf("echo '{\"questions\":%v,\"echos\":%v}'\n", string(q), string(e)))...)
- }
- for index := range questions {
- content = append(content, []byte(fmt.Sprintf("read ANSWER%v\n", index))...)
- }
- if sleepTime > 0 {
- content = append(content, []byte(fmt.Sprintf("sleep %v\n", sleepTime))...)
- }
- content = append(content, []byte(fmt.Sprintf("echo '{\"auth_result\":%v}'\n", result))...)
- return content
- }
- func getExtAuthScriptContent(user dataprovider.User, sleepTime int, nonJsonResponse bool) []byte {
- extAuthContent := []byte("#!/bin/sh\n\n")
- u, _ := json.Marshal(user)
- extAuthContent = append(extAuthContent, []byte(fmt.Sprintf("if test \"$SFTPGO_AUTHD_USERNAME\" = \"%v\"; then\n", user.Username))...)
- if nonJsonResponse {
- extAuthContent = append(extAuthContent, []byte("echo 'text response'\n")...)
- } else {
- extAuthContent = append(extAuthContent, []byte(fmt.Sprintf("echo '%v'\n", string(u)))...)
- }
- extAuthContent = append(extAuthContent, []byte("else\n")...)
- if nonJsonResponse {
- extAuthContent = append(extAuthContent, []byte("echo 'text response'\n")...)
- } else {
- extAuthContent = append(extAuthContent, []byte("echo '{\"username\":\"\"}'\n")...)
- }
- extAuthContent = append(extAuthContent, []byte("fi\n")...)
- if sleepTime > 0 {
- extAuthContent = append(extAuthContent, []byte(fmt.Sprintf("sleep %v\n", sleepTime))...)
- }
- return extAuthContent
- }
- func getPreLoginScriptContent(user dataprovider.User, nonJsonResponse bool) []byte {
- content := []byte("#!/bin/sh\n\n")
- if nonJsonResponse {
- content = append(content, []byte("echo 'text response'\n")...)
- return content
- }
- if len(user.Username) > 0 {
- u, _ := json.Marshal(user)
- content = append(content, []byte(fmt.Sprintf("echo '%v'\n", string(u)))...)
- }
- return content
- }
- 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)
- }
- }
|