http_cache_unittest.cc 509 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "net/http/http_cache.h"
  5. #include <stdint.h>
  6. #include <algorithm>
  7. #include <memory>
  8. #include <set>
  9. #include <utility>
  10. #include <vector>
  11. #include "base/bind.h"
  12. #include "base/callback_helpers.h"
  13. #include "base/containers/cxx20_erase.h"
  14. #include "base/files/scoped_temp_dir.h"
  15. #include "base/format_macros.h"
  16. #include "base/memory/ptr_util.h"
  17. #include "base/memory/raw_ptr.h"
  18. #include "base/run_loop.h"
  19. #include "base/strings/strcat.h"
  20. #include "base/strings/string_number_conversions.h"
  21. #include "base/strings/string_util.h"
  22. #include "base/strings/stringprintf.h"
  23. #include "base/test/metrics/histogram_tester.h"
  24. #include "base/test/scoped_feature_list.h"
  25. #include "base/test/simple_test_clock.h"
  26. #include "base/time/time.h"
  27. #include "base/trace_event/memory_allocator_dump.h"
  28. #include "base/trace_event/memory_dump_request_args.h"
  29. #include "base/trace_event/process_memory_dump.h"
  30. #include "base/trace_event/traced_value.h"
  31. #include "net/base/cache_type.h"
  32. #include "net/base/completion_repeating_callback.h"
  33. #include "net/base/elements_upload_data_stream.h"
  34. #include "net/base/features.h"
  35. #include "net/base/host_port_pair.h"
  36. #include "net/base/ip_address.h"
  37. #include "net/base/ip_endpoint.h"
  38. #include "net/base/load_flags.h"
  39. #include "net/base/load_timing_info.h"
  40. #include "net/base/load_timing_info_test_util.h"
  41. #include "net/base/net_errors.h"
  42. #include "net/base/schemeful_site.h"
  43. #include "net/base/upload_bytes_element_reader.h"
  44. #include "net/cert/cert_status_flags.h"
  45. #include "net/cert/x509_certificate.h"
  46. #include "net/disk_cache/disk_cache.h"
  47. #include "net/http/http_byte_range.h"
  48. #include "net/http/http_cache_transaction.h"
  49. #include "net/http/http_request_headers.h"
  50. #include "net/http/http_request_info.h"
  51. #include "net/http/http_response_headers.h"
  52. #include "net/http/http_response_info.h"
  53. #include "net/http/http_transaction.h"
  54. #include "net/http/http_transaction_test_util.h"
  55. #include "net/http/http_util.h"
  56. #include "net/http/mock_http_cache.h"
  57. #include "net/log/net_log_event_type.h"
  58. #include "net/log/net_log_source.h"
  59. #include "net/log/net_log_with_source.h"
  60. #include "net/log/test_net_log.h"
  61. #include "net/log/test_net_log_util.h"
  62. #include "net/socket/client_socket_handle.h"
  63. #include "net/ssl/ssl_cert_request_info.h"
  64. #include "net/ssl/ssl_connection_status_flags.h"
  65. #include "net/test/cert_test_util.h"
  66. #include "net/test/gtest_util.h"
  67. #include "net/test/test_data_directory.h"
  68. #include "net/test/test_with_task_environment.h"
  69. #include "net/websockets/websocket_handshake_stream_base.h"
  70. #include "testing/gmock/include/gmock/gmock.h"
  71. #include "testing/gtest/include/gtest/gtest.h"
  72. #include "url/origin.h"
  73. using net::test::IsError;
  74. using net::test::IsOk;
  75. using testing::AllOf;
  76. using testing::ByRef;
  77. using testing::Contains;
  78. using testing::ElementsAre;
  79. using testing::Eq;
  80. using testing::Field;
  81. using testing::Gt;
  82. using testing::IsEmpty;
  83. using testing::NotNull;
  84. using base::Time;
  85. namespace net {
  86. using CacheEntryStatus = HttpResponseInfo::CacheEntryStatus;
  87. class WebSocketEndpointLockManager;
  88. namespace {
  89. // Returns a simple text serialization of the given
  90. // |HttpResponseHeaders|. This is used by tests to verify that an
  91. // |HttpResponseHeaders| matches an expectation string.
  92. //
  93. // * One line per header, written as:
  94. // HEADER_NAME: HEADER_VALUE\n
  95. // * The original case of header names is preserved.
  96. // * Whitespace around head names/values is stripped.
  97. // * Repeated headers are not aggregated.
  98. // * Headers are listed in their original order.
  99. // TODO(tfarina): this is a duplicate function from
  100. // http_response_headers_unittest.cc:ToSimpleString(). Figure out how to merge
  101. // them. crbug.com/488593
  102. std::string ToSimpleString(const scoped_refptr<HttpResponseHeaders>& parsed) {
  103. std::string result = parsed->GetStatusLine() + "\n";
  104. size_t iter = 0;
  105. std::string name;
  106. std::string value;
  107. while (parsed->EnumerateHeaderLines(&iter, &name, &value)) {
  108. std::string new_line = name + ": " + value + "\n";
  109. result += new_line;
  110. }
  111. return result;
  112. }
  113. // Tests the load timing values of a request that goes through a
  114. // MockNetworkTransaction.
  115. void TestLoadTimingNetworkRequest(const LoadTimingInfo& load_timing_info) {
  116. EXPECT_FALSE(load_timing_info.socket_reused);
  117. EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id);
  118. EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null());
  119. EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null());
  120. ExpectConnectTimingHasTimes(load_timing_info.connect_timing,
  121. CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY);
  122. EXPECT_LE(load_timing_info.connect_timing.connect_end,
  123. load_timing_info.send_start);
  124. EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end);
  125. // Set by URLRequest / URLRequestHttpJob, at a higher level.
  126. EXPECT_TRUE(load_timing_info.request_start_time.is_null());
  127. EXPECT_TRUE(load_timing_info.request_start.is_null());
  128. EXPECT_TRUE(load_timing_info.receive_headers_end.is_null());
  129. }
  130. // Tests the load timing values of a request that receives a cached response.
  131. void TestLoadTimingCachedResponse(const LoadTimingInfo& load_timing_info) {
  132. EXPECT_FALSE(load_timing_info.socket_reused);
  133. EXPECT_EQ(NetLogSource::kInvalidId, load_timing_info.socket_log_id);
  134. EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null());
  135. EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null());
  136. ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing);
  137. // Only the send start / end times should be sent, and they should have the
  138. // same value.
  139. EXPECT_FALSE(load_timing_info.send_start.is_null());
  140. EXPECT_EQ(load_timing_info.send_start, load_timing_info.send_end);
  141. // Set by URLRequest / URLRequestHttpJob, at a higher level.
  142. EXPECT_TRUE(load_timing_info.request_start_time.is_null());
  143. EXPECT_TRUE(load_timing_info.request_start.is_null());
  144. EXPECT_TRUE(load_timing_info.receive_headers_end.is_null());
  145. }
  146. void DeferCallback(bool* defer) {
  147. *defer = true;
  148. }
  149. class DeleteCacheCompletionCallback : public TestCompletionCallbackBase {
  150. public:
  151. explicit DeleteCacheCompletionCallback(std::unique_ptr<MockHttpCache> cache)
  152. : cache_(std::move(cache)) {}
  153. DeleteCacheCompletionCallback(const DeleteCacheCompletionCallback&) = delete;
  154. DeleteCacheCompletionCallback& operator=(
  155. const DeleteCacheCompletionCallback&) = delete;
  156. CompletionOnceCallback callback() {
  157. return base::BindOnce(&DeleteCacheCompletionCallback::OnComplete,
  158. base::Unretained(this));
  159. }
  160. private:
  161. void OnComplete(int result) {
  162. cache_.reset();
  163. SetResult(result);
  164. }
  165. std::unique_ptr<MockHttpCache> cache_;
  166. };
  167. //-----------------------------------------------------------------------------
  168. // helpers
  169. void ReadAndVerifyTransaction(HttpTransaction* trans,
  170. const MockTransaction& trans_info) {
  171. std::string content;
  172. int rv = ReadTransaction(trans, &content);
  173. EXPECT_THAT(rv, IsOk());
  174. std::string expected(trans_info.data);
  175. EXPECT_EQ(expected, content);
  176. }
  177. void ReadRemainingAndVerifyTransaction(HttpTransaction* trans,
  178. const std::string& already_read,
  179. const MockTransaction& trans_info) {
  180. std::string content;
  181. int rv = ReadTransaction(trans, &content);
  182. EXPECT_THAT(rv, IsOk());
  183. std::string expected(trans_info.data);
  184. EXPECT_EQ(expected, already_read + content);
  185. }
  186. void RunTransactionTestBase(HttpCache* cache,
  187. const MockTransaction& trans_info,
  188. const MockHttpRequest& request,
  189. HttpResponseInfo* response_info,
  190. const NetLogWithSource& net_log,
  191. LoadTimingInfo* load_timing_info,
  192. int64_t* sent_bytes,
  193. int64_t* received_bytes,
  194. IPEndPoint* remote_endpoint) {
  195. TestCompletionCallback callback;
  196. // write to the cache
  197. std::unique_ptr<HttpTransaction> trans;
  198. int rv = cache->CreateTransaction(DEFAULT_PRIORITY, &trans);
  199. EXPECT_THAT(rv, IsOk());
  200. ASSERT_TRUE(trans.get());
  201. rv = trans->Start(&request, callback.callback(), net_log);
  202. if (rv == ERR_IO_PENDING)
  203. rv = callback.WaitForResult();
  204. ASSERT_EQ(trans_info.start_return_code, rv);
  205. if (OK != rv)
  206. return;
  207. const HttpResponseInfo* response = trans->GetResponseInfo();
  208. ASSERT_TRUE(response);
  209. if (response_info)
  210. *response_info = *response;
  211. if (load_timing_info) {
  212. // If a fake network connection is used, need a NetLog to get a fake socket
  213. // ID.
  214. EXPECT_TRUE(net_log.net_log());
  215. *load_timing_info = LoadTimingInfo();
  216. trans->GetLoadTimingInfo(load_timing_info);
  217. }
  218. if (remote_endpoint)
  219. ASSERT_TRUE(trans->GetRemoteEndpoint(remote_endpoint));
  220. ReadAndVerifyTransaction(trans.get(), trans_info);
  221. if (sent_bytes)
  222. *sent_bytes = trans->GetTotalSentBytes();
  223. if (received_bytes)
  224. *received_bytes = trans->GetTotalReceivedBytes();
  225. }
  226. void RunTransactionTestWithRequest(HttpCache* cache,
  227. const MockTransaction& trans_info,
  228. const MockHttpRequest& request,
  229. HttpResponseInfo* response_info) {
  230. RunTransactionTestBase(cache, trans_info, request, response_info,
  231. NetLogWithSource(), nullptr, nullptr, nullptr,
  232. nullptr);
  233. }
  234. void RunTransactionTestAndGetTiming(HttpCache* cache,
  235. const MockTransaction& trans_info,
  236. const NetLogWithSource& log,
  237. LoadTimingInfo* load_timing_info) {
  238. RunTransactionTestBase(cache, trans_info, MockHttpRequest(trans_info),
  239. nullptr, log, load_timing_info, nullptr, nullptr,
  240. nullptr);
  241. }
  242. void RunTransactionTestAndGetTimingAndConnectedSocketAddress(
  243. HttpCache* cache,
  244. const MockTransaction& trans_info,
  245. const NetLogWithSource& log,
  246. LoadTimingInfo* load_timing_info,
  247. IPEndPoint* remote_endpoint) {
  248. RunTransactionTestBase(cache, trans_info, MockHttpRequest(trans_info),
  249. nullptr, log, load_timing_info, nullptr, nullptr,
  250. remote_endpoint);
  251. }
  252. void RunTransactionTest(HttpCache* cache, const MockTransaction& trans_info) {
  253. RunTransactionTestAndGetTiming(cache, trans_info, NetLogWithSource(),
  254. nullptr);
  255. }
  256. void RunTransactionTestWithLog(HttpCache* cache,
  257. const MockTransaction& trans_info,
  258. const NetLogWithSource& log) {
  259. RunTransactionTestAndGetTiming(cache, trans_info, log, nullptr);
  260. }
  261. void RunTransactionTestWithResponseInfo(HttpCache* cache,
  262. const MockTransaction& trans_info,
  263. HttpResponseInfo* response) {
  264. RunTransactionTestWithRequest(cache, trans_info, MockHttpRequest(trans_info),
  265. response);
  266. }
  267. void RunTransactionTestWithResponseInfoAndGetTiming(
  268. HttpCache* cache,
  269. const MockTransaction& trans_info,
  270. HttpResponseInfo* response,
  271. const NetLogWithSource& log,
  272. LoadTimingInfo* load_timing_info) {
  273. RunTransactionTestBase(cache, trans_info, MockHttpRequest(trans_info),
  274. response, log, load_timing_info, nullptr, nullptr,
  275. nullptr);
  276. }
  277. void RunTransactionTestWithResponse(HttpCache* cache,
  278. const MockTransaction& trans_info,
  279. std::string* response_headers) {
  280. HttpResponseInfo response;
  281. RunTransactionTestWithResponseInfo(cache, trans_info, &response);
  282. *response_headers = ToSimpleString(response.headers);
  283. }
  284. void RunTransactionTestWithResponseAndGetTiming(
  285. HttpCache* cache,
  286. const MockTransaction& trans_info,
  287. std::string* response_headers,
  288. const NetLogWithSource& log,
  289. LoadTimingInfo* load_timing_info) {
  290. HttpResponseInfo response;
  291. RunTransactionTestBase(cache, trans_info, MockHttpRequest(trans_info),
  292. &response, log, load_timing_info, nullptr, nullptr,
  293. nullptr);
  294. *response_headers = ToSimpleString(response.headers);
  295. }
  296. // This class provides a handler for kFastNoStoreGET_Transaction so that the
  297. // no-store header can be included on demand.
  298. class FastTransactionServer {
  299. public:
  300. FastTransactionServer() {
  301. no_store = false;
  302. }
  303. FastTransactionServer(const FastTransactionServer&) = delete;
  304. FastTransactionServer& operator=(const FastTransactionServer&) = delete;
  305. ~FastTransactionServer() = default;
  306. void set_no_store(bool value) { no_store = value; }
  307. static void FastNoStoreHandler(const HttpRequestInfo* request,
  308. std::string* response_status,
  309. std::string* response_headers,
  310. std::string* response_data) {
  311. if (no_store)
  312. *response_headers = "Cache-Control: no-store\n";
  313. }
  314. private:
  315. static bool no_store;
  316. };
  317. bool FastTransactionServer::no_store;
  318. const MockTransaction kFastNoStoreGET_Transaction = {
  319. "http://www.google.com/nostore",
  320. "GET",
  321. base::Time(),
  322. "",
  323. LOAD_VALIDATE_CACHE,
  324. DefaultTransportInfo(),
  325. "HTTP/1.1 200 OK",
  326. "Cache-Control: max-age=10000\n",
  327. base::Time(),
  328. "<html><body>Google Blah Blah</body></html>",
  329. {},
  330. TEST_MODE_SYNC_NET_START,
  331. &FastTransactionServer::FastNoStoreHandler,
  332. nullptr,
  333. nullptr,
  334. 0,
  335. 0,
  336. OK,
  337. };
  338. // This class provides a handler for kRangeGET_TransactionOK so that the range
  339. // request can be served on demand.
  340. class RangeTransactionServer {
  341. public:
  342. RangeTransactionServer() {
  343. not_modified_ = false;
  344. modified_ = false;
  345. bad_200_ = false;
  346. redirect_ = false;
  347. length_ = 80;
  348. }
  349. RangeTransactionServer(const RangeTransactionServer&) = delete;
  350. RangeTransactionServer& operator=(const RangeTransactionServer&) = delete;
  351. ~RangeTransactionServer() {
  352. not_modified_ = false;
  353. modified_ = false;
  354. bad_200_ = false;
  355. redirect_ = false;
  356. length_ = 80;
  357. }
  358. // Returns only 416 or 304 when set.
  359. void set_not_modified(bool value) { not_modified_ = value; }
  360. // Returns 206 when revalidating a range (instead of 304).
  361. void set_modified(bool value) { modified_ = value; }
  362. // Returns 200 instead of 206 (a malformed response overall).
  363. void set_bad_200(bool value) { bad_200_ = value; }
  364. // Sets how long the resource is. (Default is 80)
  365. void set_length(int64_t length) { length_ = length; }
  366. // Sets whether to return a 301 instead of normal return.
  367. void set_redirect(bool redirect) { redirect_ = redirect; }
  368. // Other than regular range related behavior (and the flags mentioned above),
  369. // the server reacts to requests headers like so:
  370. // X-Require-Mock-Auth -> return 401.
  371. // X-Require-Mock-Auth-Alt -> return 401.
  372. // X-Return-Default-Range -> assume 40-49 was requested.
  373. // The -Alt variant doesn't cause the MockNetworkTransaction to
  374. // report that it IsReadyToRestartForAuth().
  375. static void RangeHandler(const HttpRequestInfo* request,
  376. std::string* response_status,
  377. std::string* response_headers,
  378. std::string* response_data);
  379. private:
  380. static bool not_modified_;
  381. static bool modified_;
  382. static bool bad_200_;
  383. static bool redirect_;
  384. static int64_t length_;
  385. };
  386. bool RangeTransactionServer::not_modified_ = false;
  387. bool RangeTransactionServer::modified_ = false;
  388. bool RangeTransactionServer::bad_200_ = false;
  389. bool RangeTransactionServer::redirect_ = false;
  390. int64_t RangeTransactionServer::length_ = 80;
  391. // A dummy extra header that must be preserved on a given request.
  392. // EXTRA_HEADER_LINE doesn't include a line terminator because it
  393. // will be passed to AddHeaderFromString() which doesn't accept them.
  394. #define EXTRA_HEADER_LINE "Extra: header"
  395. // EXTRA_HEADER contains a line terminator, as expected by
  396. // AddHeadersFromString() (_not_ AddHeaderFromString()).
  397. #define EXTRA_HEADER EXTRA_HEADER_LINE "\r\n"
  398. static const char kExtraHeaderKey[] = "Extra";
  399. // Static.
  400. void RangeTransactionServer::RangeHandler(const HttpRequestInfo* request,
  401. std::string* response_status,
  402. std::string* response_headers,
  403. std::string* response_data) {
  404. if (request->extra_headers.IsEmpty()) {
  405. response_status->assign("HTTP/1.1 416 Requested Range Not Satisfiable");
  406. response_data->clear();
  407. return;
  408. }
  409. // We want to make sure we don't delete extra headers.
  410. EXPECT_TRUE(request->extra_headers.HasHeader(kExtraHeaderKey));
  411. bool require_auth =
  412. request->extra_headers.HasHeader("X-Require-Mock-Auth") ||
  413. request->extra_headers.HasHeader("X-Require-Mock-Auth-Alt");
  414. if (require_auth && !request->extra_headers.HasHeader("Authorization")) {
  415. response_status->assign("HTTP/1.1 401 Unauthorized");
  416. response_data->assign("WWW-Authenticate: Foo\n");
  417. return;
  418. }
  419. if (redirect_) {
  420. response_status->assign("HTTP/1.1 301 Moved Permanently");
  421. response_headers->assign("Location: /elsewhere\nContent-Length: 5");
  422. response_data->assign("12345");
  423. return;
  424. }
  425. if (not_modified_) {
  426. response_status->assign("HTTP/1.1 304 Not Modified");
  427. response_data->clear();
  428. return;
  429. }
  430. std::vector<HttpByteRange> ranges;
  431. std::string range_header;
  432. if (!request->extra_headers.GetHeader(HttpRequestHeaders::kRange,
  433. &range_header) ||
  434. !HttpUtil::ParseRangeHeader(range_header, &ranges) || bad_200_ ||
  435. ranges.size() != 1 ||
  436. (modified_ && request->extra_headers.HasHeader("If-Range"))) {
  437. // This is not a byte range request, or a failed If-Range. We return 200.
  438. response_status->assign("HTTP/1.1 200 OK");
  439. response_headers->assign("Date: Wed, 28 Nov 2007 09:40:09 GMT");
  440. response_data->assign("Not a range");
  441. return;
  442. }
  443. // We can handle this range request.
  444. HttpByteRange byte_range = ranges[0];
  445. if (request->extra_headers.HasHeader("X-Return-Default-Range")) {
  446. byte_range.set_first_byte_position(40);
  447. byte_range.set_last_byte_position(49);
  448. }
  449. if (byte_range.first_byte_position() >= length_) {
  450. response_status->assign("HTTP/1.1 416 Requested Range Not Satisfiable");
  451. response_data->clear();
  452. return;
  453. }
  454. EXPECT_TRUE(byte_range.ComputeBounds(length_));
  455. int64_t start = byte_range.first_byte_position();
  456. int64_t end = byte_range.last_byte_position();
  457. EXPECT_LT(end, length_);
  458. std::string content_range = base::StringPrintf("Content-Range: bytes %" PRId64
  459. "-%" PRId64 "/%" PRId64 "\n",
  460. start, end, length_);
  461. response_headers->append(content_range);
  462. if (!request->extra_headers.HasHeader("If-None-Match") || modified_) {
  463. std::string data;
  464. if (end == start) {
  465. EXPECT_EQ(0, end % 10);
  466. data = "r";
  467. } else {
  468. EXPECT_EQ(9, (end - start) % 10);
  469. for (int64_t block_start = start; block_start < end; block_start += 10) {
  470. base::StringAppendF(&data, "rg: %02" PRId64 "-%02" PRId64 " ",
  471. block_start % 100, (block_start + 9) % 100);
  472. }
  473. }
  474. *response_data = data;
  475. if (end - start != 9) {
  476. // We also have to fix content-length.
  477. int64_t len = end - start + 1;
  478. std::string content_length =
  479. base::StringPrintf("Content-Length: %" PRId64 "\n", len);
  480. response_headers->replace(response_headers->find("Content-Length:"),
  481. content_length.size(), content_length);
  482. }
  483. } else {
  484. response_status->assign("HTTP/1.1 304 Not Modified");
  485. response_data->clear();
  486. }
  487. }
  488. const MockTransaction kRangeGET_TransactionOK = {
  489. "http://www.google.com/range",
  490. "GET",
  491. base::Time(),
  492. "Range: bytes = 40-49\r\n" EXTRA_HEADER,
  493. LOAD_NORMAL,
  494. DefaultTransportInfo(),
  495. "HTTP/1.1 206 Partial Content",
  496. "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
  497. "ETag: \"foo\"\n"
  498. "Accept-Ranges: bytes\n"
  499. "Content-Length: 10\n",
  500. base::Time(),
  501. "rg: 40-49 ",
  502. {},
  503. TEST_MODE_NORMAL,
  504. &RangeTransactionServer::RangeHandler,
  505. nullptr,
  506. nullptr,
  507. 0,
  508. 0,
  509. OK,
  510. };
  511. const char kFullRangeData[] =
  512. "rg: 00-09 rg: 10-19 rg: 20-29 rg: 30-39 "
  513. "rg: 40-49 rg: 50-59 rg: 60-69 rg: 70-79 ";
  514. // Verifies the response headers (|response|) match a partial content
  515. // response for the range starting at |start| and ending at |end|.
  516. void Verify206Response(const std::string& response, int start, int end) {
  517. auto headers = base::MakeRefCounted<HttpResponseHeaders>(
  518. HttpUtil::AssembleRawHeaders(response));
  519. ASSERT_EQ(206, headers->response_code());
  520. int64_t range_start, range_end, object_size;
  521. ASSERT_TRUE(
  522. headers->GetContentRangeFor206(&range_start, &range_end, &object_size));
  523. int64_t content_length = headers->GetContentLength();
  524. int length = end - start + 1;
  525. ASSERT_EQ(length, content_length);
  526. ASSERT_EQ(start, range_start);
  527. ASSERT_EQ(end, range_end);
  528. }
  529. // Creates a truncated entry that can be resumed using byte ranges.
  530. void CreateTruncatedEntry(std::string raw_headers, MockHttpCache* cache) {
  531. // Create a disk cache entry that stores an incomplete resource.
  532. disk_cache::Entry* entry;
  533. MockHttpRequest request(kRangeGET_TransactionOK);
  534. ASSERT_TRUE(cache->CreateBackendEntry(request.CacheKey(), &entry, nullptr));
  535. HttpResponseInfo response;
  536. response.response_time = base::Time::Now();
  537. response.request_time = base::Time::Now();
  538. response.headers = base::MakeRefCounted<HttpResponseHeaders>(
  539. HttpUtil::AssembleRawHeaders(raw_headers));
  540. // Set the last argument for this to be an incomplete request.
  541. EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, true));
  542. scoped_refptr<IOBuffer> buf = base::MakeRefCounted<IOBuffer>(100);
  543. int len = static_cast<int>(base::strlcpy(buf->data(),
  544. "rg: 00-09 rg: 10-19 ", 100));
  545. TestCompletionCallback cb;
  546. int rv = entry->WriteData(1, 0, buf.get(), len, cb.callback(), true);
  547. EXPECT_EQ(len, cb.GetResult(rv));
  548. entry->Close();
  549. }
  550. // Verifies that there's an entry with this |key| with the truncated flag set to
  551. // |flag_value|, and with an optional |data_size| (if not zero).
  552. void VerifyTruncatedFlag(MockHttpCache* cache,
  553. const std::string& key,
  554. bool flag_value,
  555. int data_size) {
  556. disk_cache::Entry* entry;
  557. ASSERT_TRUE(cache->OpenBackendEntry(key, &entry));
  558. disk_cache::ScopedEntryPtr closer(entry);
  559. HttpResponseInfo response;
  560. bool truncated = !flag_value;
  561. EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated));
  562. EXPECT_EQ(flag_value, truncated);
  563. if (data_size)
  564. EXPECT_EQ(data_size, entry->GetDataSize(1));
  565. }
  566. // Helper to represent a network HTTP response.
  567. struct Response {
  568. // Set this response into |trans|.
  569. void AssignTo(MockTransaction* trans) const {
  570. trans->status = status;
  571. trans->response_headers = headers;
  572. trans->data = body;
  573. }
  574. std::string status_and_headers() const {
  575. return std::string(status) + "\n" + std::string(headers);
  576. }
  577. const char* status;
  578. const char* headers;
  579. const char* body;
  580. };
  581. struct Context {
  582. Context() = default;
  583. int result = ERR_IO_PENDING;
  584. TestCompletionCallback callback;
  585. std::unique_ptr<HttpTransaction> trans;
  586. };
  587. class FakeWebSocketHandshakeStreamCreateHelper
  588. : public WebSocketHandshakeStreamBase::CreateHelper {
  589. public:
  590. ~FakeWebSocketHandshakeStreamCreateHelper() override = default;
  591. std::unique_ptr<WebSocketHandshakeStreamBase> CreateBasicStream(
  592. std::unique_ptr<ClientSocketHandle> connect,
  593. bool using_proxy,
  594. WebSocketEndpointLockManager* websocket_endpoint_lock_manager) override {
  595. return nullptr;
  596. }
  597. std::unique_ptr<WebSocketHandshakeStreamBase> CreateHttp2Stream(
  598. base::WeakPtr<SpdySession> session,
  599. std::set<std::string> dns_aliases) override {
  600. NOTREACHED();
  601. return nullptr;
  602. }
  603. };
  604. // Returns true if |entry| is not one of the log types paid attention to in this
  605. // test. Note that HTTP_CACHE_WRITE_INFO and HTTP_CACHE_*_DATA are
  606. // ignored.
  607. bool ShouldIgnoreLogEntry(const NetLogEntry& entry) {
  608. switch (entry.type) {
  609. case NetLogEventType::HTTP_CACHE_GET_BACKEND:
  610. case NetLogEventType::HTTP_CACHE_OPEN_OR_CREATE_ENTRY:
  611. case NetLogEventType::HTTP_CACHE_OPEN_ENTRY:
  612. case NetLogEventType::HTTP_CACHE_CREATE_ENTRY:
  613. case NetLogEventType::HTTP_CACHE_ADD_TO_ENTRY:
  614. case NetLogEventType::HTTP_CACHE_DOOM_ENTRY:
  615. case NetLogEventType::HTTP_CACHE_READ_INFO:
  616. return false;
  617. default:
  618. return true;
  619. }
  620. }
  621. // Gets the entries from |net_log| created by the cache layer and asserted on in
  622. // these tests.
  623. std::vector<NetLogEntry> GetFilteredNetLogEntries(
  624. const RecordingNetLogObserver& net_log_observer) {
  625. auto entries = net_log_observer.GetEntries();
  626. base::EraseIf(entries, ShouldIgnoreLogEntry);
  627. return entries;
  628. }
  629. bool LogContainsEventType(const RecordingNetLogObserver& net_log_observer,
  630. NetLogEventType expected) {
  631. return !net_log_observer.GetEntriesWithType(expected).empty();
  632. }
  633. // Returns a TransportInfo distinct from the default for mock transactions,
  634. // with the given port number.
  635. TransportInfo TestTransportInfoWithPort(uint16_t port) {
  636. TransportInfo result;
  637. result.endpoint = IPEndPoint(IPAddress(42, 0, 1, 2), port);
  638. return result;
  639. }
  640. // Returns a TransportInfo distinct from the default for mock transactions.
  641. TransportInfo TestTransportInfo() {
  642. return TestTransportInfoWithPort(1337);
  643. }
  644. TransportInfo CachedTestTransportInfo() {
  645. TransportInfo result = TestTransportInfo();
  646. result.type = TransportType::kCached;
  647. return result;
  648. }
  649. // Helper function, generating valid HTTP cache key from `url`.
  650. // See also: HttpCache::GenerateCacheKey(..)
  651. std::string GenerateCacheKey(const std::string& url) {
  652. return "1/0/" + url;
  653. }
  654. } // namespace
  655. using HttpCacheTest = TestWithTaskEnvironment;
  656. class HttpCacheIOCallbackTest : public HttpCacheTest {
  657. public:
  658. HttpCacheIOCallbackTest() = default;
  659. ~HttpCacheIOCallbackTest() override = default;
  660. // HttpCache::ActiveEntry is private, doing this allows tests to use it
  661. using ActiveEntry = HttpCache::ActiveEntry;
  662. using Transaction = HttpCache::Transaction;
  663. // The below functions are forwarding calls to the HttpCache class.
  664. int OpenEntry(HttpCache* cache,
  665. const std::string& url,
  666. HttpCache::ActiveEntry** entry,
  667. HttpCache::Transaction* trans) {
  668. return cache->OpenEntry(GenerateCacheKey(url), entry, trans);
  669. }
  670. int OpenOrCreateEntry(HttpCache* cache,
  671. const std::string& url,
  672. HttpCache::ActiveEntry** entry,
  673. HttpCache::Transaction* trans) {
  674. return cache->OpenOrCreateEntry(GenerateCacheKey(url), entry, trans);
  675. }
  676. int CreateEntry(HttpCache* cache,
  677. const std::string& url,
  678. HttpCache::ActiveEntry** entry,
  679. HttpCache::Transaction* trans) {
  680. return cache->CreateEntry(GenerateCacheKey(url), entry, trans);
  681. }
  682. int DoomEntry(HttpCache* cache,
  683. const std::string& url,
  684. HttpCache::Transaction* trans) {
  685. return cache->DoomEntry(GenerateCacheKey(url), trans);
  686. }
  687. void DeactivateEntry(HttpCache* cache, ActiveEntry* entry) {
  688. cache->DeactivateEntry(entry);
  689. }
  690. };
  691. class HttpSplitCacheKeyTest : public HttpCacheTest {
  692. public:
  693. HttpSplitCacheKeyTest() = default;
  694. ~HttpSplitCacheKeyTest() override = default;
  695. std::string ComputeCacheKey(const std::string& url_string) {
  696. GURL url(url_string);
  697. SchemefulSite site(url);
  698. net::HttpRequestInfo request_info;
  699. request_info.url = url;
  700. request_info.method = "GET";
  701. request_info.network_isolation_key = net::NetworkIsolationKey(site, site);
  702. MockHttpCache cache;
  703. return *cache.http_cache()->GenerateCacheKeyForRequest(&request_info);
  704. }
  705. };
  706. //-----------------------------------------------------------------------------
  707. // Tests.
  708. TEST_F(HttpCacheTest, CreateThenDestroy) {
  709. MockHttpCache cache;
  710. std::unique_ptr<HttpTransaction> trans;
  711. EXPECT_THAT(cache.CreateTransaction(&trans), IsOk());
  712. ASSERT_TRUE(trans.get());
  713. }
  714. TEST_F(HttpCacheTest, GetBackend) {
  715. MockHttpCache cache(HttpCache::DefaultBackend::InMemory(0));
  716. disk_cache::Backend* backend;
  717. TestCompletionCallback cb;
  718. // This will lazily initialize the backend.
  719. int rv = cache.http_cache()->GetBackend(&backend, cb.callback());
  720. EXPECT_THAT(cb.GetResult(rv), IsOk());
  721. }
  722. TEST_F(HttpCacheTest, SimpleGET) {
  723. MockHttpCache cache;
  724. LoadTimingInfo load_timing_info;
  725. // Write to the cache.
  726. RunTransactionTestAndGetTiming(cache.http_cache(), kSimpleGET_Transaction,
  727. NetLogWithSource::Make(NetLogSourceType::NONE),
  728. &load_timing_info);
  729. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  730. EXPECT_EQ(0, cache.disk_cache()->open_count());
  731. EXPECT_EQ(1, cache.disk_cache()->create_count());
  732. TestLoadTimingNetworkRequest(load_timing_info);
  733. }
  734. // This test verifies that the callback passed to SetConnectedCallback() is
  735. // called once for simple GET calls that traverse the cache.
  736. TEST_F(HttpCacheTest, SimpleGET_ConnectedCallback) {
  737. MockHttpCache cache;
  738. ScopedMockTransaction mock_transaction(kSimpleGET_Transaction);
  739. mock_transaction.transport_info = TestTransportInfo();
  740. MockHttpRequest request(mock_transaction);
  741. ConnectedHandler connected_handler;
  742. std::unique_ptr<HttpTransaction> transaction;
  743. EXPECT_THAT(cache.CreateTransaction(&transaction), IsOk());
  744. ASSERT_THAT(transaction, NotNull());
  745. transaction->SetConnectedCallback(connected_handler.Callback());
  746. TestCompletionCallback callback;
  747. ASSERT_THAT(
  748. transaction->Start(&request, callback.callback(), NetLogWithSource()),
  749. IsError(ERR_IO_PENDING));
  750. EXPECT_THAT(callback.WaitForResult(), IsOk());
  751. EXPECT_THAT(connected_handler.transports(), ElementsAre(TestTransportInfo()));
  752. }
  753. // This test verifies that when the callback passed to SetConnectedCallback()
  754. // returns an error, the transaction fails with that error.
  755. TEST_F(HttpCacheTest, SimpleGET_ConnectedCallbackReturnError) {
  756. MockHttpCache cache;
  757. MockHttpRequest request(kSimpleGET_Transaction);
  758. ConnectedHandler connected_handler;
  759. std::unique_ptr<HttpTransaction> transaction;
  760. EXPECT_THAT(cache.CreateTransaction(&transaction), IsOk());
  761. ASSERT_THAT(transaction, NotNull());
  762. // The exact error code does not matter. We only care that it is passed to
  763. // the transaction's completion callback unmodified.
  764. connected_handler.set_result(ERR_NOT_IMPLEMENTED);
  765. transaction->SetConnectedCallback(connected_handler.Callback());
  766. TestCompletionCallback callback;
  767. ASSERT_THAT(
  768. transaction->Start(&request, callback.callback(), NetLogWithSource()),
  769. IsError(ERR_IO_PENDING));
  770. EXPECT_THAT(callback.WaitForResult(), IsError(ERR_NOT_IMPLEMENTED));
  771. }
  772. // This test verifies that the callback passed to SetConnectedCallback() is
  773. // called once for requests that hit the cache.
  774. TEST_F(HttpCacheTest, SimpleGET_ConnectedCallbackOnCacheHit) {
  775. MockHttpCache cache;
  776. {
  777. // Populate the cache.
  778. ScopedMockTransaction mock_transaction(kSimpleGET_Transaction);
  779. mock_transaction.transport_info = TestTransportInfo();
  780. RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
  781. }
  782. // Establish a baseline.
  783. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  784. // Load from the cache (only), observe the callback being called.
  785. ConnectedHandler connected_handler;
  786. MockHttpRequest request(kSimpleGET_Transaction);
  787. std::unique_ptr<HttpTransaction> transaction;
  788. EXPECT_THAT(cache.CreateTransaction(&transaction), IsOk());
  789. ASSERT_THAT(transaction, NotNull());
  790. transaction->SetConnectedCallback(connected_handler.Callback());
  791. TestCompletionCallback callback;
  792. ASSERT_THAT(
  793. transaction->Start(&request, callback.callback(), NetLogWithSource()),
  794. IsError(ERR_IO_PENDING));
  795. EXPECT_THAT(callback.WaitForResult(), IsOk());
  796. // Still only 1 transaction for the previous request. The connected callback
  797. // was not called by a second network transaction.
  798. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  799. EXPECT_THAT(connected_handler.transports(),
  800. ElementsAre(CachedTestTransportInfo()));
  801. }
  802. // This test verifies that when the callback passed to SetConnectedCallback()
  803. // is called for a request that hit the cache and returns an error, the cache
  804. // entry is reusable.
  805. TEST_F(HttpCacheTest, SimpleGET_ConnectedCallbackOnCacheHitReturnError) {
  806. MockHttpCache cache;
  807. {
  808. // Populate the cache.
  809. ScopedMockTransaction mock_transaction(kSimpleGET_Transaction);
  810. mock_transaction.transport_info = TestTransportInfo();
  811. RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
  812. }
  813. MockHttpRequest request(kSimpleGET_Transaction);
  814. {
  815. // Attempt to read from cache entry, but abort transaction due to a
  816. // connected callback error.
  817. ConnectedHandler connected_handler;
  818. connected_handler.set_result(ERR_FAILED);
  819. std::unique_ptr<HttpTransaction> transaction;
  820. EXPECT_THAT(cache.CreateTransaction(&transaction), IsOk());
  821. ASSERT_THAT(transaction, NotNull());
  822. transaction->SetConnectedCallback(connected_handler.Callback());
  823. TestCompletionCallback callback;
  824. ASSERT_THAT(
  825. transaction->Start(&request, callback.callback(), NetLogWithSource()),
  826. IsError(ERR_IO_PENDING));
  827. EXPECT_THAT(callback.WaitForResult(), IsError(ERR_FAILED));
  828. // Used the cache entry only.
  829. EXPECT_THAT(connected_handler.transports(),
  830. ElementsAre(CachedTestTransportInfo()));
  831. }
  832. {
  833. // Request the same resource once more, observe that it is read from cache.
  834. ConnectedHandler connected_handler;
  835. std::unique_ptr<HttpTransaction> transaction;
  836. EXPECT_THAT(cache.CreateTransaction(&transaction), IsOk());
  837. ASSERT_THAT(transaction, NotNull());
  838. transaction->SetConnectedCallback(connected_handler.Callback());
  839. TestCompletionCallback callback;
  840. ASSERT_THAT(
  841. transaction->Start(&request, callback.callback(), NetLogWithSource()),
  842. IsError(ERR_IO_PENDING));
  843. EXPECT_THAT(callback.WaitForResult(), IsOk());
  844. // Used the cache entry only.
  845. EXPECT_THAT(connected_handler.transports(),
  846. ElementsAre(CachedTestTransportInfo()));
  847. }
  848. }
  849. // This test verifies that when the callback passed to SetConnectedCallback()
  850. // returns `ERR_INCONSISTENT_IP_ADDRESS_SPACE`, the cache entry is invalidated.
  851. TEST_F(HttpCacheTest,
  852. SimpleGET_ConnectedCallbackOnCacheHitReturnInconsistentIpError) {
  853. MockHttpCache cache;
  854. ScopedMockTransaction mock_transaction(kSimpleGET_Transaction);
  855. mock_transaction.transport_info = TestTransportInfo();
  856. // Populate the cache.
  857. RunTransactionTest(cache.http_cache(), mock_transaction);
  858. MockHttpRequest request(kSimpleGET_Transaction);
  859. {
  860. // Attempt to read from cache entry, but abort transaction due to a
  861. // connected callback error.
  862. ConnectedHandler connected_handler;
  863. connected_handler.set_result(ERR_INCONSISTENT_IP_ADDRESS_SPACE);
  864. std::unique_ptr<HttpTransaction> transaction;
  865. EXPECT_THAT(cache.CreateTransaction(&transaction), IsOk());
  866. ASSERT_THAT(transaction, NotNull());
  867. transaction->SetConnectedCallback(connected_handler.Callback());
  868. TestCompletionCallback callback;
  869. ASSERT_THAT(
  870. transaction->Start(&request, callback.callback(), NetLogWithSource()),
  871. IsError(ERR_IO_PENDING));
  872. EXPECT_THAT(callback.WaitForResult(),
  873. IsError(ERR_INCONSISTENT_IP_ADDRESS_SPACE));
  874. // Used the cache entry only.
  875. EXPECT_THAT(connected_handler.transports(),
  876. ElementsAre(CachedTestTransportInfo()));
  877. }
  878. {
  879. // Request the same resource once more, observe that it is not read from
  880. // cache.
  881. ConnectedHandler connected_handler;
  882. std::unique_ptr<HttpTransaction> transaction;
  883. EXPECT_THAT(cache.CreateTransaction(&transaction), IsOk());
  884. ASSERT_THAT(transaction, NotNull());
  885. transaction->SetConnectedCallback(connected_handler.Callback());
  886. TestCompletionCallback callback;
  887. ASSERT_THAT(
  888. transaction->Start(&request, callback.callback(), NetLogWithSource()),
  889. IsError(ERR_IO_PENDING));
  890. EXPECT_THAT(callback.WaitForResult(), IsOk());
  891. // Used the network only.
  892. EXPECT_THAT(connected_handler.transports(),
  893. ElementsAre(TestTransportInfo()));
  894. }
  895. }
  896. // This test verifies that when the callback passed to SetConnectedCallback()
  897. // returns
  898. // `ERR_CACHED_IP_ADDRESS_SPACE_BLOCKED_BY_PRIVATE_NETWORK_ACCESS_POLICY`, the
  899. // cache entry is invalidated, and we'll retry the connection from the network.
  900. TEST_F(
  901. HttpCacheTest,
  902. SimpleGET_ConnectedCallbackOnCacheHitReturnPrivateNetworkAccessBlockedError) {
  903. MockHttpCache cache;
  904. ScopedMockTransaction mock_transaction(kSimpleGET_Transaction);
  905. mock_transaction.transport_info = TestTransportInfo();
  906. // Populate the cache.
  907. RunTransactionTest(cache.http_cache(), mock_transaction);
  908. MockHttpRequest request(kSimpleGET_Transaction);
  909. {
  910. // Attempt to read from cache entry, but abort transaction due to a
  911. // connected callback error.
  912. ConnectedHandler connected_handler;
  913. connected_handler.set_result(
  914. ERR_CACHED_IP_ADDRESS_SPACE_BLOCKED_BY_PRIVATE_NETWORK_ACCESS_POLICY);
  915. std::unique_ptr<HttpTransaction> transaction;
  916. EXPECT_THAT(cache.CreateTransaction(&transaction), IsOk());
  917. ASSERT_THAT(transaction, NotNull());
  918. transaction->SetConnectedCallback(connected_handler.Callback());
  919. TestCompletionCallback callback;
  920. ASSERT_THAT(
  921. transaction->Start(&request, callback.callback(), NetLogWithSource()),
  922. IsError(ERR_IO_PENDING));
  923. EXPECT_THAT(
  924. callback.WaitForResult(),
  925. IsError(
  926. ERR_CACHED_IP_ADDRESS_SPACE_BLOCKED_BY_PRIVATE_NETWORK_ACCESS_POLICY));
  927. // Used the cache entry only.
  928. EXPECT_THAT(connected_handler.transports(),
  929. ElementsAre(CachedTestTransportInfo(), TestTransportInfo()));
  930. }
  931. {
  932. // Request the same resource once more, observe that it is not read from
  933. // cache.
  934. ConnectedHandler connected_handler;
  935. std::unique_ptr<HttpTransaction> transaction;
  936. EXPECT_THAT(cache.CreateTransaction(&transaction), IsOk());
  937. ASSERT_THAT(transaction, NotNull());
  938. transaction->SetConnectedCallback(connected_handler.Callback());
  939. TestCompletionCallback callback;
  940. ASSERT_THAT(
  941. transaction->Start(&request, callback.callback(), NetLogWithSource()),
  942. IsError(ERR_IO_PENDING));
  943. EXPECT_THAT(callback.WaitForResult(), IsOk());
  944. // Used the network only.
  945. EXPECT_THAT(connected_handler.transports(),
  946. ElementsAre(TestTransportInfo()));
  947. }
  948. }
  949. // This test verifies that the callback passed to SetConnectedCallback() is
  950. // called with the right transport type when the cached entry was originally
  951. // fetched via proxy.
  952. TEST_F(HttpCacheTest, SimpleGET_ConnectedCallbackOnCacheHitFromProxy) {
  953. MockHttpCache cache;
  954. TransportInfo proxied_transport_info = TestTransportInfo();
  955. proxied_transport_info.type = TransportType::kProxied;
  956. {
  957. // Populate the cache.
  958. ScopedMockTransaction mock_transaction(kSimpleGET_Transaction);
  959. mock_transaction.transport_info = proxied_transport_info;
  960. RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
  961. }
  962. // Establish a baseline.
  963. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  964. // Load from the cache (only), observe the callback being called.
  965. ConnectedHandler connected_handler;
  966. MockHttpRequest request(kSimpleGET_Transaction);
  967. std::unique_ptr<HttpTransaction> transaction;
  968. EXPECT_THAT(cache.CreateTransaction(&transaction), IsOk());
  969. ASSERT_THAT(transaction, NotNull());
  970. transaction->SetConnectedCallback(connected_handler.Callback());
  971. TestCompletionCallback callback;
  972. ASSERT_THAT(
  973. transaction->Start(&request, callback.callback(), NetLogWithSource()),
  974. IsError(ERR_IO_PENDING));
  975. EXPECT_THAT(callback.WaitForResult(), IsOk());
  976. // Still only 1 transaction for the previous request. The connected callback
  977. // was not called by a second network transaction.
  978. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  979. // The transport info mentions both the cache and the original proxy.
  980. TransportInfo expected_transport_info = TestTransportInfo();
  981. expected_transport_info.type = TransportType::kCachedFromProxy;
  982. EXPECT_THAT(connected_handler.transports(),
  983. ElementsAre(expected_transport_info));
  984. }
  985. class HttpCacheTest_SplitCacheFeature
  986. : public HttpCacheTest,
  987. public ::testing::WithParamInterface<bool> {
  988. public:
  989. void SetUp() override {
  990. feature_list_.InitWithFeatureState(
  991. net::features::kSplitCacheByNetworkIsolationKey, GetParam());
  992. HttpCacheTest::SetUp();
  993. }
  994. private:
  995. base::test::ScopedFeatureList feature_list_;
  996. };
  997. TEST_P(HttpCacheTest_SplitCacheFeature, SimpleGETVerifyGoogleFontMetrics) {
  998. base::HistogramTester histograms;
  999. const std::string histogram_name = "WebFont.HttpCacheStatus_roboto";
  1000. SchemefulSite site_a(GURL("http://www.a.com"));
  1001. MockHttpCache cache;
  1002. MockTransaction transaction(kSimpleGET_Transaction);
  1003. transaction.url = "http://themes.googleusercontent.com/static/fonts/roboto";
  1004. AddMockTransaction(&transaction);
  1005. MockHttpRequest request(transaction);
  1006. request.network_isolation_key = NetworkIsolationKey(site_a, site_a);
  1007. // Attempt to populate the cache.
  1008. RunTransactionTestWithRequest(cache.http_cache(), transaction, request,
  1009. nullptr);
  1010. histograms.ExpectUniqueSample(
  1011. histogram_name, static_cast<int>(CacheEntryStatus::ENTRY_NOT_IN_CACHE),
  1012. 1);
  1013. RunTransactionTestWithRequest(cache.http_cache(), transaction, request,
  1014. nullptr);
  1015. histograms.ExpectBucketCount(
  1016. histogram_name, static_cast<int>(CacheEntryStatus::ENTRY_USED), 1);
  1017. }
  1018. INSTANTIATE_TEST_SUITE_P(
  1019. All,
  1020. HttpCacheTest_SplitCacheFeature,
  1021. testing::Bool());
  1022. TEST_F(HttpCacheTest, SimpleGETNoDiskCache) {
  1023. MockHttpCache cache;
  1024. cache.disk_cache()->set_fail_requests(true);
  1025. RecordingNetLogObserver net_log_observer;
  1026. LoadTimingInfo load_timing_info;
  1027. // Read from the network, and don't use the cache.
  1028. RunTransactionTestAndGetTiming(cache.http_cache(), kSimpleGET_Transaction,
  1029. NetLogWithSource::Make(NetLogSourceType::NONE),
  1030. &load_timing_info);
  1031. // Check that the NetLog was filled as expected.
  1032. // (We attempted to OpenOrCreate entries, but fail).
  1033. auto entries = GetFilteredNetLogEntries(net_log_observer);
  1034. EXPECT_EQ(4u, entries.size());
  1035. EXPECT_TRUE(LogContainsBeginEvent(entries, 0,
  1036. NetLogEventType::HTTP_CACHE_GET_BACKEND));
  1037. EXPECT_TRUE(
  1038. LogContainsEndEvent(entries, 1, NetLogEventType::HTTP_CACHE_GET_BACKEND));
  1039. EXPECT_TRUE(LogContainsBeginEvent(
  1040. entries, 2, NetLogEventType::HTTP_CACHE_OPEN_OR_CREATE_ENTRY));
  1041. EXPECT_TRUE(LogContainsEndEvent(
  1042. entries, 3, NetLogEventType::HTTP_CACHE_OPEN_OR_CREATE_ENTRY));
  1043. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  1044. EXPECT_EQ(0, cache.disk_cache()->open_count());
  1045. EXPECT_EQ(0, cache.disk_cache()->create_count());
  1046. TestLoadTimingNetworkRequest(load_timing_info);
  1047. }
  1048. TEST_F(HttpCacheTest, SimpleGETNoDiskCache2) {
  1049. // This will initialize a cache object with NULL backend.
  1050. auto factory = std::make_unique<MockBlockingBackendFactory>();
  1051. factory->set_fail(true);
  1052. factory->FinishCreation(); // We'll complete synchronously.
  1053. MockHttpCache cache(std::move(factory));
  1054. // Read from the network, and don't use the cache.
  1055. RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
  1056. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  1057. EXPECT_FALSE(cache.http_cache()->GetCurrentBackend());
  1058. }
  1059. // Tests that IOBuffers are not referenced after IO completes.
  1060. TEST_F(HttpCacheTest, ReleaseBuffer) {
  1061. MockHttpCache cache;
  1062. // Write to the cache.
  1063. RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
  1064. MockHttpRequest request(kSimpleGET_Transaction);
  1065. std::unique_ptr<HttpTransaction> trans;
  1066. ASSERT_THAT(cache.CreateTransaction(&trans), IsOk());
  1067. const int kBufferSize = 10;
  1068. scoped_refptr<IOBuffer> buffer = base::MakeRefCounted<IOBuffer>(kBufferSize);
  1069. ReleaseBufferCompletionCallback cb(buffer.get());
  1070. int rv = trans->Start(&request, cb.callback(), NetLogWithSource());
  1071. EXPECT_THAT(cb.GetResult(rv), IsOk());
  1072. rv = trans->Read(buffer.get(), kBufferSize, cb.callback());
  1073. EXPECT_EQ(kBufferSize, cb.GetResult(rv));
  1074. }
  1075. TEST_F(HttpCacheTest, SimpleGETWithDiskFailures) {
  1076. MockHttpCache cache;
  1077. cache.disk_cache()->set_soft_failures_mask(MockDiskEntry::FAIL_ALL);
  1078. // Read from the network, and fail to write to the cache.
  1079. RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
  1080. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  1081. EXPECT_EQ(0, cache.disk_cache()->open_count());
  1082. EXPECT_EQ(1, cache.disk_cache()->create_count());
  1083. // This one should see an empty cache again.
  1084. RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
  1085. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  1086. EXPECT_EQ(0, cache.disk_cache()->open_count());
  1087. EXPECT_EQ(2, cache.disk_cache()->create_count());
  1088. }
  1089. // Tests that disk failures after the transaction has started don't cause the
  1090. // request to fail.
  1091. TEST_F(HttpCacheTest, SimpleGETWithDiskFailures2) {
  1092. MockHttpCache cache;
  1093. MockHttpRequest request(kSimpleGET_Transaction);
  1094. auto c = std::make_unique<Context>();
  1095. int rv = cache.CreateTransaction(&c->trans);
  1096. ASSERT_THAT(rv, IsOk());
  1097. rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
  1098. EXPECT_THAT(rv, IsError(ERR_IO_PENDING));
  1099. rv = c->callback.WaitForResult();
  1100. // Start failing request now.
  1101. cache.disk_cache()->set_soft_failures_mask(MockDiskEntry::FAIL_ALL);
  1102. // We have to open the entry again to propagate the failure flag.
  1103. disk_cache::Entry* en;
  1104. ASSERT_TRUE(cache.OpenBackendEntry(request.CacheKey(), &en));
  1105. en->Close();
  1106. ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
  1107. c.reset();
  1108. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  1109. EXPECT_EQ(1, cache.disk_cache()->open_count());
  1110. EXPECT_EQ(1, cache.disk_cache()->create_count());
  1111. // This one should see an empty cache again.
  1112. RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
  1113. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  1114. EXPECT_EQ(1, cache.disk_cache()->open_count());
  1115. EXPECT_EQ(2, cache.disk_cache()->create_count());
  1116. }
  1117. // Tests that we handle failures to read from the cache.
  1118. TEST_F(HttpCacheTest, SimpleGETWithDiskFailures3) {
  1119. MockHttpCache cache;
  1120. // Read from the network, and write to the cache.
  1121. RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
  1122. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  1123. EXPECT_EQ(0, cache.disk_cache()->open_count());
  1124. EXPECT_EQ(1, cache.disk_cache()->create_count());
  1125. cache.disk_cache()->set_soft_failures_mask(MockDiskEntry::FAIL_ALL);
  1126. MockHttpRequest request(kSimpleGET_Transaction);
  1127. // Now fail to read from the cache.
  1128. auto c = std::make_unique<Context>();
  1129. int rv = cache.CreateTransaction(&c->trans);
  1130. ASSERT_THAT(rv, IsOk());
  1131. rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
  1132. EXPECT_THAT(c->callback.GetResult(rv), IsOk());
  1133. // Now verify that the entry was removed from the cache.
  1134. cache.disk_cache()->set_soft_failures_mask(0);
  1135. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  1136. EXPECT_EQ(1, cache.disk_cache()->open_count());
  1137. EXPECT_EQ(2, cache.disk_cache()->create_count());
  1138. RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
  1139. EXPECT_EQ(3, cache.network_layer()->transaction_count());
  1140. EXPECT_EQ(1, cache.disk_cache()->open_count());
  1141. EXPECT_EQ(3, cache.disk_cache()->create_count());
  1142. }
  1143. TEST_F(HttpCacheTest, SimpleGET_LoadOnlyFromCache_Hit) {
  1144. MockHttpCache cache;
  1145. RecordingNetLogObserver net_log_observer;
  1146. NetLogWithSource net_log_with_source =
  1147. NetLogWithSource::Make(NetLogSourceType::NONE);
  1148. LoadTimingInfo load_timing_info;
  1149. // Write to the cache.
  1150. RunTransactionTestAndGetTiming(cache.http_cache(), kSimpleGET_Transaction,
  1151. net_log_with_source, &load_timing_info);
  1152. // Check that the NetLog was filled as expected.
  1153. auto entries = GetFilteredNetLogEntries(net_log_observer);
  1154. EXPECT_EQ(6u, entries.size());
  1155. EXPECT_TRUE(LogContainsBeginEvent(entries, 0,
  1156. NetLogEventType::HTTP_CACHE_GET_BACKEND));
  1157. EXPECT_TRUE(
  1158. LogContainsEndEvent(entries, 1, NetLogEventType::HTTP_CACHE_GET_BACKEND));
  1159. EXPECT_TRUE(LogContainsBeginEvent(
  1160. entries, 2, NetLogEventType::HTTP_CACHE_OPEN_OR_CREATE_ENTRY));
  1161. EXPECT_TRUE(LogContainsEndEvent(
  1162. entries, 3, NetLogEventType::HTTP_CACHE_OPEN_OR_CREATE_ENTRY));
  1163. EXPECT_TRUE(LogContainsBeginEvent(entries, 4,
  1164. NetLogEventType::HTTP_CACHE_ADD_TO_ENTRY));
  1165. EXPECT_TRUE(LogContainsEndEvent(entries, 5,
  1166. NetLogEventType::HTTP_CACHE_ADD_TO_ENTRY));
  1167. TestLoadTimingNetworkRequest(load_timing_info);
  1168. // Force this transaction to read from the cache.
  1169. MockTransaction transaction(kSimpleGET_Transaction);
  1170. transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
  1171. net_log_observer.Clear();
  1172. RunTransactionTestAndGetTiming(cache.http_cache(), transaction,
  1173. net_log_with_source, &load_timing_info);
  1174. // Check that the NetLog was filled as expected.
  1175. entries = GetFilteredNetLogEntries(net_log_observer);
  1176. EXPECT_EQ(8u, entries.size());
  1177. EXPECT_TRUE(LogContainsBeginEvent(entries, 0,
  1178. NetLogEventType::HTTP_CACHE_GET_BACKEND));
  1179. EXPECT_TRUE(
  1180. LogContainsEndEvent(entries, 1, NetLogEventType::HTTP_CACHE_GET_BACKEND));
  1181. EXPECT_TRUE(LogContainsBeginEvent(
  1182. entries, 2, NetLogEventType::HTTP_CACHE_OPEN_OR_CREATE_ENTRY));
  1183. EXPECT_TRUE(LogContainsEndEvent(
  1184. entries, 3, NetLogEventType::HTTP_CACHE_OPEN_OR_CREATE_ENTRY));
  1185. EXPECT_TRUE(LogContainsBeginEvent(entries, 4,
  1186. NetLogEventType::HTTP_CACHE_ADD_TO_ENTRY));
  1187. EXPECT_TRUE(LogContainsEndEvent(entries, 5,
  1188. NetLogEventType::HTTP_CACHE_ADD_TO_ENTRY));
  1189. EXPECT_TRUE(
  1190. LogContainsBeginEvent(entries, 6, NetLogEventType::HTTP_CACHE_READ_INFO));
  1191. EXPECT_TRUE(
  1192. LogContainsEndEvent(entries, 7, NetLogEventType::HTTP_CACHE_READ_INFO));
  1193. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  1194. EXPECT_EQ(1, cache.disk_cache()->open_count());
  1195. EXPECT_EQ(1, cache.disk_cache()->create_count());
  1196. TestLoadTimingCachedResponse(load_timing_info);
  1197. }
  1198. TEST_F(HttpCacheTest, SimpleGET_LoadOnlyFromCache_Miss) {
  1199. MockHttpCache cache;
  1200. // force this transaction to read from the cache
  1201. MockTransaction transaction(kSimpleGET_Transaction);
  1202. transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
  1203. MockHttpRequest request(transaction);
  1204. TestCompletionCallback callback;
  1205. std::unique_ptr<HttpTransaction> trans;
  1206. ASSERT_THAT(cache.CreateTransaction(&trans), IsOk());
  1207. int rv = trans->Start(&request, callback.callback(), NetLogWithSource());
  1208. if (rv == ERR_IO_PENDING)
  1209. rv = callback.WaitForResult();
  1210. ASSERT_THAT(rv, IsError(ERR_CACHE_MISS));
  1211. trans.reset();
  1212. EXPECT_EQ(0, cache.network_layer()->transaction_count());
  1213. EXPECT_EQ(0, cache.disk_cache()->open_count());
  1214. EXPECT_EQ(0, cache.disk_cache()->create_count());
  1215. }
  1216. TEST_F(HttpCacheTest, SimpleGET_LoadPreferringCache_Hit) {
  1217. MockHttpCache cache;
  1218. // write to the cache
  1219. RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
  1220. // force this transaction to read from the cache if valid
  1221. MockTransaction transaction(kSimpleGET_Transaction);
  1222. transaction.load_flags |= LOAD_SKIP_CACHE_VALIDATION;
  1223. RunTransactionTest(cache.http_cache(), transaction);
  1224. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  1225. EXPECT_EQ(1, cache.disk_cache()->open_count());
  1226. EXPECT_EQ(1, cache.disk_cache()->create_count());
  1227. }
  1228. TEST_F(HttpCacheTest, SimpleGET_LoadPreferringCache_Miss) {
  1229. MockHttpCache cache;
  1230. // force this transaction to read from the cache if valid
  1231. MockTransaction transaction(kSimpleGET_Transaction);
  1232. transaction.load_flags |= LOAD_SKIP_CACHE_VALIDATION;
  1233. RunTransactionTest(cache.http_cache(), transaction);
  1234. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  1235. EXPECT_EQ(0, cache.disk_cache()->open_count());
  1236. EXPECT_EQ(1, cache.disk_cache()->create_count());
  1237. }
  1238. // Tests LOAD_SKIP_CACHE_VALIDATION in the presence of vary headers.
  1239. TEST_F(HttpCacheTest, SimpleGET_LoadPreferringCache_VaryMatch) {
  1240. MockHttpCache cache;
  1241. // Write to the cache.
  1242. MockTransaction transaction(kSimpleGET_Transaction);
  1243. transaction.request_headers = "Foo: bar\r\n";
  1244. transaction.response_headers = "Cache-Control: max-age=10000\n"
  1245. "Vary: Foo\n";
  1246. AddMockTransaction(&transaction);
  1247. RunTransactionTest(cache.http_cache(), transaction);
  1248. // Read from the cache.
  1249. transaction.load_flags |= LOAD_SKIP_CACHE_VALIDATION;
  1250. RunTransactionTest(cache.http_cache(), transaction);
  1251. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  1252. EXPECT_EQ(1, cache.disk_cache()->open_count());
  1253. EXPECT_EQ(1, cache.disk_cache()->create_count());
  1254. RemoveMockTransaction(&transaction);
  1255. }
  1256. // Tests LOAD_SKIP_CACHE_VALIDATION in the presence of vary headers.
  1257. TEST_F(HttpCacheTest, SimpleGET_LoadPreferringCache_VaryMismatch) {
  1258. MockHttpCache cache;
  1259. // Write to the cache.
  1260. MockTransaction transaction(kSimpleGET_Transaction);
  1261. transaction.request_headers = "Foo: bar\r\n";
  1262. transaction.response_headers = "Cache-Control: max-age=10000\n"
  1263. "Vary: Foo\n";
  1264. AddMockTransaction(&transaction);
  1265. RunTransactionTest(cache.http_cache(), transaction);
  1266. // Attempt to read from the cache... this is a vary mismatch that must reach
  1267. // the network again.
  1268. transaction.load_flags |= LOAD_SKIP_CACHE_VALIDATION;
  1269. transaction.request_headers = "Foo: none\r\n";
  1270. LoadTimingInfo load_timing_info;
  1271. RunTransactionTestAndGetTiming(cache.http_cache(), transaction,
  1272. NetLogWithSource::Make(NetLogSourceType::NONE),
  1273. &load_timing_info);
  1274. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  1275. EXPECT_EQ(1, cache.disk_cache()->open_count());
  1276. EXPECT_EQ(1, cache.disk_cache()->create_count());
  1277. TestLoadTimingNetworkRequest(load_timing_info);
  1278. RemoveMockTransaction(&transaction);
  1279. }
  1280. // Tests that we honor Vary: * with LOAD_SKIP_CACHE_VALIDATION (crbug/778681)
  1281. TEST_F(HttpCacheTest, SimpleGET_LoadSkipCacheValidation_VaryStar) {
  1282. MockHttpCache cache;
  1283. // Write to the cache.
  1284. MockTransaction transaction(kSimpleGET_Transaction);
  1285. transaction.response_headers =
  1286. "Cache-Control: max-age=10000\n"
  1287. "Vary: *\n";
  1288. AddMockTransaction(&transaction);
  1289. RunTransactionTest(cache.http_cache(), transaction);
  1290. // Attempt to read from the cache... we will still load it from network,
  1291. // since Vary: * doesn't match.
  1292. transaction.load_flags |= LOAD_SKIP_CACHE_VALIDATION;
  1293. LoadTimingInfo load_timing_info;
  1294. RunTransactionTestAndGetTiming(cache.http_cache(), transaction,
  1295. NetLogWithSource::Make(NetLogSourceType::NONE),
  1296. &load_timing_info);
  1297. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  1298. EXPECT_EQ(1, cache.disk_cache()->open_count());
  1299. EXPECT_EQ(1, cache.disk_cache()->create_count());
  1300. RemoveMockTransaction(&transaction);
  1301. }
  1302. // Tests that was_cached was set properly on a failure, even if the cached
  1303. // response wasn't returned.
  1304. TEST_F(HttpCacheTest, SimpleGET_CacheSignal_Failure) {
  1305. for (bool use_memory_entry_data : {false, true}) {
  1306. MockHttpCache cache;
  1307. cache.disk_cache()->set_support_in_memory_entry_data(use_memory_entry_data);
  1308. // Prime cache.
  1309. MockTransaction transaction(kSimpleGET_Transaction);
  1310. transaction.response_headers = "Cache-Control: no-cache\n";
  1311. AddMockTransaction(&transaction);
  1312. RunTransactionTest(cache.http_cache(), transaction);
  1313. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  1314. EXPECT_EQ(1, cache.disk_cache()->create_count());
  1315. EXPECT_EQ(0, cache.disk_cache()->open_count());
  1316. RemoveMockTransaction(&transaction);
  1317. // Network failure with error; should fail but have was_cached set.
  1318. transaction.start_return_code = ERR_FAILED;
  1319. AddMockTransaction(&transaction);
  1320. MockHttpRequest request(transaction);
  1321. TestCompletionCallback callback;
  1322. std::unique_ptr<HttpTransaction> trans;
  1323. int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans);
  1324. EXPECT_THAT(rv, IsOk());
  1325. ASSERT_TRUE(trans.get());
  1326. rv = trans->Start(&request, callback.callback(), NetLogWithSource());
  1327. EXPECT_THAT(callback.GetResult(rv), IsError(ERR_FAILED));
  1328. const HttpResponseInfo* response_info = trans->GetResponseInfo();
  1329. ASSERT_TRUE(response_info);
  1330. // If use_memory_entry_data is true, we will not bother opening the entry,
  1331. // and just kick it out, so was_cached will end up false.
  1332. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  1333. if (use_memory_entry_data) {
  1334. EXPECT_EQ(false, response_info->was_cached);
  1335. EXPECT_EQ(2, cache.disk_cache()->create_count());
  1336. EXPECT_EQ(0, cache.disk_cache()->open_count());
  1337. } else {
  1338. EXPECT_EQ(true, response_info->was_cached);
  1339. EXPECT_EQ(1, cache.disk_cache()->create_count());
  1340. EXPECT_EQ(1, cache.disk_cache()->open_count());
  1341. }
  1342. RemoveMockTransaction(&transaction);
  1343. }
  1344. }
  1345. // Tests that if the transaction is destroyed right after setting the
  1346. // cache_entry_status_ as CANT_CONDITIONALIZE, then RecordHistograms should not
  1347. // hit a dcheck.
  1348. TEST_F(HttpCacheTest, RecordHistogramsCantConditionalize) {
  1349. MockHttpCache cache;
  1350. cache.disk_cache()->set_support_in_memory_entry_data(true);
  1351. {
  1352. // Prime cache.
  1353. ScopedMockTransaction transaction(kSimpleGET_Transaction);
  1354. transaction.response_headers = "Cache-Control: no-cache\n";
  1355. RunTransactionTest(cache.http_cache(), transaction);
  1356. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  1357. EXPECT_EQ(1, cache.disk_cache()->create_count());
  1358. EXPECT_EQ(0, cache.disk_cache()->open_count());
  1359. }
  1360. {
  1361. ScopedMockTransaction transaction(kSimpleGET_Transaction);
  1362. MockHttpRequest request(transaction);
  1363. TestCompletionCallback callback;
  1364. std::unique_ptr<HttpTransaction> trans;
  1365. int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans);
  1366. EXPECT_THAT(rv, IsOk());
  1367. ASSERT_TRUE(trans.get());
  1368. rv = trans->Start(&request, callback.callback(), NetLogWithSource());
  1369. // Now destroy the transaction so that RecordHistograms gets invoked.
  1370. trans.reset();
  1371. }
  1372. }
  1373. // Confirm if we have an empty cache, a read is marked as network verified.
  1374. TEST_F(HttpCacheTest, SimpleGET_NetworkAccessed_Network) {
  1375. MockHttpCache cache;
  1376. // write to the cache
  1377. HttpResponseInfo response_info;
  1378. RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction,
  1379. &response_info);
  1380. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  1381. EXPECT_EQ(0, cache.disk_cache()->open_count());
  1382. EXPECT_EQ(1, cache.disk_cache()->create_count());
  1383. EXPECT_TRUE(response_info.network_accessed);
  1384. EXPECT_EQ(CacheEntryStatus::ENTRY_NOT_IN_CACHE,
  1385. response_info.cache_entry_status);
  1386. }
  1387. // Confirm if we have a fresh entry in cache, it isn't marked as
  1388. // network verified.
  1389. TEST_F(HttpCacheTest, SimpleGET_NetworkAccessed_Cache) {
  1390. MockHttpCache cache;
  1391. // Prime cache.
  1392. MockTransaction transaction(kSimpleGET_Transaction);
  1393. RunTransactionTest(cache.http_cache(), transaction);
  1394. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  1395. EXPECT_EQ(1, cache.disk_cache()->create_count());
  1396. // Re-run transaction; make sure we don't mark the network as accessed.
  1397. HttpResponseInfo response_info;
  1398. RunTransactionTestWithResponseInfo(cache.http_cache(), transaction,
  1399. &response_info);
  1400. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  1401. EXPECT_FALSE(response_info.network_accessed);
  1402. EXPECT_EQ(CacheEntryStatus::ENTRY_USED, response_info.cache_entry_status);
  1403. }
  1404. TEST_F(HttpCacheTest, SimpleGET_LoadBypassCache) {
  1405. MockHttpCache cache;
  1406. // Write to the cache.
  1407. RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
  1408. // Force this transaction to write to the cache again.
  1409. MockTransaction transaction(kSimpleGET_Transaction);
  1410. transaction.load_flags |= LOAD_BYPASS_CACHE;
  1411. RecordingNetLogObserver net_log_observer;
  1412. LoadTimingInfo load_timing_info;
  1413. // Write to the cache.
  1414. RunTransactionTestAndGetTiming(cache.http_cache(), transaction,
  1415. NetLogWithSource::Make(NetLogSourceType::NONE),
  1416. &load_timing_info);
  1417. // Check that the NetLog was filled as expected.
  1418. auto entries = GetFilteredNetLogEntries(net_log_observer);
  1419. EXPECT_EQ(8u, entries.size());
  1420. EXPECT_TRUE(LogContainsBeginEvent(entries, 0,
  1421. NetLogEventType::HTTP_CACHE_GET_BACKEND));
  1422. EXPECT_TRUE(
  1423. LogContainsEndEvent(entries, 1, NetLogEventType::HTTP_CACHE_GET_BACKEND));
  1424. EXPECT_TRUE(LogContainsBeginEvent(entries, 2,
  1425. NetLogEventType::HTTP_CACHE_DOOM_ENTRY));
  1426. EXPECT_TRUE(
  1427. LogContainsEndEvent(entries, 3, NetLogEventType::HTTP_CACHE_DOOM_ENTRY));
  1428. EXPECT_TRUE(LogContainsBeginEvent(entries, 4,
  1429. NetLogEventType::HTTP_CACHE_CREATE_ENTRY));
  1430. EXPECT_TRUE(LogContainsEndEvent(entries, 5,
  1431. NetLogEventType::HTTP_CACHE_CREATE_ENTRY));
  1432. EXPECT_TRUE(LogContainsBeginEvent(entries, 6,
  1433. NetLogEventType::HTTP_CACHE_ADD_TO_ENTRY));
  1434. EXPECT_TRUE(LogContainsEndEvent(entries, 7,
  1435. NetLogEventType::HTTP_CACHE_ADD_TO_ENTRY));
  1436. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  1437. EXPECT_EQ(0, cache.disk_cache()->open_count());
  1438. EXPECT_EQ(2, cache.disk_cache()->create_count());
  1439. TestLoadTimingNetworkRequest(load_timing_info);
  1440. }
  1441. TEST_F(HttpCacheTest, SimpleGET_LoadBypassCache_Implicit) {
  1442. MockHttpCache cache;
  1443. // write to the cache
  1444. RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
  1445. // force this transaction to write to the cache again
  1446. MockTransaction transaction(kSimpleGET_Transaction);
  1447. transaction.request_headers = "pragma: no-cache\r\n";
  1448. RunTransactionTest(cache.http_cache(), transaction);
  1449. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  1450. EXPECT_EQ(0, cache.disk_cache()->open_count());
  1451. EXPECT_EQ(2, cache.disk_cache()->create_count());
  1452. }
  1453. TEST_F(HttpCacheTest, SimpleGET_LoadBypassCache_Implicit2) {
  1454. MockHttpCache cache;
  1455. // write to the cache
  1456. RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
  1457. // force this transaction to write to the cache again
  1458. MockTransaction transaction(kSimpleGET_Transaction);
  1459. transaction.request_headers = "cache-control: no-cache\r\n";
  1460. RunTransactionTest(cache.http_cache(), transaction);
  1461. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  1462. EXPECT_EQ(0, cache.disk_cache()->open_count());
  1463. EXPECT_EQ(2, cache.disk_cache()->create_count());
  1464. }
  1465. TEST_F(HttpCacheTest, SimpleGET_LoadValidateCache) {
  1466. MockHttpCache cache;
  1467. // Write to the cache.
  1468. RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
  1469. // Read from the cache.
  1470. RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
  1471. // Force this transaction to validate the cache.
  1472. MockTransaction transaction(kSimpleGET_Transaction);
  1473. transaction.load_flags |= LOAD_VALIDATE_CACHE;
  1474. HttpResponseInfo response_info;
  1475. LoadTimingInfo load_timing_info;
  1476. RunTransactionTestWithResponseInfoAndGetTiming(
  1477. cache.http_cache(), transaction, &response_info,
  1478. NetLogWithSource::Make(NetLogSourceType::NONE), &load_timing_info);
  1479. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  1480. EXPECT_EQ(1, cache.disk_cache()->open_count());
  1481. EXPECT_EQ(1, cache.disk_cache()->create_count());
  1482. EXPECT_TRUE(response_info.network_accessed);
  1483. TestLoadTimingNetworkRequest(load_timing_info);
  1484. }
  1485. TEST_F(HttpCacheTest, SimpleGET_LoadValidateCache_Implicit) {
  1486. MockHttpCache cache;
  1487. // write to the cache
  1488. RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
  1489. // read from the cache
  1490. RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
  1491. // force this transaction to validate the cache
  1492. MockTransaction transaction(kSimpleGET_Transaction);
  1493. transaction.request_headers = "cache-control: max-age=0\r\n";
  1494. RunTransactionTest(cache.http_cache(), transaction);
  1495. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  1496. EXPECT_EQ(1, cache.disk_cache()->open_count());
  1497. EXPECT_EQ(1, cache.disk_cache()->create_count());
  1498. }
  1499. // Tests that |unused_since_prefetch| is updated accordingly (e.g. it is set to
  1500. // true after a prefetch and set back to false when the prefetch is used).
  1501. TEST_F(HttpCacheTest, SimpleGET_UnusedSincePrefetch) {
  1502. MockHttpCache cache;
  1503. HttpResponseInfo response_info;
  1504. // A normal load does not have |unused_since_prefetch| set.
  1505. RunTransactionTestWithResponseInfoAndGetTiming(
  1506. cache.http_cache(), kSimpleGET_Transaction, &response_info,
  1507. NetLogWithSource::Make(NetLogSourceType::NONE), nullptr);
  1508. EXPECT_FALSE(response_info.unused_since_prefetch);
  1509. EXPECT_FALSE(response_info.was_cached);
  1510. // The prefetch itself does not have |unused_since_prefetch| set.
  1511. MockTransaction prefetch_transaction(kSimpleGET_Transaction);
  1512. prefetch_transaction.load_flags |= LOAD_PREFETCH;
  1513. RunTransactionTestWithResponseInfoAndGetTiming(
  1514. cache.http_cache(), prefetch_transaction, &response_info,
  1515. NetLogWithSource::Make(NetLogSourceType::NONE), nullptr);
  1516. EXPECT_FALSE(response_info.unused_since_prefetch);
  1517. EXPECT_TRUE(response_info.was_cached);
  1518. // A duplicated prefetch has |unused_since_prefetch| set.
  1519. RunTransactionTestWithResponseInfoAndGetTiming(
  1520. cache.http_cache(), prefetch_transaction, &response_info,
  1521. NetLogWithSource::Make(NetLogSourceType::NONE), nullptr);
  1522. EXPECT_TRUE(response_info.unused_since_prefetch);
  1523. EXPECT_TRUE(response_info.was_cached);
  1524. // |unused_since_prefetch| is still true after two prefetches in a row.
  1525. RunTransactionTestWithResponseInfoAndGetTiming(
  1526. cache.http_cache(), kSimpleGET_Transaction, &response_info,
  1527. NetLogWithSource::Make(NetLogSourceType::NONE), nullptr);
  1528. EXPECT_TRUE(response_info.unused_since_prefetch);
  1529. EXPECT_TRUE(response_info.was_cached);
  1530. // The resource has now been used, back to normal behavior.
  1531. RunTransactionTestWithResponseInfoAndGetTiming(
  1532. cache.http_cache(), kSimpleGET_Transaction, &response_info,
  1533. NetLogWithSource::Make(NetLogSourceType::NONE), nullptr);
  1534. EXPECT_FALSE(response_info.unused_since_prefetch);
  1535. EXPECT_TRUE(response_info.was_cached);
  1536. }
  1537. // Tests that requests made with the LOAD_RESTRICTED_PREFETCH load flag result
  1538. // in HttpResponseInfo entries with the |restricted_prefetch| flag set. Also
  1539. // tests that responses with |restricted_prefetch| flag set can only be used by
  1540. // requests that have the LOAD_CAN_USE_RESTRICTED_PREFETCH load flag.
  1541. TEST_F(HttpCacheTest, SimpleGET_RestrictedPrefetchIsRestrictedUntilReuse) {
  1542. MockHttpCache cache;
  1543. HttpResponseInfo response_info;
  1544. // A normal load does not have |restricted_prefetch| set.
  1545. RunTransactionTestWithResponseInfoAndGetTiming(
  1546. cache.http_cache(), kTypicalGET_Transaction, &response_info,
  1547. NetLogWithSource::Make(NetLogSourceType::NONE), nullptr);
  1548. EXPECT_FALSE(response_info.restricted_prefetch);
  1549. EXPECT_FALSE(response_info.was_cached);
  1550. EXPECT_TRUE(response_info.network_accessed);
  1551. // A restricted prefetch is marked as |restricted_prefetch|.
  1552. MockTransaction prefetch_transaction(kSimpleGET_Transaction);
  1553. prefetch_transaction.load_flags |= LOAD_PREFETCH;
  1554. prefetch_transaction.load_flags |= LOAD_RESTRICTED_PREFETCH;
  1555. RunTransactionTestWithResponseInfoAndGetTiming(
  1556. cache.http_cache(), prefetch_transaction, &response_info,
  1557. NetLogWithSource::Make(NetLogSourceType::NONE), nullptr);
  1558. EXPECT_TRUE(response_info.restricted_prefetch);
  1559. EXPECT_FALSE(response_info.was_cached);
  1560. EXPECT_TRUE(response_info.network_accessed);
  1561. // Requests that are marked as able to reuse restricted prefetches can do so
  1562. // correctly. Once it is reused, it is no longer considered as or marked
  1563. // restricted.
  1564. MockTransaction can_use_restricted_prefetch_transaction(
  1565. kSimpleGET_Transaction);
  1566. can_use_restricted_prefetch_transaction.load_flags |=
  1567. LOAD_CAN_USE_RESTRICTED_PREFETCH;
  1568. RunTransactionTestWithResponseInfoAndGetTiming(
  1569. cache.http_cache(), can_use_restricted_prefetch_transaction,
  1570. &response_info, NetLogWithSource::Make(NetLogSourceType::NONE), nullptr);
  1571. EXPECT_TRUE(response_info.restricted_prefetch);
  1572. EXPECT_TRUE(response_info.was_cached);
  1573. EXPECT_FALSE(response_info.network_accessed);
  1574. // Later reuse is still no longer marked restricted.
  1575. RunTransactionTestWithResponseInfoAndGetTiming(
  1576. cache.http_cache(), kSimpleGET_Transaction, &response_info,
  1577. NetLogWithSource::Make(NetLogSourceType::NONE), nullptr);
  1578. EXPECT_FALSE(response_info.restricted_prefetch);
  1579. EXPECT_TRUE(response_info.was_cached);
  1580. EXPECT_FALSE(response_info.network_accessed);
  1581. }
  1582. TEST_F(HttpCacheTest, SimpleGET_RestrictedPrefetchReuseIsLimited) {
  1583. MockHttpCache cache;
  1584. HttpResponseInfo response_info;
  1585. // A restricted prefetch is marked as |restricted_prefetch|.
  1586. MockTransaction prefetch_transaction(kSimpleGET_Transaction);
  1587. prefetch_transaction.load_flags |= LOAD_PREFETCH;
  1588. prefetch_transaction.load_flags |= LOAD_RESTRICTED_PREFETCH;
  1589. RunTransactionTestWithResponseInfoAndGetTiming(
  1590. cache.http_cache(), prefetch_transaction, &response_info,
  1591. NetLogWithSource::Make(NetLogSourceType::NONE), nullptr);
  1592. EXPECT_TRUE(response_info.restricted_prefetch);
  1593. EXPECT_FALSE(response_info.was_cached);
  1594. EXPECT_TRUE(response_info.network_accessed);
  1595. // Requests that cannot reuse restricted prefetches fail to do so. The network
  1596. // is accessed and the resulting response is not marked as
  1597. // |restricted_prefetch|.
  1598. RunTransactionTestWithResponseInfoAndGetTiming(
  1599. cache.http_cache(), kSimpleGET_Transaction, &response_info,
  1600. NetLogWithSource::Make(NetLogSourceType::NONE), nullptr);
  1601. EXPECT_FALSE(response_info.restricted_prefetch);
  1602. EXPECT_FALSE(response_info.was_cached);
  1603. EXPECT_TRUE(response_info.network_accessed);
  1604. // Future requests that are not marked as able to reuse restricted prefetches
  1605. // can use the entry in the cache now, since it has been evicted in favor of
  1606. // an unrestricted one.
  1607. RunTransactionTestWithResponseInfoAndGetTiming(
  1608. cache.http_cache(), kSimpleGET_Transaction, &response_info,
  1609. NetLogWithSource::Make(NetLogSourceType::NONE), nullptr);
  1610. EXPECT_FALSE(response_info.restricted_prefetch);
  1611. EXPECT_TRUE(response_info.was_cached);
  1612. EXPECT_FALSE(response_info.network_accessed);
  1613. }
  1614. TEST_F(HttpCacheTest, SimpleGET_UnusedSincePrefetchWriteError) {
  1615. MockHttpCache cache;
  1616. HttpResponseInfo response_info;
  1617. // Do a prefetch.
  1618. MockTransaction prefetch_transaction(kSimpleGET_Transaction);
  1619. prefetch_transaction.load_flags |= LOAD_PREFETCH;
  1620. RunTransactionTestWithResponseInfoAndGetTiming(
  1621. cache.http_cache(), prefetch_transaction, &response_info,
  1622. NetLogWithSource::Make(NetLogSourceType::NONE), nullptr);
  1623. EXPECT_TRUE(response_info.unused_since_prefetch);
  1624. EXPECT_FALSE(response_info.was_cached);
  1625. // Try to use it while injecting a failure on write.
  1626. cache.disk_cache()->set_soft_failures_mask(MockDiskEntry::FAIL_WRITE);
  1627. RunTransactionTestWithResponseInfoAndGetTiming(
  1628. cache.http_cache(), kSimpleGET_Transaction, &response_info,
  1629. NetLogWithSource::Make(NetLogSourceType::NONE), nullptr);
  1630. }
  1631. static void PreserveRequestHeaders_Handler(const HttpRequestInfo* request,
  1632. std::string* response_status,
  1633. std::string* response_headers,
  1634. std::string* response_data) {
  1635. EXPECT_TRUE(request->extra_headers.HasHeader(kExtraHeaderKey));
  1636. }
  1637. // Tests that we don't remove extra headers for simple requests.
  1638. TEST_F(HttpCacheTest, SimpleGET_PreserveRequestHeaders) {
  1639. for (bool use_memory_entry_data : {false, true}) {
  1640. MockHttpCache cache;
  1641. cache.disk_cache()->set_support_in_memory_entry_data(use_memory_entry_data);
  1642. MockTransaction transaction(kSimpleGET_Transaction);
  1643. transaction.handler = PreserveRequestHeaders_Handler;
  1644. transaction.request_headers = EXTRA_HEADER;
  1645. transaction.response_headers = "Cache-Control: max-age=0\n";
  1646. AddMockTransaction(&transaction);
  1647. // Write, then revalidate the entry.
  1648. RunTransactionTest(cache.http_cache(), transaction);
  1649. RunTransactionTest(cache.http_cache(), transaction);
  1650. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  1651. // If the backend supports memory entry data, we can figure out that the
  1652. // entry has caching-hostile headers w/o opening it.
  1653. if (use_memory_entry_data) {
  1654. EXPECT_EQ(0, cache.disk_cache()->open_count());
  1655. EXPECT_EQ(2, cache.disk_cache()->create_count());
  1656. } else {
  1657. EXPECT_EQ(1, cache.disk_cache()->open_count());
  1658. EXPECT_EQ(1, cache.disk_cache()->create_count());
  1659. }
  1660. RemoveMockTransaction(&transaction);
  1661. }
  1662. }
  1663. // Tests that we don't remove extra headers for conditionalized requests.
  1664. TEST_F(HttpCacheTest, ConditionalizedGET_PreserveRequestHeaders) {
  1665. for (bool use_memory_entry_data : {false, true}) {
  1666. MockHttpCache cache;
  1667. // Unlike in SimpleGET_PreserveRequestHeaders, this entry can be
  1668. // conditionalized, so memory hints don't affect behavior.
  1669. cache.disk_cache()->set_support_in_memory_entry_data(use_memory_entry_data);
  1670. // Write to the cache.
  1671. RunTransactionTest(cache.http_cache(), kETagGET_Transaction);
  1672. MockTransaction transaction(kETagGET_Transaction);
  1673. transaction.handler = PreserveRequestHeaders_Handler;
  1674. transaction.request_headers = "If-None-Match: \"foopy\"\r\n" EXTRA_HEADER;
  1675. AddMockTransaction(&transaction);
  1676. RunTransactionTest(cache.http_cache(), transaction);
  1677. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  1678. EXPECT_EQ(1, cache.disk_cache()->open_count());
  1679. EXPECT_EQ(1, cache.disk_cache()->create_count());
  1680. RemoveMockTransaction(&transaction);
  1681. }
  1682. }
  1683. TEST_F(HttpCacheTest, SimpleGET_ManyReaders) {
  1684. MockHttpCache cache;
  1685. MockHttpRequest request(kSimpleGET_Transaction);
  1686. std::vector<std::unique_ptr<Context>> context_list;
  1687. const int kNumTransactions = 5;
  1688. for (int i = 0; i < kNumTransactions; ++i) {
  1689. context_list.push_back(std::make_unique<Context>());
  1690. auto& c = context_list[i];
  1691. c->result = cache.CreateTransaction(&c->trans);
  1692. ASSERT_THAT(c->result, IsOk());
  1693. EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
  1694. c->result =
  1695. c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
  1696. }
  1697. // All requests are waiting for the active entry.
  1698. for (auto& context : context_list) {
  1699. EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, context->trans->GetLoadState());
  1700. }
  1701. // Allow all requests to move from the Create queue to the active entry.
  1702. base::RunLoop().RunUntilIdle();
  1703. // All requests are added to writers.
  1704. std::string cache_key = request.CacheKey();
  1705. EXPECT_EQ(kNumTransactions, cache.GetCountWriterTransactions(cache_key));
  1706. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  1707. EXPECT_EQ(0, cache.disk_cache()->open_count());
  1708. EXPECT_EQ(1, cache.disk_cache()->create_count());
  1709. // All requests are between Start and Read, i.e. idle.
  1710. for (auto& context : context_list) {
  1711. EXPECT_EQ(LOAD_STATE_IDLE, context->trans->GetLoadState());
  1712. }
  1713. for (int i = 0; i < kNumTransactions; ++i) {
  1714. auto& c = context_list[i];
  1715. if (c->result == ERR_IO_PENDING)
  1716. c->result = c->callback.WaitForResult();
  1717. // After the 1st transaction has completed the response, all transactions
  1718. // get added to readers.
  1719. if (i > 0) {
  1720. EXPECT_FALSE(cache.IsWriterPresent(cache_key));
  1721. EXPECT_EQ(kNumTransactions - i, cache.GetCountReaders(cache_key));
  1722. }
  1723. ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
  1724. }
  1725. // We should not have had to re-open the disk entry
  1726. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  1727. EXPECT_EQ(0, cache.disk_cache()->open_count());
  1728. EXPECT_EQ(1, cache.disk_cache()->create_count());
  1729. }
  1730. TEST_F(HttpCacheTest, RangeGET_FullAfterPartial) {
  1731. MockHttpCache cache;
  1732. // Request a prefix.
  1733. {
  1734. ScopedMockTransaction transaction_pre(kRangeGET_TransactionOK);
  1735. transaction_pre.request_headers = "Range: bytes = 0-9\r\n" EXTRA_HEADER;
  1736. transaction_pre.data = "rg: 00-09 ";
  1737. MockHttpRequest request_pre(transaction_pre);
  1738. HttpResponseInfo response_pre;
  1739. RunTransactionTestWithRequest(cache.http_cache(), transaction_pre,
  1740. request_pre, &response_pre);
  1741. ASSERT_TRUE(response_pre.headers != nullptr);
  1742. EXPECT_EQ(206, response_pre.headers->response_code());
  1743. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  1744. EXPECT_EQ(0, cache.disk_cache()->open_count());
  1745. EXPECT_EQ(1, cache.disk_cache()->create_count());
  1746. }
  1747. {
  1748. // Now request the full thing, but set validation to fail. This would
  1749. // previously fail in the middle of data and truncate it; current behavior
  1750. // restarts it, somewhat wastefully but gets the data back.
  1751. RangeTransactionServer handler;
  1752. handler.set_modified(true);
  1753. ScopedMockTransaction transaction_all(kRangeGET_TransactionOK);
  1754. transaction_all.request_headers = EXTRA_HEADER;
  1755. transaction_all.data = "Not a range";
  1756. MockHttpRequest request_all(transaction_all);
  1757. HttpResponseInfo response_all;
  1758. RunTransactionTestWithRequest(cache.http_cache(), transaction_all,
  1759. request_all, &response_all);
  1760. ASSERT_TRUE(response_all.headers != nullptr);
  1761. EXPECT_EQ(200, response_all.headers->response_code());
  1762. // 1 from previous test, failed validation, and re-try.
  1763. EXPECT_EQ(3, cache.network_layer()->transaction_count());
  1764. EXPECT_EQ(1, cache.disk_cache()->open_count());
  1765. EXPECT_EQ(1, cache.disk_cache()->create_count());
  1766. }
  1767. }
  1768. // Tests that when a range request transaction becomes a writer for the first
  1769. // range and then fails conditionalization for the next range and decides to
  1770. // doom the entry, then there should not be a dcheck assertion hit.
  1771. TEST_F(HttpCacheTest, RangeGET_OverlappingRangesCouldntConditionalize) {
  1772. MockHttpCache cache;
  1773. {
  1774. ScopedMockTransaction transaction_pre(kRangeGET_TransactionOK);
  1775. transaction_pre.request_headers = "Range: bytes = 10-19\r\n" EXTRA_HEADER;
  1776. transaction_pre.data = "rg: 10-19 ";
  1777. MockHttpRequest request_pre(transaction_pre);
  1778. HttpResponseInfo response_pre;
  1779. RunTransactionTestWithRequest(cache.http_cache(), transaction_pre,
  1780. request_pre, &response_pre);
  1781. ASSERT_TRUE(response_pre.headers != nullptr);
  1782. EXPECT_EQ(206, response_pre.headers->response_code());
  1783. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  1784. EXPECT_EQ(0, cache.disk_cache()->open_count());
  1785. EXPECT_EQ(1, cache.disk_cache()->create_count());
  1786. }
  1787. {
  1788. // First range skips validation because the response is fresh while the
  1789. // second range requires validation since that range is not present in the
  1790. // cache and during validation it fails conditionalization.
  1791. cache.FailConditionalizations();
  1792. ScopedMockTransaction transaction_pre(kRangeGET_TransactionOK);
  1793. transaction_pre.request_headers = "Range: bytes = 10-29\r\n" EXTRA_HEADER;
  1794. // TODO(crbug.com/992521): Fix this scenario to not return the cached bytes
  1795. // repeatedly.
  1796. transaction_pre.data = "rg: 10-19 rg: 10-19 rg: 20-29 ";
  1797. MockHttpRequest request_pre(transaction_pre);
  1798. HttpResponseInfo response_pre;
  1799. RunTransactionTestWithRequest(cache.http_cache(), transaction_pre,
  1800. request_pre, &response_pre);
  1801. ASSERT_TRUE(response_pre.headers != nullptr);
  1802. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  1803. EXPECT_EQ(1, cache.disk_cache()->open_count());
  1804. EXPECT_EQ(2, cache.disk_cache()->create_count());
  1805. }
  1806. }
  1807. TEST_F(HttpCacheTest, RangeGET_FullAfterPartialReuse) {
  1808. MockHttpCache cache;
  1809. // Request a prefix.
  1810. {
  1811. ScopedMockTransaction transaction_pre(kRangeGET_TransactionOK);
  1812. transaction_pre.request_headers = "Range: bytes = 0-9\r\n" EXTRA_HEADER;
  1813. transaction_pre.data = "rg: 00-09 ";
  1814. MockHttpRequest request_pre(transaction_pre);
  1815. HttpResponseInfo response_pre;
  1816. RunTransactionTestWithRequest(cache.http_cache(), transaction_pre,
  1817. request_pre, &response_pre);
  1818. ASSERT_TRUE(response_pre.headers != nullptr);
  1819. EXPECT_EQ(206, response_pre.headers->response_code());
  1820. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  1821. EXPECT_EQ(0, cache.disk_cache()->open_count());
  1822. EXPECT_EQ(1, cache.disk_cache()->create_count());
  1823. }
  1824. {
  1825. // Now request the full thing, revalidating successfully, so the full
  1826. // file gets stored via a sparse-entry.
  1827. ScopedMockTransaction transaction_all(kRangeGET_TransactionOK);
  1828. transaction_all.request_headers = EXTRA_HEADER;
  1829. transaction_all.data =
  1830. "rg: 00-09 rg: 10-19 rg: 20-29 rg: 30-39 rg: 40-49"
  1831. " rg: 50-59 rg: 60-69 rg: 70-79 ";
  1832. MockHttpRequest request_all(transaction_all);
  1833. HttpResponseInfo response_all;
  1834. RunTransactionTestWithRequest(cache.http_cache(), transaction_all,
  1835. request_all, &response_all);
  1836. ASSERT_TRUE(response_all.headers != nullptr);
  1837. EXPECT_EQ(200, response_all.headers->response_code());
  1838. // 1 from previous test, validation, and second chunk
  1839. EXPECT_EQ(3, cache.network_layer()->transaction_count());
  1840. EXPECT_EQ(1, cache.disk_cache()->open_count());
  1841. EXPECT_EQ(1, cache.disk_cache()->create_count());
  1842. }
  1843. {
  1844. // Grab it again, should not need re-validation.
  1845. ScopedMockTransaction transaction_all2(kRangeGET_TransactionOK);
  1846. transaction_all2.request_headers = EXTRA_HEADER;
  1847. transaction_all2.data =
  1848. "rg: 00-09 rg: 10-19 rg: 20-29 rg: 30-39 rg: 40-49"
  1849. " rg: 50-59 rg: 60-69 rg: 70-79 ";
  1850. MockHttpRequest request_all2(transaction_all2);
  1851. HttpResponseInfo response_all2;
  1852. RunTransactionTestWithRequest(cache.http_cache(), transaction_all2,
  1853. request_all2, &response_all2);
  1854. ASSERT_TRUE(response_all2.headers != nullptr);
  1855. EXPECT_EQ(200, response_all2.headers->response_code());
  1856. // Only one more cache open, no new network traffic.
  1857. EXPECT_EQ(3, cache.network_layer()->transaction_count());
  1858. EXPECT_EQ(2, cache.disk_cache()->open_count());
  1859. EXPECT_EQ(1, cache.disk_cache()->create_count());
  1860. }
  1861. }
  1862. // This test verifies that the ConnectedCallback passed to a cache transaction
  1863. // is called once per subrange in the case of a range request with a partial
  1864. // cache hit.
  1865. TEST_F(HttpCacheTest, RangeGET_ConnectedCallbackCalledForEachRange) {
  1866. MockHttpCache cache;
  1867. // Request an infix range and populate the cache with it.
  1868. {
  1869. ScopedMockTransaction mock_transaction(kRangeGET_TransactionOK);
  1870. mock_transaction.request_headers = "Range: bytes = 20-29\r\n" EXTRA_HEADER;
  1871. mock_transaction.data = "rg: 20-29 ";
  1872. mock_transaction.transport_info = TestTransportInfo();
  1873. RunTransactionTest(cache.http_cache(), mock_transaction);
  1874. }
  1875. // Request a surrounding range and observe that the callback is called once
  1876. // per subrange, as split up by cache hits.
  1877. {
  1878. ScopedMockTransaction mock_transaction(kRangeGET_TransactionOK);
  1879. mock_transaction.request_headers = "Range: bytes = 10-39\r\n" EXTRA_HEADER;
  1880. mock_transaction.data = "rg: 10-19 rg: 20-29 rg: 30-39 ";
  1881. mock_transaction.transport_info = TestTransportInfo();
  1882. MockHttpRequest request(mock_transaction);
  1883. ConnectedHandler connected_handler;
  1884. std::unique_ptr<HttpTransaction> transaction;
  1885. EXPECT_THAT(cache.CreateTransaction(&transaction), IsOk());
  1886. ASSERT_THAT(transaction, NotNull());
  1887. transaction->SetConnectedCallback(connected_handler.Callback());
  1888. TestCompletionCallback callback;
  1889. ASSERT_THAT(
  1890. transaction->Start(&request, callback.callback(), NetLogWithSource()),
  1891. IsError(ERR_IO_PENDING));
  1892. EXPECT_THAT(callback.WaitForResult(), IsOk());
  1893. // 1 call for the first range's network transaction.
  1894. EXPECT_THAT(connected_handler.transports(),
  1895. ElementsAre(TestTransportInfo()));
  1896. // Switch the endpoint for the next network transaction to observe.
  1897. // For ease, we just switch the port number.
  1898. //
  1899. // NOTE: This works because only the mock transaction struct's address is
  1900. // registered with the mocking framework - the pointee data is consulted
  1901. // each time it is read.
  1902. mock_transaction.transport_info = TestTransportInfoWithPort(123);
  1903. ReadAndVerifyTransaction(transaction.get(), mock_transaction);
  1904. // A second call for the cached range, reported as coming from the original
  1905. // endpoint it was cached from. A third call for the last range's network
  1906. // transaction.
  1907. EXPECT_THAT(connected_handler.transports(),
  1908. ElementsAre(TestTransportInfo(), CachedTestTransportInfo(),
  1909. TestTransportInfoWithPort(123)));
  1910. }
  1911. }
  1912. // This test verifies that when the ConnectedCallback passed to a cache range
  1913. // transaction returns an `ERR_INCONSISTENT_IP_ADDRESS_SPACE` error during a
  1914. // partial read from cache, then the cache entry is invalidated.
  1915. TEST_F(HttpCacheTest, RangeGET_ConnectedCallbackReturnInconsistentIpError) {
  1916. MockHttpCache cache;
  1917. // Request an infix range and populate the cache with it.
  1918. {
  1919. ScopedMockTransaction mock_transaction(kRangeGET_TransactionOK);
  1920. mock_transaction.request_headers = "Range: bytes = 20-29\r\n" EXTRA_HEADER;
  1921. mock_transaction.data = "rg: 20-29 ";
  1922. mock_transaction.transport_info = TestTransportInfo();
  1923. RunTransactionTest(cache.http_cache(), mock_transaction);
  1924. }
  1925. ScopedMockTransaction mock_transaction(kRangeGET_TransactionOK);
  1926. mock_transaction.request_headers = "Range: bytes = 10-39\r\n" EXTRA_HEADER;
  1927. mock_transaction.data = "rg: 10-19 rg: 20-29 rg: 30-39 ";
  1928. mock_transaction.transport_info = TestTransportInfo();
  1929. MockHttpRequest request(mock_transaction);
  1930. // Request a surrounding range. This *should* be read in three parts:
  1931. //
  1932. // 1. for the prefix: from the network
  1933. // 2. for the cached infix: from the cache
  1934. // 3. for the suffix: from the network
  1935. //
  1936. // The connected callback returns OK for 1), but fails during 2). As a result,
  1937. // the transaction fails partway and 3) is never created. The cache entry is
  1938. // invalidated as a result of the specific error code.
  1939. {
  1940. ConnectedHandler connected_handler;
  1941. std::unique_ptr<HttpTransaction> transaction;
  1942. EXPECT_THAT(cache.CreateTransaction(&transaction), IsOk());
  1943. ASSERT_THAT(transaction, NotNull());
  1944. transaction->SetConnectedCallback(connected_handler.Callback());
  1945. TestCompletionCallback callback;
  1946. ASSERT_THAT(
  1947. transaction->Start(&request, callback.callback(), NetLogWithSource()),
  1948. IsError(ERR_IO_PENDING));
  1949. EXPECT_THAT(callback.WaitForResult(), IsOk());
  1950. // 1 call for the first range's network transaction.
  1951. EXPECT_THAT(connected_handler.transports(),
  1952. ElementsAre(TestTransportInfo()));
  1953. // Set the callback to return an error the next time it is called.
  1954. connected_handler.set_result(ERR_INCONSISTENT_IP_ADDRESS_SPACE);
  1955. std::string content;
  1956. EXPECT_THAT(ReadTransaction(transaction.get(), &content),
  1957. IsError(ERR_INCONSISTENT_IP_ADDRESS_SPACE));
  1958. // A second call that failed.
  1959. EXPECT_THAT(connected_handler.transports(),
  1960. ElementsAre(TestTransportInfo(), CachedTestTransportInfo()));
  1961. }
  1962. // Request the same range again, observe that nothing is read from cache.
  1963. {
  1964. ConnectedHandler connected_handler;
  1965. std::unique_ptr<HttpTransaction> transaction;
  1966. EXPECT_THAT(cache.CreateTransaction(&transaction), IsOk());
  1967. ASSERT_THAT(transaction, NotNull());
  1968. transaction->SetConnectedCallback(connected_handler.Callback());
  1969. TestCompletionCallback callback;
  1970. ASSERT_THAT(
  1971. transaction->Start(&request, callback.callback(), NetLogWithSource()),
  1972. IsError(ERR_IO_PENDING));
  1973. EXPECT_THAT(callback.WaitForResult(), IsOk());
  1974. std::string content;
  1975. EXPECT_THAT(ReadTransaction(transaction.get(), &content), IsOk());
  1976. EXPECT_EQ(content, mock_transaction.data);
  1977. // 1 call for the network transaction from which the whole response was
  1978. // read. The first 20 bytes were cached by the previous two requests, but
  1979. // the cache entry was doomed during the last transaction so they are not
  1980. // used here.
  1981. EXPECT_THAT(connected_handler.transports(),
  1982. ElementsAre(TestTransportInfo()));
  1983. }
  1984. }
  1985. // This test verifies that when the ConnectedCallback passed to a cache range
  1986. // transaction returns an `ERR_INCONSISTENT_IP_ADDRESS_SPACE` error during a
  1987. // network transaction, then the cache entry is invalidated.
  1988. TEST_F(HttpCacheTest,
  1989. RangeGET_ConnectedCallbackReturnInconsistentIpErrorForNetwork) {
  1990. MockHttpCache cache;
  1991. // Request a prefix range and populate the cache with it.
  1992. {
  1993. ScopedMockTransaction mock_transaction(kRangeGET_TransactionOK);
  1994. mock_transaction.request_headers = "Range: bytes = 10-19\r\n" EXTRA_HEADER;
  1995. mock_transaction.data = "rg: 10-19 ";
  1996. mock_transaction.transport_info = TestTransportInfo();
  1997. RunTransactionTest(cache.http_cache(), mock_transaction);
  1998. }
  1999. ScopedMockTransaction mock_transaction(kRangeGET_TransactionOK);
  2000. mock_transaction.request_headers = "Range: bytes = 10-29\r\n" EXTRA_HEADER;
  2001. mock_transaction.data = "rg: 10-19 rg: 20-29 ";
  2002. mock_transaction.transport_info = TestTransportInfo();
  2003. MockHttpRequest request(mock_transaction);
  2004. // Request a longer range. This *should* be read in two parts:
  2005. //
  2006. // 1. for the prefix: from the cache
  2007. // 2. for the suffix: from the network
  2008. {
  2009. ConnectedHandler connected_handler;
  2010. std::unique_ptr<HttpTransaction> transaction;
  2011. EXPECT_THAT(cache.CreateTransaction(&transaction), IsOk());
  2012. ASSERT_THAT(transaction, NotNull());
  2013. transaction->SetConnectedCallback(connected_handler.Callback());
  2014. TestCompletionCallback callback;
  2015. ASSERT_THAT(
  2016. transaction->Start(&request, callback.callback(), NetLogWithSource()),
  2017. IsError(ERR_IO_PENDING));
  2018. EXPECT_THAT(callback.WaitForResult(), IsOk());
  2019. // 1 call for the first range's network transaction.
  2020. EXPECT_THAT(connected_handler.transports(),
  2021. ElementsAre(CachedTestTransportInfo()));
  2022. // Set the callback to return an error the next time it is called.
  2023. connected_handler.set_result(ERR_INCONSISTENT_IP_ADDRESS_SPACE);
  2024. std::string content;
  2025. EXPECT_THAT(ReadTransaction(transaction.get(), &content),
  2026. IsError(ERR_INCONSISTENT_IP_ADDRESS_SPACE));
  2027. // A second call that failed.
  2028. EXPECT_THAT(connected_handler.transports(),
  2029. ElementsAre(CachedTestTransportInfo(), TestTransportInfo()));
  2030. }
  2031. // Request the same range again, observe that nothing is read from cache.
  2032. {
  2033. ConnectedHandler connected_handler;
  2034. std::unique_ptr<HttpTransaction> transaction;
  2035. EXPECT_THAT(cache.CreateTransaction(&transaction), IsOk());
  2036. ASSERT_THAT(transaction, NotNull());
  2037. transaction->SetConnectedCallback(connected_handler.Callback());
  2038. TestCompletionCallback callback;
  2039. ASSERT_THAT(
  2040. transaction->Start(&request, callback.callback(), NetLogWithSource()),
  2041. IsError(ERR_IO_PENDING));
  2042. EXPECT_THAT(callback.WaitForResult(), IsOk());
  2043. std::string content;
  2044. EXPECT_THAT(ReadTransaction(transaction.get(), &content), IsOk());
  2045. EXPECT_EQ(content, mock_transaction.data);
  2046. // 1 call for the network transaction from which the whole response was
  2047. // read. The first 20 bytes were cached by the previous two requests, but
  2048. // the cache entry was doomed during the last transaction so they are not
  2049. // used here.
  2050. EXPECT_THAT(connected_handler.transports(),
  2051. ElementsAre(TestTransportInfo()));
  2052. }
  2053. }
  2054. // This test verifies that when the ConnectedCallback passed to a cache
  2055. // transaction returns an error for the second (or third) subrange transaction,
  2056. // the overall cache transaction fails with that error. The cache entry is still
  2057. // usable after that.
  2058. TEST_F(HttpCacheTest, RangeGET_ConnectedCallbackReturnErrorSecondTime) {
  2059. MockHttpCache cache;
  2060. // Request an infix range and populate the cache with it.
  2061. {
  2062. ScopedMockTransaction mock_transaction(kRangeGET_TransactionOK);
  2063. mock_transaction.request_headers = "Range: bytes = 20-29\r\n" EXTRA_HEADER;
  2064. mock_transaction.data = "rg: 20-29 ";
  2065. mock_transaction.transport_info = TestTransportInfo();
  2066. RunTransactionTest(cache.http_cache(), mock_transaction);
  2067. }
  2068. ScopedMockTransaction mock_transaction(kRangeGET_TransactionOK);
  2069. mock_transaction.request_headers = "Range: bytes = 10-39\r\n" EXTRA_HEADER;
  2070. mock_transaction.data = "rg: 10-19 rg: 20-29 rg: 30-39 ";
  2071. mock_transaction.transport_info = TestTransportInfo();
  2072. MockHttpRequest request(mock_transaction);
  2073. // Request a surrounding range. This *should* be read in three parts:
  2074. //
  2075. // 1. for the prefix: from the network
  2076. // 2. for the cached infix: from the cache
  2077. // 3. for the suffix: from the network
  2078. //
  2079. // The connected callback returns OK for 1), but fails during 2). As a result,
  2080. // the transaction fails partway and 3) is never created. The prefix is still
  2081. // cached, such that the cache entry ends up with both the prefix and infix.
  2082. {
  2083. ConnectedHandler connected_handler;
  2084. std::unique_ptr<HttpTransaction> transaction;
  2085. EXPECT_THAT(cache.CreateTransaction(&transaction), IsOk());
  2086. ASSERT_THAT(transaction, NotNull());
  2087. transaction->SetConnectedCallback(connected_handler.Callback());
  2088. TestCompletionCallback callback;
  2089. ASSERT_THAT(
  2090. transaction->Start(&request, callback.callback(), NetLogWithSource()),
  2091. IsError(ERR_IO_PENDING));
  2092. EXPECT_THAT(callback.WaitForResult(), IsOk());
  2093. // 1 call for the first range's network transaction.
  2094. EXPECT_THAT(connected_handler.transports(),
  2095. ElementsAre(TestTransportInfo()));
  2096. // Set the callback to return an error the next time it is called. The exact
  2097. // error code is irrelevant, what matters is that it is reflected in the
  2098. // overall status of the transaction.
  2099. connected_handler.set_result(ERR_NOT_IMPLEMENTED);
  2100. std::string content;
  2101. EXPECT_THAT(ReadTransaction(transaction.get(), &content),
  2102. IsError(ERR_NOT_IMPLEMENTED));
  2103. // A second call that failed.
  2104. EXPECT_THAT(connected_handler.transports(),
  2105. ElementsAre(TestTransportInfo(), CachedTestTransportInfo()));
  2106. }
  2107. // Request the same range again, observe that the prefix and infix are both
  2108. // read from cache. Only the suffix is fetched from the network.
  2109. {
  2110. ConnectedHandler connected_handler;
  2111. std::unique_ptr<HttpTransaction> transaction;
  2112. EXPECT_THAT(cache.CreateTransaction(&transaction), IsOk());
  2113. ASSERT_THAT(transaction, NotNull());
  2114. transaction->SetConnectedCallback(connected_handler.Callback());
  2115. TestCompletionCallback callback;
  2116. ASSERT_THAT(
  2117. transaction->Start(&request, callback.callback(), NetLogWithSource()),
  2118. IsError(ERR_IO_PENDING));
  2119. EXPECT_THAT(callback.WaitForResult(), IsOk());
  2120. // 1 call for the first range's cache transaction: the first 20 bytes were
  2121. // cached by the previous two requests.
  2122. EXPECT_THAT(connected_handler.transports(),
  2123. ElementsAre(CachedTestTransportInfo()));
  2124. std::string content;
  2125. EXPECT_THAT(ReadTransaction(transaction.get(), &content), IsOk());
  2126. EXPECT_EQ(content, mock_transaction.data);
  2127. // A second call from the network transaction for the last 10 bytes.
  2128. EXPECT_THAT(connected_handler.transports(),
  2129. ElementsAre(CachedTestTransportInfo(), TestTransportInfo()));
  2130. }
  2131. }
  2132. // This test verifies that the ConnectedCallback passed to a cache transaction
  2133. // is called once per subrange in the case of a range request with a partial
  2134. // cache hit, even when a prefix of the range is cached.
  2135. TEST_F(HttpCacheTest, RangeGET_ConnectedCallbackCalledForEachRangeWithPrefix) {
  2136. MockHttpCache cache;
  2137. // Request a prefix range and populate the cache with it.
  2138. {
  2139. ScopedMockTransaction mock_transaction(kRangeGET_TransactionOK);
  2140. mock_transaction.request_headers = "Range: bytes = 10-19\r\n" EXTRA_HEADER;
  2141. mock_transaction.data = "rg: 10-19 ";
  2142. mock_transaction.transport_info = TestTransportInfo();
  2143. RunTransactionTest(cache.http_cache(), mock_transaction);
  2144. }
  2145. // Request a surrounding range and observe that the callback is called once
  2146. // per subrange, as split up by cache hits.
  2147. {
  2148. ScopedMockTransaction mock_transaction(kRangeGET_TransactionOK);
  2149. mock_transaction.request_headers = "Range: bytes = 10-39\r\n" EXTRA_HEADER;
  2150. mock_transaction.data = "rg: 10-19 rg: 20-29 rg: 30-39 ";
  2151. mock_transaction.transport_info = TestTransportInfoWithPort(123);
  2152. MockHttpRequest request(mock_transaction);
  2153. ConnectedHandler connected_handler;
  2154. std::unique_ptr<HttpTransaction> transaction;
  2155. EXPECT_THAT(cache.CreateTransaction(&transaction), IsOk());
  2156. ASSERT_THAT(transaction, NotNull());
  2157. transaction->SetConnectedCallback(connected_handler.Callback());
  2158. TestCompletionCallback callback;
  2159. ASSERT_THAT(
  2160. transaction->Start(&request, callback.callback(), NetLogWithSource()),
  2161. IsError(ERR_IO_PENDING));
  2162. EXPECT_THAT(callback.WaitForResult(), IsOk());
  2163. // 1 call for the first range from the cache, reported as coming from the
  2164. // endpoint which initially served the cached range.
  2165. EXPECT_THAT(connected_handler.transports(),
  2166. ElementsAre(CachedTestTransportInfo()));
  2167. ReadAndVerifyTransaction(transaction.get(), mock_transaction);
  2168. // A second call for the last range's network transaction.
  2169. EXPECT_THAT(
  2170. connected_handler.transports(),
  2171. ElementsAre(CachedTestTransportInfo(), TestTransportInfoWithPort(123)));
  2172. }
  2173. }
  2174. // Tests that a range transaction is still usable even if it's unable to access
  2175. // the cache.
  2176. TEST_F(HttpCacheTest, RangeGET_FailedCacheAccess) {
  2177. MockHttpCache cache;
  2178. ScopedMockTransaction transaction(kRangeGET_TransactionOK);
  2179. MockHttpRequest request(transaction);
  2180. auto c = std::make_unique<Context>();
  2181. c->result = cache.CreateTransaction(&c->trans);
  2182. ASSERT_THAT(c->result, IsOk());
  2183. EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
  2184. cache.disk_cache()->set_fail_requests(true);
  2185. c->result =
  2186. c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
  2187. base::RunLoop().RunUntilIdle();
  2188. EXPECT_FALSE(cache.IsWriterPresent(kRangeGET_TransactionOK.url));
  2189. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  2190. EXPECT_EQ(0, cache.disk_cache()->open_count());
  2191. EXPECT_EQ(0, cache.disk_cache()->create_count());
  2192. c->result = c->callback.WaitForResult();
  2193. ReadAndVerifyTransaction(c->trans.get(), kRangeGET_TransactionOK);
  2194. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  2195. EXPECT_EQ(0, cache.disk_cache()->open_count());
  2196. EXPECT_EQ(0, cache.disk_cache()->create_count());
  2197. }
  2198. // Tests that we can have parallel validation on range requests.
  2199. TEST_F(HttpCacheTest, RangeGET_ParallelValidationNoMatch) {
  2200. MockHttpCache cache;
  2201. ScopedMockTransaction transaction(kRangeGET_TransactionOK);
  2202. MockHttpRequest request(transaction);
  2203. std::vector<std::unique_ptr<Context>> context_list;
  2204. const int kNumTransactions = 5;
  2205. for (int i = 0; i < kNumTransactions; ++i) {
  2206. context_list.push_back(std::make_unique<Context>());
  2207. auto& c = context_list[i];
  2208. c->result = cache.CreateTransaction(&c->trans);
  2209. ASSERT_THAT(c->result, IsOk());
  2210. EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
  2211. c->result =
  2212. c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
  2213. }
  2214. // All requests are waiting for the active entry.
  2215. for (auto& context : context_list) {
  2216. EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, context->trans->GetLoadState());
  2217. }
  2218. // Allow all requests to move from the Create queue to the active entry.
  2219. base::RunLoop().RunUntilIdle();
  2220. // First entry created is doomed due to 2nd transaction's validation leading
  2221. // to restarting of the queued transactions.
  2222. EXPECT_TRUE(cache.IsWriterPresent(request.CacheKey()));
  2223. // TODO(shivanisha): The restarted transactions race for creating the entry
  2224. // and thus instead of all 4 succeeding, 2 of them succeed. This is very
  2225. // implementation specific and happens because the queued transactions get
  2226. // restarted synchronously and get to the queue of creating the entry before
  2227. // the transaction that is restarting them. Fix the test to make it less
  2228. // vulnerable to any scheduling changes in the code.
  2229. EXPECT_EQ(5, cache.network_layer()->transaction_count());
  2230. EXPECT_EQ(0, cache.disk_cache()->open_count());
  2231. EXPECT_EQ(3, cache.disk_cache()->create_count());
  2232. for (auto& context : context_list) {
  2233. EXPECT_EQ(LOAD_STATE_IDLE, context->trans->GetLoadState());
  2234. }
  2235. for (int i = 0; i < kNumTransactions; ++i) {
  2236. auto& c = context_list[i];
  2237. if (c->result == ERR_IO_PENDING)
  2238. c->result = c->callback.WaitForResult();
  2239. ReadAndVerifyTransaction(c->trans.get(), kRangeGET_TransactionOK);
  2240. }
  2241. EXPECT_EQ(5, cache.network_layer()->transaction_count());
  2242. EXPECT_EQ(0, cache.disk_cache()->open_count());
  2243. EXPECT_EQ(3, cache.disk_cache()->create_count());
  2244. }
  2245. // Tests that if a transaction is dooming the entry and the entry was doomed by
  2246. // another transaction that was not part of the entry and created a new entry,
  2247. // the new entry should not be incorrectly doomed. (crbug.com/736993)
  2248. TEST_F(HttpCacheTest, RangeGET_ParallelValidationNoMatchDoomEntry) {
  2249. MockHttpCache cache;
  2250. ScopedMockTransaction transaction(kRangeGET_TransactionOK);
  2251. MockHttpRequest request(transaction);
  2252. MockTransaction dooming_transaction(kRangeGET_TransactionOK);
  2253. dooming_transaction.load_flags |= LOAD_BYPASS_CACHE;
  2254. MockHttpRequest dooming_request(dooming_transaction);
  2255. std::vector<std::unique_ptr<Context>> context_list;
  2256. const int kNumTransactions = 3;
  2257. scoped_refptr<MockDiskEntry> first_entry;
  2258. scoped_refptr<MockDiskEntry> second_entry;
  2259. for (int i = 0; i < kNumTransactions; ++i) {
  2260. context_list.push_back(std::make_unique<Context>());
  2261. auto& c = context_list[i];
  2262. c->result = cache.CreateTransaction(&c->trans);
  2263. ASSERT_THAT(c->result, IsOk());
  2264. EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
  2265. MockHttpRequest* this_request = &request;
  2266. if (i == 2)
  2267. this_request = &dooming_request;
  2268. if (i == 1) {
  2269. ASSERT_TRUE(first_entry);
  2270. first_entry->SetDefer(MockDiskEntry::DEFER_READ);
  2271. }
  2272. c->result = c->trans->Start(this_request, c->callback.callback(),
  2273. NetLogWithSource());
  2274. // Continue the transactions. 2nd will pause at the cache reading state and
  2275. // 3rd transaction will doom the entry.
  2276. base::RunLoop().RunUntilIdle();
  2277. std::string cache_key = request.CacheKey();
  2278. // Check status of the first and second entries after every transaction.
  2279. switch (i) {
  2280. case 0:
  2281. first_entry = cache.disk_cache()->GetDiskEntryRef(cache_key);
  2282. break;
  2283. case 1:
  2284. EXPECT_FALSE(first_entry->is_doomed());
  2285. break;
  2286. case 2:
  2287. EXPECT_TRUE(first_entry->is_doomed());
  2288. second_entry = cache.disk_cache()->GetDiskEntryRef(cache_key);
  2289. EXPECT_FALSE(second_entry->is_doomed());
  2290. break;
  2291. }
  2292. }
  2293. // Resume cache read by 1st transaction which will lead to dooming the entry
  2294. // as well since the entry cannot be validated. This double dooming should not
  2295. // lead to an assertion.
  2296. first_entry->ResumeDiskEntryOperation();
  2297. base::RunLoop().RunUntilIdle();
  2298. // Since second_entry is already created, when 1st transaction goes on to
  2299. // create an entry, it will get ERR_CACHE_RACE leading to dooming of
  2300. // second_entry and creation of a third entry.
  2301. EXPECT_TRUE(second_entry->is_doomed());
  2302. EXPECT_EQ(3, cache.network_layer()->transaction_count());
  2303. EXPECT_EQ(0, cache.disk_cache()->open_count());
  2304. EXPECT_EQ(3, cache.disk_cache()->create_count());
  2305. for (auto& context : context_list) {
  2306. EXPECT_EQ(LOAD_STATE_IDLE, context->trans->GetLoadState());
  2307. }
  2308. for (auto& c : context_list) {
  2309. ReadAndVerifyTransaction(c->trans.get(), kRangeGET_TransactionOK);
  2310. }
  2311. EXPECT_EQ(3, cache.network_layer()->transaction_count());
  2312. EXPECT_EQ(0, cache.disk_cache()->open_count());
  2313. EXPECT_EQ(3, cache.disk_cache()->create_count());
  2314. }
  2315. // Same as above but tests that the 2nd transaction does not do anything if
  2316. // there is nothing to doom. (crbug.com/736993)
  2317. TEST_F(HttpCacheTest, RangeGET_ParallelValidationNoMatchDoomEntry1) {
  2318. MockHttpCache cache;
  2319. ScopedMockTransaction transaction(kRangeGET_TransactionOK);
  2320. MockHttpRequest request(transaction);
  2321. MockTransaction dooming_transaction(kRangeGET_TransactionOK);
  2322. dooming_transaction.load_flags |= LOAD_BYPASS_CACHE;
  2323. MockHttpRequest dooming_request(dooming_transaction);
  2324. std::vector<std::unique_ptr<Context>> context_list;
  2325. const int kNumTransactions = 3;
  2326. scoped_refptr<MockDiskEntry> first_entry;
  2327. for (int i = 0; i < kNumTransactions; ++i) {
  2328. context_list.push_back(std::make_unique<Context>());
  2329. auto& c = context_list[i];
  2330. c->result = cache.CreateTransaction(&c->trans);
  2331. ASSERT_THAT(c->result, IsOk());
  2332. EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
  2333. MockHttpRequest* this_request = &request;
  2334. if (i == 2) {
  2335. this_request = &dooming_request;
  2336. cache.disk_cache()->SetDefer(MockDiskEntry::DEFER_CREATE);
  2337. }
  2338. if (i == 1) {
  2339. ASSERT_TRUE(first_entry);
  2340. first_entry->SetDefer(MockDiskEntry::DEFER_READ);
  2341. }
  2342. c->result = c->trans->Start(this_request, c->callback.callback(),
  2343. NetLogWithSource());
  2344. // Continue the transactions. 2nd will pause at the cache reading state and
  2345. // 3rd transaction will doom the entry and pause before creating a new
  2346. // entry.
  2347. base::RunLoop().RunUntilIdle();
  2348. // Check status of the entry after every transaction.
  2349. switch (i) {
  2350. case 0:
  2351. first_entry = cache.disk_cache()->GetDiskEntryRef(request.CacheKey());
  2352. break;
  2353. case 1:
  2354. EXPECT_FALSE(first_entry->is_doomed());
  2355. break;
  2356. case 2:
  2357. EXPECT_TRUE(first_entry->is_doomed());
  2358. break;
  2359. }
  2360. }
  2361. // Resume cache read by 2nd transaction which will lead to dooming the entry
  2362. // as well since the entry cannot be validated. This double dooming should not
  2363. // lead to an assertion.
  2364. first_entry->ResumeDiskEntryOperation();
  2365. base::RunLoop().RunUntilIdle();
  2366. // Resume creation of entry by 3rd transaction.
  2367. cache.disk_cache()->ResumeCacheOperation();
  2368. base::RunLoop().RunUntilIdle();
  2369. // Note that since 3rd transaction's entry is already created but its
  2370. // callback is deferred, MockDiskCache's implementation returns
  2371. // ERR_CACHE_CREATE_FAILURE when 2nd transaction tries to create an entry
  2372. // during that time, leading to it switching over to pass-through mode.
  2373. // Thus the number of entries is 2 below.
  2374. EXPECT_EQ(3, cache.network_layer()->transaction_count());
  2375. EXPECT_EQ(0, cache.disk_cache()->open_count());
  2376. EXPECT_EQ(2, cache.disk_cache()->create_count());
  2377. for (auto& context : context_list) {
  2378. EXPECT_EQ(LOAD_STATE_IDLE, context->trans->GetLoadState());
  2379. }
  2380. for (auto& c : context_list) {
  2381. ReadAndVerifyTransaction(c->trans.get(), kRangeGET_TransactionOK);
  2382. }
  2383. EXPECT_EQ(3, cache.network_layer()->transaction_count());
  2384. EXPECT_EQ(0, cache.disk_cache()->open_count());
  2385. EXPECT_EQ(2, cache.disk_cache()->create_count());
  2386. }
  2387. // Tests parallel validation on range requests with non-overlapping ranges.
  2388. TEST_F(HttpCacheTest, RangeGET_ParallelValidationDifferentRanges) {
  2389. MockHttpCache cache;
  2390. ScopedMockTransaction transaction(kRangeGET_TransactionOK);
  2391. std::vector<std::unique_ptr<Context>> context_list;
  2392. const int kNumTransactions = 2;
  2393. for (int i = 0; i < kNumTransactions; ++i) {
  2394. context_list.push_back(std::make_unique<Context>());
  2395. }
  2396. // Let 1st transaction complete headers phase for ranges 40-49.
  2397. std::string first_read;
  2398. MockHttpRequest request1(transaction);
  2399. {
  2400. auto& c = context_list[0];
  2401. c->result = cache.CreateTransaction(&c->trans);
  2402. ASSERT_THAT(c->result, IsOk());
  2403. EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
  2404. c->result =
  2405. c->trans->Start(&request1, c->callback.callback(), NetLogWithSource());
  2406. base::RunLoop().RunUntilIdle();
  2407. // Start writing to the cache so that MockDiskEntry::CouldBeSparse() returns
  2408. // true.
  2409. const int kBufferSize = 5;
  2410. scoped_refptr<IOBuffer> buffer =
  2411. base::MakeRefCounted<IOBuffer>(kBufferSize);
  2412. ReleaseBufferCompletionCallback cb(buffer.get());
  2413. c->result = c->trans->Read(buffer.get(), kBufferSize, cb.callback());
  2414. EXPECT_EQ(kBufferSize, cb.GetResult(c->result));
  2415. std::string data_read(buffer->data(), kBufferSize);
  2416. first_read = data_read;
  2417. EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
  2418. }
  2419. // 2nd transaction requests ranges 30-39.
  2420. transaction.request_headers = "Range: bytes = 30-39\r\n" EXTRA_HEADER;
  2421. MockHttpRequest request2(transaction);
  2422. {
  2423. auto& c = context_list[1];
  2424. c->result = cache.CreateTransaction(&c->trans);
  2425. ASSERT_THAT(c->result, IsOk());
  2426. EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
  2427. c->result =
  2428. c->trans->Start(&request2, c->callback.callback(), NetLogWithSource());
  2429. base::RunLoop().RunUntilIdle();
  2430. EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
  2431. }
  2432. std::string cache_key = request2.CacheKey();
  2433. EXPECT_TRUE(cache.IsWriterPresent(cache_key));
  2434. EXPECT_EQ(1, cache.GetCountDoneHeadersQueue(cache_key));
  2435. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  2436. EXPECT_EQ(0, cache.disk_cache()->open_count());
  2437. EXPECT_EQ(1, cache.disk_cache()->create_count());
  2438. for (int i = 0; i < kNumTransactions; ++i) {
  2439. auto& c = context_list[i];
  2440. if (c->result == ERR_IO_PENDING)
  2441. c->result = c->callback.WaitForResult();
  2442. if (i == 0) {
  2443. ReadRemainingAndVerifyTransaction(c->trans.get(), first_read,
  2444. transaction);
  2445. continue;
  2446. }
  2447. transaction.data = "rg: 30-39 ";
  2448. ReadAndVerifyTransaction(c->trans.get(), transaction);
  2449. }
  2450. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  2451. EXPECT_EQ(0, cache.disk_cache()->open_count());
  2452. EXPECT_EQ(1, cache.disk_cache()->create_count());
  2453. // Fetch from the cache to check that ranges 30-49 have been successfully
  2454. // cached.
  2455. {
  2456. MockTransaction range_transaction(kRangeGET_TransactionOK);
  2457. range_transaction.request_headers = "Range: bytes = 30-49\r\n" EXTRA_HEADER;
  2458. range_transaction.data = "rg: 30-39 rg: 40-49 ";
  2459. std::string headers;
  2460. RunTransactionTestWithResponse(cache.http_cache(), range_transaction,
  2461. &headers);
  2462. Verify206Response(headers, 30, 49);
  2463. }
  2464. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  2465. EXPECT_EQ(1, cache.disk_cache()->open_count());
  2466. EXPECT_EQ(1, cache.disk_cache()->create_count());
  2467. context_list.clear();
  2468. }
  2469. // Tests that a request does not create Writers when readers is not empty.
  2470. TEST_F(HttpCacheTest, RangeGET_DoNotCreateWritersWhenReaderExists) {
  2471. MockHttpCache cache;
  2472. // Save a request in the cache so that the next request can become a
  2473. // reader.
  2474. MockTransaction transaction(kRangeGET_Transaction);
  2475. transaction.request_headers = EXTRA_HEADER;
  2476. AddMockTransaction(&transaction);
  2477. RunTransactionTest(cache.http_cache(), transaction);
  2478. // Let this request be a reader since it doesn't need validation as per its
  2479. // load flag.
  2480. transaction.load_flags |= LOAD_SKIP_CACHE_VALIDATION;
  2481. MockHttpRequest request(transaction);
  2482. Context context;
  2483. context.result = cache.CreateTransaction(&context.trans);
  2484. ASSERT_THAT(context.result, IsOk());
  2485. context.result = context.trans->Start(&request, context.callback.callback(),
  2486. NetLogWithSource());
  2487. base::RunLoop().RunUntilIdle();
  2488. std::string cache_key = request.CacheKey();
  2489. EXPECT_EQ(1, cache.GetCountReaders(cache_key));
  2490. RemoveMockTransaction(&transaction);
  2491. // A range request should now "not" create Writers while readers is still
  2492. // non-empty.
  2493. MockTransaction range_transaction(kRangeGET_Transaction);
  2494. range_transaction.request_headers = "Range: bytes = 0-9\r\n" EXTRA_HEADER;
  2495. AddMockTransaction(&range_transaction);
  2496. MockHttpRequest range_request(range_transaction);
  2497. Context range_context;
  2498. range_context.result = cache.CreateTransaction(&range_context.trans);
  2499. ASSERT_THAT(range_context.result, IsOk());
  2500. range_context.result = range_context.trans->Start(
  2501. &range_request, range_context.callback.callback(), NetLogWithSource());
  2502. base::RunLoop().RunUntilIdle();
  2503. EXPECT_EQ(1, cache.GetCountReaders(cache_key));
  2504. EXPECT_FALSE(cache.IsWriterPresent(cache_key));
  2505. EXPECT_EQ(1, cache.GetCountDoneHeadersQueue(cache_key));
  2506. RemoveMockTransaction(&range_transaction);
  2507. }
  2508. // Tests parallel validation on range requests can be successfully restarted
  2509. // when there is a cache lock timeout.
  2510. TEST_F(HttpCacheTest, RangeGET_ParallelValidationCacheLockTimeout) {
  2511. MockHttpCache cache;
  2512. ScopedMockTransaction transaction(kRangeGET_TransactionOK);
  2513. std::vector<std::unique_ptr<Context>> context_list;
  2514. const int kNumTransactions = 2;
  2515. for (int i = 0; i < kNumTransactions; ++i) {
  2516. context_list.push_back(std::make_unique<Context>());
  2517. }
  2518. // Let 1st transaction complete headers phase for ranges 40-49.
  2519. std::string first_read;
  2520. MockHttpRequest request1(transaction);
  2521. {
  2522. auto& c = context_list[0];
  2523. c->result = cache.CreateTransaction(&c->trans);
  2524. ASSERT_THAT(c->result, IsOk());
  2525. EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
  2526. c->result =
  2527. c->trans->Start(&request1, c->callback.callback(), NetLogWithSource());
  2528. base::RunLoop().RunUntilIdle();
  2529. // Start writing to the cache so that MockDiskEntry::CouldBeSparse() returns
  2530. // true.
  2531. const int kBufferSize = 5;
  2532. scoped_refptr<IOBuffer> buffer =
  2533. base::MakeRefCounted<IOBuffer>(kBufferSize);
  2534. ReleaseBufferCompletionCallback cb(buffer.get());
  2535. c->result = c->trans->Read(buffer.get(), kBufferSize, cb.callback());
  2536. EXPECT_EQ(kBufferSize, cb.GetResult(c->result));
  2537. std::string data_read(buffer->data(), kBufferSize);
  2538. first_read = data_read;
  2539. EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
  2540. }
  2541. // Cache lock timeout will lead to dooming the entry since the transaction may
  2542. // have already written the headers.
  2543. cache.SimulateCacheLockTimeoutAfterHeaders();
  2544. // 2nd transaction requests ranges 30-39.
  2545. transaction.request_headers = "Range: bytes = 30-39\r\n" EXTRA_HEADER;
  2546. MockHttpRequest request2(transaction);
  2547. {
  2548. auto& c = context_list[1];
  2549. c->result = cache.CreateTransaction(&c->trans);
  2550. ASSERT_THAT(c->result, IsOk());
  2551. EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
  2552. c->result =
  2553. c->trans->Start(&request2, c->callback.callback(), NetLogWithSource());
  2554. base::RunLoop().RunUntilIdle();
  2555. EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
  2556. }
  2557. EXPECT_EQ(0, cache.GetCountDoneHeadersQueue(request1.CacheKey()));
  2558. EXPECT_EQ(3, cache.network_layer()->transaction_count());
  2559. EXPECT_EQ(0, cache.disk_cache()->open_count());
  2560. EXPECT_EQ(1, cache.disk_cache()->create_count());
  2561. for (int i = 0; i < kNumTransactions; ++i) {
  2562. auto& c = context_list[i];
  2563. if (c->result == ERR_IO_PENDING)
  2564. c->result = c->callback.WaitForResult();
  2565. if (i == 0) {
  2566. ReadRemainingAndVerifyTransaction(c->trans.get(), first_read,
  2567. transaction);
  2568. continue;
  2569. }
  2570. transaction.data = "rg: 30-39 ";
  2571. ReadAndVerifyTransaction(c->trans.get(), transaction);
  2572. }
  2573. EXPECT_EQ(3, cache.network_layer()->transaction_count());
  2574. EXPECT_EQ(0, cache.disk_cache()->open_count());
  2575. EXPECT_EQ(1, cache.disk_cache()->create_count());
  2576. }
  2577. // Tests a full request and a simultaneous range request and the range request
  2578. // dooms the entry created by the full request due to not being able to
  2579. // conditionalize.
  2580. TEST_F(HttpCacheTest, RangeGET_ParallelValidationCouldntConditionalize) {
  2581. MockHttpCache cache;
  2582. MockTransaction mock_transaction(kSimpleGET_Transaction);
  2583. mock_transaction.url = kRangeGET_TransactionOK.url;
  2584. ScopedMockTransaction transaction(mock_transaction);
  2585. // Remove the cache-control and other headers so that the response cannot be
  2586. // conditionalized.
  2587. transaction.response_headers = "";
  2588. std::vector<std::unique_ptr<Context>> context_list;
  2589. const int kNumTransactions = 2;
  2590. for (int i = 0; i < kNumTransactions; ++i) {
  2591. context_list.push_back(std::make_unique<Context>());
  2592. }
  2593. // Let 1st transaction complete headers phase for no range and read some part
  2594. // of the response and write in the cache.
  2595. std::string first_read;
  2596. MockHttpRequest request1(transaction);
  2597. {
  2598. request1.url = GURL(kRangeGET_TransactionOK.url);
  2599. auto& c = context_list[0];
  2600. c->result = cache.CreateTransaction(&c->trans);
  2601. ASSERT_THAT(c->result, IsOk());
  2602. EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
  2603. c->result =
  2604. c->trans->Start(&request1, c->callback.callback(), NetLogWithSource());
  2605. base::RunLoop().RunUntilIdle();
  2606. const int kBufferSize = 5;
  2607. scoped_refptr<IOBuffer> buffer =
  2608. base::MakeRefCounted<IOBuffer>(kBufferSize);
  2609. ReleaseBufferCompletionCallback cb(buffer.get());
  2610. c->result = c->trans->Read(buffer.get(), kBufferSize, cb.callback());
  2611. EXPECT_EQ(kBufferSize, cb.GetResult(c->result));
  2612. std::string data_read(buffer->data(), kBufferSize);
  2613. first_read = data_read;
  2614. EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
  2615. }
  2616. // 2nd transaction requests a range.
  2617. ScopedMockTransaction range_transaction(kRangeGET_TransactionOK);
  2618. range_transaction.request_headers = "Range: bytes = 0-29\r\n" EXTRA_HEADER;
  2619. MockHttpRequest request2(range_transaction);
  2620. {
  2621. auto& c = context_list[1];
  2622. c->result = cache.CreateTransaction(&c->trans);
  2623. ASSERT_THAT(c->result, IsOk());
  2624. EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
  2625. c->result =
  2626. c->trans->Start(&request2, c->callback.callback(), NetLogWithSource());
  2627. base::RunLoop().RunUntilIdle();
  2628. EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
  2629. }
  2630. // The second request would have doomed the 1st entry and created a new entry.
  2631. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  2632. EXPECT_EQ(0, cache.disk_cache()->open_count());
  2633. EXPECT_EQ(2, cache.disk_cache()->create_count());
  2634. for (int i = 0; i < kNumTransactions; ++i) {
  2635. auto& c = context_list[i];
  2636. if (c->result == ERR_IO_PENDING)
  2637. c->result = c->callback.WaitForResult();
  2638. if (i == 0) {
  2639. ReadRemainingAndVerifyTransaction(c->trans.get(), first_read,
  2640. transaction);
  2641. continue;
  2642. }
  2643. range_transaction.data = "rg: 00-09 rg: 10-19 rg: 20-29 ";
  2644. ReadAndVerifyTransaction(c->trans.get(), range_transaction);
  2645. }
  2646. context_list.clear();
  2647. }
  2648. // Tests a 200 request and a simultaneous range request where conditionalization
  2649. // is possible.
  2650. TEST_F(HttpCacheTest, RangeGET_ParallelValidationCouldConditionalize) {
  2651. MockHttpCache cache;
  2652. MockTransaction mock_transaction(kSimpleGET_Transaction);
  2653. mock_transaction.url = kRangeGET_TransactionOK.url;
  2654. mock_transaction.data = kFullRangeData;
  2655. std::string response_headers_str = base::StrCat(
  2656. {"ETag: StrongOne\n",
  2657. "Content-Length:", base::NumberToString(strlen(kFullRangeData)), "\n"});
  2658. mock_transaction.response_headers = response_headers_str.c_str();
  2659. ScopedMockTransaction transaction(mock_transaction);
  2660. std::vector<std::unique_ptr<Context>> context_list;
  2661. const int kNumTransactions = 2;
  2662. for (int i = 0; i < kNumTransactions; ++i) {
  2663. context_list.push_back(std::make_unique<Context>());
  2664. }
  2665. // Let 1st transaction complete headers phase for no range and read some part
  2666. // of the response and write in the cache.
  2667. std::string first_read;
  2668. MockHttpRequest request1(transaction);
  2669. {
  2670. request1.url = GURL(kRangeGET_TransactionOK.url);
  2671. auto& c = context_list[0];
  2672. c->result = cache.CreateTransaction(&c->trans);
  2673. ASSERT_THAT(c->result, IsOk());
  2674. EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
  2675. c->result =
  2676. c->trans->Start(&request1, c->callback.callback(), NetLogWithSource());
  2677. base::RunLoop().RunUntilIdle();
  2678. const int kBufferSize = 5;
  2679. scoped_refptr<IOBuffer> buffer =
  2680. base::MakeRefCounted<IOBuffer>(kBufferSize);
  2681. ReleaseBufferCompletionCallback cb(buffer.get());
  2682. c->result = c->trans->Read(buffer.get(), kBufferSize, cb.callback());
  2683. EXPECT_EQ(kBufferSize, cb.GetResult(c->result));
  2684. std::string data_read(buffer->data(), kBufferSize);
  2685. first_read = data_read;
  2686. EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
  2687. }
  2688. // 2nd transaction requests a range.
  2689. ScopedMockTransaction range_transaction(kRangeGET_TransactionOK);
  2690. range_transaction.request_headers = "Range: bytes = 0-29\r\n" EXTRA_HEADER;
  2691. MockHttpRequest request2(range_transaction);
  2692. {
  2693. auto& c = context_list[1];
  2694. c->result = cache.CreateTransaction(&c->trans);
  2695. ASSERT_THAT(c->result, IsOk());
  2696. EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
  2697. c->result =
  2698. c->trans->Start(&request2, c->callback.callback(), NetLogWithSource());
  2699. base::RunLoop().RunUntilIdle();
  2700. EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
  2701. }
  2702. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  2703. EXPECT_EQ(0, cache.disk_cache()->open_count());
  2704. EXPECT_EQ(1, cache.disk_cache()->create_count());
  2705. // Finish and verify the first request.
  2706. auto& c0 = context_list[0];
  2707. c0->result = c0->callback.WaitForResult();
  2708. ReadRemainingAndVerifyTransaction(c0->trans.get(), first_read, transaction);
  2709. // And the second.
  2710. auto& c1 = context_list[1];
  2711. c1->result = c1->callback.WaitForResult();
  2712. range_transaction.data = "rg: 00-09 rg: 10-19 rg: 20-29 ";
  2713. ReadAndVerifyTransaction(c1->trans.get(), range_transaction);
  2714. context_list.clear();
  2715. }
  2716. // Tests parallel validation on range requests with overlapping ranges.
  2717. TEST_F(HttpCacheTest, RangeGET_ParallelValidationOverlappingRanges) {
  2718. MockHttpCache cache;
  2719. ScopedMockTransaction transaction(kRangeGET_TransactionOK);
  2720. std::vector<std::unique_ptr<Context>> context_list;
  2721. const int kNumTransactions = 2;
  2722. for (int i = 0; i < kNumTransactions; ++i) {
  2723. context_list.push_back(std::make_unique<Context>());
  2724. }
  2725. // Let 1st transaction complete headers phase for ranges 40-49.
  2726. std::string first_read;
  2727. MockHttpRequest request1(transaction);
  2728. {
  2729. auto& c = context_list[0];
  2730. c->result = cache.CreateTransaction(&c->trans);
  2731. ASSERT_THAT(c->result, IsOk());
  2732. EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
  2733. c->result =
  2734. c->trans->Start(&request1, c->callback.callback(), NetLogWithSource());
  2735. base::RunLoop().RunUntilIdle();
  2736. // Start writing to the cache so that MockDiskEntry::CouldBeSparse() returns
  2737. // true.
  2738. const int kBufferSize = 5;
  2739. scoped_refptr<IOBuffer> buffer =
  2740. base::MakeRefCounted<IOBuffer>(kBufferSize);
  2741. ReleaseBufferCompletionCallback cb(buffer.get());
  2742. c->result = c->trans->Read(buffer.get(), kBufferSize, cb.callback());
  2743. EXPECT_EQ(kBufferSize, cb.GetResult(c->result));
  2744. std::string data_read(buffer->data(), kBufferSize);
  2745. first_read = data_read;
  2746. EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
  2747. }
  2748. // 2nd transaction requests ranges 30-49.
  2749. transaction.request_headers = "Range: bytes = 30-49\r\n" EXTRA_HEADER;
  2750. MockHttpRequest request2(transaction);
  2751. {
  2752. auto& c = context_list[1];
  2753. c->result = cache.CreateTransaction(&c->trans);
  2754. ASSERT_THAT(c->result, IsOk());
  2755. EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
  2756. c->result =
  2757. c->trans->Start(&request2, c->callback.callback(), NetLogWithSource());
  2758. base::RunLoop().RunUntilIdle();
  2759. EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
  2760. }
  2761. std::string cache_key = request1.CacheKey();
  2762. EXPECT_TRUE(cache.IsWriterPresent(cache_key));
  2763. EXPECT_EQ(1, cache.GetCountDoneHeadersQueue(cache_key));
  2764. // Should have created another transaction for the uncached range.
  2765. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  2766. EXPECT_EQ(0, cache.disk_cache()->open_count());
  2767. EXPECT_EQ(1, cache.disk_cache()->create_count());
  2768. for (int i = 0; i < kNumTransactions; ++i) {
  2769. auto& c = context_list[i];
  2770. if (c->result == ERR_IO_PENDING)
  2771. c->result = c->callback.WaitForResult();
  2772. if (i == 0) {
  2773. ReadRemainingAndVerifyTransaction(c->trans.get(), first_read,
  2774. transaction);
  2775. continue;
  2776. }
  2777. transaction.data = "rg: 30-39 rg: 40-49 ";
  2778. ReadAndVerifyTransaction(c->trans.get(), transaction);
  2779. }
  2780. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  2781. EXPECT_EQ(0, cache.disk_cache()->open_count());
  2782. EXPECT_EQ(1, cache.disk_cache()->create_count());
  2783. // Fetch from the cache to check that ranges 30-49 have been successfully
  2784. // cached.
  2785. {
  2786. MockTransaction range_transaction(kRangeGET_TransactionOK);
  2787. range_transaction.request_headers = "Range: bytes = 30-49\r\n" EXTRA_HEADER;
  2788. range_transaction.data = "rg: 30-39 rg: 40-49 ";
  2789. std::string headers;
  2790. RunTransactionTestWithResponse(cache.http_cache(), range_transaction,
  2791. &headers);
  2792. Verify206Response(headers, 30, 49);
  2793. }
  2794. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  2795. EXPECT_EQ(0, cache.disk_cache()->open_count());
  2796. EXPECT_EQ(1, cache.disk_cache()->create_count());
  2797. }
  2798. // Tests parallel validation on range requests with overlapping ranges and the
  2799. // impact of deleting the writer on transactions that have validated.
  2800. TEST_F(HttpCacheTest, RangeGET_ParallelValidationRestartDoneHeaders) {
  2801. MockHttpCache cache;
  2802. ScopedMockTransaction transaction(kRangeGET_TransactionOK);
  2803. std::vector<std::unique_ptr<Context>> context_list;
  2804. const int kNumTransactions = 2;
  2805. for (int i = 0; i < kNumTransactions; ++i) {
  2806. context_list.push_back(std::make_unique<Context>());
  2807. }
  2808. // Let 1st transaction complete headers phase for ranges 40-59.
  2809. std::string first_read;
  2810. transaction.request_headers = "Range: bytes = 40-59\r\n" EXTRA_HEADER;
  2811. MockHttpRequest request1(transaction);
  2812. {
  2813. auto& c = context_list[0];
  2814. c->result = cache.CreateTransaction(&c->trans);
  2815. ASSERT_THAT(c->result, IsOk());
  2816. EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
  2817. c->result =
  2818. c->trans->Start(&request1, c->callback.callback(), NetLogWithSource());
  2819. base::RunLoop().RunUntilIdle();
  2820. // Start writing to the cache so that MockDiskEntry::CouldBeSparse() returns
  2821. // true.
  2822. const int kBufferSize = 10;
  2823. scoped_refptr<IOBuffer> buffer =
  2824. base::MakeRefCounted<IOBuffer>(kBufferSize);
  2825. ReleaseBufferCompletionCallback cb(buffer.get());
  2826. c->result = c->trans->Read(buffer.get(), kBufferSize, cb.callback());
  2827. EXPECT_EQ(kBufferSize, cb.GetResult(c->result));
  2828. std::string data_read(buffer->data(), kBufferSize);
  2829. first_read = data_read;
  2830. EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
  2831. }
  2832. // 2nd transaction requests ranges 30-59.
  2833. transaction.request_headers = "Range: bytes = 30-59\r\n" EXTRA_HEADER;
  2834. MockHttpRequest request2(transaction);
  2835. {
  2836. auto& c = context_list[1];
  2837. c->result = cache.CreateTransaction(&c->trans);
  2838. ASSERT_THAT(c->result, IsOk());
  2839. EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
  2840. c->result =
  2841. c->trans->Start(&request2, c->callback.callback(), NetLogWithSource());
  2842. base::RunLoop().RunUntilIdle();
  2843. EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
  2844. }
  2845. std::string cache_key = request1.CacheKey();
  2846. EXPECT_TRUE(cache.IsWriterPresent(cache_key));
  2847. EXPECT_EQ(1, cache.GetCountDoneHeadersQueue(cache_key));
  2848. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  2849. EXPECT_EQ(0, cache.disk_cache()->open_count());
  2850. EXPECT_EQ(1, cache.disk_cache()->create_count());
  2851. // Delete the writer transaction.
  2852. context_list[0].reset();
  2853. base::RunLoop().RunUntilIdle();
  2854. transaction.data = "rg: 30-39 rg: 40-49 rg: 50-59 ";
  2855. ReadAndVerifyTransaction(context_list[1]->trans.get(), transaction);
  2856. // Create another network transaction since the 2nd transaction is restarted.
  2857. // 30-39 will be read from network, 40-49 from the cache and 50-59 from the
  2858. // network.
  2859. EXPECT_EQ(4, cache.network_layer()->transaction_count());
  2860. EXPECT_EQ(0, cache.disk_cache()->open_count());
  2861. EXPECT_EQ(1, cache.disk_cache()->create_count());
  2862. // Fetch from the cache to check that ranges 30-49 have been successfully
  2863. // cached.
  2864. {
  2865. MockTransaction range_transaction(kRangeGET_TransactionOK);
  2866. range_transaction.request_headers = "Range: bytes = 30-49\r\n" EXTRA_HEADER;
  2867. range_transaction.data = "rg: 30-39 rg: 40-49 ";
  2868. std::string headers;
  2869. RunTransactionTestWithResponse(cache.http_cache(), range_transaction,
  2870. &headers);
  2871. Verify206Response(headers, 30, 49);
  2872. }
  2873. EXPECT_EQ(4, cache.network_layer()->transaction_count());
  2874. EXPECT_EQ(1, cache.disk_cache()->open_count());
  2875. EXPECT_EQ(1, cache.disk_cache()->create_count());
  2876. }
  2877. // A test of doing a range request to a cached 301 response
  2878. TEST_F(HttpCacheTest, RangeGET_CachedRedirect) {
  2879. RangeTransactionServer handler;
  2880. handler.set_redirect(true);
  2881. MockHttpCache cache;
  2882. ScopedMockTransaction transaction(kRangeGET_TransactionOK);
  2883. transaction.request_headers = "Range: bytes = 0-\r\n" EXTRA_HEADER;
  2884. transaction.status = "HTTP/1.1 301 Moved Permanently";
  2885. transaction.response_headers = "Location: /elsewhere\nContent-Length:5";
  2886. transaction.data = "12345";
  2887. MockHttpRequest request(transaction);
  2888. TestCompletionCallback callback;
  2889. // Write to the cache.
  2890. {
  2891. std::unique_ptr<HttpTransaction> trans;
  2892. ASSERT_THAT(cache.CreateTransaction(&trans), IsOk());
  2893. int rv = trans->Start(&request, callback.callback(), NetLogWithSource());
  2894. if (rv == ERR_IO_PENDING)
  2895. rv = callback.WaitForResult();
  2896. ASSERT_THAT(rv, IsOk());
  2897. const HttpResponseInfo* info = trans->GetResponseInfo();
  2898. ASSERT_TRUE(info);
  2899. EXPECT_EQ(info->headers->response_code(), 301);
  2900. std::string location;
  2901. info->headers->EnumerateHeader(nullptr, "Location", &location);
  2902. EXPECT_EQ(location, "/elsewhere");
  2903. ReadAndVerifyTransaction(trans.get(), transaction);
  2904. }
  2905. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  2906. EXPECT_EQ(0, cache.disk_cache()->open_count());
  2907. EXPECT_EQ(1, cache.disk_cache()->create_count());
  2908. // Active entries in the cache are not retired synchronously. Make
  2909. // sure the next run hits the MockHttpCache and open_count is
  2910. // correct.
  2911. base::RunLoop().RunUntilIdle();
  2912. // Read from the cache.
  2913. {
  2914. std::unique_ptr<HttpTransaction> trans;
  2915. ASSERT_THAT(cache.CreateTransaction(&trans), IsOk());
  2916. int rv = trans->Start(&request, callback.callback(), NetLogWithSource());
  2917. if (rv == ERR_IO_PENDING)
  2918. rv = callback.WaitForResult();
  2919. ASSERT_THAT(rv, IsOk());
  2920. const HttpResponseInfo* info = trans->GetResponseInfo();
  2921. ASSERT_TRUE(info);
  2922. EXPECT_EQ(info->headers->response_code(), 301);
  2923. std::string location;
  2924. info->headers->EnumerateHeader(nullptr, "Location", &location);
  2925. EXPECT_EQ(location, "/elsewhere");
  2926. trans->DoneReading();
  2927. }
  2928. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  2929. EXPECT_EQ(1, cache.disk_cache()->open_count());
  2930. EXPECT_EQ(1, cache.disk_cache()->create_count());
  2931. // Now read the full body. This normally would not be done for a 301 by
  2932. // higher layers, but e.g. a 500 could hit a further bug here.
  2933. {
  2934. std::unique_ptr<HttpTransaction> trans;
  2935. ASSERT_THAT(cache.CreateTransaction(&trans), IsOk());
  2936. int rv = trans->Start(&request, callback.callback(), NetLogWithSource());
  2937. if (rv == ERR_IO_PENDING)
  2938. rv = callback.WaitForResult();
  2939. ASSERT_THAT(rv, IsOk());
  2940. const HttpResponseInfo* info = trans->GetResponseInfo();
  2941. ASSERT_TRUE(info);
  2942. EXPECT_EQ(info->headers->response_code(), 301);
  2943. std::string location;
  2944. info->headers->EnumerateHeader(nullptr, "Location", &location);
  2945. EXPECT_EQ(location, "/elsewhere");
  2946. ReadAndVerifyTransaction(trans.get(), transaction);
  2947. }
  2948. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  2949. // No extra open since it picks up a previous ActiveEntry.
  2950. EXPECT_EQ(1, cache.disk_cache()->open_count());
  2951. EXPECT_EQ(1, cache.disk_cache()->create_count());
  2952. }
  2953. // A transaction that fails to validate an entry, while attempting to write
  2954. // the response, should still get data to its consumer even if the attempt to
  2955. // create a new entry fails.
  2956. TEST_F(HttpCacheTest, SimpleGET_ValidationFailureWithCreateFailure) {
  2957. MockHttpCache cache;
  2958. MockHttpRequest request(kSimpleGET_Transaction);
  2959. request.load_flags |= LOAD_VALIDATE_CACHE;
  2960. std::vector<std::unique_ptr<Context>> context_list;
  2961. // Create and run the first, successful, transaction to prime the cache.
  2962. context_list.push_back(std::make_unique<Context>());
  2963. auto& c1 = context_list.back();
  2964. c1->result = cache.CreateTransaction(&c1->trans);
  2965. ASSERT_THAT(c1->result, IsOk());
  2966. EXPECT_EQ(LOAD_STATE_IDLE, c1->trans->GetLoadState());
  2967. c1->result =
  2968. c1->trans->Start(&request, c1->callback.callback(), NetLogWithSource());
  2969. EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, c1->trans->GetLoadState());
  2970. base::RunLoop().RunUntilIdle();
  2971. EXPECT_TRUE(cache.IsWriterPresent(request.CacheKey()));
  2972. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  2973. EXPECT_EQ(0, cache.disk_cache()->open_count());
  2974. EXPECT_EQ(1, cache.disk_cache()->create_count());
  2975. // Create and start the second transaction, which will fail its validation
  2976. // during the call to RunUntilIdle().
  2977. context_list.push_back(std::make_unique<Context>());
  2978. auto& c2 = context_list.back();
  2979. c2->result = cache.CreateTransaction(&c2->trans);
  2980. ASSERT_THAT(c2->result, IsOk());
  2981. EXPECT_EQ(LOAD_STATE_IDLE, c2->trans->GetLoadState());
  2982. c2->result =
  2983. c2->trans->Start(&request, c2->callback.callback(), NetLogWithSource());
  2984. // Expect idle at this point because we should be able to find and use the
  2985. // Active Entry that c1 created instead of waiting on the cache to open the
  2986. // entry.
  2987. EXPECT_EQ(LOAD_STATE_IDLE, c2->trans->GetLoadState());
  2988. cache.disk_cache()->set_fail_requests(true);
  2989. // The transaction, c2, should now attempt to validate the entry, fail when it
  2990. // receives a 200 OK response, attempt to create a new entry, fail to create,
  2991. // and then continue onward without an entry.
  2992. base::RunLoop().RunUntilIdle();
  2993. // All requests depend on the writer, and the writer is between Start and
  2994. // Read, i.e. idle.
  2995. for (auto& context : context_list) {
  2996. EXPECT_EQ(LOAD_STATE_IDLE, context->trans->GetLoadState());
  2997. }
  2998. // Confirm that both transactions correctly Read() the data.
  2999. for (auto& context : context_list) {
  3000. if (context->result == ERR_IO_PENDING)
  3001. context->result = context->callback.WaitForResult();
  3002. ReadAndVerifyTransaction(context->trans.get(), kSimpleGET_Transaction);
  3003. }
  3004. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  3005. EXPECT_EQ(0, cache.disk_cache()->open_count());
  3006. EXPECT_EQ(1, cache.disk_cache()->create_count());
  3007. }
  3008. // Parallel validation results in 200.
  3009. TEST_F(HttpCacheTest, SimpleGET_ParallelValidationNoMatch) {
  3010. MockHttpCache cache;
  3011. MockHttpRequest request(kSimpleGET_Transaction);
  3012. request.load_flags |= LOAD_VALIDATE_CACHE;
  3013. std::vector<std::unique_ptr<Context>> context_list;
  3014. const int kNumTransactions = 5;
  3015. for (int i = 0; i < kNumTransactions; ++i) {
  3016. context_list.push_back(std::make_unique<Context>());
  3017. auto& c = context_list[i];
  3018. c->result = cache.CreateTransaction(&c->trans);
  3019. ASSERT_THAT(c->result, IsOk());
  3020. EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
  3021. c->result =
  3022. c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
  3023. }
  3024. // All requests are waiting for the active entry.
  3025. for (auto& context : context_list) {
  3026. EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, context->trans->GetLoadState());
  3027. }
  3028. // Allow all requests to move from the Create queue to the active entry.
  3029. base::RunLoop().RunUntilIdle();
  3030. // The first request should be a writer at this point, and the subsequent
  3031. // requests should have passed the validation phase and created their own
  3032. // entries since none of them matched the headers of the earlier one.
  3033. EXPECT_TRUE(cache.IsWriterPresent(request.CacheKey()));
  3034. EXPECT_EQ(5, cache.network_layer()->transaction_count());
  3035. EXPECT_EQ(0, cache.disk_cache()->open_count());
  3036. EXPECT_EQ(5, cache.disk_cache()->create_count());
  3037. // All requests depend on the writer, and the writer is between Start and
  3038. // Read, i.e. idle.
  3039. for (auto& context : context_list) {
  3040. EXPECT_EQ(LOAD_STATE_IDLE, context->trans->GetLoadState());
  3041. }
  3042. for (auto& context : context_list) {
  3043. if (context->result == ERR_IO_PENDING)
  3044. context->result = context->callback.WaitForResult();
  3045. ReadAndVerifyTransaction(context->trans.get(), kSimpleGET_Transaction);
  3046. }
  3047. EXPECT_EQ(5, cache.network_layer()->transaction_count());
  3048. EXPECT_EQ(0, cache.disk_cache()->open_count());
  3049. EXPECT_EQ(5, cache.disk_cache()->create_count());
  3050. }
  3051. TEST_F(HttpCacheTest, RangeGET_Enormous) {
  3052. // Test for how blockfile's limit on range namespace interacts with
  3053. // HttpCache::Transaction.
  3054. // See https://crbug.com/770694
  3055. base::ScopedTempDir temp_dir;
  3056. ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
  3057. auto backend_factory = std::make_unique<HttpCache::DefaultBackend>(
  3058. DISK_CACHE, CACHE_BACKEND_BLOCKFILE,
  3059. /*file_operations_factory=*/nullptr, temp_dir.GetPath(), 1024 * 1024,
  3060. false);
  3061. MockHttpCache cache(std::move(backend_factory));
  3062. RangeTransactionServer handler;
  3063. handler.set_length(2305843009213693962);
  3064. // Prime with a range it can store.
  3065. {
  3066. ScopedMockTransaction transaction(kRangeGET_TransactionOK);
  3067. transaction.request_headers = "Range: bytes = 0-9\r\n" EXTRA_HEADER;
  3068. transaction.data = "rg: 00-09 ";
  3069. MockHttpRequest request(transaction);
  3070. HttpResponseInfo response;
  3071. RunTransactionTestWithRequest(cache.http_cache(), transaction, request,
  3072. &response);
  3073. ASSERT_TRUE(response.headers != nullptr);
  3074. EXPECT_EQ(206, response.headers->response_code());
  3075. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  3076. }
  3077. // Try with a range it can't. Should still work.
  3078. {
  3079. ScopedMockTransaction transaction(kRangeGET_TransactionOK);
  3080. transaction.request_headers =
  3081. "Range: bytes = "
  3082. "2305843009213693952-2305843009213693961\r\n" EXTRA_HEADER;
  3083. transaction.data = "rg: 52-61 ";
  3084. MockHttpRequest request(transaction);
  3085. HttpResponseInfo response;
  3086. RunTransactionTestWithRequest(cache.http_cache(), transaction, request,
  3087. &response);
  3088. ASSERT_TRUE(response.headers != nullptr);
  3089. EXPECT_EQ(206, response.headers->response_code());
  3090. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  3091. }
  3092. // Can't actually cache it due to backend limitations. If the network
  3093. // transaction count is 2, this test isn't covering what it needs to.
  3094. {
  3095. ScopedMockTransaction transaction(kRangeGET_TransactionOK);
  3096. transaction.request_headers =
  3097. "Range: bytes = "
  3098. "2305843009213693952-2305843009213693961\r\n" EXTRA_HEADER;
  3099. transaction.data = "rg: 52-61 ";
  3100. MockHttpRequest request(transaction);
  3101. HttpResponseInfo response;
  3102. RunTransactionTestWithRequest(cache.http_cache(), transaction, request,
  3103. &response);
  3104. ASSERT_TRUE(response.headers != nullptr);
  3105. EXPECT_EQ(206, response.headers->response_code());
  3106. EXPECT_EQ(3, cache.network_layer()->transaction_count());
  3107. }
  3108. }
  3109. // Parallel validation results in 200 for 1 transaction and validation matches
  3110. // for subsequent transactions.
  3111. TEST_F(HttpCacheTest, SimpleGET_ParallelValidationNoMatch1) {
  3112. MockHttpCache cache;
  3113. MockHttpRequest request(kSimpleGET_Transaction);
  3114. MockTransaction transaction(kSimpleGET_Transaction);
  3115. transaction.load_flags |= LOAD_VALIDATE_CACHE;
  3116. MockHttpRequest validate_request(transaction);
  3117. std::vector<std::unique_ptr<Context>> context_list;
  3118. const int kNumTransactions = 5;
  3119. for (int i = 0; i < kNumTransactions; ++i) {
  3120. context_list.push_back(std::make_unique<Context>());
  3121. auto& c = context_list[i];
  3122. c->result = cache.CreateTransaction(&c->trans);
  3123. ASSERT_THAT(c->result, IsOk());
  3124. EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
  3125. MockHttpRequest* this_request = &request;
  3126. if (i == 1)
  3127. this_request = &validate_request;
  3128. c->result = c->trans->Start(this_request, c->callback.callback(),
  3129. NetLogWithSource());
  3130. }
  3131. // All requests are waiting for the active entry.
  3132. for (auto& context : context_list) {
  3133. EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, context->trans->GetLoadState());
  3134. }
  3135. // Allow all requests to move from the Create queue to the active entry.
  3136. base::RunLoop().RunUntilIdle();
  3137. // The new entry will have all the transactions except the first one which
  3138. // will continue in the doomed entry.
  3139. EXPECT_EQ(kNumTransactions - 1,
  3140. cache.GetCountWriterTransactions(validate_request.CacheKey()));
  3141. EXPECT_EQ(1, cache.disk_cache()->doomed_count());
  3142. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  3143. EXPECT_EQ(0, cache.disk_cache()->open_count());
  3144. EXPECT_EQ(2, cache.disk_cache()->create_count());
  3145. for (auto& context : context_list) {
  3146. EXPECT_EQ(LOAD_STATE_IDLE, context->trans->GetLoadState());
  3147. }
  3148. for (auto& context : context_list) {
  3149. if (context->result == ERR_IO_PENDING)
  3150. context->result = context->callback.WaitForResult();
  3151. ReadAndVerifyTransaction(context->trans.get(), kSimpleGET_Transaction);
  3152. }
  3153. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  3154. EXPECT_EQ(0, cache.disk_cache()->open_count());
  3155. EXPECT_EQ(2, cache.disk_cache()->create_count());
  3156. }
  3157. // Tests that a GET followed by a DELETE results in DELETE immediately starting
  3158. // the headers phase and the entry is doomed.
  3159. TEST_F(HttpCacheTest, SimpleGET_ParallelValidationDelete) {
  3160. MockHttpCache cache;
  3161. MockHttpRequest request(kSimpleGET_Transaction);
  3162. request.load_flags |= LOAD_VALIDATE_CACHE;
  3163. MockHttpRequest delete_request(kSimpleGET_Transaction);
  3164. delete_request.method = "DELETE";
  3165. std::vector<std::unique_ptr<Context>> context_list;
  3166. const int kNumTransactions = 2;
  3167. for (int i = 0; i < kNumTransactions; ++i) {
  3168. context_list.push_back(std::make_unique<Context>());
  3169. auto& c = context_list[i];
  3170. MockHttpRequest* this_request = &request;
  3171. if (i == 1)
  3172. this_request = &delete_request;
  3173. c->result = cache.CreateTransaction(&c->trans);
  3174. ASSERT_THAT(c->result, IsOk());
  3175. EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
  3176. c->result = c->trans->Start(this_request, c->callback.callback(),
  3177. NetLogWithSource());
  3178. }
  3179. // All requests are waiting for the active entry.
  3180. for (auto& context : context_list) {
  3181. EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, context->trans->GetLoadState());
  3182. }
  3183. // Allow all requests to move from the Create queue to the active entry.
  3184. base::RunLoop().RunUntilIdle();
  3185. // The first request should be a writer at this point, and the subsequent
  3186. // request should have passed the validation phase and doomed the existing
  3187. // entry.
  3188. EXPECT_TRUE(cache.disk_cache()->IsDiskEntryDoomed(request.CacheKey()));
  3189. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  3190. EXPECT_EQ(0, cache.disk_cache()->open_count());
  3191. EXPECT_EQ(1, cache.disk_cache()->create_count());
  3192. // All requests depend on the writer, and the writer is between Start and
  3193. // Read, i.e. idle.
  3194. for (auto& context : context_list) {
  3195. EXPECT_EQ(LOAD_STATE_IDLE, context->trans->GetLoadState());
  3196. }
  3197. for (auto& context : context_list) {
  3198. if (context->result == ERR_IO_PENDING)
  3199. context->result = context->callback.WaitForResult();
  3200. ReadAndVerifyTransaction(context->trans.get(), kSimpleGET_Transaction);
  3201. }
  3202. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  3203. EXPECT_EQ(0, cache.disk_cache()->open_count());
  3204. EXPECT_EQ(1, cache.disk_cache()->create_count());
  3205. }
  3206. // Tests that a transaction which is in validated queue can be destroyed without
  3207. // any impact to other transactions.
  3208. TEST_F(HttpCacheTest, SimpleGET_ParallelValidationCancelValidated) {
  3209. MockHttpCache cache;
  3210. MockHttpRequest request(kSimpleGET_Transaction);
  3211. MockTransaction transaction(kSimpleGET_Transaction);
  3212. transaction.load_flags |= LOAD_ONLY_FROM_CACHE;
  3213. MockHttpRequest read_only_request(transaction);
  3214. std::vector<std::unique_ptr<Context>> context_list;
  3215. const int kNumTransactions = 2;
  3216. for (int i = 0; i < kNumTransactions; ++i) {
  3217. context_list.push_back(std::make_unique<Context>());
  3218. auto& c = context_list[i];
  3219. c->result = cache.CreateTransaction(&c->trans);
  3220. ASSERT_THAT(c->result, IsOk());
  3221. MockHttpRequest* current_request = i == 1 ? &read_only_request : &request;
  3222. c->result = c->trans->Start(current_request, c->callback.callback(),
  3223. NetLogWithSource());
  3224. }
  3225. // Allow all requests to move from the Create queue to the active entry.
  3226. base::RunLoop().RunUntilIdle();
  3227. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  3228. EXPECT_EQ(0, cache.disk_cache()->open_count());
  3229. EXPECT_EQ(1, cache.disk_cache()->create_count());
  3230. std::string cache_key = request.CacheKey();
  3231. EXPECT_EQ(1, cache.GetCountWriterTransactions(cache_key));
  3232. EXPECT_EQ(1, cache.GetCountDoneHeadersQueue(cache_key));
  3233. context_list[1].reset();
  3234. EXPECT_EQ(0, cache.GetCountDoneHeadersQueue(cache_key));
  3235. // Complete the rest of the transactions.
  3236. for (auto& context : context_list) {
  3237. if (!context)
  3238. continue;
  3239. ReadAndVerifyTransaction(context->trans.get(), kSimpleGET_Transaction);
  3240. }
  3241. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  3242. EXPECT_EQ(0, cache.disk_cache()->open_count());
  3243. EXPECT_EQ(1, cache.disk_cache()->create_count());
  3244. }
  3245. // Tests that an idle writer transaction can be deleted without impacting the
  3246. // existing writers.
  3247. TEST_F(HttpCacheTest, SimpleGET_ParallelWritingCancelIdleTransaction) {
  3248. MockHttpCache cache;
  3249. MockHttpRequest request(kSimpleGET_Transaction);
  3250. std::vector<std::unique_ptr<Context>> context_list;
  3251. const int kNumTransactions = 2;
  3252. for (int i = 0; i < kNumTransactions; ++i) {
  3253. context_list.push_back(std::make_unique<Context>());
  3254. auto& c = context_list[i];
  3255. c->result = cache.CreateTransaction(&c->trans);
  3256. ASSERT_THAT(c->result, IsOk());
  3257. c->result =
  3258. c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
  3259. }
  3260. // Allow all requests to move from the Create queue to the active entry.
  3261. base::RunLoop().RunUntilIdle();
  3262. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  3263. EXPECT_EQ(0, cache.disk_cache()->open_count());
  3264. EXPECT_EQ(1, cache.disk_cache()->create_count());
  3265. // Both transactions would be added to writers.
  3266. std::string cache_key = request.CacheKey();
  3267. EXPECT_EQ(kNumTransactions, cache.GetCountWriterTransactions(cache_key));
  3268. context_list[1].reset();
  3269. EXPECT_EQ(kNumTransactions - 1, cache.GetCountWriterTransactions(cache_key));
  3270. // Complete the rest of the transactions.
  3271. for (auto& context : context_list) {
  3272. if (!context)
  3273. continue;
  3274. ReadAndVerifyTransaction(context->trans.get(), kSimpleGET_Transaction);
  3275. }
  3276. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  3277. EXPECT_EQ(0, cache.disk_cache()->open_count());
  3278. EXPECT_EQ(1, cache.disk_cache()->create_count());
  3279. }
  3280. // Tests that a transaction which is in validated queue can timeout and start
  3281. // the headers phase again.
  3282. TEST_F(HttpCacheTest, SimpleGET_ParallelValidationValidatedTimeout) {
  3283. MockHttpCache cache;
  3284. MockHttpRequest request(kSimpleGET_Transaction);
  3285. MockTransaction transaction(kSimpleGET_Transaction);
  3286. transaction.load_flags |= LOAD_ONLY_FROM_CACHE;
  3287. MockHttpRequest read_only_request(transaction);
  3288. std::vector<std::unique_ptr<Context>> context_list;
  3289. const int kNumTransactions = 2;
  3290. for (int i = 0; i < kNumTransactions; ++i) {
  3291. context_list.push_back(std::make_unique<Context>());
  3292. auto& c = context_list[i];
  3293. MockHttpRequest* this_request = &request;
  3294. if (i == 1) {
  3295. this_request = &read_only_request;
  3296. cache.SimulateCacheLockTimeoutAfterHeaders();
  3297. }
  3298. c->result = cache.CreateTransaction(&c->trans);
  3299. ASSERT_THAT(c->result, IsOk());
  3300. c->result = c->trans->Start(this_request, c->callback.callback(),
  3301. NetLogWithSource());
  3302. }
  3303. // Allow all requests to move from the Create queue to the active entry.
  3304. base::RunLoop().RunUntilIdle();
  3305. // The first request should be a writer at this point, and the subsequent
  3306. // requests should have completed validation, timed out and restarted.
  3307. // Since it is a read only request, it will error out.
  3308. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  3309. EXPECT_EQ(0, cache.disk_cache()->open_count());
  3310. EXPECT_EQ(1, cache.disk_cache()->create_count());
  3311. std::string cache_key = request.CacheKey();
  3312. EXPECT_TRUE(cache.IsWriterPresent(cache_key));
  3313. EXPECT_EQ(0, cache.GetCountDoneHeadersQueue(cache_key));
  3314. base::RunLoop().RunUntilIdle();
  3315. int rv = context_list[1]->callback.WaitForResult();
  3316. EXPECT_EQ(ERR_CACHE_MISS, rv);
  3317. ReadAndVerifyTransaction(context_list[0]->trans.get(),
  3318. kSimpleGET_Transaction);
  3319. }
  3320. // Tests that a transaction which is in readers can be destroyed without
  3321. // any impact to other transactions.
  3322. TEST_F(HttpCacheTest, SimpleGET_ParallelValidationCancelReader) {
  3323. MockHttpCache cache;
  3324. MockHttpRequest request(kSimpleGET_Transaction);
  3325. MockTransaction transaction(kSimpleGET_Transaction);
  3326. transaction.load_flags |= LOAD_VALIDATE_CACHE;
  3327. MockHttpRequest validate_request(transaction);
  3328. int kNumTransactions = 4;
  3329. std::vector<std::unique_ptr<Context>> context_list;
  3330. for (int i = 0; i < kNumTransactions; ++i) {
  3331. context_list.push_back(std::make_unique<Context>());
  3332. auto& c = context_list[i];
  3333. c->result = cache.CreateTransaction(&c->trans);
  3334. ASSERT_THAT(c->result, IsOk());
  3335. MockHttpRequest* this_request = &request;
  3336. if (i == 3) {
  3337. this_request = &validate_request;
  3338. c->trans->SetBeforeNetworkStartCallback(base::BindOnce(&DeferCallback));
  3339. }
  3340. c->result = c->trans->Start(this_request, c->callback.callback(),
  3341. NetLogWithSource());
  3342. }
  3343. // Allow all requests to move from the Create queue to the active entry.
  3344. base::RunLoop().RunUntilIdle();
  3345. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  3346. EXPECT_EQ(0, cache.disk_cache()->open_count());
  3347. EXPECT_EQ(1, cache.disk_cache()->create_count());
  3348. std::string cache_key = request.CacheKey();
  3349. EXPECT_EQ(kNumTransactions - 1, cache.GetCountWriterTransactions(cache_key));
  3350. EXPECT_TRUE(cache.IsHeadersTransactionPresent(cache_key));
  3351. // Complete the response body.
  3352. ReadAndVerifyTransaction(context_list[0]->trans.get(),
  3353. kSimpleGET_Transaction);
  3354. // Rest of the transactions should move to readers.
  3355. EXPECT_FALSE(cache.IsWriterPresent(cache_key));
  3356. EXPECT_EQ(kNumTransactions - 2, cache.GetCountReaders(cache_key));
  3357. EXPECT_EQ(0, cache.GetCountDoneHeadersQueue(cache_key));
  3358. EXPECT_TRUE(cache.IsHeadersTransactionPresent(cache_key));
  3359. // Add 2 new transactions.
  3360. kNumTransactions = 6;
  3361. for (int i = 4; i < kNumTransactions; ++i) {
  3362. context_list.push_back(std::make_unique<Context>());
  3363. auto& c = context_list[i];
  3364. c->result = cache.CreateTransaction(&c->trans);
  3365. ASSERT_THAT(c->result, IsOk());
  3366. c->result =
  3367. c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
  3368. }
  3369. EXPECT_EQ(2, cache.GetCountAddToEntryQueue(cache_key));
  3370. // Delete a reader.
  3371. context_list[1].reset();
  3372. // Deleting the reader did not impact any other transaction.
  3373. EXPECT_EQ(1, cache.GetCountReaders(cache_key));
  3374. EXPECT_EQ(2, cache.GetCountAddToEntryQueue(cache_key));
  3375. EXPECT_TRUE(cache.IsHeadersTransactionPresent(cache_key));
  3376. // Resume network start for headers_transaction. It will doom the entry as it
  3377. // will be a 200 and will go to network for the response body.
  3378. context_list[3]->trans->ResumeNetworkStart();
  3379. // The pending transactions will be added to a new entry as writers.
  3380. base::RunLoop().RunUntilIdle();
  3381. EXPECT_EQ(3, cache.GetCountWriterTransactions(cache_key));
  3382. // Complete the rest of the transactions.
  3383. for (int i = 2; i < kNumTransactions; ++i) {
  3384. ReadAndVerifyTransaction(context_list[i]->trans.get(),
  3385. kSimpleGET_Transaction);
  3386. }
  3387. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  3388. EXPECT_EQ(0, cache.disk_cache()->open_count());
  3389. EXPECT_EQ(2, cache.disk_cache()->create_count());
  3390. }
  3391. // Tests that when the only writer goes away, it immediately cleans up rather
  3392. // than wait for the network request to finish. See https://crbug.com/804868.
  3393. TEST_F(HttpCacheTest, SimpleGET_HangingCacheWriteCleanup) {
  3394. MockHttpCache mock_cache;
  3395. MockHttpRequest request(kSimpleGET_Transaction);
  3396. std::unique_ptr<HttpTransaction> transaction;
  3397. mock_cache.CreateTransaction(&transaction);
  3398. TestCompletionCallback callback;
  3399. int result =
  3400. transaction->Start(&request, callback.callback(), NetLogWithSource());
  3401. // Get the transaction ready to read.
  3402. result = callback.GetResult(result);
  3403. // Read the first byte.
  3404. scoped_refptr<IOBuffer> buffer = base::MakeRefCounted<IOBuffer>(1);
  3405. ReleaseBufferCompletionCallback buffer_callback(buffer.get());
  3406. result = transaction->Read(buffer.get(), 1, buffer_callback.callback());
  3407. EXPECT_EQ(1, buffer_callback.GetResult(result));
  3408. // Read the second byte, but leave the cache write hanging.
  3409. std::string cache_key = request.CacheKey();
  3410. scoped_refptr<MockDiskEntry> entry =
  3411. mock_cache.disk_cache()->GetDiskEntryRef(cache_key);
  3412. entry->SetDefer(MockDiskEntry::DEFER_WRITE);
  3413. buffer = base::MakeRefCounted<IOBuffer>(1);
  3414. ReleaseBufferCompletionCallback buffer_callback2(buffer.get());
  3415. result = transaction->Read(buffer.get(), 1, buffer_callback2.callback());
  3416. EXPECT_EQ(ERR_IO_PENDING, result);
  3417. base::RunLoop().RunUntilIdle();
  3418. EXPECT_TRUE(mock_cache.IsWriterPresent(cache_key));
  3419. // At this point the next byte should have been read from the network but is
  3420. // waiting to be written to the cache. Destroy the transaction and make sure
  3421. // that everything has been cleaned up.
  3422. transaction = nullptr;
  3423. EXPECT_FALSE(mock_cache.IsWriterPresent(cache_key));
  3424. EXPECT_FALSE(mock_cache.network_layer()->last_transaction());
  3425. }
  3426. // Tests that a transaction writer can be destroyed mid-read.
  3427. // A waiting for read transaction should be able to read the data that was
  3428. // driven by the Read started by the cancelled writer.
  3429. TEST_F(HttpCacheTest, SimpleGET_ParallelWritingCancelWriter) {
  3430. MockHttpCache cache;
  3431. MockHttpRequest request(kSimpleGET_Transaction);
  3432. MockTransaction transaction(kSimpleGET_Transaction);
  3433. transaction.load_flags |= LOAD_VALIDATE_CACHE;
  3434. MockHttpRequest validate_request(transaction);
  3435. const int kNumTransactions = 3;
  3436. std::vector<std::unique_ptr<Context>> context_list;
  3437. for (int i = 0; i < kNumTransactions; ++i) {
  3438. context_list.push_back(std::make_unique<Context>());
  3439. auto& c = context_list[i];
  3440. c->result = cache.CreateTransaction(&c->trans);
  3441. ASSERT_THAT(c->result, IsOk());
  3442. MockHttpRequest* this_request = &request;
  3443. if (i == 2) {
  3444. this_request = &validate_request;
  3445. c->trans->SetBeforeNetworkStartCallback(base::BindOnce(&DeferCallback));
  3446. }
  3447. c->result = c->trans->Start(this_request, c->callback.callback(),
  3448. NetLogWithSource());
  3449. }
  3450. // Allow all requests to move from the Create queue to the active entry.
  3451. base::RunLoop().RunUntilIdle();
  3452. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  3453. EXPECT_EQ(0, cache.disk_cache()->open_count());
  3454. EXPECT_EQ(1, cache.disk_cache()->create_count());
  3455. std::string cache_key = validate_request.CacheKey();
  3456. EXPECT_TRUE(cache.IsHeadersTransactionPresent(cache_key));
  3457. EXPECT_EQ(2, cache.GetCountWriterTransactions(cache_key));
  3458. // Initiate Read from both writers and kill 1 of them mid-read.
  3459. std::string first_read;
  3460. for (int i = 0; i < 2; i++) {
  3461. auto& c = context_list[i];
  3462. const int kBufferSize = 5;
  3463. scoped_refptr<IOBuffer> buffer =
  3464. base::MakeRefCounted<IOBuffer>(kBufferSize);
  3465. ReleaseBufferCompletionCallback cb(buffer.get());
  3466. c->result = c->trans->Read(buffer.get(), kBufferSize, cb.callback());
  3467. EXPECT_EQ(ERR_IO_PENDING, c->result);
  3468. // Deleting one writer at this point will not impact other transactions
  3469. // since writers contain more transactions.
  3470. if (i == 1) {
  3471. context_list[0].reset();
  3472. base::RunLoop().RunUntilIdle();
  3473. EXPECT_EQ(kBufferSize, cb.GetResult(c->result));
  3474. std::string data_read(buffer->data(), kBufferSize);
  3475. first_read = data_read;
  3476. }
  3477. }
  3478. // Resume network start for headers_transaction. It will doom the existing
  3479. // entry and create a new entry due to validation returning a 200.
  3480. auto& c = context_list[2];
  3481. c->trans->ResumeNetworkStart();
  3482. base::RunLoop().RunUntilIdle();
  3483. EXPECT_EQ(1, cache.GetCountWriterTransactions(cache_key));
  3484. // Complete the rest of the transactions.
  3485. for (int i = 0; i < kNumTransactions; i++) {
  3486. auto& context = context_list[i];
  3487. if (!context)
  3488. continue;
  3489. if (i == 1)
  3490. ReadRemainingAndVerifyTransaction(context->trans.get(), first_read,
  3491. kSimpleGET_Transaction);
  3492. else
  3493. ReadAndVerifyTransaction(context->trans.get(), kSimpleGET_Transaction);
  3494. }
  3495. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  3496. EXPECT_EQ(0, cache.disk_cache()->open_count());
  3497. EXPECT_EQ(2, cache.disk_cache()->create_count());
  3498. }
  3499. // Tests the case when network read failure happens. Idle and waiting
  3500. // transactions should fail and headers transaction should be restarted.
  3501. TEST_F(HttpCacheTest, SimpleGET_ParallelWritingNetworkReadFailed) {
  3502. MockHttpCache cache;
  3503. ScopedMockTransaction fail_transaction(kSimpleGET_Transaction);
  3504. fail_transaction.read_return_code = ERR_INTERNET_DISCONNECTED;
  3505. MockHttpRequest failing_request(fail_transaction);
  3506. MockHttpRequest request(kSimpleGET_Transaction);
  3507. MockTransaction transaction(kSimpleGET_Transaction);
  3508. transaction.load_flags |= LOAD_ONLY_FROM_CACHE;
  3509. MockHttpRequest read_request(transaction);
  3510. const int kNumTransactions = 4;
  3511. std::vector<std::unique_ptr<Context>> context_list;
  3512. for (int i = 0; i < kNumTransactions; ++i) {
  3513. context_list.push_back(std::make_unique<Context>());
  3514. auto& c = context_list[i];
  3515. c->result = cache.CreateTransaction(&c->trans);
  3516. ASSERT_THAT(c->result, IsOk());
  3517. MockHttpRequest* this_request = &request;
  3518. if (i == 0)
  3519. this_request = &failing_request;
  3520. if (i == 3)
  3521. this_request = &read_request;
  3522. c->result = c->trans->Start(this_request, c->callback.callback(),
  3523. NetLogWithSource());
  3524. }
  3525. // Allow all requests to move from the Create queue to the active entry.
  3526. base::RunLoop().RunUntilIdle();
  3527. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  3528. EXPECT_EQ(0, cache.disk_cache()->open_count());
  3529. EXPECT_EQ(1, cache.disk_cache()->create_count());
  3530. std::string cache_key = read_request.CacheKey();
  3531. EXPECT_EQ(3, cache.GetCountWriterTransactions(cache_key));
  3532. EXPECT_EQ(1, cache.GetCountDoneHeadersQueue(cache_key));
  3533. // Initiate Read from two writers and let the first get a network failure.
  3534. for (int i = 0; i < 2; i++) {
  3535. auto& c = context_list[i];
  3536. const int kBufferSize = 5;
  3537. scoped_refptr<IOBuffer> buffer =
  3538. base::MakeRefCounted<IOBuffer>(kBufferSize);
  3539. c->result =
  3540. c->trans->Read(buffer.get(), kBufferSize, c->callback.callback());
  3541. EXPECT_EQ(ERR_IO_PENDING, c->result);
  3542. }
  3543. base::RunLoop().RunUntilIdle();
  3544. for (int i = 0; i < 2; i++) {
  3545. auto& c = context_list[i];
  3546. c->result = c->callback.WaitForResult();
  3547. EXPECT_EQ(ERR_INTERNET_DISCONNECTED, c->result);
  3548. }
  3549. // The entry should have been doomed and destroyed and the headers transaction
  3550. // restarted. Since headers transaction is read-only it will error out.
  3551. auto& read_only = context_list[3];
  3552. read_only->result = read_only->callback.WaitForResult();
  3553. EXPECT_EQ(ERR_CACHE_MISS, read_only->result);
  3554. EXPECT_FALSE(cache.IsWriterPresent(cache_key));
  3555. // Invoke Read on the 3rd transaction and it should get the error code back.
  3556. auto& c = context_list[2];
  3557. const int kBufferSize = 5;
  3558. scoped_refptr<IOBuffer> buffer = base::MakeRefCounted<IOBuffer>(kBufferSize);
  3559. c->result = c->trans->Read(buffer.get(), kBufferSize, c->callback.callback());
  3560. EXPECT_EQ(ERR_INTERNET_DISCONNECTED, c->result);
  3561. }
  3562. // Tests the case when cache write failure happens. Idle and waiting
  3563. // transactions should fail and headers transaction should be restarted.
  3564. TEST_F(HttpCacheTest, SimpleGET_ParallelWritingCacheWriteFailed) {
  3565. MockHttpCache cache;
  3566. MockHttpRequest request(kSimpleGET_Transaction);
  3567. MockTransaction transaction(kSimpleGET_Transaction);
  3568. transaction.load_flags |= LOAD_ONLY_FROM_CACHE;
  3569. MockHttpRequest read_request(transaction);
  3570. const int kNumTransactions = 4;
  3571. std::vector<std::unique_ptr<Context>> context_list;
  3572. for (int i = 0; i < kNumTransactions; ++i) {
  3573. context_list.push_back(std::make_unique<Context>());
  3574. auto& c = context_list[i];
  3575. c->result = cache.CreateTransaction(&c->trans);
  3576. ASSERT_THAT(c->result, IsOk());
  3577. MockHttpRequest* this_request = &request;
  3578. if (i == 3)
  3579. this_request = &read_request;
  3580. c->result = c->trans->Start(this_request, c->callback.callback(),
  3581. NetLogWithSource());
  3582. }
  3583. // Allow all requests to move from the Create queue to the active entry.
  3584. base::RunLoop().RunUntilIdle();
  3585. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  3586. EXPECT_EQ(0, cache.disk_cache()->open_count());
  3587. EXPECT_EQ(1, cache.disk_cache()->create_count());
  3588. std::string cache_key = read_request.CacheKey();
  3589. EXPECT_EQ(3, cache.GetCountWriterTransactions(cache_key));
  3590. EXPECT_EQ(1, cache.GetCountDoneHeadersQueue(cache_key));
  3591. // Initiate Read from two writers and let the first get a cache write failure.
  3592. cache.disk_cache()->set_soft_failures_mask(MockDiskEntry::FAIL_ALL);
  3593. // We have to open the entry again to propagate the failure flag.
  3594. disk_cache::Entry* en;
  3595. cache.OpenBackendEntry(cache_key, &en);
  3596. en->Close();
  3597. const int kBufferSize = 5;
  3598. std::vector<scoped_refptr<IOBuffer>> buffer(
  3599. 3, base::MakeRefCounted<IOBuffer>(kBufferSize));
  3600. for (int i = 0; i < 2; i++) {
  3601. auto& c = context_list[i];
  3602. c->result =
  3603. c->trans->Read(buffer[i].get(), kBufferSize, c->callback.callback());
  3604. EXPECT_EQ(ERR_IO_PENDING, c->result);
  3605. }
  3606. std::string first_read;
  3607. base::RunLoop().RunUntilIdle();
  3608. for (int i = 0; i < 2; i++) {
  3609. auto& c = context_list[i];
  3610. c->result = c->callback.WaitForResult();
  3611. if (i == 0) {
  3612. EXPECT_EQ(5, c->result);
  3613. std::string data_read(buffer[i]->data(), kBufferSize);
  3614. first_read = data_read;
  3615. } else {
  3616. EXPECT_EQ(ERR_CACHE_WRITE_FAILURE, c->result);
  3617. }
  3618. }
  3619. // The entry should have been doomed and destroyed and the headers transaction
  3620. // restarted. Since headers transaction is read-only it will error out.
  3621. auto& read_only = context_list[3];
  3622. read_only->result = read_only->callback.WaitForResult();
  3623. EXPECT_EQ(ERR_CACHE_MISS, read_only->result);
  3624. EXPECT_FALSE(cache.IsWriterPresent(cache_key));
  3625. // Invoke Read on the 3rd transaction and it should get the error code back.
  3626. auto& c = context_list[2];
  3627. c->result =
  3628. c->trans->Read(buffer[2].get(), kBufferSize, c->callback.callback());
  3629. EXPECT_EQ(ERR_CACHE_WRITE_FAILURE, c->result);
  3630. // The first transaction should be able to continue to read from the network
  3631. // without writing to the cache.
  3632. auto& succ_read = context_list[0];
  3633. ReadRemainingAndVerifyTransaction(succ_read->trans.get(), first_read,
  3634. kSimpleGET_Transaction);
  3635. }
  3636. // Tests that POST requests do not join existing transactions for parallel
  3637. // writing to the cache. Note that two POSTs only map to the same entry if their
  3638. // upload data identifier is same and that should happen for back-forward case
  3639. // (LOAD_ONLY_FROM_CACHE). But this test tests without LOAD_ONLY_FROM_CACHE
  3640. // because read-only transactions anyways do not join parallel writing.
  3641. // TODO(shivanisha) Testing this because it is allowed by the code but looks
  3642. // like the code should disallow two POSTs without LOAD_ONLY_FROM_CACHE with the
  3643. // same upload data identifier to map to the same entry.
  3644. TEST_F(HttpCacheTest, SimplePOST_ParallelWritingDisallowed) {
  3645. MockHttpCache cache;
  3646. MockTransaction transaction(kSimplePOST_Transaction);
  3647. const int64_t kUploadId = 1; // Just a dummy value.
  3648. std::vector<std::unique_ptr<UploadElementReader>> element_readers;
  3649. element_readers.push_back(
  3650. std::make_unique<UploadBytesElementReader>("hello", 5));
  3651. ElementsUploadDataStream upload_data_stream(std::move(element_readers),
  3652. kUploadId);
  3653. // Note that both transactions should have the same upload_data_stream
  3654. // identifier to map to the same entry.
  3655. transaction.load_flags = LOAD_SKIP_CACHE_VALIDATION;
  3656. MockHttpRequest request(transaction);
  3657. request.upload_data_stream = &upload_data_stream;
  3658. const int kNumTransactions = 2;
  3659. std::vector<std::unique_ptr<Context>> context_list;
  3660. for (int i = 0; i < kNumTransactions; ++i) {
  3661. context_list.push_back(std::make_unique<Context>());
  3662. auto& c = context_list[i];
  3663. c->result = cache.CreateTransaction(&c->trans);
  3664. ASSERT_THAT(c->result, IsOk());
  3665. c->result =
  3666. c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
  3667. // Complete the headers phase request.
  3668. base::RunLoop().RunUntilIdle();
  3669. }
  3670. std::string cache_key = request.CacheKey();
  3671. // Only the 1st transaction gets added to writers.
  3672. EXPECT_EQ(1, cache.GetCountDoneHeadersQueue(cache_key));
  3673. EXPECT_EQ(1, cache.GetCountWriterTransactions(cache_key));
  3674. // Read the 1st transaction.
  3675. ReadAndVerifyTransaction(context_list[0]->trans.get(),
  3676. kSimplePOST_Transaction);
  3677. // 2nd transaction should now become a reader.
  3678. base::RunLoop().RunUntilIdle();
  3679. EXPECT_EQ(1, cache.GetCountReaders(cache_key));
  3680. EXPECT_EQ(0, cache.GetCountDoneHeadersQueue(cache_key));
  3681. ReadAndVerifyTransaction(context_list[1]->trans.get(),
  3682. kSimplePOST_Transaction);
  3683. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  3684. EXPECT_EQ(0, cache.disk_cache()->open_count());
  3685. EXPECT_EQ(1, cache.disk_cache()->create_count());
  3686. context_list.clear();
  3687. }
  3688. // Tests the case when parallel writing succeeds. Tests both idle and waiting
  3689. // transactions.
  3690. TEST_F(HttpCacheTest, SimpleGET_ParallelWritingSuccess) {
  3691. MockHttpCache cache;
  3692. MockHttpRequest request(kSimpleGET_Transaction);
  3693. MockTransaction transaction(kSimpleGET_Transaction);
  3694. transaction.load_flags |= LOAD_ONLY_FROM_CACHE;
  3695. MockHttpRequest read_request(transaction);
  3696. const int kNumTransactions = 4;
  3697. std::vector<std::unique_ptr<Context>> context_list;
  3698. for (int i = 0; i < kNumTransactions; ++i) {
  3699. context_list.push_back(std::make_unique<Context>());
  3700. auto& c = context_list[i];
  3701. c->result = cache.CreateTransaction(&c->trans);
  3702. ASSERT_THAT(c->result, IsOk());
  3703. MockHttpRequest* this_request = &request;
  3704. if (i == 3)
  3705. this_request = &read_request;
  3706. c->result = c->trans->Start(this_request, c->callback.callback(),
  3707. NetLogWithSource());
  3708. }
  3709. // Allow all requests to move from the Create queue to the active entry.
  3710. base::RunLoop().RunUntilIdle();
  3711. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  3712. EXPECT_EQ(0, cache.disk_cache()->open_count());
  3713. EXPECT_EQ(1, cache.disk_cache()->create_count());
  3714. std::string cache_key = request.CacheKey();
  3715. EXPECT_EQ(3, cache.GetCountWriterTransactions(cache_key));
  3716. EXPECT_EQ(1, cache.GetCountDoneHeadersQueue(cache_key));
  3717. // Initiate Read from two writers.
  3718. const int kBufferSize = 5;
  3719. std::vector<scoped_refptr<IOBuffer>> buffer(
  3720. 3, base::MakeRefCounted<IOBuffer>(kBufferSize));
  3721. for (int i = 0; i < 2; i++) {
  3722. auto& c = context_list[i];
  3723. c->result =
  3724. c->trans->Read(buffer[i].get(), kBufferSize, c->callback.callback());
  3725. EXPECT_EQ(ERR_IO_PENDING, c->result);
  3726. }
  3727. std::vector<std::string> first_read(2);
  3728. base::RunLoop().RunUntilIdle();
  3729. for (int i = 0; i < 2; i++) {
  3730. auto& c = context_list[i];
  3731. c->result = c->callback.WaitForResult();
  3732. EXPECT_EQ(5, c->result);
  3733. std::string data_read(buffer[i]->data(), kBufferSize);
  3734. first_read[i] = data_read;
  3735. }
  3736. EXPECT_EQ(first_read[0], first_read[1]);
  3737. // The first transaction should be able to continue to read from the network
  3738. // without writing to the cache.
  3739. for (int i = 0; i < 2; i++) {
  3740. auto& c = context_list[i];
  3741. ReadRemainingAndVerifyTransaction(c->trans.get(), first_read[i],
  3742. kSimpleGET_Transaction);
  3743. if (i == 0) {
  3744. // Remaining transactions should now be readers.
  3745. EXPECT_EQ(3, cache.GetCountReaders(cache_key));
  3746. }
  3747. }
  3748. // Verify the rest of the transactions.
  3749. for (int i = 2; i < kNumTransactions; i++) {
  3750. auto& c = context_list[i];
  3751. ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
  3752. }
  3753. context_list.clear();
  3754. }
  3755. // Tests the case when parallel writing involves things bigger than what cache
  3756. // can store. In this case, the best we can do is re-fetch it.
  3757. TEST_F(HttpCacheTest, SimpleGET_ParallelWritingHuge) {
  3758. MockHttpCache cache;
  3759. cache.disk_cache()->set_max_file_size(10);
  3760. MockTransaction transaction(kSimpleGET_Transaction);
  3761. std::string response_headers = base::StrCat(
  3762. {kSimpleGET_Transaction.response_headers, "Content-Length: ",
  3763. base::NumberToString(strlen(kSimpleGET_Transaction.data)), "\n"});
  3764. transaction.response_headers = response_headers.c_str();
  3765. AddMockTransaction(&transaction);
  3766. MockHttpRequest request(transaction);
  3767. const int kNumTransactions = 4;
  3768. std::vector<std::unique_ptr<Context>> context_list;
  3769. for (int i = 0; i < kNumTransactions; ++i) {
  3770. context_list.push_back(std::make_unique<Context>());
  3771. auto& c = context_list[i];
  3772. c->result = cache.CreateTransaction(&c->trans);
  3773. ASSERT_THAT(c->result, IsOk());
  3774. MockHttpRequest* this_request = &request;
  3775. c->result = c->trans->Start(this_request, c->callback.callback(),
  3776. NetLogWithSource());
  3777. }
  3778. // Start them up.
  3779. base::RunLoop().RunUntilIdle();
  3780. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  3781. EXPECT_EQ(0, cache.disk_cache()->open_count());
  3782. EXPECT_EQ(1, cache.disk_cache()->create_count());
  3783. std::string cache_key = request.CacheKey();
  3784. EXPECT_EQ(1, cache.GetCountWriterTransactions(cache_key));
  3785. EXPECT_EQ(kNumTransactions - 1, cache.GetCountDoneHeadersQueue(cache_key));
  3786. // Initiate Read from first transaction.
  3787. const int kBufferSize = 5;
  3788. std::vector<scoped_refptr<IOBuffer>> buffer(
  3789. kNumTransactions, base::MakeRefCounted<IOBuffer>(kBufferSize));
  3790. auto& c = context_list[0];
  3791. c->result =
  3792. c->trans->Read(buffer[0].get(), kBufferSize, c->callback.callback());
  3793. EXPECT_EQ(ERR_IO_PENDING, c->result);
  3794. // ... and complete it.
  3795. std::vector<std::string> first_read(kNumTransactions);
  3796. base::RunLoop().RunUntilIdle();
  3797. c->result = c->callback.WaitForResult();
  3798. EXPECT_EQ(kBufferSize, c->result);
  3799. std::string data_read(buffer[0]->data(), kBufferSize);
  3800. first_read[0] = data_read;
  3801. EXPECT_EQ("<html", first_read[0]);
  3802. // Complete all of them.
  3803. for (int i = 0; i < kNumTransactions; i++) {
  3804. ReadRemainingAndVerifyTransaction(context_list[i]->trans.get(),
  3805. first_read[i], kSimpleGET_Transaction);
  3806. }
  3807. // Sadly all of them have to hit the network
  3808. EXPECT_EQ(kNumTransactions, cache.network_layer()->transaction_count());
  3809. context_list.clear();
  3810. RemoveMockTransaction(&transaction);
  3811. }
  3812. // Tests that network transaction's info is saved correctly when a writer
  3813. // transaction that created the network transaction becomes a reader. Also
  3814. // verifies that the network bytes are only attributed to the transaction that
  3815. // created the network transaction.
  3816. TEST_F(HttpCacheTest, SimpleGET_ParallelWritingVerifyNetworkBytes) {
  3817. MockHttpCache cache;
  3818. MockHttpRequest request(kSimpleGET_Transaction);
  3819. const int kNumTransactions = 2;
  3820. std::vector<std::unique_ptr<Context>> context_list;
  3821. for (int i = 0; i < kNumTransactions; ++i) {
  3822. context_list.push_back(std::make_unique<Context>());
  3823. auto& c = context_list[i];
  3824. c->result = cache.CreateTransaction(&c->trans);
  3825. ASSERT_THAT(c->result, IsOk());
  3826. c->result =
  3827. c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
  3828. }
  3829. // Allow all requests to move from the Create queue to the active entry.
  3830. base::RunLoop().RunUntilIdle();
  3831. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  3832. EXPECT_EQ(0, cache.disk_cache()->open_count());
  3833. EXPECT_EQ(1, cache.disk_cache()->create_count());
  3834. std::string cache_key = request.CacheKey();
  3835. EXPECT_EQ(2, cache.GetCountWriterTransactions(cache_key));
  3836. EXPECT_EQ(0, cache.GetCountDoneHeadersQueue(cache_key));
  3837. // Get the network bytes read by the first transaction.
  3838. int total_received_bytes = context_list[0]->trans->GetTotalReceivedBytes();
  3839. EXPECT_GT(total_received_bytes, 0);
  3840. // Complete Read by the 2nd transaction so that the 1st transaction that
  3841. // created the network transaction is now a reader.
  3842. ReadAndVerifyTransaction(context_list[1]->trans.get(),
  3843. kSimpleGET_Transaction);
  3844. EXPECT_EQ(1, cache.GetCountReaders(cache_key));
  3845. // Verify that the network bytes read are not attributed to the 2nd
  3846. // transaction but to the 1st.
  3847. EXPECT_EQ(0, context_list[1]->trans->GetTotalReceivedBytes());
  3848. EXPECT_GE(total_received_bytes,
  3849. context_list[0]->trans->GetTotalReceivedBytes());
  3850. ReadAndVerifyTransaction(context_list[0]->trans.get(),
  3851. kSimpleGET_Transaction);
  3852. }
  3853. // Tests than extra Read from the consumer should not hang/crash the browser.
  3854. TEST_F(HttpCacheTest, SimpleGET_ExtraRead) {
  3855. MockHttpCache cache;
  3856. MockHttpRequest request(kSimpleGET_Transaction);
  3857. Context c;
  3858. c.result = cache.CreateTransaction(&c.trans);
  3859. ASSERT_THAT(c.result, IsOk());
  3860. c.result =
  3861. c.trans->Start(&request, c.callback.callback(), NetLogWithSource());
  3862. base::RunLoop().RunUntilIdle();
  3863. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  3864. EXPECT_EQ(0, cache.disk_cache()->open_count());
  3865. EXPECT_EQ(1, cache.disk_cache()->create_count());
  3866. std::string cache_key = request.CacheKey();
  3867. EXPECT_EQ(1, cache.GetCountWriterTransactions(cache_key));
  3868. EXPECT_EQ(0, cache.GetCountDoneHeadersQueue(cache_key));
  3869. ReadAndVerifyTransaction(c.trans.get(), kSimpleGET_Transaction);
  3870. // Perform an extra Read.
  3871. const int kBufferSize = 10;
  3872. scoped_refptr<IOBuffer> buffer = base::MakeRefCounted<IOBuffer>(kBufferSize);
  3873. c.result = c.trans->Read(buffer.get(), kBufferSize, c.callback.callback());
  3874. EXPECT_EQ(0, c.result);
  3875. }
  3876. // Tests when a writer is destroyed mid-read, all the other writer transactions
  3877. // can continue writing to the entry.
  3878. TEST_F(HttpCacheTest, SimpleGET_ParallelValidationCancelWriter) {
  3879. MockHttpCache cache;
  3880. ScopedMockTransaction transaction(kSimpleGET_Transaction);
  3881. transaction.response_headers =
  3882. "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n"
  3883. "Content-Length: 22\n"
  3884. "Etag: \"foopy\"\n";
  3885. MockHttpRequest request(transaction);
  3886. const int kNumTransactions = 3;
  3887. std::vector<std::unique_ptr<Context>> context_list;
  3888. for (int i = 0; i < kNumTransactions; ++i) {
  3889. context_list.push_back(std::make_unique<Context>());
  3890. auto& c = context_list[i];
  3891. c->result = cache.CreateTransaction(&c->trans);
  3892. ASSERT_THAT(c->result, IsOk());
  3893. c->result =
  3894. c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
  3895. }
  3896. // Allow all requests to move from the Create queue to the active entry.
  3897. base::RunLoop().RunUntilIdle();
  3898. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  3899. EXPECT_EQ(0, cache.disk_cache()->open_count());
  3900. EXPECT_EQ(1, cache.disk_cache()->create_count());
  3901. std::string cache_key = request.CacheKey();
  3902. EXPECT_EQ(kNumTransactions, cache.GetCountWriterTransactions(cache_key));
  3903. // Let first transaction read some bytes.
  3904. {
  3905. auto& c = context_list[0];
  3906. const int kBufferSize = 5;
  3907. scoped_refptr<IOBuffer> buffer =
  3908. base::MakeRefCounted<IOBuffer>(kBufferSize);
  3909. ReleaseBufferCompletionCallback cb(buffer.get());
  3910. c->result = c->trans->Read(buffer.get(), kBufferSize, cb.callback());
  3911. EXPECT_EQ(kBufferSize, cb.GetResult(c->result));
  3912. }
  3913. // Deleting the active transaction at this point will not impact the other
  3914. // transactions since there are other transactions in writers.
  3915. context_list[0].reset();
  3916. base::RunLoop().RunUntilIdle();
  3917. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  3918. EXPECT_EQ(0, cache.disk_cache()->open_count());
  3919. EXPECT_EQ(1, cache.disk_cache()->create_count());
  3920. // Complete the rest of the transactions.
  3921. for (auto& context : context_list) {
  3922. if (!context)
  3923. continue;
  3924. ReadAndVerifyTransaction(context->trans.get(), kSimpleGET_Transaction);
  3925. }
  3926. }
  3927. // Tests that when StopCaching is invoked on a writer, dependent transactions
  3928. // are restarted.
  3929. TEST_F(HttpCacheTest, SimpleGET_ParallelValidationStopCaching) {
  3930. MockHttpCache cache;
  3931. MockHttpRequest request(kSimpleGET_Transaction);
  3932. MockTransaction transaction(kSimpleGET_Transaction);
  3933. transaction.load_flags |= LOAD_ONLY_FROM_CACHE;
  3934. MockHttpRequest read_only_request(transaction);
  3935. const int kNumTransactions = 2;
  3936. std::vector<std::unique_ptr<Context>> context_list;
  3937. for (int i = 0; i < kNumTransactions; ++i) {
  3938. context_list.push_back(std::make_unique<Context>());
  3939. auto& c = context_list[i];
  3940. c->result = cache.CreateTransaction(&c->trans);
  3941. ASSERT_THAT(c->result, IsOk());
  3942. MockHttpRequest* this_request = &request;
  3943. if (i == 1) {
  3944. this_request = &read_only_request;
  3945. }
  3946. c->result = c->trans->Start(this_request, c->callback.callback(),
  3947. NetLogWithSource());
  3948. }
  3949. // Allow all requests to move from the Create queue to the active entry.
  3950. base::RunLoop().RunUntilIdle();
  3951. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  3952. EXPECT_EQ(0, cache.disk_cache()->open_count());
  3953. EXPECT_EQ(1, cache.disk_cache()->create_count());
  3954. std::string cache_key = request.CacheKey();
  3955. EXPECT_EQ(kNumTransactions - 1, cache.GetCountWriterTransactions(cache_key));
  3956. EXPECT_EQ(1, cache.GetCountDoneHeadersQueue(cache_key));
  3957. // Invoking StopCaching on the writer will lead to dooming the entry and
  3958. // restarting the validated transactions. Since it is a read-only transaction
  3959. // it will error out.
  3960. context_list[0]->trans->StopCaching();
  3961. base::RunLoop().RunUntilIdle();
  3962. int rv = context_list[1]->callback.WaitForResult();
  3963. EXPECT_EQ(ERR_CACHE_MISS, rv);
  3964. ReadAndVerifyTransaction(context_list[0]->trans.get(),
  3965. kSimpleGET_Transaction);
  3966. }
  3967. // Tests that when StopCaching is invoked on a writer transaction, it is a
  3968. // no-op if there are other writer transactions.
  3969. TEST_F(HttpCacheTest, SimpleGET_ParallelWritersStopCachingNoOp) {
  3970. MockHttpCache cache;
  3971. MockHttpRequest request(kSimpleGET_Transaction);
  3972. MockTransaction transaction(kSimpleGET_Transaction);
  3973. transaction.load_flags |= LOAD_VALIDATE_CACHE;
  3974. MockHttpRequest validate_request(transaction);
  3975. const int kNumTransactions = 3;
  3976. std::vector<std::unique_ptr<Context>> context_list;
  3977. for (int i = 0; i < kNumTransactions; ++i) {
  3978. context_list.push_back(std::make_unique<Context>());
  3979. auto& c = context_list[i];
  3980. c->result = cache.CreateTransaction(&c->trans);
  3981. ASSERT_THAT(c->result, IsOk());
  3982. MockHttpRequest* this_request = &request;
  3983. if (i == 2) {
  3984. this_request = &validate_request;
  3985. c->trans->SetBeforeNetworkStartCallback(base::BindOnce(&DeferCallback));
  3986. }
  3987. c->result = c->trans->Start(this_request, c->callback.callback(),
  3988. NetLogWithSource());
  3989. }
  3990. // Allow all requests to move from the Create queue to the active entry.
  3991. base::RunLoop().RunUntilIdle();
  3992. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  3993. EXPECT_EQ(0, cache.disk_cache()->open_count());
  3994. EXPECT_EQ(1, cache.disk_cache()->create_count());
  3995. std::string cache_key = request.CacheKey();
  3996. EXPECT_TRUE(cache.IsHeadersTransactionPresent(cache_key));
  3997. EXPECT_EQ(kNumTransactions - 1, cache.GetCountWriterTransactions(cache_key));
  3998. // Invoking StopCaching on the writer will be a no-op since there are multiple
  3999. // transaction in writers.
  4000. context_list[0]->trans->StopCaching();
  4001. // Resume network start for headers_transaction.
  4002. auto& c = context_list[2];
  4003. c->trans->ResumeNetworkStart();
  4004. base::RunLoop().RunUntilIdle();
  4005. // After validation old entry will be doomed and headers_transaction will be
  4006. // added to the new entry.
  4007. EXPECT_EQ(1, cache.GetCountWriterTransactions(cache_key));
  4008. // Complete the rest of the transactions.
  4009. for (auto& context : context_list) {
  4010. if (!context)
  4011. continue;
  4012. ReadAndVerifyTransaction(context->trans.get(), kSimpleGET_Transaction);
  4013. }
  4014. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  4015. EXPECT_EQ(0, cache.disk_cache()->open_count());
  4016. EXPECT_EQ(2, cache.disk_cache()->create_count());
  4017. }
  4018. // Tests that a transaction is currently in headers phase and is destroyed
  4019. // leading to destroying the entry.
  4020. TEST_F(HttpCacheTest, SimpleGET_ParallelValidationCancelHeaders) {
  4021. MockHttpCache cache;
  4022. MockHttpRequest request(kSimpleGET_Transaction);
  4023. const int kNumTransactions = 2;
  4024. std::vector<std::unique_ptr<Context>> context_list;
  4025. for (int i = 0; i < kNumTransactions; ++i) {
  4026. context_list.push_back(std::make_unique<Context>());
  4027. auto& c = context_list[i];
  4028. c->result = cache.CreateTransaction(&c->trans);
  4029. ASSERT_THAT(c->result, IsOk());
  4030. if (i == 0)
  4031. c->trans->SetBeforeNetworkStartCallback(base::BindOnce(&DeferCallback));
  4032. c->result =
  4033. c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
  4034. }
  4035. base::RunLoop().RunUntilIdle();
  4036. std::string cache_key = request.CacheKey();
  4037. EXPECT_TRUE(cache.IsHeadersTransactionPresent(cache_key));
  4038. EXPECT_EQ(1, cache.GetCountAddToEntryQueue(cache_key));
  4039. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  4040. EXPECT_EQ(0, cache.disk_cache()->open_count());
  4041. EXPECT_EQ(1, cache.disk_cache()->create_count());
  4042. // Delete the headers transaction.
  4043. context_list[0].reset();
  4044. base::RunLoop().RunUntilIdle();
  4045. // Complete the rest of the transactions.
  4046. for (auto& context : context_list) {
  4047. if (!context)
  4048. continue;
  4049. ReadAndVerifyTransaction(context->trans.get(), kSimpleGET_Transaction);
  4050. }
  4051. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  4052. EXPECT_EQ(0, cache.disk_cache()->open_count());
  4053. EXPECT_EQ(2, cache.disk_cache()->create_count());
  4054. }
  4055. // Similar to the above test, except here cache write fails and the
  4056. // validated transactions should be restarted.
  4057. TEST_F(HttpCacheTest, SimpleGET_ParallelWritersFailWrite) {
  4058. MockHttpCache cache;
  4059. MockHttpRequest request(kSimpleGET_Transaction);
  4060. const int kNumTransactions = 5;
  4061. std::vector<std::unique_ptr<Context>> context_list;
  4062. for (int i = 0; i < kNumTransactions; ++i) {
  4063. context_list.push_back(std::make_unique<Context>());
  4064. auto& c = context_list[i];
  4065. c->result = cache.CreateTransaction(&c->trans);
  4066. ASSERT_THAT(c->result, IsOk());
  4067. EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState());
  4068. c->result =
  4069. c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
  4070. }
  4071. // All requests are waiting for the active entry.
  4072. for (auto& context : context_list) {
  4073. EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, context->trans->GetLoadState());
  4074. }
  4075. // Allow all requests to move from the Create queue to the active entry.
  4076. base::RunLoop().RunUntilIdle();
  4077. // All transactions become writers.
  4078. std::string cache_key = request.CacheKey();
  4079. EXPECT_EQ(kNumTransactions, cache.GetCountWriterTransactions(cache_key));
  4080. // All requests depend on the writer, and the writer is between Start and
  4081. // Read, i.e. idle.
  4082. for (auto& context : context_list) {
  4083. EXPECT_EQ(LOAD_STATE_IDLE, context->trans->GetLoadState());
  4084. }
  4085. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  4086. EXPECT_EQ(0, cache.disk_cache()->open_count());
  4087. EXPECT_EQ(1, cache.disk_cache()->create_count());
  4088. // Fail the request.
  4089. cache.disk_cache()->set_soft_failures_mask(MockDiskEntry::FAIL_ALL);
  4090. // We have to open the entry again to propagate the failure flag.
  4091. disk_cache::Entry* en;
  4092. cache.OpenBackendEntry(cache_key, &en);
  4093. en->Close();
  4094. for (int i = 0; i < kNumTransactions; ++i) {
  4095. auto& c = context_list[i];
  4096. if (c->result == ERR_IO_PENDING)
  4097. c->result = c->callback.WaitForResult();
  4098. if (i == 1) {
  4099. // The earlier entry must be destroyed and its disk entry doomed.
  4100. EXPECT_TRUE(cache.disk_cache()->IsDiskEntryDoomed(cache_key));
  4101. }
  4102. if (i == 0) {
  4103. // Consumer gets the response even if cache write failed.
  4104. ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
  4105. } else {
  4106. // Read should lead to a failure being returned.
  4107. const int kBufferSize = 5;
  4108. scoped_refptr<IOBuffer> buffer =
  4109. base::MakeRefCounted<IOBuffer>(kBufferSize);
  4110. ReleaseBufferCompletionCallback cb(buffer.get());
  4111. c->result = c->trans->Read(buffer.get(), kBufferSize, cb.callback());
  4112. EXPECT_EQ(ERR_CACHE_WRITE_FAILURE, cb.GetResult(c->result));
  4113. }
  4114. }
  4115. }
  4116. // This is a test for http://code.google.com/p/chromium/issues/detail?id=4769.
  4117. // If cancelling a request is racing with another request for the same resource
  4118. // finishing, we have to make sure that we remove both transactions from the
  4119. // entry.
  4120. TEST_F(HttpCacheTest, SimpleGET_RacingReaders) {
  4121. MockHttpCache cache;
  4122. MockHttpRequest request(kSimpleGET_Transaction);
  4123. MockHttpRequest reader_request(kSimpleGET_Transaction);
  4124. reader_request.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
  4125. std::vector<std::unique_ptr<Context>> context_list;
  4126. const int kNumTransactions = 5;
  4127. for (int i = 0; i < kNumTransactions; ++i) {
  4128. context_list.push_back(std::make_unique<Context>());
  4129. Context* c = context_list[i].get();
  4130. c->result = cache.CreateTransaction(&c->trans);
  4131. ASSERT_THAT(c->result, IsOk());
  4132. MockHttpRequest* this_request = &request;
  4133. if (i == 1 || i == 2)
  4134. this_request = &reader_request;
  4135. c->result = c->trans->Start(this_request, c->callback.callback(),
  4136. NetLogWithSource());
  4137. }
  4138. // Allow all requests to move from the Create queue to the active entry.
  4139. base::RunLoop().RunUntilIdle();
  4140. // The first request should be a writer at this point, and the subsequent
  4141. // requests should be pending.
  4142. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  4143. EXPECT_EQ(0, cache.disk_cache()->open_count());
  4144. EXPECT_EQ(1, cache.disk_cache()->create_count());
  4145. Context* c = context_list[0].get();
  4146. ASSERT_THAT(c->result, IsError(ERR_IO_PENDING));
  4147. c->result = c->callback.WaitForResult();
  4148. ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
  4149. // Now all transactions should be waiting for read to be invoked. Two readers
  4150. // are because of the load flags and remaining two transactions were converted
  4151. // to readers after skipping validation. Note that the remaining two went on
  4152. // to process the headers in parallel with readers present on the entry.
  4153. EXPECT_EQ(LOAD_STATE_IDLE, context_list[2]->trans->GetLoadState());
  4154. EXPECT_EQ(LOAD_STATE_IDLE, context_list[3]->trans->GetLoadState());
  4155. c = context_list[1].get();
  4156. ASSERT_THAT(c->result, IsError(ERR_IO_PENDING));
  4157. c->result = c->callback.WaitForResult();
  4158. if (c->result == OK)
  4159. ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
  4160. // At this point we have one reader, two pending transactions and a task on
  4161. // the queue to move to the next transaction. Now we cancel the request that
  4162. // is the current reader, and expect the queued task to be able to start the
  4163. // next request.
  4164. c = context_list[2].get();
  4165. c->trans.reset();
  4166. for (int i = 3; i < kNumTransactions; ++i) {
  4167. c = context_list[i].get();
  4168. if (c->result == ERR_IO_PENDING)
  4169. c->result = c->callback.WaitForResult();
  4170. if (c->result == OK)
  4171. ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
  4172. }
  4173. // We should not have had to re-open the disk entry.
  4174. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  4175. EXPECT_EQ(0, cache.disk_cache()->open_count());
  4176. EXPECT_EQ(1, cache.disk_cache()->create_count());
  4177. }
  4178. // Tests that we can doom an entry with pending transactions and delete one of
  4179. // the pending transactions before the first one completes.
  4180. // See http://code.google.com/p/chromium/issues/detail?id=25588
  4181. TEST_F(HttpCacheTest, SimpleGET_DoomWithPending) {
  4182. // We need simultaneous doomed / not_doomed entries so let's use a real cache.
  4183. MockHttpCache cache(HttpCache::DefaultBackend::InMemory(1024 * 1024));
  4184. MockHttpRequest request(kSimpleGET_Transaction);
  4185. MockHttpRequest writer_request(kSimpleGET_Transaction);
  4186. writer_request.load_flags = LOAD_BYPASS_CACHE;
  4187. std::vector<std::unique_ptr<Context>> context_list;
  4188. const int kNumTransactions = 4;
  4189. for (int i = 0; i < kNumTransactions; ++i) {
  4190. context_list.push_back(std::make_unique<Context>());
  4191. Context* c = context_list[i].get();
  4192. c->result = cache.CreateTransaction(&c->trans);
  4193. ASSERT_THAT(c->result, IsOk());
  4194. MockHttpRequest* this_request = &request;
  4195. if (i == 3)
  4196. this_request = &writer_request;
  4197. c->result = c->trans->Start(this_request, c->callback.callback(),
  4198. NetLogWithSource());
  4199. }
  4200. base::RunLoop().RunUntilIdle();
  4201. // The first request should be a writer at this point, and the two subsequent
  4202. // requests should be pending. The last request doomed the first entry.
  4203. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  4204. // Cancel the second transaction. Note that this and the 3rd transactions
  4205. // would have completed their headers phase and would be waiting in the
  4206. // done_headers_queue when the 2nd transaction is cancelled.
  4207. context_list[1].reset();
  4208. for (int i = 0; i < kNumTransactions; ++i) {
  4209. if (i == 1)
  4210. continue;
  4211. Context* c = context_list[i].get();
  4212. ASSERT_THAT(c->result, IsError(ERR_IO_PENDING));
  4213. c->result = c->callback.WaitForResult();
  4214. ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
  4215. }
  4216. }
  4217. TEST_F(HttpCacheTest, DoomDoesNotSetHints) {
  4218. // Test that a doomed writer doesn't set in-memory index hints.
  4219. MockHttpCache cache;
  4220. cache.disk_cache()->set_support_in_memory_entry_data(true);
  4221. // Request 1 is a normal one to a no-cache/no-etag resource, to potentially
  4222. // set a "this is unvalidatable" hint in the cache. We also need it to
  4223. // actually write out to the doomed entry after request 2 does its thing,
  4224. // so its transaction is paused.
  4225. MockTransaction no_cache_transaction(kSimpleGET_Transaction);
  4226. no_cache_transaction.response_headers = "Cache-Control: no-cache\n";
  4227. AddMockTransaction(&no_cache_transaction);
  4228. MockHttpRequest request1(no_cache_transaction);
  4229. Context c1;
  4230. c1.result = cache.CreateTransaction(&c1.trans);
  4231. ASSERT_THAT(c1.result, IsOk());
  4232. c1.trans->SetBeforeNetworkStartCallback(
  4233. base::BindOnce([](bool* defer) { *defer = true; }));
  4234. c1.result =
  4235. c1.trans->Start(&request1, c1.callback.callback(), NetLogWithSource());
  4236. ASSERT_THAT(c1.result, IsError(ERR_IO_PENDING));
  4237. // It starts, copies over headers info, but doesn't get to proceed.
  4238. base::RunLoop().RunUntilIdle();
  4239. RemoveMockTransaction(&no_cache_transaction);
  4240. // Request 2 sets LOAD_BYPASS_CACHE to force the first one to be doomed ---
  4241. // it'll want to be a writer.
  4242. MockHttpRequest request2(kSimpleGET_Transaction);
  4243. request2.load_flags = LOAD_BYPASS_CACHE;
  4244. Context c2;
  4245. c2.result = cache.CreateTransaction(&c2.trans);
  4246. ASSERT_THAT(c2.result, IsOk());
  4247. c2.result =
  4248. c2.trans->Start(&request2, c2.callback.callback(), NetLogWithSource());
  4249. ASSERT_THAT(c2.result, IsError(ERR_IO_PENDING));
  4250. // Run Request2, then let the first one wrap up.
  4251. base::RunLoop().RunUntilIdle();
  4252. c2.callback.WaitForResult();
  4253. ReadAndVerifyTransaction(c2.trans.get(), kSimpleGET_Transaction);
  4254. c1.trans->ResumeNetworkStart();
  4255. c1.callback.WaitForResult();
  4256. ReadAndVerifyTransaction(c1.trans.get(), no_cache_transaction);
  4257. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  4258. EXPECT_EQ(0, cache.disk_cache()->open_count());
  4259. EXPECT_EQ(2, cache.disk_cache()->create_count());
  4260. // Request 3 tries to read from cache, and it should successfully do so. It's
  4261. // run after the previous two transactions finish so it doesn't try to
  4262. // cooperate with them, and is entirely driven by the state of the cache.
  4263. MockHttpRequest request3(kSimpleGET_Transaction);
  4264. Context context3;
  4265. context3.result = cache.CreateTransaction(&context3.trans);
  4266. ASSERT_THAT(context3.result, IsOk());
  4267. context3.result = context3.trans->Start(
  4268. &request3, context3.callback.callback(), NetLogWithSource());
  4269. base::RunLoop().RunUntilIdle();
  4270. ASSERT_THAT(context3.result, IsError(ERR_IO_PENDING));
  4271. context3.result = context3.callback.WaitForResult();
  4272. ReadAndVerifyTransaction(context3.trans.get(), kSimpleGET_Transaction);
  4273. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  4274. EXPECT_EQ(1, cache.disk_cache()->open_count());
  4275. EXPECT_EQ(2, cache.disk_cache()->create_count());
  4276. }
  4277. // This is a test for http://code.google.com/p/chromium/issues/detail?id=4731.
  4278. // We may attempt to delete an entry synchronously with the act of adding a new
  4279. // transaction to said entry.
  4280. TEST_F(HttpCacheTest, FastNoStoreGET_DoneWithPending) {
  4281. MockHttpCache cache;
  4282. // The headers will be served right from the call to Start() the request.
  4283. MockHttpRequest request(kFastNoStoreGET_Transaction);
  4284. FastTransactionServer request_handler;
  4285. AddMockTransaction(&kFastNoStoreGET_Transaction);
  4286. std::vector<std::unique_ptr<Context>> context_list;
  4287. const int kNumTransactions = 3;
  4288. for (int i = 0; i < kNumTransactions; ++i) {
  4289. context_list.push_back(std::make_unique<Context>());
  4290. Context* c = context_list[i].get();
  4291. c->result = cache.CreateTransaction(&c->trans);
  4292. ASSERT_THAT(c->result, IsOk());
  4293. c->result =
  4294. c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
  4295. }
  4296. // Allow all requests to move from the Create queue to the active entry.
  4297. base::RunLoop().RunUntilIdle();
  4298. // The first request should be a writer at this point, and the subsequent
  4299. // requests should have completed validation. Since the validation does not
  4300. // result in a match, a new entry would be created.
  4301. EXPECT_EQ(3, cache.network_layer()->transaction_count());
  4302. EXPECT_EQ(0, cache.disk_cache()->open_count());
  4303. EXPECT_EQ(3, cache.disk_cache()->create_count());
  4304. // Now, make sure that the second request asks for the entry not to be stored.
  4305. request_handler.set_no_store(true);
  4306. for (int i = 0; i < kNumTransactions; ++i) {
  4307. Context* c = context_list[i].get();
  4308. if (c->result == ERR_IO_PENDING)
  4309. c->result = c->callback.WaitForResult();
  4310. ReadAndVerifyTransaction(c->trans.get(), kFastNoStoreGET_Transaction);
  4311. context_list[i].reset();
  4312. }
  4313. EXPECT_EQ(3, cache.network_layer()->transaction_count());
  4314. EXPECT_EQ(0, cache.disk_cache()->open_count());
  4315. EXPECT_EQ(3, cache.disk_cache()->create_count());
  4316. RemoveMockTransaction(&kFastNoStoreGET_Transaction);
  4317. }
  4318. TEST_F(HttpCacheTest, SimpleGET_ManyWriters_CancelFirst) {
  4319. MockHttpCache cache;
  4320. MockHttpRequest request(kSimpleGET_Transaction);
  4321. std::vector<std::unique_ptr<Context>> context_list;
  4322. const int kNumTransactions = 2;
  4323. for (int i = 0; i < kNumTransactions; ++i) {
  4324. context_list.push_back(std::make_unique<Context>());
  4325. Context* c = context_list[i].get();
  4326. c->result = cache.CreateTransaction(&c->trans);
  4327. ASSERT_THAT(c->result, IsOk());
  4328. c->result =
  4329. c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
  4330. }
  4331. // Allow all requests to move from the Create queue to the active entry.
  4332. // All would have been added to writers.
  4333. base::RunLoop().RunUntilIdle();
  4334. std::string cache_key =
  4335. *cache.http_cache()->GenerateCacheKeyForRequest(&request);
  4336. EXPECT_EQ(kNumTransactions, cache.GetCountWriterTransactions(cache_key));
  4337. // The second transaction skipped validation, thus only one network
  4338. // transaction is created.
  4339. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  4340. EXPECT_EQ(0, cache.disk_cache()->open_count());
  4341. EXPECT_EQ(1, cache.disk_cache()->create_count());
  4342. for (int i = 0; i < kNumTransactions; ++i) {
  4343. Context* c = context_list[i].get();
  4344. if (c->result == ERR_IO_PENDING)
  4345. c->result = c->callback.WaitForResult();
  4346. // Destroy only the first transaction.
  4347. // This should not impact the other writer transaction and the network
  4348. // transaction will continue to be used by that transaction.
  4349. if (i == 0) {
  4350. context_list[i].reset();
  4351. }
  4352. }
  4353. // Complete the rest of the transactions.
  4354. for (int i = 1; i < kNumTransactions; ++i) {
  4355. Context* c = context_list[i].get();
  4356. ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
  4357. }
  4358. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  4359. EXPECT_EQ(0, cache.disk_cache()->open_count());
  4360. EXPECT_EQ(1, cache.disk_cache()->create_count());
  4361. }
  4362. // Tests that we can cancel requests that are queued waiting to open the disk
  4363. // cache entry.
  4364. TEST_F(HttpCacheTest, SimpleGET_ManyWriters_CancelCreate) {
  4365. MockHttpCache cache;
  4366. MockHttpRequest request(kSimpleGET_Transaction);
  4367. std::vector<std::unique_ptr<Context>> context_list;
  4368. const int kNumTransactions = 5;
  4369. for (int i = 0; i < kNumTransactions; i++) {
  4370. context_list.push_back(std::make_unique<Context>());
  4371. Context* c = context_list[i].get();
  4372. c->result = cache.CreateTransaction(&c->trans);
  4373. ASSERT_THAT(c->result, IsOk());
  4374. c->result =
  4375. c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
  4376. }
  4377. // The first request should be creating the disk cache entry and the others
  4378. // should be pending.
  4379. EXPECT_EQ(0, cache.network_layer()->transaction_count());
  4380. EXPECT_EQ(0, cache.disk_cache()->open_count());
  4381. EXPECT_EQ(1, cache.disk_cache()->create_count());
  4382. // Cancel a request from the pending queue.
  4383. context_list[3].reset();
  4384. // Cancel the request that is creating the entry. This will force the pending
  4385. // operations to restart.
  4386. context_list[0].reset();
  4387. // Complete the rest of the transactions.
  4388. for (int i = 1; i < kNumTransactions; i++) {
  4389. Context* c = context_list[i].get();
  4390. if (c) {
  4391. c->result = c->callback.GetResult(c->result);
  4392. ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
  4393. }
  4394. }
  4395. // We should have had to re-create the disk entry.
  4396. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  4397. EXPECT_EQ(0, cache.disk_cache()->open_count());
  4398. EXPECT_EQ(2, cache.disk_cache()->create_count());
  4399. }
  4400. // Tests that we can cancel a single request to open a disk cache entry.
  4401. TEST_F(HttpCacheTest, SimpleGET_CancelCreate) {
  4402. MockHttpCache cache;
  4403. MockHttpRequest request(kSimpleGET_Transaction);
  4404. auto c = std::make_unique<Context>();
  4405. c->result = cache.CreateTransaction(&c->trans);
  4406. ASSERT_THAT(c->result, IsOk());
  4407. c->result =
  4408. c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
  4409. EXPECT_THAT(c->result, IsError(ERR_IO_PENDING));
  4410. // Release the reference that the mock disk cache keeps for this entry, so
  4411. // that we test that the http cache handles the cancellation correctly.
  4412. cache.disk_cache()->ReleaseAll();
  4413. c.reset();
  4414. base::RunLoop().RunUntilIdle();
  4415. EXPECT_EQ(1, cache.disk_cache()->create_count());
  4416. }
  4417. // Tests that we delete/create entries even if multiple requests are queued.
  4418. TEST_F(HttpCacheTest, SimpleGET_ManyWriters_BypassCache) {
  4419. MockHttpCache cache;
  4420. MockHttpRequest request(kSimpleGET_Transaction);
  4421. request.load_flags = LOAD_BYPASS_CACHE;
  4422. std::vector<std::unique_ptr<Context>> context_list;
  4423. const int kNumTransactions = 5;
  4424. for (int i = 0; i < kNumTransactions; i++) {
  4425. context_list.push_back(std::make_unique<Context>());
  4426. Context* c = context_list[i].get();
  4427. c->result = cache.CreateTransaction(&c->trans);
  4428. ASSERT_THAT(c->result, IsOk());
  4429. c->result =
  4430. c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
  4431. }
  4432. // The first request should be deleting the disk cache entry and the others
  4433. // should be pending.
  4434. EXPECT_EQ(0, cache.network_layer()->transaction_count());
  4435. EXPECT_EQ(0, cache.disk_cache()->open_count());
  4436. EXPECT_EQ(0, cache.disk_cache()->create_count());
  4437. // Complete the transactions.
  4438. for (int i = 0; i < kNumTransactions; i++) {
  4439. Context* c = context_list[i].get();
  4440. c->result = c->callback.GetResult(c->result);
  4441. ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction);
  4442. }
  4443. // We should have had to re-create the disk entry multiple times.
  4444. EXPECT_EQ(5, cache.network_layer()->transaction_count());
  4445. EXPECT_EQ(0, cache.disk_cache()->open_count());
  4446. EXPECT_EQ(5, cache.disk_cache()->create_count());
  4447. }
  4448. // Tests that a (simulated) timeout allows transactions waiting on the cache
  4449. // lock to continue.
  4450. TEST_F(HttpCacheTest, SimpleGET_WriterTimeout) {
  4451. MockHttpCache cache;
  4452. cache.SimulateCacheLockTimeout();
  4453. MockHttpRequest request(kSimpleGET_Transaction);
  4454. Context c1, c2;
  4455. ASSERT_THAT(cache.CreateTransaction(&c1.trans), IsOk());
  4456. ASSERT_EQ(ERR_IO_PENDING, c1.trans->Start(&request, c1.callback.callback(),
  4457. NetLogWithSource()));
  4458. ASSERT_THAT(cache.CreateTransaction(&c2.trans), IsOk());
  4459. ASSERT_EQ(ERR_IO_PENDING, c2.trans->Start(&request, c2.callback.callback(),
  4460. NetLogWithSource()));
  4461. // The second request is queued after the first one.
  4462. c2.callback.WaitForResult();
  4463. ReadAndVerifyTransaction(c2.trans.get(), kSimpleGET_Transaction);
  4464. // Complete the first transaction.
  4465. c1.callback.WaitForResult();
  4466. ReadAndVerifyTransaction(c1.trans.get(), kSimpleGET_Transaction);
  4467. }
  4468. // Tests that a (simulated) timeout allows transactions waiting on the cache
  4469. // lock to continue but read only transactions to error out.
  4470. TEST_F(HttpCacheTest, SimpleGET_WriterTimeoutReadOnlyError) {
  4471. MockHttpCache cache;
  4472. // Simulate timeout.
  4473. cache.SimulateCacheLockTimeout();
  4474. MockHttpRequest request(kSimpleGET_Transaction);
  4475. Context c1, c2;
  4476. ASSERT_THAT(cache.CreateTransaction(&c1.trans), IsOk());
  4477. ASSERT_EQ(ERR_IO_PENDING, c1.trans->Start(&request, c1.callback.callback(),
  4478. NetLogWithSource()));
  4479. request.load_flags = LOAD_ONLY_FROM_CACHE;
  4480. ASSERT_THAT(cache.CreateTransaction(&c2.trans), IsOk());
  4481. ASSERT_EQ(ERR_IO_PENDING, c2.trans->Start(&request, c2.callback.callback(),
  4482. NetLogWithSource()));
  4483. // The second request is queued after the first one.
  4484. int res = c2.callback.WaitForResult();
  4485. ASSERT_EQ(ERR_CACHE_MISS, res);
  4486. // Complete the first transaction.
  4487. c1.callback.WaitForResult();
  4488. ReadAndVerifyTransaction(c1.trans.get(), kSimpleGET_Transaction);
  4489. }
  4490. TEST_F(HttpCacheTest, SimpleGET_AbandonedCacheRead) {
  4491. MockHttpCache cache;
  4492. // write to the cache
  4493. RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
  4494. MockHttpRequest request(kSimpleGET_Transaction);
  4495. TestCompletionCallback callback;
  4496. std::unique_ptr<HttpTransaction> trans;
  4497. ASSERT_THAT(cache.CreateTransaction(&trans), IsOk());
  4498. int rv = trans->Start(&request, callback.callback(), NetLogWithSource());
  4499. if (rv == ERR_IO_PENDING)
  4500. rv = callback.WaitForResult();
  4501. ASSERT_THAT(rv, IsOk());
  4502. scoped_refptr<IOBuffer> buf = base::MakeRefCounted<IOBuffer>(256);
  4503. rv = trans->Read(buf.get(), 256, callback.callback());
  4504. EXPECT_THAT(rv, IsError(ERR_IO_PENDING));
  4505. // Test that destroying the transaction while it is reading from the cache
  4506. // works properly.
  4507. trans.reset();
  4508. // Make sure we pump any pending events, which should include a call to
  4509. // HttpCache::Transaction::OnCacheReadCompleted.
  4510. base::RunLoop().RunUntilIdle();
  4511. }
  4512. // Tests that we can delete the HttpCache and deal with queued transactions
  4513. // ("waiting for the backend" as opposed to Active or Doomed entries).
  4514. TEST_F(HttpCacheTest, SimpleGET_ManyWriters_DeleteCache) {
  4515. auto cache = std::make_unique<MockHttpCache>(
  4516. std::make_unique<MockBackendNoCbFactory>());
  4517. MockHttpRequest request(kSimpleGET_Transaction);
  4518. std::vector<std::unique_ptr<Context>> context_list;
  4519. const int kNumTransactions = 5;
  4520. for (int i = 0; i < kNumTransactions; i++) {
  4521. context_list.push_back(std::make_unique<Context>());
  4522. Context* c = context_list[i].get();
  4523. c->result = cache->CreateTransaction(&c->trans);
  4524. ASSERT_THAT(c->result, IsOk());
  4525. c->result =
  4526. c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
  4527. }
  4528. // The first request should be creating the disk cache entry and the others
  4529. // should be pending.
  4530. EXPECT_EQ(0, cache->network_layer()->transaction_count());
  4531. EXPECT_EQ(0, cache->disk_cache()->open_count());
  4532. EXPECT_EQ(0, cache->disk_cache()->create_count());
  4533. cache.reset();
  4534. }
  4535. // Tests that we queue requests when initializing the backend.
  4536. TEST_F(HttpCacheTest, SimpleGET_WaitForBackend) {
  4537. auto factory = std::make_unique<MockBlockingBackendFactory>();
  4538. MockBlockingBackendFactory* factory_ptr = factory.get();
  4539. MockHttpCache cache(std::move(factory));
  4540. MockHttpRequest request0(kSimpleGET_Transaction);
  4541. MockHttpRequest request1(kTypicalGET_Transaction);
  4542. MockHttpRequest request2(kETagGET_Transaction);
  4543. std::vector<std::unique_ptr<Context>> context_list;
  4544. const int kNumTransactions = 3;
  4545. for (int i = 0; i < kNumTransactions; i++) {
  4546. context_list.push_back(std::make_unique<Context>());
  4547. Context* c = context_list[i].get();
  4548. c->result = cache.CreateTransaction(&c->trans);
  4549. ASSERT_THAT(c->result, IsOk());
  4550. }
  4551. context_list[0]->result = context_list[0]->trans->Start(
  4552. &request0, context_list[0]->callback.callback(), NetLogWithSource());
  4553. context_list[1]->result = context_list[1]->trans->Start(
  4554. &request1, context_list[1]->callback.callback(), NetLogWithSource());
  4555. context_list[2]->result = context_list[2]->trans->Start(
  4556. &request2, context_list[2]->callback.callback(), NetLogWithSource());
  4557. // Just to make sure that everything is still pending.
  4558. base::RunLoop().RunUntilIdle();
  4559. // The first request should be creating the disk cache.
  4560. EXPECT_FALSE(context_list[0]->callback.have_result());
  4561. factory_ptr->FinishCreation();
  4562. base::RunLoop().RunUntilIdle();
  4563. EXPECT_EQ(3, cache.network_layer()->transaction_count());
  4564. EXPECT_EQ(3, cache.disk_cache()->create_count());
  4565. for (int i = 0; i < kNumTransactions; ++i) {
  4566. EXPECT_TRUE(context_list[i]->callback.have_result());
  4567. context_list[i].reset();
  4568. }
  4569. }
  4570. // Tests that we can cancel requests that are queued waiting for the backend
  4571. // to be initialized.
  4572. TEST_F(HttpCacheTest, SimpleGET_WaitForBackend_CancelCreate) {
  4573. auto factory = std::make_unique<MockBlockingBackendFactory>();
  4574. MockBlockingBackendFactory* factory_ptr = factory.get();
  4575. MockHttpCache cache(std::move(factory));
  4576. MockHttpRequest request0(kSimpleGET_Transaction);
  4577. MockHttpRequest request1(kTypicalGET_Transaction);
  4578. MockHttpRequest request2(kETagGET_Transaction);
  4579. std::vector<std::unique_ptr<Context>> context_list;
  4580. const int kNumTransactions = 3;
  4581. for (int i = 0; i < kNumTransactions; i++) {
  4582. context_list.push_back(std::make_unique<Context>());
  4583. Context* c = context_list[i].get();
  4584. c->result = cache.CreateTransaction(&c->trans);
  4585. ASSERT_THAT(c->result, IsOk());
  4586. }
  4587. context_list[0]->result = context_list[0]->trans->Start(
  4588. &request0, context_list[0]->callback.callback(), NetLogWithSource());
  4589. context_list[1]->result = context_list[1]->trans->Start(
  4590. &request1, context_list[1]->callback.callback(), NetLogWithSource());
  4591. context_list[2]->result = context_list[2]->trans->Start(
  4592. &request2, context_list[2]->callback.callback(), NetLogWithSource());
  4593. // Just to make sure that everything is still pending.
  4594. base::RunLoop().RunUntilIdle();
  4595. // The first request should be creating the disk cache.
  4596. EXPECT_FALSE(context_list[0]->callback.have_result());
  4597. // Cancel a request from the pending queue.
  4598. context_list[1].reset();
  4599. // Cancel the request that is creating the entry.
  4600. context_list[0].reset();
  4601. // Complete the last transaction.
  4602. factory_ptr->FinishCreation();
  4603. context_list[2]->result =
  4604. context_list[2]->callback.GetResult(context_list[2]->result);
  4605. ReadAndVerifyTransaction(context_list[2]->trans.get(), kETagGET_Transaction);
  4606. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  4607. EXPECT_EQ(1, cache.disk_cache()->create_count());
  4608. }
  4609. // Tests that we can delete the HttpCache while creating the backend.
  4610. TEST_F(HttpCacheTest, DeleteCacheWaitingForBackend) {
  4611. auto factory = std::make_unique<MockBlockingBackendFactory>();
  4612. MockBlockingBackendFactory* factory_ptr = factory.get();
  4613. auto cache = std::make_unique<MockHttpCache>(std::move(factory));
  4614. MockHttpRequest request(kSimpleGET_Transaction);
  4615. auto c = std::make_unique<Context>();
  4616. c->result = cache->CreateTransaction(&c->trans);
  4617. ASSERT_THAT(c->result, IsOk());
  4618. c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
  4619. // Just to make sure that everything is still pending.
  4620. base::RunLoop().RunUntilIdle();
  4621. // The request should be creating the disk cache.
  4622. EXPECT_FALSE(c->callback.have_result());
  4623. // Manually arrange for completion to happen after ~HttpCache.
  4624. // This can't be done via FinishCreation() since that's in `factory`, and
  4625. // that's owned by `cache`.
  4626. disk_cache::BackendResultCallback callback = factory_ptr->ReleaseCallback();
  4627. cache.reset();
  4628. base::RunLoop().RunUntilIdle();
  4629. // Simulate the backend completion callback running now the HttpCache is gone.
  4630. std::move(callback).Run(disk_cache::BackendResult::MakeError(ERR_ABORTED));
  4631. }
  4632. // Tests that we can delete the cache while creating the backend, from within
  4633. // one of the callbacks.
  4634. TEST_F(HttpCacheTest, DeleteCacheWaitingForBackend2) {
  4635. auto factory = std::make_unique<MockBlockingBackendFactory>();
  4636. MockBlockingBackendFactory* factory_ptr = factory.get();
  4637. auto cache = std::make_unique<MockHttpCache>(std::move(factory));
  4638. auto* cache_ptr = cache.get();
  4639. DeleteCacheCompletionCallback cb(std::move(cache));
  4640. disk_cache::Backend* backend;
  4641. int rv = cache_ptr->http_cache()->GetBackend(&backend, cb.callback());
  4642. EXPECT_THAT(rv, IsError(ERR_IO_PENDING));
  4643. // Now let's queue a regular transaction
  4644. MockHttpRequest request(kSimpleGET_Transaction);
  4645. auto c = std::make_unique<Context>();
  4646. c->result = cache_ptr->CreateTransaction(&c->trans);
  4647. ASSERT_THAT(c->result, IsOk());
  4648. c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
  4649. // And another direct backend request.
  4650. TestCompletionCallback cb2;
  4651. rv = cache_ptr->http_cache()->GetBackend(&backend, cb2.callback());
  4652. EXPECT_THAT(rv, IsError(ERR_IO_PENDING));
  4653. // Just to make sure that everything is still pending.
  4654. base::RunLoop().RunUntilIdle();
  4655. // The request should be queued.
  4656. EXPECT_FALSE(c->callback.have_result());
  4657. // Generate the callback.
  4658. factory_ptr->FinishCreation();
  4659. rv = cb.WaitForResult();
  4660. // The cache should be gone by now.
  4661. base::RunLoop().RunUntilIdle();
  4662. EXPECT_THAT(c->callback.GetResult(c->result), IsOk());
  4663. EXPECT_FALSE(cb2.have_result());
  4664. }
  4665. TEST_F(HttpCacheTest, TypicalGET_ConditionalRequest) {
  4666. MockHttpCache cache;
  4667. // write to the cache
  4668. RunTransactionTest(cache.http_cache(), kTypicalGET_Transaction);
  4669. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  4670. EXPECT_EQ(0, cache.disk_cache()->open_count());
  4671. EXPECT_EQ(1, cache.disk_cache()->create_count());
  4672. // Get the same URL again, but this time we expect it to result
  4673. // in a conditional request.
  4674. LoadTimingInfo load_timing_info;
  4675. RunTransactionTestAndGetTiming(cache.http_cache(), kTypicalGET_Transaction,
  4676. NetLogWithSource::Make(NetLogSourceType::NONE),
  4677. &load_timing_info);
  4678. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  4679. EXPECT_EQ(1, cache.disk_cache()->open_count());
  4680. EXPECT_EQ(1, cache.disk_cache()->create_count());
  4681. TestLoadTimingNetworkRequest(load_timing_info);
  4682. }
  4683. static void ETagGet_ConditionalRequest_Handler(const HttpRequestInfo* request,
  4684. std::string* response_status,
  4685. std::string* response_headers,
  4686. std::string* response_data) {
  4687. EXPECT_TRUE(
  4688. request->extra_headers.HasHeader(HttpRequestHeaders::kIfNoneMatch));
  4689. response_status->assign("HTTP/1.1 304 Not Modified");
  4690. response_headers->assign(kETagGET_Transaction.response_headers);
  4691. response_data->clear();
  4692. }
  4693. TEST_F(HttpCacheTest, ETagGET_ConditionalRequest_304) {
  4694. MockHttpCache cache;
  4695. ScopedMockTransaction transaction(kETagGET_Transaction);
  4696. // write to the cache
  4697. RunTransactionTest(cache.http_cache(), transaction);
  4698. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  4699. EXPECT_EQ(0, cache.disk_cache()->open_count());
  4700. EXPECT_EQ(1, cache.disk_cache()->create_count());
  4701. // Get the same URL again, but this time we expect it to result
  4702. // in a conditional request.
  4703. transaction.load_flags = LOAD_VALIDATE_CACHE;
  4704. transaction.handler = ETagGet_ConditionalRequest_Handler;
  4705. LoadTimingInfo load_timing_info;
  4706. IPEndPoint remote_endpoint;
  4707. RunTransactionTestAndGetTimingAndConnectedSocketAddress(
  4708. cache.http_cache(), transaction,
  4709. NetLogWithSource::Make(NetLogSourceType::NONE), &load_timing_info,
  4710. &remote_endpoint);
  4711. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  4712. EXPECT_EQ(1, cache.disk_cache()->open_count());
  4713. EXPECT_EQ(1, cache.disk_cache()->create_count());
  4714. TestLoadTimingNetworkRequest(load_timing_info);
  4715. EXPECT_FALSE(remote_endpoint.address().empty());
  4716. }
  4717. class RevalidationServer {
  4718. public:
  4719. RevalidationServer() {
  4720. s_etag_used_ = false;
  4721. s_last_modified_used_ = false;
  4722. }
  4723. bool EtagUsed() { return s_etag_used_; }
  4724. bool LastModifiedUsed() { return s_last_modified_used_; }
  4725. static void Handler(const HttpRequestInfo* request,
  4726. std::string* response_status,
  4727. std::string* response_headers,
  4728. std::string* response_data);
  4729. private:
  4730. static bool s_etag_used_;
  4731. static bool s_last_modified_used_;
  4732. };
  4733. bool RevalidationServer::s_etag_used_ = false;
  4734. bool RevalidationServer::s_last_modified_used_ = false;
  4735. void RevalidationServer::Handler(const HttpRequestInfo* request,
  4736. std::string* response_status,
  4737. std::string* response_headers,
  4738. std::string* response_data) {
  4739. if (request->extra_headers.HasHeader(HttpRequestHeaders::kIfNoneMatch))
  4740. s_etag_used_ = true;
  4741. if (request->extra_headers.HasHeader(HttpRequestHeaders::kIfModifiedSince)) {
  4742. s_last_modified_used_ = true;
  4743. }
  4744. if (s_etag_used_ || s_last_modified_used_) {
  4745. response_status->assign("HTTP/1.1 304 Not Modified");
  4746. response_headers->assign(kTypicalGET_Transaction.response_headers);
  4747. response_data->clear();
  4748. } else {
  4749. response_status->assign(kTypicalGET_Transaction.status);
  4750. response_headers->assign(kTypicalGET_Transaction.response_headers);
  4751. response_data->assign(kTypicalGET_Transaction.data);
  4752. }
  4753. }
  4754. // Tests revalidation after a vary match.
  4755. TEST_F(HttpCacheTest, GET_ValidateCache_VaryMatch) {
  4756. MockHttpCache cache;
  4757. // Write to the cache.
  4758. MockTransaction transaction(kTypicalGET_Transaction);
  4759. transaction.request_headers = "Foo: bar\r\n";
  4760. transaction.response_headers =
  4761. "Date: Wed, 28 Nov 2007 09:40:09 GMT\n"
  4762. "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n"
  4763. "Etag: \"foopy\"\n"
  4764. "Cache-Control: max-age=0\n"
  4765. "Vary: Foo\n";
  4766. AddMockTransaction(&transaction);
  4767. RunTransactionTest(cache.http_cache(), transaction);
  4768. // Read from the cache.
  4769. RevalidationServer server;
  4770. transaction.handler = server.Handler;
  4771. LoadTimingInfo load_timing_info;
  4772. RunTransactionTestAndGetTiming(cache.http_cache(), transaction,
  4773. NetLogWithSource::Make(NetLogSourceType::NONE),
  4774. &load_timing_info);
  4775. EXPECT_TRUE(server.EtagUsed());
  4776. EXPECT_TRUE(server.LastModifiedUsed());
  4777. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  4778. EXPECT_EQ(1, cache.disk_cache()->open_count());
  4779. EXPECT_EQ(1, cache.disk_cache()->create_count());
  4780. TestLoadTimingNetworkRequest(load_timing_info);
  4781. RemoveMockTransaction(&transaction);
  4782. }
  4783. // Tests revalidation after a vary mismatch if etag is present.
  4784. TEST_F(HttpCacheTest, GET_ValidateCache_VaryMismatch) {
  4785. MockHttpCache cache;
  4786. // Write to the cache.
  4787. MockTransaction transaction(kTypicalGET_Transaction);
  4788. transaction.request_headers = "Foo: bar\r\n";
  4789. transaction.response_headers =
  4790. "Date: Wed, 28 Nov 2007 09:40:09 GMT\n"
  4791. "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n"
  4792. "Etag: \"foopy\"\n"
  4793. "Cache-Control: max-age=0\n"
  4794. "Vary: Foo\n";
  4795. AddMockTransaction(&transaction);
  4796. RunTransactionTest(cache.http_cache(), transaction);
  4797. // Read from the cache and revalidate the entry.
  4798. RevalidationServer server;
  4799. transaction.handler = server.Handler;
  4800. transaction.request_headers = "Foo: none\r\n";
  4801. LoadTimingInfo load_timing_info;
  4802. RunTransactionTestAndGetTiming(cache.http_cache(), transaction,
  4803. NetLogWithSource::Make(NetLogSourceType::NONE),
  4804. &load_timing_info);
  4805. EXPECT_TRUE(server.EtagUsed());
  4806. EXPECT_FALSE(server.LastModifiedUsed());
  4807. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  4808. EXPECT_EQ(1, cache.disk_cache()->open_count());
  4809. EXPECT_EQ(1, cache.disk_cache()->create_count());
  4810. TestLoadTimingNetworkRequest(load_timing_info);
  4811. RemoveMockTransaction(&transaction);
  4812. }
  4813. // Tests revalidation after a vary mismatch due to vary: * if etag is present.
  4814. TEST_F(HttpCacheTest, GET_ValidateCache_VaryMismatchStar) {
  4815. MockHttpCache cache;
  4816. // Write to the cache.
  4817. MockTransaction transaction(kTypicalGET_Transaction);
  4818. transaction.response_headers =
  4819. "Date: Wed, 28 Nov 2007 09:40:09 GMT\n"
  4820. "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n"
  4821. "Etag: \"foopy\"\n"
  4822. "Cache-Control: max-age=0\n"
  4823. "Vary: *\n";
  4824. AddMockTransaction(&transaction);
  4825. RunTransactionTest(cache.http_cache(), transaction);
  4826. // Read from the cache and revalidate the entry.
  4827. RevalidationServer server;
  4828. transaction.handler = server.Handler;
  4829. LoadTimingInfo load_timing_info;
  4830. RunTransactionTestAndGetTiming(cache.http_cache(), transaction,
  4831. NetLogWithSource::Make(NetLogSourceType::NONE),
  4832. &load_timing_info);
  4833. EXPECT_TRUE(server.EtagUsed());
  4834. EXPECT_FALSE(server.LastModifiedUsed());
  4835. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  4836. EXPECT_EQ(1, cache.disk_cache()->open_count());
  4837. EXPECT_EQ(1, cache.disk_cache()->create_count());
  4838. TestLoadTimingNetworkRequest(load_timing_info);
  4839. RemoveMockTransaction(&transaction);
  4840. }
  4841. // Tests lack of revalidation after a vary mismatch and no etag.
  4842. TEST_F(HttpCacheTest, GET_DontValidateCache_VaryMismatch) {
  4843. MockHttpCache cache;
  4844. // Write to the cache.
  4845. MockTransaction transaction(kTypicalGET_Transaction);
  4846. transaction.request_headers = "Foo: bar\r\n";
  4847. transaction.response_headers =
  4848. "Date: Wed, 28 Nov 2007 09:40:09 GMT\n"
  4849. "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n"
  4850. "Cache-Control: max-age=0\n"
  4851. "Vary: Foo\n";
  4852. AddMockTransaction(&transaction);
  4853. RunTransactionTest(cache.http_cache(), transaction);
  4854. // Read from the cache and don't revalidate the entry.
  4855. RevalidationServer server;
  4856. transaction.handler = server.Handler;
  4857. transaction.request_headers = "Foo: none\r\n";
  4858. LoadTimingInfo load_timing_info;
  4859. RunTransactionTestAndGetTiming(cache.http_cache(), transaction,
  4860. NetLogWithSource::Make(NetLogSourceType::NONE),
  4861. &load_timing_info);
  4862. EXPECT_FALSE(server.EtagUsed());
  4863. EXPECT_FALSE(server.LastModifiedUsed());
  4864. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  4865. EXPECT_EQ(1, cache.disk_cache()->open_count());
  4866. EXPECT_EQ(1, cache.disk_cache()->create_count());
  4867. TestLoadTimingNetworkRequest(load_timing_info);
  4868. RemoveMockTransaction(&transaction);
  4869. }
  4870. // Tests that a new vary header provided when revalidating an entry is saved.
  4871. TEST_F(HttpCacheTest, GET_ValidateCache_VaryMatch_UpdateVary) {
  4872. MockHttpCache cache;
  4873. // Write to the cache.
  4874. ScopedMockTransaction transaction(kTypicalGET_Transaction);
  4875. transaction.request_headers = "Foo: bar\r\n Name: bar\r\n";
  4876. transaction.response_headers =
  4877. "Etag: \"foopy\"\n"
  4878. "Cache-Control: max-age=0\n"
  4879. "Vary: Foo\n";
  4880. RunTransactionTest(cache.http_cache(), transaction);
  4881. // Validate the entry and change the vary field in the response.
  4882. transaction.request_headers = "Foo: bar\r\n Name: none\r\n";
  4883. transaction.status = "HTTP/1.1 304 Not Modified";
  4884. transaction.response_headers =
  4885. "Etag: \"foopy\"\n"
  4886. "Cache-Control: max-age=3600\n"
  4887. "Vary: Name\n";
  4888. RunTransactionTest(cache.http_cache(), transaction);
  4889. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  4890. EXPECT_EQ(1, cache.disk_cache()->open_count());
  4891. EXPECT_EQ(1, cache.disk_cache()->create_count());
  4892. // Make sure that the ActiveEntry is gone.
  4893. base::RunLoop().RunUntilIdle();
  4894. // Generate a vary mismatch.
  4895. transaction.request_headers = "Foo: bar\r\n Name: bar\r\n";
  4896. RunTransactionTest(cache.http_cache(), transaction);
  4897. EXPECT_EQ(3, cache.network_layer()->transaction_count());
  4898. EXPECT_EQ(2, cache.disk_cache()->open_count());
  4899. EXPECT_EQ(1, cache.disk_cache()->create_count());
  4900. }
  4901. // Tests that new request headers causing a vary mismatch are paired with the
  4902. // new response when the server says the old response can be used.
  4903. TEST_F(HttpCacheTest, GET_ValidateCache_VaryMismatch_UpdateRequestHeader) {
  4904. MockHttpCache cache;
  4905. // Write to the cache.
  4906. ScopedMockTransaction transaction(kTypicalGET_Transaction);
  4907. transaction.request_headers = "Foo: bar\r\n";
  4908. transaction.response_headers =
  4909. "Etag: \"foopy\"\n"
  4910. "Cache-Control: max-age=3600\n"
  4911. "Vary: Foo\n";
  4912. RunTransactionTest(cache.http_cache(), transaction);
  4913. // Vary-mismatch validation receives 304.
  4914. transaction.request_headers = "Foo: none\r\n";
  4915. transaction.status = "HTTP/1.1 304 Not Modified";
  4916. RunTransactionTest(cache.http_cache(), transaction);
  4917. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  4918. EXPECT_EQ(1, cache.disk_cache()->open_count());
  4919. EXPECT_EQ(1, cache.disk_cache()->create_count());
  4920. // Make sure that the ActiveEntry is gone.
  4921. base::RunLoop().RunUntilIdle();
  4922. // Generate a vary mismatch.
  4923. transaction.request_headers = "Foo: bar\r\n";
  4924. RunTransactionTest(cache.http_cache(), transaction);
  4925. EXPECT_EQ(3, cache.network_layer()->transaction_count());
  4926. EXPECT_EQ(2, cache.disk_cache()->open_count());
  4927. EXPECT_EQ(1, cache.disk_cache()->create_count());
  4928. }
  4929. // Tests that a 304 without vary headers doesn't delete the previously stored
  4930. // vary data after a vary match revalidation.
  4931. TEST_F(HttpCacheTest, GET_ValidateCache_VaryMatch_DontDeleteVary) {
  4932. MockHttpCache cache;
  4933. // Write to the cache.
  4934. ScopedMockTransaction transaction(kTypicalGET_Transaction);
  4935. transaction.request_headers = "Foo: bar\r\n";
  4936. transaction.response_headers =
  4937. "Etag: \"foopy\"\n"
  4938. "Cache-Control: max-age=0\n"
  4939. "Vary: Foo\n";
  4940. RunTransactionTest(cache.http_cache(), transaction);
  4941. // Validate the entry and remove the vary field in the response.
  4942. transaction.status = "HTTP/1.1 304 Not Modified";
  4943. transaction.response_headers =
  4944. "Etag: \"foopy\"\n"
  4945. "Cache-Control: max-age=3600\n";
  4946. RunTransactionTest(cache.http_cache(), transaction);
  4947. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  4948. EXPECT_EQ(1, cache.disk_cache()->open_count());
  4949. EXPECT_EQ(1, cache.disk_cache()->create_count());
  4950. // Make sure that the ActiveEntry is gone.
  4951. base::RunLoop().RunUntilIdle();
  4952. // Generate a vary mismatch.
  4953. transaction.request_headers = "Foo: none\r\n";
  4954. RunTransactionTest(cache.http_cache(), transaction);
  4955. EXPECT_EQ(3, cache.network_layer()->transaction_count());
  4956. EXPECT_EQ(2, cache.disk_cache()->open_count());
  4957. EXPECT_EQ(1, cache.disk_cache()->create_count());
  4958. }
  4959. // Tests that a 304 without vary headers doesn't delete the previously stored
  4960. // vary data after a vary mismatch.
  4961. TEST_F(HttpCacheTest, GET_ValidateCache_VaryMismatch_DontDeleteVary) {
  4962. MockHttpCache cache;
  4963. // Write to the cache.
  4964. ScopedMockTransaction transaction(kTypicalGET_Transaction);
  4965. transaction.request_headers = "Foo: bar\r\n";
  4966. transaction.response_headers =
  4967. "Etag: \"foopy\"\n"
  4968. "Cache-Control: max-age=3600\n"
  4969. "Vary: Foo\n";
  4970. RunTransactionTest(cache.http_cache(), transaction);
  4971. // Vary-mismatch validation receives 304 and no vary header.
  4972. transaction.request_headers = "Foo: none\r\n";
  4973. transaction.status = "HTTP/1.1 304 Not Modified";
  4974. transaction.response_headers =
  4975. "Etag: \"foopy\"\n"
  4976. "Cache-Control: max-age=3600\n";
  4977. RunTransactionTest(cache.http_cache(), transaction);
  4978. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  4979. EXPECT_EQ(1, cache.disk_cache()->open_count());
  4980. EXPECT_EQ(1, cache.disk_cache()->create_count());
  4981. // Make sure that the ActiveEntry is gone.
  4982. base::RunLoop().RunUntilIdle();
  4983. // Generate a vary mismatch.
  4984. transaction.request_headers = "Foo: bar\r\n";
  4985. RunTransactionTest(cache.http_cache(), transaction);
  4986. EXPECT_EQ(3, cache.network_layer()->transaction_count());
  4987. EXPECT_EQ(2, cache.disk_cache()->open_count());
  4988. EXPECT_EQ(1, cache.disk_cache()->create_count());
  4989. }
  4990. static void ETagGet_UnconditionalRequest_Handler(const HttpRequestInfo* request,
  4991. std::string* response_status,
  4992. std::string* response_headers,
  4993. std::string* response_data) {
  4994. EXPECT_FALSE(
  4995. request->extra_headers.HasHeader(HttpRequestHeaders::kIfNoneMatch));
  4996. }
  4997. TEST_F(HttpCacheTest, ETagGET_Http10) {
  4998. MockHttpCache cache;
  4999. ScopedMockTransaction transaction(kETagGET_Transaction);
  5000. transaction.status = "HTTP/1.0 200 OK";
  5001. // Write to the cache.
  5002. RunTransactionTest(cache.http_cache(), transaction);
  5003. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  5004. EXPECT_EQ(0, cache.disk_cache()->open_count());
  5005. EXPECT_EQ(1, cache.disk_cache()->create_count());
  5006. // Get the same URL again, without generating a conditional request.
  5007. transaction.load_flags = LOAD_VALIDATE_CACHE;
  5008. transaction.handler = ETagGet_UnconditionalRequest_Handler;
  5009. RunTransactionTest(cache.http_cache(), transaction);
  5010. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  5011. EXPECT_EQ(1, cache.disk_cache()->open_count());
  5012. EXPECT_EQ(1, cache.disk_cache()->create_count());
  5013. }
  5014. TEST_F(HttpCacheTest, ETagGET_Http10_Range) {
  5015. MockHttpCache cache;
  5016. ScopedMockTransaction transaction(kETagGET_Transaction);
  5017. transaction.status = "HTTP/1.0 200 OK";
  5018. // Write to the cache.
  5019. RunTransactionTest(cache.http_cache(), transaction);
  5020. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  5021. EXPECT_EQ(0, cache.disk_cache()->open_count());
  5022. EXPECT_EQ(1, cache.disk_cache()->create_count());
  5023. // Get the same URL again, but use a byte range request.
  5024. transaction.load_flags = LOAD_VALIDATE_CACHE;
  5025. transaction.handler = ETagGet_UnconditionalRequest_Handler;
  5026. transaction.request_headers = "Range: bytes = 5-\r\n";
  5027. RunTransactionTest(cache.http_cache(), transaction);
  5028. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  5029. EXPECT_EQ(1, cache.disk_cache()->open_count());
  5030. EXPECT_EQ(2, cache.disk_cache()->create_count());
  5031. }
  5032. static void ETagGet_ConditionalRequest_NoStore_Handler(
  5033. const HttpRequestInfo* request,
  5034. std::string* response_status,
  5035. std::string* response_headers,
  5036. std::string* response_data) {
  5037. EXPECT_TRUE(
  5038. request->extra_headers.HasHeader(HttpRequestHeaders::kIfNoneMatch));
  5039. response_status->assign("HTTP/1.1 304 Not Modified");
  5040. response_headers->assign("Cache-Control: no-store\n");
  5041. response_data->clear();
  5042. }
  5043. TEST_F(HttpCacheTest, ETagGET_ConditionalRequest_304_NoStore) {
  5044. MockHttpCache cache;
  5045. ScopedMockTransaction transaction(kETagGET_Transaction);
  5046. // Write to the cache.
  5047. RunTransactionTest(cache.http_cache(), transaction);
  5048. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  5049. EXPECT_EQ(0, cache.disk_cache()->open_count());
  5050. EXPECT_EQ(1, cache.disk_cache()->create_count());
  5051. // Get the same URL again, but this time we expect it to result
  5052. // in a conditional request.
  5053. transaction.load_flags = LOAD_VALIDATE_CACHE;
  5054. transaction.handler = ETagGet_ConditionalRequest_NoStore_Handler;
  5055. RunTransactionTest(cache.http_cache(), transaction);
  5056. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  5057. EXPECT_EQ(1, cache.disk_cache()->open_count());
  5058. EXPECT_EQ(1, cache.disk_cache()->create_count());
  5059. ScopedMockTransaction transaction2(kETagGET_Transaction);
  5060. // Write to the cache again. This should create a new entry.
  5061. RunTransactionTest(cache.http_cache(), transaction2);
  5062. EXPECT_EQ(3, cache.network_layer()->transaction_count());
  5063. EXPECT_EQ(1, cache.disk_cache()->open_count());
  5064. EXPECT_EQ(2, cache.disk_cache()->create_count());
  5065. }
  5066. // Helper that does 4 requests using HttpCache:
  5067. //
  5068. // (1) loads |kUrl| -- expects |net_response_1| to be returned.
  5069. // (2) loads |kUrl| from cache only -- expects |net_response_1| to be returned.
  5070. // (3) loads |kUrl| using |extra_request_headers| -- expects |net_response_2| to
  5071. // be returned.
  5072. // (4) loads |kUrl| from cache only -- expects |cached_response_2| to be
  5073. // returned.
  5074. // The entry will be created once and will be opened for the 3 subsequent
  5075. // requests.
  5076. static void ConditionalizedRequestUpdatesCacheHelper(
  5077. const Response& net_response_1,
  5078. const Response& net_response_2,
  5079. const Response& cached_response_2,
  5080. const char* extra_request_headers) {
  5081. MockHttpCache cache;
  5082. // The URL we will be requesting.
  5083. const char kUrl[] = "http://foobar.com/main.css";
  5084. // Junk network response.
  5085. static const Response kUnexpectedResponse = {
  5086. "HTTP/1.1 500 Unexpected",
  5087. "Server: unexpected_header",
  5088. "unexpected body"
  5089. };
  5090. // We will control the network layer's responses for |kUrl| using
  5091. // |mock_network_response|.
  5092. MockTransaction mock_network_response = {nullptr};
  5093. mock_network_response.url = kUrl;
  5094. AddMockTransaction(&mock_network_response);
  5095. // Request |kUrl| for the first time. It should hit the network and
  5096. // receive |kNetResponse1|, which it saves into the HTTP cache.
  5097. MockTransaction request = {nullptr};
  5098. request.url = kUrl;
  5099. request.method = "GET";
  5100. request.request_headers = "";
  5101. net_response_1.AssignTo(&mock_network_response); // Network mock.
  5102. net_response_1.AssignTo(&request); // Expected result.
  5103. std::string response_headers;
  5104. RunTransactionTestWithResponse(
  5105. cache.http_cache(), request, &response_headers);
  5106. EXPECT_EQ(net_response_1.status_and_headers(), response_headers);
  5107. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  5108. EXPECT_EQ(0, cache.disk_cache()->open_count());
  5109. EXPECT_EQ(1, cache.disk_cache()->create_count());
  5110. // Request |kUrl| a second time. Now |kNetResponse1| it is in the HTTP
  5111. // cache, so we don't hit the network.
  5112. request.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
  5113. kUnexpectedResponse.AssignTo(&mock_network_response); // Network mock.
  5114. net_response_1.AssignTo(&request); // Expected result.
  5115. RunTransactionTestWithResponse(
  5116. cache.http_cache(), request, &response_headers);
  5117. EXPECT_EQ(net_response_1.status_and_headers(), response_headers);
  5118. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  5119. EXPECT_EQ(1, cache.disk_cache()->open_count());
  5120. EXPECT_EQ(1, cache.disk_cache()->create_count());
  5121. // Request |kUrl| yet again, but this time give the request an
  5122. // "If-Modified-Since" header. This will cause the request to re-hit the
  5123. // network. However now the network response is going to be
  5124. // different -- this simulates a change made to the CSS file.
  5125. request.request_headers = extra_request_headers;
  5126. request.load_flags = LOAD_NORMAL;
  5127. net_response_2.AssignTo(&mock_network_response); // Network mock.
  5128. net_response_2.AssignTo(&request); // Expected result.
  5129. RunTransactionTestWithResponse(
  5130. cache.http_cache(), request, &response_headers);
  5131. EXPECT_EQ(net_response_2.status_and_headers(), response_headers);
  5132. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  5133. EXPECT_EQ(1, cache.disk_cache()->open_count());
  5134. EXPECT_EQ(1, cache.disk_cache()->create_count());
  5135. // Finally, request |kUrl| again. This request should be serviced from
  5136. // the cache. Moreover, the value in the cache should be |kNetResponse2|
  5137. // and NOT |kNetResponse1|. The previous step should have replaced the
  5138. // value in the cache with the modified response.
  5139. request.request_headers = "";
  5140. request.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
  5141. kUnexpectedResponse.AssignTo(&mock_network_response); // Network mock.
  5142. cached_response_2.AssignTo(&request); // Expected result.
  5143. RunTransactionTestWithResponse(
  5144. cache.http_cache(), request, &response_headers);
  5145. EXPECT_EQ(cached_response_2.status_and_headers(), response_headers);
  5146. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  5147. EXPECT_EQ(2, cache.disk_cache()->open_count());
  5148. EXPECT_EQ(1, cache.disk_cache()->create_count());
  5149. RemoveMockTransaction(&mock_network_response);
  5150. }
  5151. // Check that when an "if-modified-since" header is attached
  5152. // to the request, the result still updates the cached entry.
  5153. TEST_F(HttpCacheTest, ConditionalizedRequestUpdatesCache1) {
  5154. // First network response for |kUrl|.
  5155. static const Response kNetResponse1 = {
  5156. "HTTP/1.1 200 OK",
  5157. "Date: Fri, 12 Jun 2009 21:46:42 GMT\n"
  5158. "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
  5159. "body1"
  5160. };
  5161. // Second network response for |kUrl|.
  5162. static const Response kNetResponse2 = {
  5163. "HTTP/1.1 200 OK",
  5164. "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
  5165. "Last-Modified: Fri, 03 Jul 2009 02:14:27 GMT\n",
  5166. "body2"
  5167. };
  5168. const char extra_headers[] =
  5169. "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n";
  5170. ConditionalizedRequestUpdatesCacheHelper(
  5171. kNetResponse1, kNetResponse2, kNetResponse2, extra_headers);
  5172. }
  5173. // Check that when an "if-none-match" header is attached
  5174. // to the request, the result updates the cached entry.
  5175. TEST_F(HttpCacheTest, ConditionalizedRequestUpdatesCache2) {
  5176. // First network response for |kUrl|.
  5177. static const Response kNetResponse1 = {
  5178. "HTTP/1.1 200 OK",
  5179. "Date: Fri, 12 Jun 2009 21:46:42 GMT\n"
  5180. "Etag: \"ETAG1\"\n"
  5181. "Expires: Wed, 7 Sep 2033 21:46:42 GMT\n", // Should never expire.
  5182. "body1"
  5183. };
  5184. // Second network response for |kUrl|.
  5185. static const Response kNetResponse2 = {
  5186. "HTTP/1.1 200 OK",
  5187. "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
  5188. "Etag: \"ETAG2\"\n"
  5189. "Expires: Wed, 7 Sep 2033 21:46:42 GMT\n", // Should never expire.
  5190. "body2"
  5191. };
  5192. const char extra_headers[] = "If-None-Match: \"ETAG1\"\r\n";
  5193. ConditionalizedRequestUpdatesCacheHelper(
  5194. kNetResponse1, kNetResponse2, kNetResponse2, extra_headers);
  5195. }
  5196. // Check that when an "if-modified-since" header is attached
  5197. // to a request, the 304 (not modified result) result updates the cached
  5198. // headers, and the 304 response is returned rather than the cached response.
  5199. TEST_F(HttpCacheTest, ConditionalizedRequestUpdatesCache3) {
  5200. // First network response for |kUrl|.
  5201. static const Response kNetResponse1 = {
  5202. "HTTP/1.1 200 OK",
  5203. "Date: Fri, 12 Jun 2009 21:46:42 GMT\n"
  5204. "Server: server1\n"
  5205. "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
  5206. "body1"
  5207. };
  5208. // Second network response for |kUrl|.
  5209. static const Response kNetResponse2 = {
  5210. "HTTP/1.1 304 Not Modified",
  5211. "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
  5212. "Server: server2\n"
  5213. "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
  5214. ""
  5215. };
  5216. static const Response kCachedResponse2 = {
  5217. "HTTP/1.1 200 OK",
  5218. "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
  5219. "Server: server2\n"
  5220. "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
  5221. "body1"
  5222. };
  5223. const char extra_headers[] =
  5224. "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n";
  5225. ConditionalizedRequestUpdatesCacheHelper(
  5226. kNetResponse1, kNetResponse2, kCachedResponse2, extra_headers);
  5227. }
  5228. // Test that when doing an externally conditionalized if-modified-since
  5229. // and there is no corresponding cache entry, a new cache entry is NOT
  5230. // created (304 response).
  5231. TEST_F(HttpCacheTest, ConditionalizedRequestUpdatesCache4) {
  5232. MockHttpCache cache;
  5233. const char kUrl[] = "http://foobar.com/main.css";
  5234. static const Response kNetResponse = {
  5235. "HTTP/1.1 304 Not Modified",
  5236. "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
  5237. "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
  5238. ""
  5239. };
  5240. const char kExtraRequestHeaders[] =
  5241. "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n";
  5242. // We will control the network layer's responses for |kUrl| using
  5243. // |mock_network_response|.
  5244. MockTransaction mock_network_response = {nullptr};
  5245. mock_network_response.url = kUrl;
  5246. AddMockTransaction(&mock_network_response);
  5247. MockTransaction request = {nullptr};
  5248. request.url = kUrl;
  5249. request.method = "GET";
  5250. request.request_headers = kExtraRequestHeaders;
  5251. kNetResponse.AssignTo(&mock_network_response); // Network mock.
  5252. kNetResponse.AssignTo(&request); // Expected result.
  5253. std::string response_headers;
  5254. RunTransactionTestWithResponse(
  5255. cache.http_cache(), request, &response_headers);
  5256. EXPECT_EQ(kNetResponse.status_and_headers(), response_headers);
  5257. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  5258. EXPECT_EQ(0, cache.disk_cache()->open_count());
  5259. EXPECT_EQ(0, cache.disk_cache()->create_count());
  5260. RemoveMockTransaction(&mock_network_response);
  5261. }
  5262. // Test that when doing an externally conditionalized if-modified-since
  5263. // and there is no corresponding cache entry, a new cache entry is NOT
  5264. // created (200 response).
  5265. TEST_F(HttpCacheTest, ConditionalizedRequestUpdatesCache5) {
  5266. MockHttpCache cache;
  5267. const char kUrl[] = "http://foobar.com/main.css";
  5268. static const Response kNetResponse = {
  5269. "HTTP/1.1 200 OK",
  5270. "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
  5271. "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
  5272. "foobar!!!"
  5273. };
  5274. const char kExtraRequestHeaders[] =
  5275. "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n";
  5276. // We will control the network layer's responses for |kUrl| using
  5277. // |mock_network_response|.
  5278. MockTransaction mock_network_response = {nullptr};
  5279. mock_network_response.url = kUrl;
  5280. AddMockTransaction(&mock_network_response);
  5281. MockTransaction request = {nullptr};
  5282. request.url = kUrl;
  5283. request.method = "GET";
  5284. request.request_headers = kExtraRequestHeaders;
  5285. kNetResponse.AssignTo(&mock_network_response); // Network mock.
  5286. kNetResponse.AssignTo(&request); // Expected result.
  5287. std::string response_headers;
  5288. RunTransactionTestWithResponse(
  5289. cache.http_cache(), request, &response_headers);
  5290. EXPECT_EQ(kNetResponse.status_and_headers(), response_headers);
  5291. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  5292. EXPECT_EQ(0, cache.disk_cache()->open_count());
  5293. EXPECT_EQ(0, cache.disk_cache()->create_count());
  5294. RemoveMockTransaction(&mock_network_response);
  5295. }
  5296. // Test that when doing an externally conditionalized if-modified-since
  5297. // if the date does not match the cache entry's last-modified date,
  5298. // then we do NOT use the response (304) to update the cache.
  5299. // (the if-modified-since date is 2 days AFTER the cache's modification date).
  5300. TEST_F(HttpCacheTest, ConditionalizedRequestUpdatesCache6) {
  5301. static const Response kNetResponse1 = {
  5302. "HTTP/1.1 200 OK",
  5303. "Date: Fri, 12 Jun 2009 21:46:42 GMT\n"
  5304. "Server: server1\n"
  5305. "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
  5306. "body1"
  5307. };
  5308. // Second network response for |kUrl|.
  5309. static const Response kNetResponse2 = {
  5310. "HTTP/1.1 304 Not Modified",
  5311. "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
  5312. "Server: server2\n"
  5313. "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
  5314. ""
  5315. };
  5316. // This is two days in the future from the original response's last-modified
  5317. // date!
  5318. const char kExtraRequestHeaders[] =
  5319. "If-Modified-Since: Fri, 08 Feb 2008 22:38:21 GMT\r\n";
  5320. ConditionalizedRequestUpdatesCacheHelper(
  5321. kNetResponse1, kNetResponse2, kNetResponse1, kExtraRequestHeaders);
  5322. }
  5323. // Test that when doing an externally conditionalized if-none-match
  5324. // if the etag does not match the cache entry's etag, then we do not use the
  5325. // response (304) to update the cache.
  5326. TEST_F(HttpCacheTest, ConditionalizedRequestUpdatesCache7) {
  5327. static const Response kNetResponse1 = {
  5328. "HTTP/1.1 200 OK",
  5329. "Date: Fri, 12 Jun 2009 21:46:42 GMT\n"
  5330. "Etag: \"Foo1\"\n"
  5331. "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
  5332. "body1"
  5333. };
  5334. // Second network response for |kUrl|.
  5335. static const Response kNetResponse2 = {
  5336. "HTTP/1.1 304 Not Modified",
  5337. "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
  5338. "Etag: \"Foo2\"\n"
  5339. "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
  5340. ""
  5341. };
  5342. // Different etag from original response.
  5343. const char kExtraRequestHeaders[] = "If-None-Match: \"Foo2\"\r\n";
  5344. ConditionalizedRequestUpdatesCacheHelper(
  5345. kNetResponse1, kNetResponse2, kNetResponse1, kExtraRequestHeaders);
  5346. }
  5347. // Test that doing an externally conditionalized request with both if-none-match
  5348. // and if-modified-since updates the cache.
  5349. TEST_F(HttpCacheTest, ConditionalizedRequestUpdatesCache8) {
  5350. static const Response kNetResponse1 = {
  5351. "HTTP/1.1 200 OK",
  5352. "Date: Fri, 12 Jun 2009 21:46:42 GMT\n"
  5353. "Etag: \"Foo1\"\n"
  5354. "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
  5355. "body1"
  5356. };
  5357. // Second network response for |kUrl|.
  5358. static const Response kNetResponse2 = {
  5359. "HTTP/1.1 200 OK",
  5360. "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
  5361. "Etag: \"Foo2\"\n"
  5362. "Last-Modified: Fri, 03 Jul 2009 02:14:27 GMT\n",
  5363. "body2"
  5364. };
  5365. const char kExtraRequestHeaders[] =
  5366. "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n"
  5367. "If-None-Match: \"Foo1\"\r\n";
  5368. ConditionalizedRequestUpdatesCacheHelper(
  5369. kNetResponse1, kNetResponse2, kNetResponse2, kExtraRequestHeaders);
  5370. }
  5371. // Test that doing an externally conditionalized request with both if-none-match
  5372. // and if-modified-since does not update the cache with only one match.
  5373. TEST_F(HttpCacheTest, ConditionalizedRequestUpdatesCache9) {
  5374. static const Response kNetResponse1 = {
  5375. "HTTP/1.1 200 OK",
  5376. "Date: Fri, 12 Jun 2009 21:46:42 GMT\n"
  5377. "Etag: \"Foo1\"\n"
  5378. "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
  5379. "body1"
  5380. };
  5381. // Second network response for |kUrl|.
  5382. static const Response kNetResponse2 = {
  5383. "HTTP/1.1 200 OK",
  5384. "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
  5385. "Etag: \"Foo2\"\n"
  5386. "Last-Modified: Fri, 03 Jul 2009 02:14:27 GMT\n",
  5387. "body2"
  5388. };
  5389. // The etag doesn't match what we have stored.
  5390. const char kExtraRequestHeaders[] =
  5391. "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n"
  5392. "If-None-Match: \"Foo2\"\r\n";
  5393. ConditionalizedRequestUpdatesCacheHelper(
  5394. kNetResponse1, kNetResponse2, kNetResponse1, kExtraRequestHeaders);
  5395. }
  5396. // Test that doing an externally conditionalized request with both if-none-match
  5397. // and if-modified-since does not update the cache with only one match.
  5398. TEST_F(HttpCacheTest, ConditionalizedRequestUpdatesCache10) {
  5399. static const Response kNetResponse1 = {
  5400. "HTTP/1.1 200 OK",
  5401. "Date: Fri, 12 Jun 2009 21:46:42 GMT\n"
  5402. "Etag: \"Foo1\"\n"
  5403. "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
  5404. "body1"
  5405. };
  5406. // Second network response for |kUrl|.
  5407. static const Response kNetResponse2 = {
  5408. "HTTP/1.1 200 OK",
  5409. "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
  5410. "Etag: \"Foo2\"\n"
  5411. "Last-Modified: Fri, 03 Jul 2009 02:14:27 GMT\n",
  5412. "body2"
  5413. };
  5414. // The modification date doesn't match what we have stored.
  5415. const char kExtraRequestHeaders[] =
  5416. "If-Modified-Since: Fri, 08 Feb 2008 22:38:21 GMT\r\n"
  5417. "If-None-Match: \"Foo1\"\r\n";
  5418. ConditionalizedRequestUpdatesCacheHelper(
  5419. kNetResponse1, kNetResponse2, kNetResponse1, kExtraRequestHeaders);
  5420. }
  5421. TEST_F(HttpCacheTest, UrlContainingHash) {
  5422. MockHttpCache cache;
  5423. // Do a typical GET request -- should write an entry into our cache.
  5424. MockTransaction trans(kTypicalGET_Transaction);
  5425. RunTransactionTest(cache.http_cache(), trans);
  5426. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  5427. EXPECT_EQ(0, cache.disk_cache()->open_count());
  5428. EXPECT_EQ(1, cache.disk_cache()->create_count());
  5429. // Request the same URL, but this time with a reference section (hash).
  5430. // Since the cache key strips the hash sections, this should be a cache hit.
  5431. std::string url_with_hash = std::string(trans.url) + "#multiple#hashes";
  5432. trans.url = url_with_hash.c_str();
  5433. trans.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
  5434. RunTransactionTest(cache.http_cache(), trans);
  5435. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  5436. EXPECT_EQ(1, cache.disk_cache()->open_count());
  5437. EXPECT_EQ(1, cache.disk_cache()->create_count());
  5438. }
  5439. // Tests that we skip the cache for POST requests that do not have an upload
  5440. // identifier.
  5441. TEST_F(HttpCacheTest, SimplePOST_SkipsCache) {
  5442. MockHttpCache cache;
  5443. RunTransactionTest(cache.http_cache(), kSimplePOST_Transaction);
  5444. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  5445. EXPECT_EQ(0, cache.disk_cache()->open_count());
  5446. EXPECT_EQ(0, cache.disk_cache()->create_count());
  5447. }
  5448. // Tests POST handling with a disabled cache (no DCHECK).
  5449. TEST_F(HttpCacheTest, SimplePOST_DisabledCache) {
  5450. MockHttpCache cache;
  5451. cache.http_cache()->set_mode(HttpCache::Mode::DISABLE);
  5452. RunTransactionTest(cache.http_cache(), kSimplePOST_Transaction);
  5453. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  5454. EXPECT_EQ(0, cache.disk_cache()->open_count());
  5455. EXPECT_EQ(0, cache.disk_cache()->create_count());
  5456. }
  5457. TEST_F(HttpCacheTest, SimplePOST_LoadOnlyFromCache_Miss) {
  5458. MockHttpCache cache;
  5459. MockTransaction transaction(kSimplePOST_Transaction);
  5460. transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
  5461. MockHttpRequest request(transaction);
  5462. TestCompletionCallback callback;
  5463. std::unique_ptr<HttpTransaction> trans;
  5464. ASSERT_THAT(cache.CreateTransaction(&trans), IsOk());
  5465. ASSERT_TRUE(trans.get());
  5466. int rv = trans->Start(&request, callback.callback(), NetLogWithSource());
  5467. ASSERT_THAT(callback.GetResult(rv), IsError(ERR_CACHE_MISS));
  5468. trans.reset();
  5469. EXPECT_EQ(0, cache.network_layer()->transaction_count());
  5470. EXPECT_EQ(0, cache.disk_cache()->open_count());
  5471. EXPECT_EQ(0, cache.disk_cache()->create_count());
  5472. }
  5473. TEST_F(HttpCacheTest, SimplePOST_LoadOnlyFromCache_Hit) {
  5474. MockHttpCache cache;
  5475. // Test that we hit the cache for POST requests.
  5476. MockTransaction transaction(kSimplePOST_Transaction);
  5477. const int64_t kUploadId = 1; // Just a dummy value.
  5478. std::vector<std::unique_ptr<UploadElementReader>> element_readers;
  5479. element_readers.push_back(
  5480. std::make_unique<UploadBytesElementReader>("hello", 5));
  5481. ElementsUploadDataStream upload_data_stream(std::move(element_readers),
  5482. kUploadId);
  5483. MockHttpRequest request(transaction);
  5484. request.upload_data_stream = &upload_data_stream;
  5485. // Populate the cache.
  5486. RunTransactionTestWithRequest(cache.http_cache(), transaction, request,
  5487. nullptr);
  5488. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  5489. EXPECT_EQ(0, cache.disk_cache()->open_count());
  5490. EXPECT_EQ(1, cache.disk_cache()->create_count());
  5491. // Load from cache.
  5492. request.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
  5493. RunTransactionTestWithRequest(cache.http_cache(), transaction, request,
  5494. nullptr);
  5495. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  5496. EXPECT_EQ(1, cache.disk_cache()->open_count());
  5497. EXPECT_EQ(1, cache.disk_cache()->create_count());
  5498. }
  5499. // Test that we don't hit the cache for POST requests if there is a byte range.
  5500. TEST_F(HttpCacheTest, SimplePOST_WithRanges) {
  5501. MockHttpCache cache;
  5502. MockTransaction transaction(kSimplePOST_Transaction);
  5503. transaction.request_headers = "Range: bytes = 0-4\r\n";
  5504. const int64_t kUploadId = 1; // Just a dummy value.
  5505. std::vector<std::unique_ptr<UploadElementReader>> element_readers;
  5506. element_readers.push_back(
  5507. std::make_unique<UploadBytesElementReader>("hello", 5));
  5508. ElementsUploadDataStream upload_data_stream(std::move(element_readers),
  5509. kUploadId);
  5510. MockHttpRequest request(transaction);
  5511. request.upload_data_stream = &upload_data_stream;
  5512. // Attempt to populate the cache.
  5513. RunTransactionTestWithRequest(cache.http_cache(), transaction, request,
  5514. nullptr);
  5515. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  5516. EXPECT_EQ(0, cache.disk_cache()->open_count());
  5517. EXPECT_EQ(0, cache.disk_cache()->create_count());
  5518. }
  5519. // Tests that a POST is cached separately from a GET.
  5520. TEST_F(HttpCacheTest, SimplePOST_SeparateCache) {
  5521. MockHttpCache cache;
  5522. std::vector<std::unique_ptr<UploadElementReader>> element_readers;
  5523. element_readers.push_back(
  5524. std::make_unique<UploadBytesElementReader>("hello", 5));
  5525. ElementsUploadDataStream upload_data_stream(std::move(element_readers), 1);
  5526. MockTransaction transaction(kSimplePOST_Transaction);
  5527. MockHttpRequest req1(transaction);
  5528. req1.upload_data_stream = &upload_data_stream;
  5529. RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, nullptr);
  5530. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  5531. EXPECT_EQ(0, cache.disk_cache()->open_count());
  5532. EXPECT_EQ(1, cache.disk_cache()->create_count());
  5533. transaction.method = "GET";
  5534. MockHttpRequest req2(transaction);
  5535. RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, nullptr);
  5536. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  5537. EXPECT_EQ(0, cache.disk_cache()->open_count());
  5538. EXPECT_EQ(2, cache.disk_cache()->create_count());
  5539. }
  5540. // Tests that a successful POST invalidates a previously cached GET.
  5541. TEST_F(HttpCacheTest, SimplePOST_Invalidate_205) {
  5542. MockHttpCache cache;
  5543. MockTransaction transaction(kSimpleGET_Transaction);
  5544. AddMockTransaction(&transaction);
  5545. MockHttpRequest req1(transaction);
  5546. // Attempt to populate the cache.
  5547. RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, nullptr);
  5548. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  5549. EXPECT_EQ(0, cache.disk_cache()->open_count());
  5550. EXPECT_EQ(1, cache.disk_cache()->create_count());
  5551. std::vector<std::unique_ptr<UploadElementReader>> element_readers;
  5552. element_readers.push_back(
  5553. std::make_unique<UploadBytesElementReader>("hello", 5));
  5554. ElementsUploadDataStream upload_data_stream(std::move(element_readers), 1);
  5555. transaction.method = "POST";
  5556. transaction.status = "HTTP/1.1 205 No Content";
  5557. MockHttpRequest req2(transaction);
  5558. req2.upload_data_stream = &upload_data_stream;
  5559. RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, nullptr);
  5560. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  5561. EXPECT_EQ(0, cache.disk_cache()->open_count());
  5562. EXPECT_EQ(2, cache.disk_cache()->create_count());
  5563. RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, nullptr);
  5564. EXPECT_EQ(3, cache.network_layer()->transaction_count());
  5565. EXPECT_EQ(0, cache.disk_cache()->open_count());
  5566. EXPECT_EQ(3, cache.disk_cache()->create_count());
  5567. RemoveMockTransaction(&transaction);
  5568. }
  5569. // Tests that a successful POST invalidates a previously cached GET,
  5570. // with cache split by top-frame origin.
  5571. TEST_F(HttpCacheTest, SimplePOST_Invalidate_205_SplitCache) {
  5572. base::test::ScopedFeatureList feature_list;
  5573. feature_list.InitAndEnableFeature(
  5574. net::features::kSplitCacheByNetworkIsolationKey);
  5575. SchemefulSite site_a(GURL("http://a.com"));
  5576. SchemefulSite site_b(GURL("http://b.com"));
  5577. MockHttpCache cache;
  5578. MockTransaction transaction(kSimpleGET_Transaction);
  5579. AddMockTransaction(&transaction);
  5580. MockHttpRequest req1(transaction);
  5581. req1.network_isolation_key = NetworkIsolationKey(site_a, site_a);
  5582. // Attempt to populate the cache.
  5583. RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, nullptr);
  5584. // Same for a different origin.
  5585. MockHttpRequest req1b(transaction);
  5586. req1b.network_isolation_key = NetworkIsolationKey(site_b, site_b);
  5587. RunTransactionTestWithRequest(cache.http_cache(), transaction, req1b,
  5588. nullptr);
  5589. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  5590. EXPECT_EQ(0, cache.disk_cache()->open_count());
  5591. EXPECT_EQ(2, cache.disk_cache()->create_count());
  5592. std::vector<std::unique_ptr<UploadElementReader>> element_readers;
  5593. element_readers.push_back(
  5594. std::make_unique<UploadBytesElementReader>("hello", 5));
  5595. ElementsUploadDataStream upload_data_stream(std::move(element_readers), 1);
  5596. transaction.method = "POST";
  5597. transaction.status = "HTTP/1.1 205 No Content";
  5598. MockHttpRequest req2(transaction);
  5599. req2.upload_data_stream = &upload_data_stream;
  5600. req2.network_isolation_key = NetworkIsolationKey(site_a, site_a);
  5601. RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, nullptr);
  5602. EXPECT_EQ(3, cache.network_layer()->transaction_count());
  5603. EXPECT_EQ(0, cache.disk_cache()->open_count());
  5604. EXPECT_EQ(3, cache.disk_cache()->create_count());
  5605. // req1b should still be cached, since it has a different top-level frame
  5606. // origin.
  5607. RunTransactionTestWithRequest(cache.http_cache(), transaction, req1b,
  5608. nullptr);
  5609. EXPECT_EQ(3, cache.network_layer()->transaction_count());
  5610. EXPECT_EQ(1, cache.disk_cache()->open_count());
  5611. EXPECT_EQ(3, cache.disk_cache()->create_count());
  5612. // req1 should not be cached after the POST.
  5613. RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, nullptr);
  5614. EXPECT_EQ(4, cache.network_layer()->transaction_count());
  5615. EXPECT_EQ(1, cache.disk_cache()->open_count());
  5616. EXPECT_EQ(4, cache.disk_cache()->create_count());
  5617. RemoveMockTransaction(&transaction);
  5618. }
  5619. // Tests that a successful POST invalidates a previously cached GET, even when
  5620. // there is no upload identifier.
  5621. TEST_F(HttpCacheTest, SimplePOST_NoUploadId_Invalidate_205) {
  5622. MockHttpCache cache;
  5623. MockTransaction transaction(kSimpleGET_Transaction);
  5624. AddMockTransaction(&transaction);
  5625. MockHttpRequest req1(transaction);
  5626. // Attempt to populate the cache.
  5627. RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, nullptr);
  5628. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  5629. EXPECT_EQ(0, cache.disk_cache()->open_count());
  5630. EXPECT_EQ(1, cache.disk_cache()->create_count());
  5631. std::vector<std::unique_ptr<UploadElementReader>> element_readers;
  5632. element_readers.push_back(
  5633. std::make_unique<UploadBytesElementReader>("hello", 5));
  5634. ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0);
  5635. transaction.method = "POST";
  5636. transaction.status = "HTTP/1.1 205 No Content";
  5637. MockHttpRequest req2(transaction);
  5638. req2.upload_data_stream = &upload_data_stream;
  5639. RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, nullptr);
  5640. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  5641. EXPECT_EQ(0, cache.disk_cache()->open_count());
  5642. EXPECT_EQ(1, cache.disk_cache()->create_count());
  5643. RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, nullptr);
  5644. EXPECT_EQ(3, cache.network_layer()->transaction_count());
  5645. EXPECT_EQ(0, cache.disk_cache()->open_count());
  5646. EXPECT_EQ(2, cache.disk_cache()->create_count());
  5647. RemoveMockTransaction(&transaction);
  5648. }
  5649. // Tests that processing a POST before creating the backend doesn't crash.
  5650. TEST_F(HttpCacheTest, SimplePOST_NoUploadId_NoBackend) {
  5651. // This will initialize a cache object with NULL backend.
  5652. auto factory = std::make_unique<MockBlockingBackendFactory>();
  5653. factory->set_fail(true);
  5654. factory->FinishCreation();
  5655. MockHttpCache cache(std::move(factory));
  5656. std::vector<std::unique_ptr<UploadElementReader>> element_readers;
  5657. element_readers.push_back(
  5658. std::make_unique<UploadBytesElementReader>("hello", 5));
  5659. ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0);
  5660. MockTransaction transaction(kSimplePOST_Transaction);
  5661. AddMockTransaction(&transaction);
  5662. MockHttpRequest req(transaction);
  5663. req.upload_data_stream = &upload_data_stream;
  5664. RunTransactionTestWithRequest(cache.http_cache(), transaction, req, nullptr);
  5665. RemoveMockTransaction(&transaction);
  5666. }
  5667. // Tests that we don't invalidate entries as a result of a failed POST.
  5668. TEST_F(HttpCacheTest, SimplePOST_DontInvalidate_100) {
  5669. MockHttpCache cache;
  5670. MockTransaction transaction(kSimpleGET_Transaction);
  5671. AddMockTransaction(&transaction);
  5672. MockHttpRequest req1(transaction);
  5673. // Attempt to populate the cache.
  5674. RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, nullptr);
  5675. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  5676. EXPECT_EQ(0, cache.disk_cache()->open_count());
  5677. EXPECT_EQ(1, cache.disk_cache()->create_count());
  5678. std::vector<std::unique_ptr<UploadElementReader>> element_readers;
  5679. element_readers.push_back(
  5680. std::make_unique<UploadBytesElementReader>("hello", 5));
  5681. ElementsUploadDataStream upload_data_stream(std::move(element_readers), 1);
  5682. transaction.method = "POST";
  5683. transaction.status = "HTTP/1.1 100 Continue";
  5684. MockHttpRequest req2(transaction);
  5685. req2.upload_data_stream = &upload_data_stream;
  5686. RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, nullptr);
  5687. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  5688. EXPECT_EQ(0, cache.disk_cache()->open_count());
  5689. EXPECT_EQ(2, cache.disk_cache()->create_count());
  5690. RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, nullptr);
  5691. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  5692. EXPECT_EQ(1, cache.disk_cache()->open_count());
  5693. EXPECT_EQ(2, cache.disk_cache()->create_count());
  5694. RemoveMockTransaction(&transaction);
  5695. }
  5696. // Tests that a HEAD request is not cached by itself.
  5697. TEST_F(HttpCacheTest, SimpleHEAD_LoadOnlyFromCache_Miss) {
  5698. MockHttpCache cache;
  5699. MockTransaction transaction(kSimplePOST_Transaction);
  5700. AddMockTransaction(&transaction);
  5701. transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
  5702. transaction.method = "HEAD";
  5703. MockHttpRequest request(transaction);
  5704. TestCompletionCallback callback;
  5705. std::unique_ptr<HttpTransaction> trans;
  5706. ASSERT_THAT(cache.CreateTransaction(&trans), IsOk());
  5707. ASSERT_TRUE(trans.get());
  5708. int rv = trans->Start(&request, callback.callback(), NetLogWithSource());
  5709. ASSERT_THAT(callback.GetResult(rv), IsError(ERR_CACHE_MISS));
  5710. trans.reset();
  5711. EXPECT_EQ(0, cache.network_layer()->transaction_count());
  5712. EXPECT_EQ(0, cache.disk_cache()->open_count());
  5713. EXPECT_EQ(0, cache.disk_cache()->create_count());
  5714. RemoveMockTransaction(&transaction);
  5715. }
  5716. // Tests that a HEAD request is served from a cached GET.
  5717. TEST_F(HttpCacheTest, SimpleHEAD_LoadOnlyFromCache_Hit) {
  5718. MockHttpCache cache;
  5719. MockTransaction transaction(kSimpleGET_Transaction);
  5720. AddMockTransaction(&transaction);
  5721. // Populate the cache.
  5722. RunTransactionTest(cache.http_cache(), transaction);
  5723. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  5724. EXPECT_EQ(0, cache.disk_cache()->open_count());
  5725. EXPECT_EQ(1, cache.disk_cache()->create_count());
  5726. // Load from cache.
  5727. transaction.method = "HEAD";
  5728. transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
  5729. transaction.data = "";
  5730. RunTransactionTest(cache.http_cache(), transaction);
  5731. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  5732. EXPECT_EQ(1, cache.disk_cache()->open_count());
  5733. EXPECT_EQ(1, cache.disk_cache()->create_count());
  5734. RemoveMockTransaction(&transaction);
  5735. }
  5736. // Tests that a read-only request served from the cache preserves CL.
  5737. TEST_F(HttpCacheTest, SimpleHEAD_ContentLengthOnHit_Read) {
  5738. MockHttpCache cache;
  5739. MockTransaction transaction(kSimpleGET_Transaction);
  5740. AddMockTransaction(&transaction);
  5741. transaction.response_headers = "Content-Length: 42\n";
  5742. // Populate the cache.
  5743. RunTransactionTest(cache.http_cache(), transaction);
  5744. // Load from cache.
  5745. transaction.method = "HEAD";
  5746. transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
  5747. transaction.data = "";
  5748. std::string headers;
  5749. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  5750. EXPECT_EQ("HTTP/1.1 200 OK\nContent-Length: 42\n", headers);
  5751. RemoveMockTransaction(&transaction);
  5752. }
  5753. // Tests that a read-write request served from the cache preserves CL.
  5754. TEST_F(HttpCacheTest, ETagHEAD_ContentLengthOnHit_ReadWrite) {
  5755. MockHttpCache cache;
  5756. MockTransaction transaction(kETagGET_Transaction);
  5757. AddMockTransaction(&transaction);
  5758. std::string server_headers(kETagGET_Transaction.response_headers);
  5759. server_headers.append("Content-Length: 42\n");
  5760. transaction.response_headers = server_headers.data();
  5761. // Populate the cache.
  5762. RunTransactionTest(cache.http_cache(), transaction);
  5763. // Load from cache.
  5764. transaction.method = "HEAD";
  5765. transaction.data = "";
  5766. std::string headers;
  5767. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  5768. EXPECT_NE(std::string::npos, headers.find("Content-Length: 42\n"));
  5769. RemoveMockTransaction(&transaction);
  5770. }
  5771. // Tests that a HEAD request that includes byte ranges bypasses the cache.
  5772. TEST_F(HttpCacheTest, SimpleHEAD_WithRanges) {
  5773. MockHttpCache cache;
  5774. MockTransaction transaction(kSimpleGET_Transaction);
  5775. AddMockTransaction(&transaction);
  5776. // Populate the cache.
  5777. RunTransactionTest(cache.http_cache(), transaction);
  5778. // Load from cache.
  5779. transaction.method = "HEAD";
  5780. transaction.request_headers = "Range: bytes = 0-4\r\n";
  5781. transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
  5782. transaction.start_return_code = ERR_CACHE_MISS;
  5783. RunTransactionTest(cache.http_cache(), transaction);
  5784. EXPECT_EQ(0, cache.disk_cache()->open_count());
  5785. EXPECT_EQ(1, cache.disk_cache()->create_count());
  5786. RemoveMockTransaction(&transaction);
  5787. }
  5788. // Tests that a HEAD request can be served from a partialy cached resource.
  5789. TEST_F(HttpCacheTest, SimpleHEAD_WithCachedRanges) {
  5790. MockHttpCache cache;
  5791. AddMockTransaction(&kRangeGET_TransactionOK);
  5792. // Write to the cache (40-49).
  5793. RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK);
  5794. RemoveMockTransaction(&kRangeGET_TransactionOK);
  5795. MockTransaction transaction(kSimpleGET_Transaction);
  5796. transaction.url = kRangeGET_TransactionOK.url;
  5797. transaction.method = "HEAD";
  5798. transaction.data = "";
  5799. AddMockTransaction(&transaction);
  5800. std::string headers;
  5801. // Load from cache.
  5802. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  5803. EXPECT_NE(std::string::npos, headers.find("HTTP/1.1 200 OK\n"));
  5804. EXPECT_NE(std::string::npos, headers.find("Content-Length: 80\n"));
  5805. EXPECT_EQ(std::string::npos, headers.find("Content-Range"));
  5806. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  5807. EXPECT_EQ(1, cache.disk_cache()->open_count());
  5808. EXPECT_EQ(1, cache.disk_cache()->create_count());
  5809. RemoveMockTransaction(&transaction);
  5810. }
  5811. // Tests that a HEAD request can be served from a truncated resource.
  5812. TEST_F(HttpCacheTest, SimpleHEAD_WithTruncatedEntry) {
  5813. MockHttpCache cache;
  5814. AddMockTransaction(&kRangeGET_TransactionOK);
  5815. std::string raw_headers("HTTP/1.1 200 OK\n"
  5816. "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
  5817. "ETag: \"foo\"\n"
  5818. "Accept-Ranges: bytes\n"
  5819. "Content-Length: 80\n");
  5820. CreateTruncatedEntry(raw_headers, &cache);
  5821. RemoveMockTransaction(&kRangeGET_TransactionOK);
  5822. MockTransaction transaction(kSimpleGET_Transaction);
  5823. transaction.url = kRangeGET_TransactionOK.url;
  5824. transaction.method = "HEAD";
  5825. transaction.data = "";
  5826. AddMockTransaction(&transaction);
  5827. std::string headers;
  5828. // Load from cache.
  5829. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  5830. EXPECT_NE(std::string::npos, headers.find("HTTP/1.1 200 OK\n"));
  5831. EXPECT_NE(std::string::npos, headers.find("Content-Length: 80\n"));
  5832. EXPECT_EQ(std::string::npos, headers.find("Content-Range"));
  5833. EXPECT_EQ(0, cache.network_layer()->transaction_count());
  5834. EXPECT_EQ(1, cache.disk_cache()->open_count());
  5835. EXPECT_EQ(1, cache.disk_cache()->create_count());
  5836. RemoveMockTransaction(&transaction);
  5837. }
  5838. // Tests that a HEAD request updates the cached response.
  5839. TEST_F(HttpCacheTest, TypicalHEAD_UpdatesResponse) {
  5840. MockHttpCache cache;
  5841. MockTransaction transaction(kTypicalGET_Transaction);
  5842. AddMockTransaction(&transaction);
  5843. // Populate the cache.
  5844. RunTransactionTest(cache.http_cache(), transaction);
  5845. // Update the cache.
  5846. transaction.method = "HEAD";
  5847. transaction.response_headers = "Foo: bar\n";
  5848. transaction.data = "";
  5849. transaction.status = "HTTP/1.1 304 Not Modified\n";
  5850. std::string headers;
  5851. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  5852. RemoveMockTransaction(&transaction);
  5853. EXPECT_NE(std::string::npos, headers.find("HTTP/1.1 200 OK\n"));
  5854. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  5855. MockTransaction transaction2(kTypicalGET_Transaction);
  5856. AddMockTransaction(&transaction2);
  5857. // Make sure we are done with the previous transaction.
  5858. base::RunLoop().RunUntilIdle();
  5859. // Load from the cache.
  5860. transaction2.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
  5861. RunTransactionTestWithResponse(cache.http_cache(), transaction2, &headers);
  5862. EXPECT_NE(std::string::npos, headers.find("Foo: bar\n"));
  5863. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  5864. EXPECT_EQ(2, cache.disk_cache()->open_count());
  5865. EXPECT_EQ(1, cache.disk_cache()->create_count());
  5866. RemoveMockTransaction(&transaction2);
  5867. }
  5868. // Tests that an externally conditionalized HEAD request updates the cache.
  5869. TEST_F(HttpCacheTest, TypicalHEAD_ConditionalizedRequestUpdatesResponse) {
  5870. MockHttpCache cache;
  5871. MockTransaction transaction(kTypicalGET_Transaction);
  5872. AddMockTransaction(&transaction);
  5873. // Populate the cache.
  5874. RunTransactionTest(cache.http_cache(), transaction);
  5875. // Update the cache.
  5876. transaction.method = "HEAD";
  5877. transaction.request_headers =
  5878. "If-Modified-Since: Wed, 28 Nov 2007 00:40:09 GMT\r\n";
  5879. transaction.response_headers = "Foo: bar\n";
  5880. transaction.data = "";
  5881. transaction.status = "HTTP/1.1 304 Not Modified\n";
  5882. std::string headers;
  5883. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  5884. RemoveMockTransaction(&transaction);
  5885. EXPECT_NE(std::string::npos, headers.find("HTTP/1.1 304 Not Modified\n"));
  5886. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  5887. MockTransaction transaction2(kTypicalGET_Transaction);
  5888. AddMockTransaction(&transaction2);
  5889. // Make sure we are done with the previous transaction.
  5890. base::RunLoop().RunUntilIdle();
  5891. // Load from the cache.
  5892. transaction2.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
  5893. RunTransactionTestWithResponse(cache.http_cache(), transaction2, &headers);
  5894. EXPECT_NE(std::string::npos, headers.find("Foo: bar\n"));
  5895. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  5896. EXPECT_EQ(2, cache.disk_cache()->open_count());
  5897. EXPECT_EQ(1, cache.disk_cache()->create_count());
  5898. RemoveMockTransaction(&transaction2);
  5899. }
  5900. // Tests that a HEAD request invalidates an old cached entry.
  5901. TEST_F(HttpCacheTest, SimpleHEAD_InvalidatesEntry) {
  5902. MockHttpCache cache;
  5903. MockTransaction transaction(kTypicalGET_Transaction);
  5904. AddMockTransaction(&transaction);
  5905. // Populate the cache.
  5906. RunTransactionTest(cache.http_cache(), transaction);
  5907. // Update the cache.
  5908. transaction.method = "HEAD";
  5909. transaction.data = "";
  5910. RunTransactionTest(cache.http_cache(), transaction);
  5911. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  5912. // Load from the cache.
  5913. transaction.method = "GET";
  5914. transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
  5915. transaction.start_return_code = ERR_CACHE_MISS;
  5916. RunTransactionTest(cache.http_cache(), transaction);
  5917. RemoveMockTransaction(&transaction);
  5918. }
  5919. // Tests that we do not cache the response of a PUT.
  5920. TEST_F(HttpCacheTest, SimplePUT_Miss) {
  5921. MockHttpCache cache;
  5922. MockTransaction transaction(kSimplePOST_Transaction);
  5923. transaction.method = "PUT";
  5924. std::vector<std::unique_ptr<UploadElementReader>> element_readers;
  5925. element_readers.push_back(
  5926. std::make_unique<UploadBytesElementReader>("hello", 5));
  5927. ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0);
  5928. MockHttpRequest request(transaction);
  5929. request.upload_data_stream = &upload_data_stream;
  5930. // Attempt to populate the cache.
  5931. RunTransactionTestWithRequest(cache.http_cache(), transaction, request,
  5932. nullptr);
  5933. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  5934. EXPECT_EQ(0, cache.disk_cache()->open_count());
  5935. EXPECT_EQ(0, cache.disk_cache()->create_count());
  5936. }
  5937. // Tests that we invalidate entries as a result of a PUT.
  5938. TEST_F(HttpCacheTest, SimplePUT_Invalidate) {
  5939. MockHttpCache cache;
  5940. MockTransaction transaction(kSimpleGET_Transaction);
  5941. MockHttpRequest req1(transaction);
  5942. // Attempt to populate the cache.
  5943. RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, nullptr);
  5944. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  5945. EXPECT_EQ(0, cache.disk_cache()->open_count());
  5946. EXPECT_EQ(1, cache.disk_cache()->create_count());
  5947. std::vector<std::unique_ptr<UploadElementReader>> element_readers;
  5948. element_readers.push_back(
  5949. std::make_unique<UploadBytesElementReader>("hello", 5));
  5950. ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0);
  5951. transaction.method = "PUT";
  5952. MockHttpRequest req2(transaction);
  5953. req2.upload_data_stream = &upload_data_stream;
  5954. RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, nullptr);
  5955. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  5956. EXPECT_EQ(1, cache.disk_cache()->open_count());
  5957. EXPECT_EQ(1, cache.disk_cache()->create_count());
  5958. RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, nullptr);
  5959. EXPECT_EQ(3, cache.network_layer()->transaction_count());
  5960. EXPECT_EQ(1, cache.disk_cache()->open_count());
  5961. EXPECT_EQ(2, cache.disk_cache()->create_count());
  5962. }
  5963. // Tests that we invalidate entries as a result of a PUT.
  5964. TEST_F(HttpCacheTest, SimplePUT_Invalidate_305) {
  5965. MockHttpCache cache;
  5966. MockTransaction transaction(kSimpleGET_Transaction);
  5967. AddMockTransaction(&transaction);
  5968. MockHttpRequest req1(transaction);
  5969. // Attempt to populate the cache.
  5970. RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, nullptr);
  5971. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  5972. EXPECT_EQ(0, cache.disk_cache()->open_count());
  5973. EXPECT_EQ(1, cache.disk_cache()->create_count());
  5974. std::vector<std::unique_ptr<UploadElementReader>> element_readers;
  5975. element_readers.push_back(
  5976. std::make_unique<UploadBytesElementReader>("hello", 5));
  5977. ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0);
  5978. transaction.method = "PUT";
  5979. transaction.status = "HTTP/1.1 305 Use Proxy";
  5980. MockHttpRequest req2(transaction);
  5981. req2.upload_data_stream = &upload_data_stream;
  5982. RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, nullptr);
  5983. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  5984. EXPECT_EQ(1, cache.disk_cache()->open_count());
  5985. EXPECT_EQ(1, cache.disk_cache()->create_count());
  5986. RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, nullptr);
  5987. EXPECT_EQ(3, cache.network_layer()->transaction_count());
  5988. EXPECT_EQ(1, cache.disk_cache()->open_count());
  5989. EXPECT_EQ(2, cache.disk_cache()->create_count());
  5990. RemoveMockTransaction(&transaction);
  5991. }
  5992. // Tests that we don't invalidate entries as a result of a failed PUT.
  5993. TEST_F(HttpCacheTest, SimplePUT_DontInvalidate_404) {
  5994. MockHttpCache cache;
  5995. MockTransaction transaction(kSimpleGET_Transaction);
  5996. AddMockTransaction(&transaction);
  5997. MockHttpRequest req1(transaction);
  5998. // Attempt to populate the cache.
  5999. RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, nullptr);
  6000. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  6001. EXPECT_EQ(0, cache.disk_cache()->open_count());
  6002. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6003. std::vector<std::unique_ptr<UploadElementReader>> element_readers;
  6004. element_readers.push_back(
  6005. std::make_unique<UploadBytesElementReader>("hello", 5));
  6006. ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0);
  6007. transaction.method = "PUT";
  6008. transaction.status = "HTTP/1.1 404 Not Found";
  6009. MockHttpRequest req2(transaction);
  6010. req2.upload_data_stream = &upload_data_stream;
  6011. RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, nullptr);
  6012. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  6013. EXPECT_EQ(1, cache.disk_cache()->open_count());
  6014. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6015. RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, nullptr);
  6016. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  6017. EXPECT_EQ(2, cache.disk_cache()->open_count());
  6018. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6019. RemoveMockTransaction(&transaction);
  6020. }
  6021. // Tests that we do not cache the response of a DELETE.
  6022. TEST_F(HttpCacheTest, SimpleDELETE_Miss) {
  6023. MockHttpCache cache;
  6024. MockTransaction transaction(kSimplePOST_Transaction);
  6025. transaction.method = "DELETE";
  6026. std::vector<std::unique_ptr<UploadElementReader>> element_readers;
  6027. element_readers.push_back(
  6028. std::make_unique<UploadBytesElementReader>("hello", 5));
  6029. ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0);
  6030. MockHttpRequest request(transaction);
  6031. request.upload_data_stream = &upload_data_stream;
  6032. // Attempt to populate the cache.
  6033. RunTransactionTestWithRequest(cache.http_cache(), transaction, request,
  6034. nullptr);
  6035. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  6036. EXPECT_EQ(0, cache.disk_cache()->open_count());
  6037. EXPECT_EQ(0, cache.disk_cache()->create_count());
  6038. }
  6039. // Tests that we invalidate entries as a result of a DELETE.
  6040. TEST_F(HttpCacheTest, SimpleDELETE_Invalidate) {
  6041. MockHttpCache cache;
  6042. MockTransaction transaction(kSimpleGET_Transaction);
  6043. MockHttpRequest req1(transaction);
  6044. // Attempt to populate the cache.
  6045. RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, nullptr);
  6046. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  6047. EXPECT_EQ(0, cache.disk_cache()->open_count());
  6048. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6049. std::vector<std::unique_ptr<UploadElementReader>> element_readers;
  6050. element_readers.push_back(
  6051. std::make_unique<UploadBytesElementReader>("hello", 5));
  6052. ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0);
  6053. transaction.method = "DELETE";
  6054. MockHttpRequest req2(transaction);
  6055. req2.upload_data_stream = &upload_data_stream;
  6056. RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, nullptr);
  6057. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  6058. EXPECT_EQ(1, cache.disk_cache()->open_count());
  6059. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6060. RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, nullptr);
  6061. EXPECT_EQ(3, cache.network_layer()->transaction_count());
  6062. EXPECT_EQ(1, cache.disk_cache()->open_count());
  6063. EXPECT_EQ(2, cache.disk_cache()->create_count());
  6064. }
  6065. // Tests that we invalidate entries as a result of a DELETE.
  6066. TEST_F(HttpCacheTest, SimpleDELETE_Invalidate_301) {
  6067. MockHttpCache cache;
  6068. MockTransaction transaction(kSimpleGET_Transaction);
  6069. AddMockTransaction(&transaction);
  6070. // Attempt to populate the cache.
  6071. RunTransactionTest(cache.http_cache(), transaction);
  6072. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  6073. EXPECT_EQ(0, cache.disk_cache()->open_count());
  6074. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6075. transaction.method = "DELETE";
  6076. transaction.status = "HTTP/1.1 301 Moved Permanently ";
  6077. RunTransactionTest(cache.http_cache(), transaction);
  6078. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  6079. EXPECT_EQ(1, cache.disk_cache()->open_count());
  6080. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6081. transaction.method = "GET";
  6082. RunTransactionTest(cache.http_cache(), transaction);
  6083. EXPECT_EQ(3, cache.network_layer()->transaction_count());
  6084. EXPECT_EQ(1, cache.disk_cache()->open_count());
  6085. EXPECT_EQ(2, cache.disk_cache()->create_count());
  6086. RemoveMockTransaction(&transaction);
  6087. }
  6088. // Tests that we don't invalidate entries as a result of a failed DELETE.
  6089. TEST_F(HttpCacheTest, SimpleDELETE_DontInvalidate_416) {
  6090. MockHttpCache cache;
  6091. MockTransaction transaction(kSimpleGET_Transaction);
  6092. AddMockTransaction(&transaction);
  6093. // Attempt to populate the cache.
  6094. RunTransactionTest(cache.http_cache(), transaction);
  6095. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  6096. EXPECT_EQ(0, cache.disk_cache()->open_count());
  6097. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6098. transaction.method = "DELETE";
  6099. transaction.status = "HTTP/1.1 416 Requested Range Not Satisfiable";
  6100. RunTransactionTest(cache.http_cache(), transaction);
  6101. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  6102. EXPECT_EQ(1, cache.disk_cache()->open_count());
  6103. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6104. transaction.method = "GET";
  6105. transaction.status = "HTTP/1.1 200 OK";
  6106. RunTransactionTest(cache.http_cache(), transaction);
  6107. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  6108. EXPECT_EQ(2, cache.disk_cache()->open_count());
  6109. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6110. RemoveMockTransaction(&transaction);
  6111. }
  6112. // Tests that we invalidate entries as a result of a PATCH.
  6113. TEST_F(HttpCacheTest, SimplePATCH_Invalidate) {
  6114. MockHttpCache cache;
  6115. MockTransaction transaction(kSimpleGET_Transaction);
  6116. MockHttpRequest req1(transaction);
  6117. // Attempt to populate the cache.
  6118. RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, nullptr);
  6119. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  6120. EXPECT_EQ(0, cache.disk_cache()->open_count());
  6121. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6122. std::vector<std::unique_ptr<UploadElementReader>> element_readers;
  6123. element_readers.push_back(
  6124. std::make_unique<UploadBytesElementReader>("hello", 5));
  6125. ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0);
  6126. transaction.method = "PATCH";
  6127. MockHttpRequest req2(transaction);
  6128. req2.upload_data_stream = &upload_data_stream;
  6129. RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, nullptr);
  6130. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  6131. EXPECT_EQ(1, cache.disk_cache()->open_count());
  6132. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6133. RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, nullptr);
  6134. EXPECT_EQ(3, cache.network_layer()->transaction_count());
  6135. EXPECT_EQ(1, cache.disk_cache()->open_count());
  6136. EXPECT_EQ(2, cache.disk_cache()->create_count());
  6137. }
  6138. // Tests that we invalidate entries as a result of a PATCH.
  6139. TEST_F(HttpCacheTest, SimplePATCH_Invalidate_301) {
  6140. MockHttpCache cache;
  6141. MockTransaction transaction(kSimpleGET_Transaction);
  6142. AddMockTransaction(&transaction);
  6143. // Attempt to populate the cache.
  6144. RunTransactionTest(cache.http_cache(), transaction);
  6145. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  6146. EXPECT_EQ(0, cache.disk_cache()->open_count());
  6147. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6148. transaction.method = "PATCH";
  6149. transaction.status = "HTTP/1.1 301 Moved Permanently ";
  6150. RunTransactionTest(cache.http_cache(), transaction);
  6151. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  6152. EXPECT_EQ(1, cache.disk_cache()->open_count());
  6153. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6154. transaction.method = "GET";
  6155. RunTransactionTest(cache.http_cache(), transaction);
  6156. EXPECT_EQ(3, cache.network_layer()->transaction_count());
  6157. EXPECT_EQ(1, cache.disk_cache()->open_count());
  6158. EXPECT_EQ(2, cache.disk_cache()->create_count());
  6159. RemoveMockTransaction(&transaction);
  6160. }
  6161. // Tests that we don't invalidate entries as a result of a failed PATCH.
  6162. TEST_F(HttpCacheTest, SimplePATCH_DontInvalidate_416) {
  6163. MockHttpCache cache;
  6164. MockTransaction transaction(kSimpleGET_Transaction);
  6165. AddMockTransaction(&transaction);
  6166. // Attempt to populate the cache.
  6167. RunTransactionTest(cache.http_cache(), transaction);
  6168. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  6169. EXPECT_EQ(0, cache.disk_cache()->open_count());
  6170. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6171. transaction.method = "PATCH";
  6172. transaction.status = "HTTP/1.1 416 Requested Range Not Satisfiable";
  6173. RunTransactionTest(cache.http_cache(), transaction);
  6174. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  6175. EXPECT_EQ(1, cache.disk_cache()->open_count());
  6176. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6177. transaction.method = "GET";
  6178. transaction.status = "HTTP/1.1 200 OK";
  6179. RunTransactionTest(cache.http_cache(), transaction);
  6180. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  6181. EXPECT_EQ(2, cache.disk_cache()->open_count());
  6182. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6183. RemoveMockTransaction(&transaction);
  6184. }
  6185. // Tests that we don't invalidate entries after a failed network transaction.
  6186. TEST_F(HttpCacheTest, SimpleGET_DontInvalidateOnFailure) {
  6187. MockHttpCache cache;
  6188. // Populate the cache.
  6189. RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
  6190. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  6191. // Fail the network request.
  6192. MockTransaction transaction(kSimpleGET_Transaction);
  6193. transaction.start_return_code = ERR_FAILED;
  6194. transaction.load_flags |= LOAD_VALIDATE_CACHE;
  6195. AddMockTransaction(&transaction);
  6196. RunTransactionTest(cache.http_cache(), transaction);
  6197. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  6198. RemoveMockTransaction(&transaction);
  6199. transaction.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
  6200. transaction.start_return_code = OK;
  6201. AddMockTransaction(&transaction);
  6202. RunTransactionTest(cache.http_cache(), transaction);
  6203. // Make sure the transaction didn't reach the network.
  6204. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  6205. RemoveMockTransaction(&transaction);
  6206. }
  6207. TEST_F(HttpCacheTest, RangeGET_SkipsCache) {
  6208. MockHttpCache cache;
  6209. // Test that we skip the cache for range GET requests. Eventually, we will
  6210. // want to cache these, but we'll still have cases where skipping the cache
  6211. // makes sense, so we want to make sure that it works properly.
  6212. RunTransactionTest(cache.http_cache(), kRangeGET_Transaction);
  6213. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  6214. EXPECT_EQ(0, cache.disk_cache()->open_count());
  6215. EXPECT_EQ(0, cache.disk_cache()->create_count());
  6216. MockTransaction transaction(kSimpleGET_Transaction);
  6217. transaction.request_headers = "If-None-Match: foo\r\n";
  6218. RunTransactionTest(cache.http_cache(), transaction);
  6219. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  6220. EXPECT_EQ(0, cache.disk_cache()->open_count());
  6221. EXPECT_EQ(0, cache.disk_cache()->create_count());
  6222. transaction.request_headers =
  6223. "If-Modified-Since: Wed, 28 Nov 2007 00:45:20 GMT\r\n";
  6224. RunTransactionTest(cache.http_cache(), transaction);
  6225. EXPECT_EQ(3, cache.network_layer()->transaction_count());
  6226. EXPECT_EQ(0, cache.disk_cache()->open_count());
  6227. EXPECT_EQ(0, cache.disk_cache()->create_count());
  6228. }
  6229. // Test that we skip the cache for range requests that include a validation
  6230. // header.
  6231. TEST_F(HttpCacheTest, RangeGET_SkipsCache2) {
  6232. MockHttpCache cache;
  6233. MockTransaction transaction(kRangeGET_Transaction);
  6234. transaction.request_headers = "If-None-Match: foo\r\n"
  6235. EXTRA_HEADER
  6236. "Range: bytes = 40-49\r\n";
  6237. RunTransactionTest(cache.http_cache(), transaction);
  6238. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  6239. EXPECT_EQ(0, cache.disk_cache()->open_count());
  6240. EXPECT_EQ(0, cache.disk_cache()->create_count());
  6241. transaction.request_headers =
  6242. "If-Modified-Since: Wed, 28 Nov 2007 00:45:20 GMT\r\n"
  6243. EXTRA_HEADER
  6244. "Range: bytes = 40-49\r\n";
  6245. RunTransactionTest(cache.http_cache(), transaction);
  6246. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  6247. EXPECT_EQ(0, cache.disk_cache()->open_count());
  6248. EXPECT_EQ(0, cache.disk_cache()->create_count());
  6249. transaction.request_headers = "If-Range: bla\r\n"
  6250. EXTRA_HEADER
  6251. "Range: bytes = 40-49\r\n";
  6252. RunTransactionTest(cache.http_cache(), transaction);
  6253. EXPECT_EQ(3, cache.network_layer()->transaction_count());
  6254. EXPECT_EQ(0, cache.disk_cache()->open_count());
  6255. EXPECT_EQ(0, cache.disk_cache()->create_count());
  6256. }
  6257. TEST_F(HttpCacheTest, SimpleGET_DoesntLogHeaders) {
  6258. MockHttpCache cache;
  6259. RecordingNetLogObserver net_log_observer;
  6260. RunTransactionTestWithLog(cache.http_cache(), kSimpleGET_Transaction,
  6261. NetLogWithSource::Make(NetLogSourceType::NONE));
  6262. EXPECT_FALSE(LogContainsEventType(
  6263. net_log_observer, NetLogEventType::HTTP_CACHE_CALLER_REQUEST_HEADERS));
  6264. }
  6265. TEST_F(HttpCacheTest, RangeGET_LogsHeaders) {
  6266. MockHttpCache cache;
  6267. RecordingNetLogObserver net_log_observer;
  6268. RunTransactionTestWithLog(cache.http_cache(), kRangeGET_Transaction,
  6269. NetLogWithSource::Make(NetLogSourceType::NONE));
  6270. EXPECT_TRUE(LogContainsEventType(
  6271. net_log_observer, NetLogEventType::HTTP_CACHE_CALLER_REQUEST_HEADERS));
  6272. }
  6273. TEST_F(HttpCacheTest, ExternalValidation_LogsHeaders) {
  6274. MockHttpCache cache;
  6275. RecordingNetLogObserver net_log_observer;
  6276. MockTransaction transaction(kSimpleGET_Transaction);
  6277. transaction.request_headers = "If-None-Match: foo\r\n" EXTRA_HEADER;
  6278. RunTransactionTestWithLog(cache.http_cache(), transaction,
  6279. NetLogWithSource::Make(NetLogSourceType::NONE));
  6280. EXPECT_TRUE(LogContainsEventType(
  6281. net_log_observer, NetLogEventType::HTTP_CACHE_CALLER_REQUEST_HEADERS));
  6282. }
  6283. TEST_F(HttpCacheTest, SpecialHeaders_LogsHeaders) {
  6284. MockHttpCache cache;
  6285. RecordingNetLogObserver net_log_observer;
  6286. MockTransaction transaction(kSimpleGET_Transaction);
  6287. transaction.request_headers = "cache-control: no-cache\r\n" EXTRA_HEADER;
  6288. RunTransactionTestWithLog(cache.http_cache(), transaction,
  6289. NetLogWithSource::Make(NetLogSourceType::NONE));
  6290. EXPECT_TRUE(LogContainsEventType(
  6291. net_log_observer, NetLogEventType::HTTP_CACHE_CALLER_REQUEST_HEADERS));
  6292. }
  6293. // Tests that receiving 206 for a regular request is handled correctly.
  6294. TEST_F(HttpCacheTest, GET_Crazy206) {
  6295. MockHttpCache cache;
  6296. // Write to the cache.
  6297. MockTransaction transaction(kRangeGET_TransactionOK);
  6298. AddMockTransaction(&transaction);
  6299. transaction.request_headers = EXTRA_HEADER;
  6300. transaction.handler = nullptr;
  6301. RunTransactionTest(cache.http_cache(), transaction);
  6302. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  6303. EXPECT_EQ(0, cache.disk_cache()->open_count());
  6304. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6305. // This should read again from the net.
  6306. RunTransactionTest(cache.http_cache(), transaction);
  6307. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  6308. EXPECT_EQ(0, cache.disk_cache()->open_count());
  6309. EXPECT_EQ(2, cache.disk_cache()->create_count());
  6310. RemoveMockTransaction(&transaction);
  6311. }
  6312. // Tests that receiving 416 for a regular request is handled correctly.
  6313. TEST_F(HttpCacheTest, GET_Crazy416) {
  6314. MockHttpCache cache;
  6315. // Write to the cache.
  6316. MockTransaction transaction(kSimpleGET_Transaction);
  6317. AddMockTransaction(&transaction);
  6318. transaction.status = "HTTP/1.1 416 Requested Range Not Satisfiable";
  6319. RunTransactionTest(cache.http_cache(), transaction);
  6320. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  6321. EXPECT_EQ(0, cache.disk_cache()->open_count());
  6322. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6323. RemoveMockTransaction(&transaction);
  6324. }
  6325. // Tests that we don't store partial responses that can't be validated.
  6326. TEST_F(HttpCacheTest, RangeGET_NoStrongValidators) {
  6327. MockHttpCache cache;
  6328. std::string headers;
  6329. // Attempt to write to the cache (40-49).
  6330. ScopedMockTransaction transaction(kRangeGET_TransactionOK);
  6331. transaction.response_headers = "Content-Length: 10\n"
  6332. "Cache-Control: max-age=3600\n"
  6333. "ETag: w/\"foo\"\n";
  6334. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  6335. Verify206Response(headers, 40, 49);
  6336. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  6337. EXPECT_EQ(0, cache.disk_cache()->open_count());
  6338. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6339. // Now verify that there's no cached data.
  6340. RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
  6341. &headers);
  6342. Verify206Response(headers, 40, 49);
  6343. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  6344. EXPECT_EQ(0, cache.disk_cache()->open_count());
  6345. EXPECT_EQ(2, cache.disk_cache()->create_count());
  6346. }
  6347. // Tests failures to conditionalize byte range requests.
  6348. TEST_F(HttpCacheTest, RangeGET_NoConditionalization) {
  6349. MockHttpCache cache;
  6350. cache.FailConditionalizations();
  6351. std::string headers;
  6352. // Write to the cache (40-49).
  6353. ScopedMockTransaction transaction(kRangeGET_TransactionOK);
  6354. transaction.response_headers = "Content-Length: 10\n"
  6355. "ETag: \"foo\"\n";
  6356. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  6357. Verify206Response(headers, 40, 49);
  6358. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  6359. EXPECT_EQ(0, cache.disk_cache()->open_count());
  6360. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6361. // Now verify that the cached data is not used.
  6362. RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
  6363. &headers);
  6364. Verify206Response(headers, 40, 49);
  6365. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  6366. EXPECT_EQ(1, cache.disk_cache()->open_count());
  6367. EXPECT_EQ(2, cache.disk_cache()->create_count());
  6368. }
  6369. // Tests that restarting a partial request when the cached data cannot be
  6370. // revalidated logs an event.
  6371. TEST_F(HttpCacheTest, RangeGET_NoValidation_LogsRestart) {
  6372. MockHttpCache cache;
  6373. cache.FailConditionalizations();
  6374. // Write to the cache (40-49).
  6375. ScopedMockTransaction transaction(kRangeGET_TransactionOK);
  6376. transaction.response_headers = "Content-Length: 10\n"
  6377. "ETag: \"foo\"\n";
  6378. RunTransactionTest(cache.http_cache(), transaction);
  6379. // Now verify that the cached data is not used.
  6380. RecordingNetLogObserver net_log_observer;
  6381. RunTransactionTestWithLog(cache.http_cache(), kRangeGET_TransactionOK,
  6382. NetLogWithSource::Make(NetLogSourceType::NONE));
  6383. EXPECT_TRUE(LogContainsEventType(
  6384. net_log_observer, NetLogEventType::HTTP_CACHE_RESTART_PARTIAL_REQUEST));
  6385. }
  6386. // Tests that a failure to conditionalize a regular request (no range) with a
  6387. // sparse entry results in a full response.
  6388. TEST_F(HttpCacheTest, GET_NoConditionalization) {
  6389. for (bool use_memory_entry_data : {false, true}) {
  6390. MockHttpCache cache;
  6391. cache.disk_cache()->set_support_in_memory_entry_data(use_memory_entry_data);
  6392. cache.FailConditionalizations();
  6393. std::string headers;
  6394. // Write to the cache (40-49).
  6395. ScopedMockTransaction transaction(kRangeGET_TransactionOK);
  6396. transaction.response_headers =
  6397. "Content-Length: 10\n"
  6398. "ETag: \"foo\"\n";
  6399. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  6400. Verify206Response(headers, 40, 49);
  6401. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  6402. EXPECT_EQ(0, cache.disk_cache()->open_count());
  6403. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6404. // Now verify that the cached data is not used.
  6405. // Don't ask for a range. The cache will attempt to use the cached data but
  6406. // should discard it as it cannot be validated. A regular request should go
  6407. // to the server and a new entry should be created.
  6408. transaction.request_headers = EXTRA_HEADER;
  6409. transaction.data = "Not a range";
  6410. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  6411. EXPECT_EQ(0U, headers.find("HTTP/1.1 200 OK\n"));
  6412. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  6413. EXPECT_EQ(1, cache.disk_cache()->open_count());
  6414. EXPECT_EQ(2, cache.disk_cache()->create_count());
  6415. // The last response was saved.
  6416. RunTransactionTest(cache.http_cache(), transaction);
  6417. EXPECT_EQ(3, cache.network_layer()->transaction_count());
  6418. if (use_memory_entry_data) {
  6419. // The cache entry isn't really useful, since when
  6420. // &RangeTransactionServer::RangeHandler gets a non-range request,
  6421. // (the network transaction #2) it returns headers without ETag,
  6422. // Last-Modified or caching headers, with a Date in 2007 (so no heuristic
  6423. // freshness), so it's both expired and not conditionalizable --- so in
  6424. // this branch we avoid opening it.
  6425. EXPECT_EQ(1, cache.disk_cache()->open_count());
  6426. EXPECT_EQ(3, cache.disk_cache()->create_count());
  6427. } else {
  6428. EXPECT_EQ(2, cache.disk_cache()->open_count());
  6429. EXPECT_EQ(2, cache.disk_cache()->create_count());
  6430. }
  6431. }
  6432. }
  6433. // Verifies that conditionalization failures when asking for a range that would
  6434. // require the cache to modify the range to ask, result in a network request
  6435. // that matches the user's one.
  6436. TEST_F(HttpCacheTest, RangeGET_NoConditionalization2) {
  6437. MockHttpCache cache;
  6438. cache.FailConditionalizations();
  6439. std::string headers;
  6440. // Write to the cache (40-49).
  6441. ScopedMockTransaction transaction(kRangeGET_TransactionOK);
  6442. transaction.response_headers = "Content-Length: 10\n"
  6443. "ETag: \"foo\"\n";
  6444. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  6445. Verify206Response(headers, 40, 49);
  6446. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  6447. EXPECT_EQ(0, cache.disk_cache()->open_count());
  6448. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6449. // Now verify that the cached data is not used.
  6450. // Ask for a range that extends before and after the cached data so that the
  6451. // cache would normally mix data from three sources. After deleting the entry,
  6452. // the response will come from a single network request.
  6453. transaction.request_headers = "Range: bytes = 20-59\r\n" EXTRA_HEADER;
  6454. transaction.data = "rg: 20-29 rg: 30-39 rg: 40-49 rg: 50-59 ";
  6455. transaction.response_headers = kRangeGET_TransactionOK.response_headers;
  6456. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  6457. Verify206Response(headers, 20, 59);
  6458. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  6459. EXPECT_EQ(1, cache.disk_cache()->open_count());
  6460. EXPECT_EQ(2, cache.disk_cache()->create_count());
  6461. // The last response was saved.
  6462. RunTransactionTest(cache.http_cache(), transaction);
  6463. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  6464. EXPECT_EQ(2, cache.disk_cache()->open_count());
  6465. EXPECT_EQ(2, cache.disk_cache()->create_count());
  6466. }
  6467. // Tests that we cache partial responses that lack content-length.
  6468. TEST_F(HttpCacheTest, RangeGET_NoContentLength) {
  6469. MockHttpCache cache;
  6470. std::string headers;
  6471. // Attempt to write to the cache (40-49).
  6472. MockTransaction transaction(kRangeGET_TransactionOK);
  6473. AddMockTransaction(&transaction);
  6474. transaction.response_headers = "ETag: \"foo\"\n"
  6475. "Accept-Ranges: bytes\n"
  6476. "Content-Range: bytes 40-49/80\n";
  6477. transaction.handler = nullptr;
  6478. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  6479. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  6480. EXPECT_EQ(0, cache.disk_cache()->open_count());
  6481. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6482. // Now verify that there's no cached data.
  6483. transaction.handler = &RangeTransactionServer::RangeHandler;
  6484. RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
  6485. &headers);
  6486. Verify206Response(headers, 40, 49);
  6487. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  6488. EXPECT_EQ(1, cache.disk_cache()->open_count());
  6489. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6490. RemoveMockTransaction(&transaction);
  6491. }
  6492. // Tests that we can cache range requests and fetch random blocks from the
  6493. // cache and the network.
  6494. TEST_F(HttpCacheTest, RangeGET_OK) {
  6495. MockHttpCache cache;
  6496. AddMockTransaction(&kRangeGET_TransactionOK);
  6497. std::string headers;
  6498. // Write to the cache (40-49).
  6499. RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
  6500. &headers);
  6501. Verify206Response(headers, 40, 49);
  6502. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  6503. EXPECT_EQ(0, cache.disk_cache()->open_count());
  6504. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6505. // Read from the cache (40-49).
  6506. RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
  6507. &headers);
  6508. Verify206Response(headers, 40, 49);
  6509. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  6510. EXPECT_EQ(1, cache.disk_cache()->open_count());
  6511. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6512. // Make sure we are done with the previous transaction.
  6513. base::RunLoop().RunUntilIdle();
  6514. // Write to the cache (30-39).
  6515. MockTransaction transaction(kRangeGET_TransactionOK);
  6516. transaction.request_headers = "Range: bytes = 30-39\r\n" EXTRA_HEADER;
  6517. transaction.data = "rg: 30-39 ";
  6518. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  6519. Verify206Response(headers, 30, 39);
  6520. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  6521. EXPECT_EQ(2, cache.disk_cache()->open_count());
  6522. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6523. // Make sure we are done with the previous transaction.
  6524. base::RunLoop().RunUntilIdle();
  6525. // Write and read from the cache (20-59).
  6526. transaction.request_headers = "Range: bytes = 20-59\r\n" EXTRA_HEADER;
  6527. transaction.data = "rg: 20-29 rg: 30-39 rg: 40-49 rg: 50-59 ";
  6528. LoadTimingInfo load_timing_info;
  6529. RunTransactionTestWithResponseAndGetTiming(
  6530. cache.http_cache(), transaction, &headers,
  6531. NetLogWithSource::Make(NetLogSourceType::NONE), &load_timing_info);
  6532. Verify206Response(headers, 20, 59);
  6533. EXPECT_EQ(4, cache.network_layer()->transaction_count());
  6534. EXPECT_EQ(3, cache.disk_cache()->open_count());
  6535. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6536. TestLoadTimingNetworkRequest(load_timing_info);
  6537. RemoveMockTransaction(&kRangeGET_TransactionOK);
  6538. }
  6539. TEST_F(HttpCacheTest, RangeGET_CacheReadError) {
  6540. // Tests recovery on cache read error on range request.
  6541. MockHttpCache cache;
  6542. AddMockTransaction(&kRangeGET_TransactionOK);
  6543. std::string headers;
  6544. // Write to the cache (40-49).
  6545. RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
  6546. &headers);
  6547. Verify206Response(headers, 40, 49);
  6548. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  6549. EXPECT_EQ(0, cache.disk_cache()->open_count());
  6550. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6551. cache.disk_cache()->set_soft_failures_one_instance(MockDiskEntry::FAIL_ALL);
  6552. // Try to read from the cache (40-49), which will fail quickly enough to
  6553. // restart, due to the failure injected above. This should still be a range
  6554. // request. (https://crbug.com/891212)
  6555. RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
  6556. &headers);
  6557. Verify206Response(headers, 40, 49);
  6558. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  6559. EXPECT_EQ(1, cache.disk_cache()->open_count());
  6560. EXPECT_EQ(2, cache.disk_cache()->create_count());
  6561. RemoveMockTransaction(&kRangeGET_TransactionOK);
  6562. }
  6563. // Tests that range requests with no-store get correct content-length
  6564. // (https://crbug.com/700197).
  6565. TEST_F(HttpCacheTest, RangeGET_NoStore) {
  6566. MockHttpCache cache;
  6567. MockTransaction transaction(kRangeGET_TransactionOK);
  6568. std::string response_headers = base::StrCat(
  6569. {kRangeGET_TransactionOK.response_headers, "Cache-Control: no-store\n"});
  6570. transaction.response_headers = response_headers.c_str();
  6571. AddMockTransaction(&transaction);
  6572. std::string headers;
  6573. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  6574. Verify206Response(headers, 40, 49);
  6575. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  6576. EXPECT_EQ(0, cache.disk_cache()->open_count());
  6577. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6578. RemoveMockTransaction(&transaction);
  6579. }
  6580. // Tests a 304 setting no-store on existing 206 entry.
  6581. TEST_F(HttpCacheTest, RangeGET_NoStore304) {
  6582. MockHttpCache cache;
  6583. MockTransaction transaction(kRangeGET_TransactionOK);
  6584. std::string response_headers = base::StrCat(
  6585. {kRangeGET_TransactionOK.response_headers, "Cache-Control: max-age=0\n"});
  6586. transaction.response_headers = response_headers.c_str();
  6587. AddMockTransaction(&transaction);
  6588. std::string headers;
  6589. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  6590. Verify206Response(headers, 40, 49);
  6591. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  6592. EXPECT_EQ(0, cache.disk_cache()->open_count());
  6593. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6594. response_headers = base::StrCat(
  6595. {kRangeGET_TransactionOK.response_headers, "Cache-Control: no-store\n"});
  6596. transaction.response_headers = response_headers.c_str();
  6597. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  6598. Verify206Response(headers, 40, 49);
  6599. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  6600. EXPECT_EQ(1, cache.disk_cache()->open_count());
  6601. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6602. // Fetch again, this one should be from newly created cache entry, due to
  6603. // earlier no-store.
  6604. transaction.response_headers = kRangeGET_TransactionOK.response_headers;
  6605. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  6606. EXPECT_EQ(3, cache.network_layer()->transaction_count());
  6607. EXPECT_EQ(1, cache.disk_cache()->open_count());
  6608. EXPECT_EQ(2, cache.disk_cache()->create_count());
  6609. Verify206Response(headers, 40, 49);
  6610. RemoveMockTransaction(&transaction);
  6611. }
  6612. // Tests that we can cache range requests and fetch random blocks from the
  6613. // cache and the network, with synchronous responses.
  6614. TEST_F(HttpCacheTest, RangeGET_SyncOK) {
  6615. MockHttpCache cache;
  6616. MockTransaction transaction(kRangeGET_TransactionOK);
  6617. transaction.test_mode = TEST_MODE_SYNC_ALL;
  6618. AddMockTransaction(&transaction);
  6619. // Write to the cache (40-49).
  6620. std::string headers;
  6621. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  6622. Verify206Response(headers, 40, 49);
  6623. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  6624. EXPECT_EQ(0, cache.disk_cache()->open_count());
  6625. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6626. // Read from the cache (40-49).
  6627. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  6628. Verify206Response(headers, 40, 49);
  6629. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  6630. EXPECT_EQ(0, cache.disk_cache()->open_count());
  6631. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6632. // Make sure we are done with the previous transaction.
  6633. base::RunLoop().RunUntilIdle();
  6634. // Write to the cache (30-39).
  6635. transaction.request_headers = "Range: bytes = 30-39\r\n" EXTRA_HEADER;
  6636. transaction.data = "rg: 30-39 ";
  6637. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  6638. Verify206Response(headers, 30, 39);
  6639. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  6640. EXPECT_EQ(1, cache.disk_cache()->open_count());
  6641. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6642. // Make sure we are done with the previous transaction.
  6643. base::RunLoop().RunUntilIdle();
  6644. // Write and read from the cache (20-59).
  6645. transaction.request_headers = "Range: bytes = 20-59\r\n" EXTRA_HEADER;
  6646. transaction.data = "rg: 20-29 rg: 30-39 rg: 40-49 rg: 50-59 ";
  6647. LoadTimingInfo load_timing_info;
  6648. RunTransactionTestWithResponseAndGetTiming(
  6649. cache.http_cache(), transaction, &headers,
  6650. NetLogWithSource::Make(NetLogSourceType::NONE), &load_timing_info);
  6651. Verify206Response(headers, 20, 59);
  6652. EXPECT_EQ(4, cache.network_layer()->transaction_count());
  6653. EXPECT_EQ(2, cache.disk_cache()->open_count());
  6654. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6655. TestLoadTimingNetworkRequest(load_timing_info);
  6656. RemoveMockTransaction(&transaction);
  6657. }
  6658. // Tests that if the previous transaction is cancelled while busy (doing sparse
  6659. // IO), a new transaction (that reuses that same ActiveEntry) waits until the
  6660. // entry is ready again.
  6661. TEST_F(HttpCacheTest, Sparse_WaitForEntry) {
  6662. MockHttpCache cache;
  6663. ScopedMockTransaction transaction(kRangeGET_TransactionOK);
  6664. // Create a sparse entry.
  6665. RunTransactionTest(cache.http_cache(), transaction);
  6666. // Simulate a previous transaction being cancelled.
  6667. disk_cache::Entry* entry;
  6668. MockHttpRequest request(transaction);
  6669. std::string cache_key =
  6670. *cache.http_cache()->GenerateCacheKeyForRequest(&request);
  6671. ASSERT_TRUE(cache.OpenBackendEntry(cache_key, &entry));
  6672. entry->CancelSparseIO();
  6673. // Test with a range request.
  6674. RunTransactionTest(cache.http_cache(), transaction);
  6675. // Now test with a regular request.
  6676. entry->CancelSparseIO();
  6677. transaction.request_headers = EXTRA_HEADER;
  6678. transaction.data = kFullRangeData;
  6679. RunTransactionTest(cache.http_cache(), transaction);
  6680. entry->Close();
  6681. }
  6682. // Tests that we don't revalidate an entry unless we are required to do so.
  6683. TEST_F(HttpCacheTest, RangeGET_Revalidate1) {
  6684. MockHttpCache cache;
  6685. std::string headers;
  6686. // Write to the cache (40-49).
  6687. MockTransaction transaction(kRangeGET_TransactionOK);
  6688. transaction.response_headers =
  6689. "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n"
  6690. "Expires: Wed, 7 Sep 2033 21:46:42 GMT\n" // Should never expire.
  6691. "ETag: \"foo\"\n"
  6692. "Accept-Ranges: bytes\n"
  6693. "Content-Length: 10\n";
  6694. AddMockTransaction(&transaction);
  6695. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  6696. Verify206Response(headers, 40, 49);
  6697. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  6698. EXPECT_EQ(0, cache.disk_cache()->open_count());
  6699. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6700. // Read from the cache (40-49).
  6701. NetLogWithSource net_log_with_source =
  6702. NetLogWithSource::Make(NetLogSourceType::NONE);
  6703. LoadTimingInfo load_timing_info;
  6704. RunTransactionTestWithResponseAndGetTiming(cache.http_cache(), transaction,
  6705. &headers, net_log_with_source,
  6706. &load_timing_info);
  6707. Verify206Response(headers, 40, 49);
  6708. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  6709. EXPECT_EQ(1, cache.disk_cache()->open_count());
  6710. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6711. TestLoadTimingCachedResponse(load_timing_info);
  6712. // Read again forcing the revalidation.
  6713. transaction.load_flags |= LOAD_VALIDATE_CACHE;
  6714. RunTransactionTestWithResponseAndGetTiming(cache.http_cache(), transaction,
  6715. &headers, net_log_with_source,
  6716. &load_timing_info);
  6717. Verify206Response(headers, 40, 49);
  6718. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  6719. EXPECT_EQ(1, cache.disk_cache()->open_count());
  6720. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6721. TestLoadTimingNetworkRequest(load_timing_info);
  6722. RemoveMockTransaction(&transaction);
  6723. }
  6724. // Checks that we revalidate an entry when the headers say so.
  6725. TEST_F(HttpCacheTest, RangeGET_Revalidate2) {
  6726. MockHttpCache cache;
  6727. std::string headers;
  6728. // Write to the cache (40-49).
  6729. MockTransaction transaction(kRangeGET_TransactionOK);
  6730. transaction.response_headers =
  6731. "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n"
  6732. "Expires: Sat, 18 Apr 2009 01:10:43 GMT\n" // Expired.
  6733. "ETag: \"foo\"\n"
  6734. "Accept-Ranges: bytes\n"
  6735. "Content-Length: 10\n";
  6736. AddMockTransaction(&transaction);
  6737. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  6738. Verify206Response(headers, 40, 49);
  6739. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  6740. EXPECT_EQ(0, cache.disk_cache()->open_count());
  6741. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6742. // Read from the cache (40-49).
  6743. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  6744. Verify206Response(headers, 40, 49);
  6745. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  6746. EXPECT_EQ(1, cache.disk_cache()->open_count());
  6747. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6748. RemoveMockTransaction(&transaction);
  6749. }
  6750. // Tests that we deal with 304s for range requests.
  6751. TEST_F(HttpCacheTest, RangeGET_304) {
  6752. MockHttpCache cache;
  6753. AddMockTransaction(&kRangeGET_TransactionOK);
  6754. std::string headers;
  6755. // Write to the cache (40-49).
  6756. RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
  6757. &headers);
  6758. Verify206Response(headers, 40, 49);
  6759. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  6760. EXPECT_EQ(0, cache.disk_cache()->open_count());
  6761. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6762. // Read from the cache (40-49).
  6763. RangeTransactionServer handler;
  6764. handler.set_not_modified(true);
  6765. MockTransaction transaction(kRangeGET_TransactionOK);
  6766. transaction.load_flags |= LOAD_VALIDATE_CACHE;
  6767. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  6768. Verify206Response(headers, 40, 49);
  6769. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  6770. EXPECT_EQ(1, cache.disk_cache()->open_count());
  6771. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6772. RemoveMockTransaction(&kRangeGET_TransactionOK);
  6773. }
  6774. // Tests that we deal with 206s when revalidating range requests.
  6775. TEST_F(HttpCacheTest, RangeGET_ModifiedResult) {
  6776. MockHttpCache cache;
  6777. AddMockTransaction(&kRangeGET_TransactionOK);
  6778. std::string headers;
  6779. // Write to the cache (40-49).
  6780. RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
  6781. &headers);
  6782. Verify206Response(headers, 40, 49);
  6783. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  6784. EXPECT_EQ(0, cache.disk_cache()->open_count());
  6785. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6786. // Attempt to read from the cache (40-49).
  6787. RangeTransactionServer handler;
  6788. handler.set_modified(true);
  6789. MockTransaction transaction(kRangeGET_TransactionOK);
  6790. transaction.load_flags |= LOAD_VALIDATE_CACHE;
  6791. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  6792. Verify206Response(headers, 40, 49);
  6793. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  6794. EXPECT_EQ(1, cache.disk_cache()->open_count());
  6795. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6796. // And the entry should be gone.
  6797. RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK);
  6798. EXPECT_EQ(3, cache.network_layer()->transaction_count());
  6799. EXPECT_EQ(1, cache.disk_cache()->open_count());
  6800. EXPECT_EQ(2, cache.disk_cache()->create_count());
  6801. RemoveMockTransaction(&kRangeGET_TransactionOK);
  6802. }
  6803. // Tests that when a server returns 206 with a sub-range of the requested range,
  6804. // and there is nothing stored in the cache, the returned response is passed to
  6805. // the caller as is. In this context, a subrange means a response that starts
  6806. // with the same byte that was requested, but that is not the whole range that
  6807. // was requested.
  6808. TEST_F(HttpCacheTest, RangeGET_206ReturnsSubrangeRange_NoCachedContent) {
  6809. MockHttpCache cache;
  6810. std::string headers;
  6811. // Request a large range (40-59). The server sends 40-49.
  6812. ScopedMockTransaction transaction(kRangeGET_TransactionOK);
  6813. transaction.request_headers = "Range: bytes = 40-59\r\n" EXTRA_HEADER;
  6814. transaction.response_headers =
  6815. "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
  6816. "ETag: \"foo\"\n"
  6817. "Accept-Ranges: bytes\n"
  6818. "Content-Length: 10\n"
  6819. "Content-Range: bytes 40-49/80\n";
  6820. transaction.handler = nullptr;
  6821. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  6822. Verify206Response(headers, 40, 49);
  6823. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  6824. EXPECT_EQ(0, cache.disk_cache()->open_count());
  6825. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6826. }
  6827. // Tests that when a server returns 206 with a sub-range of the requested range,
  6828. // and there was an entry stored in the cache, the cache gets out of the way.
  6829. TEST_F(HttpCacheTest, RangeGET_206ReturnsSubrangeRange_CachedContent) {
  6830. MockHttpCache cache;
  6831. std::string headers;
  6832. // Write to the cache (70-79).
  6833. ScopedMockTransaction transaction(kRangeGET_TransactionOK);
  6834. transaction.request_headers = "Range: bytes = 70-79\r\n" EXTRA_HEADER;
  6835. transaction.data = "rg: 70-79 ";
  6836. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  6837. Verify206Response(headers, 70, 79);
  6838. // Request a large range (40-79). The cache will ask the server for 40-59.
  6839. // The server returns 40-49. The cache should consider the server confused and
  6840. // abort caching, restarting the request without caching.
  6841. transaction.request_headers = "Range: bytes = 40-79\r\n" EXTRA_HEADER;
  6842. transaction.response_headers =
  6843. "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
  6844. "ETag: \"foo\"\n"
  6845. "Accept-Ranges: bytes\n"
  6846. "Content-Length: 10\n"
  6847. "Content-Range: bytes 40-49/80\n";
  6848. transaction.handler = nullptr;
  6849. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  6850. // Two new network requests were issued, one from the cache and another after
  6851. // deleting the entry.
  6852. Verify206Response(headers, 40, 49);
  6853. EXPECT_EQ(3, cache.network_layer()->transaction_count());
  6854. EXPECT_EQ(1, cache.disk_cache()->open_count());
  6855. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6856. // The entry was deleted.
  6857. RunTransactionTest(cache.http_cache(), transaction);
  6858. EXPECT_EQ(4, cache.network_layer()->transaction_count());
  6859. EXPECT_EQ(1, cache.disk_cache()->open_count());
  6860. EXPECT_EQ(2, cache.disk_cache()->create_count());
  6861. }
  6862. // Tests that when a server returns 206 with a sub-range of the requested range,
  6863. // and there was an entry stored in the cache, the cache gets out of the way,
  6864. // when the caller is not using ranges.
  6865. TEST_F(HttpCacheTest, GET_206ReturnsSubrangeRange_CachedContent) {
  6866. MockHttpCache cache;
  6867. std::string headers;
  6868. // Write to the cache (70-79).
  6869. ScopedMockTransaction transaction(kRangeGET_TransactionOK);
  6870. transaction.request_headers = "Range: bytes = 70-79\r\n" EXTRA_HEADER;
  6871. transaction.data = "rg: 70-79 ";
  6872. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  6873. Verify206Response(headers, 70, 79);
  6874. // Don't ask for a range. The cache will ask the server for 0-69.
  6875. // The server returns 40-49. The cache should consider the server confused and
  6876. // abort caching, restarting the request.
  6877. // The second network request should not be a byte range request so the server
  6878. // should return 200 + "Not a range"
  6879. transaction.request_headers = "X-Return-Default-Range:\r\n" EXTRA_HEADER;
  6880. transaction.data = "Not a range";
  6881. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  6882. EXPECT_EQ(0U, headers.find("HTTP/1.1 200 OK\n"));
  6883. EXPECT_EQ(3, cache.network_layer()->transaction_count());
  6884. EXPECT_EQ(1, cache.disk_cache()->open_count());
  6885. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6886. // The entry was deleted.
  6887. RunTransactionTest(cache.http_cache(), transaction);
  6888. EXPECT_EQ(4, cache.network_layer()->transaction_count());
  6889. EXPECT_EQ(1, cache.disk_cache()->open_count());
  6890. EXPECT_EQ(2, cache.disk_cache()->create_count());
  6891. }
  6892. // Tests that when a server returns 206 with a random range and there is
  6893. // nothing stored in the cache, the returned response is passed to the caller
  6894. // as is. In this context, a WrongRange means that the returned range may or may
  6895. // not have any relationship with the requested range (may or may not be
  6896. // contained). The important part is that the first byte doesn't match the first
  6897. // requested byte.
  6898. TEST_F(HttpCacheTest, RangeGET_206ReturnsWrongRange_NoCachedContent) {
  6899. MockHttpCache cache;
  6900. std::string headers;
  6901. // Request a large range (30-59). The server sends (40-49).
  6902. ScopedMockTransaction transaction(kRangeGET_TransactionOK);
  6903. transaction.request_headers = "Range: bytes = 30-59\r\n" EXTRA_HEADER;
  6904. transaction.response_headers =
  6905. "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
  6906. "ETag: \"foo\"\n"
  6907. "Accept-Ranges: bytes\n"
  6908. "Content-Length: 10\n"
  6909. "Content-Range: bytes 40-49/80\n";
  6910. transaction.handler = nullptr;
  6911. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  6912. Verify206Response(headers, 40, 49);
  6913. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  6914. EXPECT_EQ(0, cache.disk_cache()->open_count());
  6915. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6916. // The entry was deleted.
  6917. RunTransactionTest(cache.http_cache(), transaction);
  6918. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  6919. EXPECT_EQ(0, cache.disk_cache()->open_count());
  6920. EXPECT_EQ(2, cache.disk_cache()->create_count());
  6921. }
  6922. // Tests that when a server returns 206 with a random range and there is
  6923. // an entry stored in the cache, the cache gets out of the way.
  6924. TEST_F(HttpCacheTest, RangeGET_206ReturnsWrongRange_CachedContent) {
  6925. MockHttpCache cache;
  6926. std::string headers;
  6927. // Write to the cache (70-79).
  6928. ScopedMockTransaction transaction(kRangeGET_TransactionOK);
  6929. transaction.request_headers = "Range: bytes = 70-79\r\n" EXTRA_HEADER;
  6930. transaction.data = "rg: 70-79 ";
  6931. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  6932. Verify206Response(headers, 70, 79);
  6933. // Request a large range (30-79). The cache will ask the server for 30-69.
  6934. // The server returns 40-49. The cache should consider the server confused and
  6935. // abort caching, returning the weird range to the caller.
  6936. transaction.request_headers = "Range: bytes = 30-79\r\n" EXTRA_HEADER;
  6937. transaction.response_headers =
  6938. "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
  6939. "ETag: \"foo\"\n"
  6940. "Accept-Ranges: bytes\n"
  6941. "Content-Length: 10\n"
  6942. "Content-Range: bytes 40-49/80\n";
  6943. transaction.handler = nullptr;
  6944. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  6945. Verify206Response(headers, 40, 49);
  6946. EXPECT_EQ(3, cache.network_layer()->transaction_count());
  6947. EXPECT_EQ(1, cache.disk_cache()->open_count());
  6948. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6949. // The entry was deleted.
  6950. RunTransactionTest(cache.http_cache(), transaction);
  6951. EXPECT_EQ(4, cache.network_layer()->transaction_count());
  6952. EXPECT_EQ(1, cache.disk_cache()->open_count());
  6953. EXPECT_EQ(2, cache.disk_cache()->create_count());
  6954. }
  6955. // Tests that when a caller asks for a range beyond EOF, with an empty cache,
  6956. // the response matches the one provided by the server.
  6957. TEST_F(HttpCacheTest, RangeGET_206ReturnsSmallerFile_NoCachedContent) {
  6958. MockHttpCache cache;
  6959. std::string headers;
  6960. // Request a large range (70-99). The server sends 70-79.
  6961. ScopedMockTransaction transaction(kRangeGET_TransactionOK);
  6962. transaction.request_headers = "Range: bytes = 70-99\r\n" EXTRA_HEADER;
  6963. transaction.data = "rg: 70-79 ";
  6964. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  6965. Verify206Response(headers, 70, 79);
  6966. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  6967. EXPECT_EQ(0, cache.disk_cache()->open_count());
  6968. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6969. RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK);
  6970. EXPECT_EQ(1, cache.disk_cache()->open_count());
  6971. }
  6972. // Tests that when a caller asks for a range beyond EOF, with a cached entry,
  6973. // the cache automatically fixes the request.
  6974. TEST_F(HttpCacheTest, RangeGET_206ReturnsSmallerFile_CachedContent) {
  6975. MockHttpCache cache;
  6976. std::string headers;
  6977. // Write to the cache (40-49).
  6978. ScopedMockTransaction transaction(kRangeGET_TransactionOK);
  6979. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  6980. // Request a large range (70-99). The server sends 70-79.
  6981. transaction.request_headers = "Range: bytes = 70-99\r\n" EXTRA_HEADER;
  6982. transaction.data = "rg: 70-79 ";
  6983. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  6984. Verify206Response(headers, 70, 79);
  6985. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  6986. EXPECT_EQ(1, cache.disk_cache()->open_count());
  6987. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6988. // The entry was not deleted (the range was automatically fixed).
  6989. RunTransactionTest(cache.http_cache(), transaction);
  6990. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  6991. EXPECT_EQ(2, cache.disk_cache()->open_count());
  6992. EXPECT_EQ(1, cache.disk_cache()->create_count());
  6993. }
  6994. // Tests that when a caller asks for a not-satisfiable range, the server's
  6995. // response is forwarded to the caller.
  6996. TEST_F(HttpCacheTest, RangeGET_416_NoCachedContent) {
  6997. MockHttpCache cache;
  6998. std::string headers;
  6999. // Request a range beyond EOF (80-99).
  7000. ScopedMockTransaction transaction(kRangeGET_TransactionOK);
  7001. transaction.request_headers = "Range: bytes = 80-99\r\n" EXTRA_HEADER;
  7002. transaction.data = "";
  7003. transaction.status = "HTTP/1.1 416 Requested Range Not Satisfiable";
  7004. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  7005. EXPECT_EQ(0U, headers.find(transaction.status));
  7006. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  7007. EXPECT_EQ(0, cache.disk_cache()->open_count());
  7008. EXPECT_EQ(1, cache.disk_cache()->create_count());
  7009. // The entry was deleted.
  7010. RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK);
  7011. EXPECT_EQ(2, cache.disk_cache()->create_count());
  7012. }
  7013. // Tests that we cache 301s for range requests.
  7014. TEST_F(HttpCacheTest, RangeGET_301) {
  7015. MockHttpCache cache;
  7016. ScopedMockTransaction transaction(kRangeGET_TransactionOK);
  7017. transaction.status = "HTTP/1.1 301 Moved Permanently";
  7018. transaction.response_headers = "Location: http://www.bar.com/\n";
  7019. transaction.data = "";
  7020. transaction.handler = nullptr;
  7021. // Write to the cache.
  7022. RunTransactionTest(cache.http_cache(), transaction);
  7023. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  7024. EXPECT_EQ(0, cache.disk_cache()->open_count());
  7025. EXPECT_EQ(1, cache.disk_cache()->create_count());
  7026. // Read from the cache.
  7027. RunTransactionTest(cache.http_cache(), transaction);
  7028. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  7029. EXPECT_EQ(1, cache.disk_cache()->open_count());
  7030. EXPECT_EQ(1, cache.disk_cache()->create_count());
  7031. }
  7032. // Tests that we can cache range requests when the start or end is unknown.
  7033. // We start with one suffix request, followed by a request from a given point.
  7034. TEST_F(HttpCacheTest, UnknownRangeGET_1) {
  7035. MockHttpCache cache;
  7036. AddMockTransaction(&kRangeGET_TransactionOK);
  7037. std::string headers;
  7038. // Write to the cache (70-79).
  7039. MockTransaction transaction(kRangeGET_TransactionOK);
  7040. transaction.request_headers = "Range: bytes = -10\r\n" EXTRA_HEADER;
  7041. transaction.data = "rg: 70-79 ";
  7042. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  7043. Verify206Response(headers, 70, 79);
  7044. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  7045. EXPECT_EQ(0, cache.disk_cache()->open_count());
  7046. EXPECT_EQ(1, cache.disk_cache()->create_count());
  7047. // Make sure we are done with the previous transaction.
  7048. base::RunLoop().RunUntilIdle();
  7049. // Write and read from the cache (60-79).
  7050. transaction.request_headers = "Range: bytes = 60-\r\n" EXTRA_HEADER;
  7051. transaction.data = "rg: 60-69 rg: 70-79 ";
  7052. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  7053. Verify206Response(headers, 60, 79);
  7054. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  7055. EXPECT_EQ(1, cache.disk_cache()->open_count());
  7056. EXPECT_EQ(1, cache.disk_cache()->create_count());
  7057. RemoveMockTransaction(&kRangeGET_TransactionOK);
  7058. }
  7059. // Tests that we can cache range requests when the start or end is unknown.
  7060. // We start with one request from a given point, followed by a suffix request.
  7061. // We'll also verify that synchronous cache responses work as intended.
  7062. TEST_F(HttpCacheTest, UnknownRangeGET_2) {
  7063. MockHttpCache cache;
  7064. std::string headers;
  7065. MockTransaction transaction(kRangeGET_TransactionOK);
  7066. transaction.test_mode = TEST_MODE_SYNC_CACHE_START |
  7067. TEST_MODE_SYNC_CACHE_READ |
  7068. TEST_MODE_SYNC_CACHE_WRITE;
  7069. AddMockTransaction(&transaction);
  7070. // Write to the cache (70-79).
  7071. transaction.request_headers = "Range: bytes = 70-\r\n" EXTRA_HEADER;
  7072. transaction.data = "rg: 70-79 ";
  7073. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  7074. Verify206Response(headers, 70, 79);
  7075. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  7076. EXPECT_EQ(0, cache.disk_cache()->open_count());
  7077. EXPECT_EQ(1, cache.disk_cache()->create_count());
  7078. // Make sure we are done with the previous transaction.
  7079. base::RunLoop().RunUntilIdle();
  7080. // Write and read from the cache (60-79).
  7081. transaction.request_headers = "Range: bytes = -20\r\n" EXTRA_HEADER;
  7082. transaction.data = "rg: 60-69 rg: 70-79 ";
  7083. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  7084. Verify206Response(headers, 60, 79);
  7085. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  7086. EXPECT_EQ(1, cache.disk_cache()->open_count());
  7087. EXPECT_EQ(1, cache.disk_cache()->create_count());
  7088. RemoveMockTransaction(&transaction);
  7089. }
  7090. // Tests that receiving Not Modified when asking for an open range doesn't mess
  7091. // up things.
  7092. TEST_F(HttpCacheTest, UnknownRangeGET_304) {
  7093. MockHttpCache cache;
  7094. std::string headers;
  7095. MockTransaction transaction(kRangeGET_TransactionOK);
  7096. AddMockTransaction(&transaction);
  7097. RangeTransactionServer handler;
  7098. handler.set_not_modified(true);
  7099. // Ask for the end of the file, without knowing the length.
  7100. transaction.request_headers = "Range: bytes = 70-\r\n" EXTRA_HEADER;
  7101. transaction.data = "";
  7102. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  7103. // We just bypass the cache.
  7104. EXPECT_EQ(0U, headers.find("HTTP/1.1 304 Not Modified\n"));
  7105. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  7106. EXPECT_EQ(0, cache.disk_cache()->open_count());
  7107. EXPECT_EQ(1, cache.disk_cache()->create_count());
  7108. RunTransactionTest(cache.http_cache(), transaction);
  7109. EXPECT_EQ(2, cache.disk_cache()->create_count());
  7110. RemoveMockTransaction(&transaction);
  7111. }
  7112. // Tests that we can handle non-range requests when we have cached a range.
  7113. TEST_F(HttpCacheTest, GET_Previous206) {
  7114. MockHttpCache cache;
  7115. AddMockTransaction(&kRangeGET_TransactionOK);
  7116. std::string headers;
  7117. NetLogWithSource net_log_with_source =
  7118. NetLogWithSource::Make(NetLogSourceType::NONE);
  7119. LoadTimingInfo load_timing_info;
  7120. // Write to the cache (40-49).
  7121. RunTransactionTestWithResponseAndGetTiming(
  7122. cache.http_cache(), kRangeGET_TransactionOK, &headers,
  7123. net_log_with_source, &load_timing_info);
  7124. Verify206Response(headers, 40, 49);
  7125. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  7126. EXPECT_EQ(0, cache.disk_cache()->open_count());
  7127. EXPECT_EQ(1, cache.disk_cache()->create_count());
  7128. TestLoadTimingNetworkRequest(load_timing_info);
  7129. // Write and read from the cache (0-79), when not asked for a range.
  7130. MockTransaction transaction(kRangeGET_TransactionOK);
  7131. transaction.request_headers = EXTRA_HEADER;
  7132. transaction.data = kFullRangeData;
  7133. RunTransactionTestWithResponseAndGetTiming(cache.http_cache(), transaction,
  7134. &headers, net_log_with_source,
  7135. &load_timing_info);
  7136. EXPECT_EQ(0U, headers.find("HTTP/1.1 200 OK\n"));
  7137. EXPECT_EQ(3, cache.network_layer()->transaction_count());
  7138. EXPECT_EQ(1, cache.disk_cache()->open_count());
  7139. EXPECT_EQ(1, cache.disk_cache()->create_count());
  7140. TestLoadTimingNetworkRequest(load_timing_info);
  7141. RemoveMockTransaction(&kRangeGET_TransactionOK);
  7142. }
  7143. // Tests that we can handle non-range requests when we have cached the first
  7144. // part of the object and the server replies with 304 (Not Modified).
  7145. TEST_F(HttpCacheTest, GET_Previous206_NotModified) {
  7146. MockHttpCache cache;
  7147. MockTransaction transaction(kRangeGET_TransactionOK);
  7148. AddMockTransaction(&transaction);
  7149. std::string headers;
  7150. NetLogWithSource net_log_with_source =
  7151. NetLogWithSource::Make(NetLogSourceType::NONE);
  7152. LoadTimingInfo load_timing_info;
  7153. // Write to the cache (0-9).
  7154. transaction.request_headers = "Range: bytes = 0-9\r\n" EXTRA_HEADER;
  7155. transaction.data = "rg: 00-09 ";
  7156. RunTransactionTestWithResponseAndGetTiming(cache.http_cache(), transaction,
  7157. &headers, net_log_with_source,
  7158. &load_timing_info);
  7159. Verify206Response(headers, 0, 9);
  7160. TestLoadTimingNetworkRequest(load_timing_info);
  7161. // Write to the cache (70-79).
  7162. transaction.request_headers = "Range: bytes = 70-79\r\n" EXTRA_HEADER;
  7163. transaction.data = "rg: 70-79 ";
  7164. RunTransactionTestWithResponseAndGetTiming(cache.http_cache(), transaction,
  7165. &headers, net_log_with_source,
  7166. &load_timing_info);
  7167. Verify206Response(headers, 70, 79);
  7168. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  7169. EXPECT_EQ(1, cache.disk_cache()->open_count());
  7170. EXPECT_EQ(1, cache.disk_cache()->create_count());
  7171. TestLoadTimingNetworkRequest(load_timing_info);
  7172. // Read from the cache (0-9), write and read from cache (10 - 79).
  7173. transaction.load_flags |= LOAD_VALIDATE_CACHE;
  7174. transaction.request_headers = "Foo: bar\r\n" EXTRA_HEADER;
  7175. transaction.data = kFullRangeData;
  7176. RunTransactionTestWithResponseAndGetTiming(cache.http_cache(), transaction,
  7177. &headers, net_log_with_source,
  7178. &load_timing_info);
  7179. EXPECT_EQ(0U, headers.find("HTTP/1.1 200 OK\n"));
  7180. EXPECT_EQ(4, cache.network_layer()->transaction_count());
  7181. EXPECT_EQ(2, cache.disk_cache()->open_count());
  7182. EXPECT_EQ(1, cache.disk_cache()->create_count());
  7183. TestLoadTimingNetworkRequest(load_timing_info);
  7184. RemoveMockTransaction(&transaction);
  7185. }
  7186. // Tests that we can handle a regular request to a sparse entry, that results in
  7187. // new content provided by the server (206).
  7188. TEST_F(HttpCacheTest, GET_Previous206_NewContent) {
  7189. MockHttpCache cache;
  7190. AddMockTransaction(&kRangeGET_TransactionOK);
  7191. std::string headers;
  7192. // Write to the cache (0-9).
  7193. MockTransaction transaction(kRangeGET_TransactionOK);
  7194. transaction.request_headers = "Range: bytes = 0-9\r\n" EXTRA_HEADER;
  7195. transaction.data = "rg: 00-09 ";
  7196. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  7197. Verify206Response(headers, 0, 9);
  7198. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  7199. EXPECT_EQ(0, cache.disk_cache()->open_count());
  7200. EXPECT_EQ(1, cache.disk_cache()->create_count());
  7201. // Now we'll issue a request without any range that should result first in a
  7202. // 206 (when revalidating), and then in a weird standard answer: the test
  7203. // server will not modify the response so we'll get the default range... a
  7204. // real server will answer with 200.
  7205. MockTransaction transaction2(kRangeGET_TransactionOK);
  7206. transaction2.request_headers = EXTRA_HEADER;
  7207. transaction2.load_flags |= LOAD_VALIDATE_CACHE;
  7208. transaction2.data = "Not a range";
  7209. RangeTransactionServer handler;
  7210. handler.set_modified(true);
  7211. LoadTimingInfo load_timing_info;
  7212. RunTransactionTestWithResponseAndGetTiming(
  7213. cache.http_cache(), transaction2, &headers,
  7214. NetLogWithSource::Make(NetLogSourceType::NONE), &load_timing_info);
  7215. EXPECT_EQ(0U, headers.find("HTTP/1.1 200 OK\n"));
  7216. EXPECT_EQ(3, cache.network_layer()->transaction_count());
  7217. EXPECT_EQ(1, cache.disk_cache()->open_count());
  7218. EXPECT_EQ(1, cache.disk_cache()->create_count());
  7219. TestLoadTimingNetworkRequest(load_timing_info);
  7220. // Verify that the previous request deleted the entry.
  7221. RunTransactionTest(cache.http_cache(), transaction);
  7222. EXPECT_EQ(2, cache.disk_cache()->create_count());
  7223. RemoveMockTransaction(&transaction);
  7224. }
  7225. // Tests that we can handle cached 206 responses that are not sparse.
  7226. TEST_F(HttpCacheTest, GET_Previous206_NotSparse) {
  7227. MockHttpCache cache;
  7228. MockHttpRequest request(kSimpleGET_Transaction);
  7229. // Create a disk cache entry that stores 206 headers while not being sparse.
  7230. disk_cache::Entry* entry;
  7231. ASSERT_TRUE(cache.CreateBackendEntry(request.CacheKey(), &entry, nullptr));
  7232. std::string raw_headers(kRangeGET_TransactionOK.status);
  7233. raw_headers.append("\n");
  7234. raw_headers.append(kRangeGET_TransactionOK.response_headers);
  7235. HttpResponseInfo response;
  7236. response.headers = base::MakeRefCounted<HttpResponseHeaders>(
  7237. HttpUtil::AssembleRawHeaders(raw_headers));
  7238. EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, false));
  7239. scoped_refptr<IOBuffer> buf(base::MakeRefCounted<IOBuffer>(500));
  7240. int len = static_cast<int>(base::strlcpy(buf->data(),
  7241. kRangeGET_TransactionOK.data, 500));
  7242. TestCompletionCallback cb;
  7243. int rv = entry->WriteData(1, 0, buf.get(), len, cb.callback(), true);
  7244. EXPECT_EQ(len, cb.GetResult(rv));
  7245. entry->Close();
  7246. // Now see that we don't use the stored entry.
  7247. std::string headers;
  7248. LoadTimingInfo load_timing_info;
  7249. RunTransactionTestWithResponseAndGetTiming(
  7250. cache.http_cache(), kSimpleGET_Transaction, &headers,
  7251. NetLogWithSource::Make(NetLogSourceType::NONE), &load_timing_info);
  7252. // We are expecting a 200.
  7253. std::string expected_headers(kSimpleGET_Transaction.status);
  7254. expected_headers.append("\n");
  7255. expected_headers.append(kSimpleGET_Transaction.response_headers);
  7256. EXPECT_EQ(expected_headers, headers);
  7257. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  7258. EXPECT_EQ(1, cache.disk_cache()->open_count());
  7259. EXPECT_EQ(2, cache.disk_cache()->create_count());
  7260. TestLoadTimingNetworkRequest(load_timing_info);
  7261. }
  7262. // Tests that we can handle cached 206 responses that are not sparse. This time
  7263. // we issue a range request and expect to receive a range.
  7264. TEST_F(HttpCacheTest, RangeGET_Previous206_NotSparse_2) {
  7265. MockHttpCache cache;
  7266. AddMockTransaction(&kRangeGET_TransactionOK);
  7267. // Create a disk cache entry that stores 206 headers while not being sparse.
  7268. MockHttpRequest request(kRangeGET_TransactionOK);
  7269. disk_cache::Entry* entry;
  7270. ASSERT_TRUE(cache.CreateBackendEntry(request.CacheKey(), &entry, nullptr));
  7271. std::string raw_headers(kRangeGET_TransactionOK.status);
  7272. raw_headers.append("\n");
  7273. raw_headers.append(kRangeGET_TransactionOK.response_headers);
  7274. HttpResponseInfo response;
  7275. response.headers = base::MakeRefCounted<HttpResponseHeaders>(
  7276. HttpUtil::AssembleRawHeaders(raw_headers));
  7277. EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, false));
  7278. scoped_refptr<IOBuffer> buf = base::MakeRefCounted<IOBuffer>(500);
  7279. int len = static_cast<int>(base::strlcpy(buf->data(),
  7280. kRangeGET_TransactionOK.data, 500));
  7281. TestCompletionCallback cb;
  7282. int rv = entry->WriteData(1, 0, buf.get(), len, cb.callback(), true);
  7283. EXPECT_EQ(len, cb.GetResult(rv));
  7284. entry->Close();
  7285. // Now see that we don't use the stored entry.
  7286. std::string headers;
  7287. RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
  7288. &headers);
  7289. // We are expecting a 206.
  7290. Verify206Response(headers, 40, 49);
  7291. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  7292. EXPECT_EQ(1, cache.disk_cache()->open_count());
  7293. EXPECT_EQ(2, cache.disk_cache()->create_count());
  7294. RemoveMockTransaction(&kRangeGET_TransactionOK);
  7295. }
  7296. // Tests that we can handle cached 206 responses that can't be validated.
  7297. TEST_F(HttpCacheTest, GET_Previous206_NotValidation) {
  7298. MockHttpCache cache;
  7299. MockHttpRequest request(kSimpleGET_Transaction);
  7300. // Create a disk cache entry that stores 206 headers.
  7301. disk_cache::Entry* entry;
  7302. ASSERT_TRUE(cache.CreateBackendEntry(request.CacheKey(), &entry, nullptr));
  7303. // Make sure that the headers cannot be validated with the server.
  7304. std::string raw_headers(kRangeGET_TransactionOK.status);
  7305. raw_headers.append("\n");
  7306. raw_headers.append("Content-Length: 80\n");
  7307. HttpResponseInfo response;
  7308. response.headers = base::MakeRefCounted<HttpResponseHeaders>(
  7309. HttpUtil::AssembleRawHeaders(raw_headers));
  7310. EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, false));
  7311. scoped_refptr<IOBuffer> buf = base::MakeRefCounted<IOBuffer>(500);
  7312. int len = static_cast<int>(base::strlcpy(buf->data(),
  7313. kRangeGET_TransactionOK.data, 500));
  7314. TestCompletionCallback cb;
  7315. int rv = entry->WriteData(1, 0, buf.get(), len, cb.callback(), true);
  7316. EXPECT_EQ(len, cb.GetResult(rv));
  7317. entry->Close();
  7318. // Now see that we don't use the stored entry.
  7319. std::string headers;
  7320. RunTransactionTestWithResponse(cache.http_cache(), kSimpleGET_Transaction,
  7321. &headers);
  7322. // We are expecting a 200.
  7323. std::string expected_headers(kSimpleGET_Transaction.status);
  7324. expected_headers.append("\n");
  7325. expected_headers.append(kSimpleGET_Transaction.response_headers);
  7326. EXPECT_EQ(expected_headers, headers);
  7327. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  7328. EXPECT_EQ(1, cache.disk_cache()->open_count());
  7329. EXPECT_EQ(2, cache.disk_cache()->create_count());
  7330. }
  7331. // Tests that we can handle range requests with cached 200 responses.
  7332. TEST_F(HttpCacheTest, RangeGET_Previous200) {
  7333. MockHttpCache cache;
  7334. // Store the whole thing with status 200.
  7335. MockTransaction transaction(kTypicalGET_Transaction);
  7336. transaction.url = kRangeGET_TransactionOK.url;
  7337. transaction.data = kFullRangeData;
  7338. AddMockTransaction(&transaction);
  7339. RunTransactionTest(cache.http_cache(), transaction);
  7340. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  7341. EXPECT_EQ(0, cache.disk_cache()->open_count());
  7342. EXPECT_EQ(1, cache.disk_cache()->create_count());
  7343. RemoveMockTransaction(&transaction);
  7344. AddMockTransaction(&kRangeGET_TransactionOK);
  7345. // Now see that we use the stored entry.
  7346. std::string headers;
  7347. MockTransaction transaction2(kRangeGET_TransactionOK);
  7348. RangeTransactionServer handler;
  7349. handler.set_not_modified(true);
  7350. RunTransactionTestWithResponse(cache.http_cache(), transaction2, &headers);
  7351. // We are expecting a 206.
  7352. Verify206Response(headers, 40, 49);
  7353. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  7354. EXPECT_EQ(1, cache.disk_cache()->open_count());
  7355. EXPECT_EQ(1, cache.disk_cache()->create_count());
  7356. // The last transaction has finished so make sure the entry is deactivated.
  7357. base::RunLoop().RunUntilIdle();
  7358. // Make a request for an invalid range.
  7359. MockTransaction transaction3(kRangeGET_TransactionOK);
  7360. transaction3.request_headers = "Range: bytes = 80-90\r\n" EXTRA_HEADER;
  7361. transaction3.data = transaction.data;
  7362. transaction3.load_flags = LOAD_SKIP_CACHE_VALIDATION;
  7363. RunTransactionTestWithResponse(cache.http_cache(), transaction3, &headers);
  7364. EXPECT_EQ(2, cache.disk_cache()->open_count());
  7365. EXPECT_EQ(0U, headers.find("HTTP/1.1 200 "));
  7366. EXPECT_EQ(std::string::npos, headers.find("Content-Range:"));
  7367. EXPECT_EQ(std::string::npos, headers.find("Content-Length: 80"));
  7368. // Make sure the entry is deactivated.
  7369. base::RunLoop().RunUntilIdle();
  7370. // Even though the request was invalid, we should have the entry.
  7371. RunTransactionTest(cache.http_cache(), transaction2);
  7372. EXPECT_EQ(3, cache.disk_cache()->open_count());
  7373. // Make sure the entry is deactivated.
  7374. base::RunLoop().RunUntilIdle();
  7375. // Now we should receive a range from the server and drop the stored entry.
  7376. handler.set_not_modified(false);
  7377. transaction2.request_headers = kRangeGET_TransactionOK.request_headers;
  7378. RunTransactionTestWithResponse(cache.http_cache(), transaction2, &headers);
  7379. Verify206Response(headers, 40, 49);
  7380. EXPECT_EQ(4, cache.network_layer()->transaction_count());
  7381. EXPECT_EQ(4, cache.disk_cache()->open_count());
  7382. EXPECT_EQ(1, cache.disk_cache()->create_count());
  7383. RunTransactionTest(cache.http_cache(), transaction2);
  7384. EXPECT_EQ(2, cache.disk_cache()->create_count());
  7385. RemoveMockTransaction(&kRangeGET_TransactionOK);
  7386. }
  7387. // Tests that we can handle a 200 response when dealing with sparse entries.
  7388. TEST_F(HttpCacheTest, RangeRequestResultsIn200) {
  7389. MockHttpCache cache;
  7390. AddMockTransaction(&kRangeGET_TransactionOK);
  7391. std::string headers;
  7392. // Write to the cache (70-79).
  7393. MockTransaction transaction(kRangeGET_TransactionOK);
  7394. transaction.request_headers = "Range: bytes = -10\r\n" EXTRA_HEADER;
  7395. transaction.data = "rg: 70-79 ";
  7396. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  7397. Verify206Response(headers, 70, 79);
  7398. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  7399. EXPECT_EQ(0, cache.disk_cache()->open_count());
  7400. EXPECT_EQ(1, cache.disk_cache()->create_count());
  7401. // Now we'll issue a request that results in a plain 200 response, but to
  7402. // the to the same URL that we used to store sparse data, and making sure
  7403. // that we ask for a range.
  7404. RemoveMockTransaction(&kRangeGET_TransactionOK);
  7405. MockTransaction transaction2(kSimpleGET_Transaction);
  7406. transaction2.url = kRangeGET_TransactionOK.url;
  7407. transaction2.request_headers = kRangeGET_TransactionOK.request_headers;
  7408. AddMockTransaction(&transaction2);
  7409. RunTransactionTestWithResponse(cache.http_cache(), transaction2, &headers);
  7410. std::string expected_headers(kSimpleGET_Transaction.status);
  7411. expected_headers.append("\n");
  7412. expected_headers.append(kSimpleGET_Transaction.response_headers);
  7413. EXPECT_EQ(expected_headers, headers);
  7414. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  7415. EXPECT_EQ(1, cache.disk_cache()->open_count());
  7416. EXPECT_EQ(1, cache.disk_cache()->create_count());
  7417. RemoveMockTransaction(&transaction2);
  7418. }
  7419. // Tests that a range request that falls outside of the size that we know about
  7420. // only deletes the entry if the resource has indeed changed.
  7421. TEST_F(HttpCacheTest, RangeGET_MoreThanCurrentSize) {
  7422. MockHttpCache cache;
  7423. AddMockTransaction(&kRangeGET_TransactionOK);
  7424. std::string headers;
  7425. // Write to the cache (40-49).
  7426. RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
  7427. &headers);
  7428. Verify206Response(headers, 40, 49);
  7429. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  7430. EXPECT_EQ(0, cache.disk_cache()->open_count());
  7431. EXPECT_EQ(1, cache.disk_cache()->create_count());
  7432. // A weird request should not delete this entry. Ask for bytes 120-.
  7433. MockTransaction transaction(kRangeGET_TransactionOK);
  7434. transaction.request_headers = "Range: bytes = 120-\r\n" EXTRA_HEADER;
  7435. transaction.data = "";
  7436. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  7437. EXPECT_EQ(0U, headers.find("HTTP/1.1 416 "));
  7438. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  7439. EXPECT_EQ(1, cache.disk_cache()->open_count());
  7440. EXPECT_EQ(1, cache.disk_cache()->create_count());
  7441. RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK);
  7442. EXPECT_EQ(2, cache.disk_cache()->open_count());
  7443. EXPECT_EQ(1, cache.disk_cache()->create_count());
  7444. RemoveMockTransaction(&kRangeGET_TransactionOK);
  7445. }
  7446. // Tests that we don't delete a sparse entry when we cancel a request.
  7447. TEST_F(HttpCacheTest, RangeGET_Cancel) {
  7448. MockHttpCache cache;
  7449. AddMockTransaction(&kRangeGET_TransactionOK);
  7450. MockHttpRequest request(kRangeGET_TransactionOK);
  7451. auto c = std::make_unique<Context>();
  7452. int rv = cache.CreateTransaction(&c->trans);
  7453. ASSERT_THAT(rv, IsOk());
  7454. rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
  7455. if (rv == ERR_IO_PENDING)
  7456. rv = c->callback.WaitForResult();
  7457. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  7458. EXPECT_EQ(0, cache.disk_cache()->open_count());
  7459. EXPECT_EQ(1, cache.disk_cache()->create_count());
  7460. // Make sure that the entry has some data stored.
  7461. scoped_refptr<IOBufferWithSize> buf =
  7462. base::MakeRefCounted<IOBufferWithSize>(10);
  7463. rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback());
  7464. if (rv == ERR_IO_PENDING)
  7465. rv = c->callback.WaitForResult();
  7466. EXPECT_EQ(buf->size(), rv);
  7467. // Destroy the transaction.
  7468. c.reset();
  7469. // Verify that the entry has not been deleted.
  7470. disk_cache::Entry* entry;
  7471. ASSERT_TRUE(cache.OpenBackendEntry(request.CacheKey(), &entry));
  7472. entry->Close();
  7473. RemoveMockTransaction(&kRangeGET_TransactionOK);
  7474. }
  7475. // Tests that we don't mark an entry as truncated if it is partial and not
  7476. // already truncated.
  7477. TEST_F(HttpCacheTest, RangeGET_CancelWhileReading) {
  7478. MockHttpCache cache;
  7479. AddMockTransaction(&kRangeGET_TransactionOK);
  7480. MockHttpRequest request(kRangeGET_TransactionOK);
  7481. auto context = std::make_unique<Context>();
  7482. int rv = cache.CreateTransaction(&context->trans);
  7483. ASSERT_THAT(rv, IsOk());
  7484. rv = context->trans->Start(&request, context->callback.callback(),
  7485. NetLogWithSource());
  7486. if (rv == ERR_IO_PENDING)
  7487. rv = context->callback.WaitForResult();
  7488. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  7489. EXPECT_EQ(0, cache.disk_cache()->open_count());
  7490. EXPECT_EQ(1, cache.disk_cache()->create_count());
  7491. // Start Read.
  7492. scoped_refptr<IOBufferWithSize> buf =
  7493. base::MakeRefCounted<IOBufferWithSize>(5);
  7494. rv = context->trans->Read(buf.get(), buf->size(),
  7495. context->callback.callback());
  7496. EXPECT_THAT(rv, IsError(ERR_IO_PENDING));
  7497. // Destroy the transaction.
  7498. context.reset();
  7499. // Complete Read.
  7500. base::RunLoop().RunUntilIdle();
  7501. // Verify that the entry has not been marked as truncated.
  7502. VerifyTruncatedFlag(&cache, request.CacheKey(), false, 0);
  7503. RemoveMockTransaction(&kRangeGET_TransactionOK);
  7504. }
  7505. // Tests that we don't delete a sparse entry when we start a new request after
  7506. // cancelling the previous one.
  7507. TEST_F(HttpCacheTest, RangeGET_Cancel2) {
  7508. MockHttpCache cache;
  7509. AddMockTransaction(&kRangeGET_TransactionOK);
  7510. RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK);
  7511. MockHttpRequest request(kRangeGET_TransactionOK);
  7512. request.load_flags |= LOAD_VALIDATE_CACHE;
  7513. auto c = std::make_unique<Context>();
  7514. int rv = cache.CreateTransaction(&c->trans);
  7515. ASSERT_THAT(rv, IsOk());
  7516. rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
  7517. if (rv == ERR_IO_PENDING)
  7518. rv = c->callback.WaitForResult();
  7519. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  7520. EXPECT_EQ(1, cache.disk_cache()->open_count());
  7521. EXPECT_EQ(1, cache.disk_cache()->create_count());
  7522. // Make sure that we revalidate the entry and read from the cache (a single
  7523. // read will return while waiting for the network).
  7524. scoped_refptr<IOBufferWithSize> buf =
  7525. base::MakeRefCounted<IOBufferWithSize>(5);
  7526. rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback());
  7527. EXPECT_EQ(5, c->callback.GetResult(rv));
  7528. rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback());
  7529. EXPECT_THAT(rv, IsError(ERR_IO_PENDING));
  7530. // Destroy the transaction before completing the read.
  7531. c.reset();
  7532. // We have the read and the delete (OnProcessPendingQueue) waiting on the
  7533. // message loop. This means that a new transaction will just reuse the same
  7534. // active entry (no open or create).
  7535. RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK);
  7536. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  7537. EXPECT_EQ(1, cache.disk_cache()->open_count());
  7538. EXPECT_EQ(1, cache.disk_cache()->create_count());
  7539. RemoveMockTransaction(&kRangeGET_TransactionOK);
  7540. }
  7541. // A slight variation of the previous test, this time we cancel two requests in
  7542. // a row, making sure that the second is waiting for the entry to be ready.
  7543. TEST_F(HttpCacheTest, RangeGET_Cancel3) {
  7544. MockHttpCache cache;
  7545. AddMockTransaction(&kRangeGET_TransactionOK);
  7546. RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK);
  7547. MockHttpRequest request(kRangeGET_TransactionOK);
  7548. request.load_flags |= LOAD_VALIDATE_CACHE;
  7549. auto c = std::make_unique<Context>();
  7550. int rv = cache.CreateTransaction(&c->trans);
  7551. ASSERT_THAT(rv, IsOk());
  7552. rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
  7553. EXPECT_THAT(rv, IsError(ERR_IO_PENDING));
  7554. rv = c->callback.WaitForResult();
  7555. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  7556. EXPECT_EQ(1, cache.disk_cache()->open_count());
  7557. EXPECT_EQ(1, cache.disk_cache()->create_count());
  7558. // Make sure that we revalidate the entry and read from the cache (a single
  7559. // read will return while waiting for the network).
  7560. scoped_refptr<IOBufferWithSize> buf =
  7561. base::MakeRefCounted<IOBufferWithSize>(5);
  7562. rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback());
  7563. EXPECT_EQ(5, c->callback.GetResult(rv));
  7564. rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback());
  7565. EXPECT_THAT(rv, IsError(ERR_IO_PENDING));
  7566. // Destroy the previous transaction before completing the read.
  7567. c.reset();
  7568. // We have the read and the delete (OnProcessPendingQueue) waiting on the
  7569. // message loop. This means that a new transaction will just reuse the same
  7570. // active entry (no open or create).
  7571. c = std::make_unique<Context>();
  7572. rv = cache.CreateTransaction(&c->trans);
  7573. ASSERT_THAT(rv, IsOk());
  7574. rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
  7575. EXPECT_THAT(rv, IsError(ERR_IO_PENDING));
  7576. MockDiskEntry::IgnoreCallbacks(true);
  7577. base::RunLoop().RunUntilIdle();
  7578. MockDiskEntry::IgnoreCallbacks(false);
  7579. // The new transaction is waiting for the query range callback.
  7580. c.reset();
  7581. // And we should not crash when the callback is delivered.
  7582. base::RunLoop().RunUntilIdle();
  7583. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  7584. EXPECT_EQ(1, cache.disk_cache()->open_count());
  7585. EXPECT_EQ(1, cache.disk_cache()->create_count());
  7586. RemoveMockTransaction(&kRangeGET_TransactionOK);
  7587. }
  7588. // Tests that an invalid range response results in no cached entry.
  7589. TEST_F(HttpCacheTest, RangeGET_InvalidResponse1) {
  7590. MockHttpCache cache;
  7591. std::string headers;
  7592. MockTransaction transaction(kRangeGET_TransactionOK);
  7593. transaction.handler = nullptr;
  7594. transaction.response_headers = "Content-Range: bytes 40-49/45\n"
  7595. "Content-Length: 10\n";
  7596. AddMockTransaction(&transaction);
  7597. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  7598. std::string expected(transaction.status);
  7599. expected.append("\n");
  7600. expected.append(transaction.response_headers);
  7601. EXPECT_EQ(expected, headers);
  7602. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  7603. EXPECT_EQ(0, cache.disk_cache()->open_count());
  7604. EXPECT_EQ(1, cache.disk_cache()->create_count());
  7605. // Verify that we don't have a cached entry.
  7606. disk_cache::Entry* entry;
  7607. MockHttpRequest request(transaction);
  7608. EXPECT_FALSE(cache.OpenBackendEntry(request.CacheKey(), &entry));
  7609. RemoveMockTransaction(&kRangeGET_TransactionOK);
  7610. }
  7611. // Tests that we reject a range that doesn't match the content-length.
  7612. TEST_F(HttpCacheTest, RangeGET_InvalidResponse2) {
  7613. MockHttpCache cache;
  7614. std::string headers;
  7615. MockTransaction transaction(kRangeGET_TransactionOK);
  7616. transaction.handler = nullptr;
  7617. transaction.response_headers = "Content-Range: bytes 40-49/80\n"
  7618. "Content-Length: 20\n";
  7619. AddMockTransaction(&transaction);
  7620. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  7621. std::string expected(transaction.status);
  7622. expected.append("\n");
  7623. expected.append(transaction.response_headers);
  7624. EXPECT_EQ(expected, headers);
  7625. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  7626. EXPECT_EQ(0, cache.disk_cache()->open_count());
  7627. EXPECT_EQ(1, cache.disk_cache()->create_count());
  7628. // Verify that we don't have a cached entry.
  7629. disk_cache::Entry* entry;
  7630. MockHttpRequest request(transaction);
  7631. EXPECT_FALSE(cache.OpenBackendEntry(request.CacheKey(), &entry));
  7632. RemoveMockTransaction(&kRangeGET_TransactionOK);
  7633. }
  7634. // Tests that if a server tells us conflicting information about a resource we
  7635. // drop the entry.
  7636. TEST_F(HttpCacheTest, RangeGET_InvalidResponse3) {
  7637. MockHttpCache cache;
  7638. std::string headers;
  7639. MockTransaction transaction(kRangeGET_TransactionOK);
  7640. transaction.handler = nullptr;
  7641. transaction.request_headers = "Range: bytes = 50-59\r\n" EXTRA_HEADER;
  7642. std::string response_headers(transaction.response_headers);
  7643. response_headers.append("Content-Range: bytes 50-59/160\n");
  7644. transaction.response_headers = response_headers.c_str();
  7645. AddMockTransaction(&transaction);
  7646. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  7647. Verify206Response(headers, 50, 59);
  7648. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  7649. EXPECT_EQ(0, cache.disk_cache()->open_count());
  7650. EXPECT_EQ(1, cache.disk_cache()->create_count());
  7651. RemoveMockTransaction(&transaction);
  7652. AddMockTransaction(&kRangeGET_TransactionOK);
  7653. // This transaction will report a resource size of 80 bytes, and we think it's
  7654. // 160 so we should ignore the response.
  7655. RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
  7656. &headers);
  7657. Verify206Response(headers, 40, 49);
  7658. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  7659. EXPECT_EQ(1, cache.disk_cache()->open_count());
  7660. EXPECT_EQ(1, cache.disk_cache()->create_count());
  7661. // Verify that the entry is gone.
  7662. RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK);
  7663. EXPECT_EQ(1, cache.disk_cache()->open_count());
  7664. EXPECT_EQ(2, cache.disk_cache()->create_count());
  7665. RemoveMockTransaction(&kRangeGET_TransactionOK);
  7666. }
  7667. // Tests that we handle large range values properly.
  7668. TEST_F(HttpCacheTest, RangeGET_LargeValues) {
  7669. // We need a real sparse cache for this test.
  7670. MockHttpCache cache(HttpCache::DefaultBackend::InMemory(1024 * 1024));
  7671. std::string headers;
  7672. MockTransaction transaction(kRangeGET_TransactionOK);
  7673. transaction.handler = nullptr;
  7674. transaction.request_headers = "Range: bytes = 4294967288-4294967297\r\n"
  7675. EXTRA_HEADER;
  7676. transaction.response_headers =
  7677. "ETag: \"foo\"\n"
  7678. "Content-Range: bytes 4294967288-4294967297/4294967299\n"
  7679. "Content-Length: 10\n";
  7680. AddMockTransaction(&transaction);
  7681. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  7682. std::string expected(transaction.status);
  7683. expected.append("\n");
  7684. expected.append(transaction.response_headers);
  7685. EXPECT_EQ(expected, headers);
  7686. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  7687. // Verify that we have a cached entry.
  7688. disk_cache::Entry* en;
  7689. MockHttpRequest request(transaction);
  7690. ASSERT_TRUE(cache.OpenBackendEntry(request.CacheKey(), &en));
  7691. en->Close();
  7692. RemoveMockTransaction(&kRangeGET_TransactionOK);
  7693. }
  7694. // Tests that we don't crash with a range request if the disk cache was not
  7695. // initialized properly.
  7696. TEST_F(HttpCacheTest, RangeGET_NoDiskCache) {
  7697. auto factory = std::make_unique<MockBlockingBackendFactory>();
  7698. factory->set_fail(true);
  7699. factory->FinishCreation(); // We'll complete synchronously.
  7700. MockHttpCache cache(std::move(factory));
  7701. AddMockTransaction(&kRangeGET_TransactionOK);
  7702. RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK);
  7703. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  7704. RemoveMockTransaction(&kRangeGET_TransactionOK);
  7705. }
  7706. // Tests that we handle byte range requests that skip the cache.
  7707. TEST_F(HttpCacheTest, RangeHEAD) {
  7708. MockHttpCache cache;
  7709. AddMockTransaction(&kRangeGET_TransactionOK);
  7710. MockTransaction transaction(kRangeGET_TransactionOK);
  7711. transaction.request_headers = "Range: bytes = -10\r\n" EXTRA_HEADER;
  7712. transaction.method = "HEAD";
  7713. transaction.data = "rg: 70-79 ";
  7714. std::string headers;
  7715. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  7716. Verify206Response(headers, 70, 79);
  7717. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  7718. EXPECT_EQ(0, cache.disk_cache()->open_count());
  7719. EXPECT_EQ(0, cache.disk_cache()->create_count());
  7720. RemoveMockTransaction(&kRangeGET_TransactionOK);
  7721. }
  7722. // Tests that we don't crash when after reading from the cache we issue a
  7723. // request for the next range and the server gives us a 200 synchronously.
  7724. TEST_F(HttpCacheTest, RangeGET_FastFlakyServer) {
  7725. MockHttpCache cache;
  7726. ScopedMockTransaction transaction(kRangeGET_TransactionOK);
  7727. transaction.request_headers = "Range: bytes = 40-\r\n" EXTRA_HEADER;
  7728. transaction.test_mode = TEST_MODE_SYNC_NET_START;
  7729. transaction.load_flags |= LOAD_VALIDATE_CACHE;
  7730. // Write to the cache.
  7731. RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK);
  7732. // And now read from the cache and the network.
  7733. RangeTransactionServer handler;
  7734. handler.set_bad_200(true);
  7735. transaction.data = "Not a range";
  7736. RecordingNetLogObserver net_log_observer;
  7737. RunTransactionTestWithLog(cache.http_cache(), transaction,
  7738. NetLogWithSource::Make(NetLogSourceType::NONE));
  7739. EXPECT_EQ(3, cache.network_layer()->transaction_count());
  7740. EXPECT_EQ(1, cache.disk_cache()->open_count());
  7741. EXPECT_EQ(1, cache.disk_cache()->create_count());
  7742. EXPECT_TRUE(LogContainsEventType(
  7743. net_log_observer, NetLogEventType::HTTP_CACHE_RE_SEND_PARTIAL_REQUEST));
  7744. }
  7745. // Tests that when the server gives us less data than expected, we don't keep
  7746. // asking for more data.
  7747. TEST_F(HttpCacheTest, RangeGET_FastFlakyServer2) {
  7748. MockHttpCache cache;
  7749. // First, check with an empty cache (WRITE mode).
  7750. MockTransaction transaction(kRangeGET_TransactionOK);
  7751. transaction.request_headers = "Range: bytes = 40-49\r\n" EXTRA_HEADER;
  7752. transaction.data = "rg: 40-"; // Less than expected.
  7753. transaction.handler = nullptr;
  7754. std::string headers(transaction.response_headers);
  7755. headers.append("Content-Range: bytes 40-49/80\n");
  7756. transaction.response_headers = headers.c_str();
  7757. AddMockTransaction(&transaction);
  7758. // Write to the cache.
  7759. RunTransactionTest(cache.http_cache(), transaction);
  7760. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  7761. EXPECT_EQ(0, cache.disk_cache()->open_count());
  7762. EXPECT_EQ(1, cache.disk_cache()->create_count());
  7763. // Now verify that even in READ_WRITE mode, we forward the bad response to
  7764. // the caller.
  7765. transaction.request_headers = "Range: bytes = 60-69\r\n" EXTRA_HEADER;
  7766. transaction.data = "rg: 60-"; // Less than expected.
  7767. headers = kRangeGET_TransactionOK.response_headers;
  7768. headers.append("Content-Range: bytes 60-69/80\n");
  7769. transaction.response_headers = headers.c_str();
  7770. RunTransactionTest(cache.http_cache(), transaction);
  7771. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  7772. EXPECT_EQ(1, cache.disk_cache()->open_count());
  7773. EXPECT_EQ(1, cache.disk_cache()->create_count());
  7774. RemoveMockTransaction(&transaction);
  7775. }
  7776. #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
  7777. // This test hits a NOTREACHED so it is a release mode only test.
  7778. TEST_F(HttpCacheTest, RangeGET_OK_LoadOnlyFromCache) {
  7779. MockHttpCache cache;
  7780. AddMockTransaction(&kRangeGET_TransactionOK);
  7781. // Write to the cache (40-49).
  7782. RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK);
  7783. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  7784. EXPECT_EQ(0, cache.disk_cache()->open_count());
  7785. EXPECT_EQ(1, cache.disk_cache()->create_count());
  7786. // Force this transaction to read from the cache.
  7787. MockTransaction transaction(kRangeGET_TransactionOK);
  7788. transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
  7789. MockHttpRequest request(transaction);
  7790. TestCompletionCallback callback;
  7791. std::unique_ptr<HttpTransaction> trans;
  7792. int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans);
  7793. EXPECT_THAT(rv, IsOk());
  7794. ASSERT_TRUE(trans.get());
  7795. rv = trans->Start(&request, callback.callback(), NetLogWithSource());
  7796. if (rv == ERR_IO_PENDING)
  7797. rv = callback.WaitForResult();
  7798. ASSERT_THAT(rv, IsError(ERR_CACHE_MISS));
  7799. trans.reset();
  7800. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  7801. EXPECT_EQ(1, cache.disk_cache()->open_count());
  7802. EXPECT_EQ(1, cache.disk_cache()->create_count());
  7803. RemoveMockTransaction(&kRangeGET_TransactionOK);
  7804. }
  7805. #endif
  7806. // Tests the handling of the "truncation" flag.
  7807. TEST_F(HttpCacheTest, WriteResponseInfo_Truncated) {
  7808. MockHttpCache cache;
  7809. disk_cache::Entry* entry;
  7810. ASSERT_TRUE(cache.CreateBackendEntry(
  7811. GenerateCacheKey("http://www.google.com"), &entry, nullptr));
  7812. HttpResponseInfo response;
  7813. response.headers = base::MakeRefCounted<HttpResponseHeaders>(
  7814. HttpUtil::AssembleRawHeaders("HTTP/1.1 200 OK"));
  7815. // Set the last argument for this to be an incomplete request.
  7816. EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, true));
  7817. bool truncated = false;
  7818. EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated));
  7819. EXPECT_TRUE(truncated);
  7820. // And now test the opposite case.
  7821. EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, false));
  7822. truncated = true;
  7823. EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated));
  7824. EXPECT_FALSE(truncated);
  7825. entry->Close();
  7826. }
  7827. // Tests basic pickling/unpickling of HttpResponseInfo.
  7828. TEST_F(HttpCacheTest, PersistHttpResponseInfo) {
  7829. const IPEndPoint expected_endpoint = IPEndPoint(IPAddress(1, 2, 3, 4), 80);
  7830. // Set some fields (add more if needed.)
  7831. HttpResponseInfo response1;
  7832. response1.was_cached = false;
  7833. response1.remote_endpoint = expected_endpoint;
  7834. response1.headers =
  7835. base::MakeRefCounted<HttpResponseHeaders>("HTTP/1.1 200 OK");
  7836. // Pickle.
  7837. base::Pickle pickle;
  7838. response1.Persist(&pickle, false, false);
  7839. // Unpickle.
  7840. HttpResponseInfo response2;
  7841. bool response_truncated;
  7842. EXPECT_TRUE(response2.InitFromPickle(pickle, &response_truncated));
  7843. EXPECT_FALSE(response_truncated);
  7844. // Verify fields.
  7845. EXPECT_TRUE(response2.was_cached); // InitFromPickle sets this flag.
  7846. EXPECT_EQ(expected_endpoint, response2.remote_endpoint);
  7847. EXPECT_EQ("HTTP/1.1 200 OK", response2.headers->GetStatusLine());
  7848. }
  7849. // Tests that we delete an entry when the request is cancelled before starting
  7850. // to read from the network.
  7851. TEST_F(HttpCacheTest, DoomOnDestruction) {
  7852. MockHttpCache cache;
  7853. MockHttpRequest request(kSimpleGET_Transaction);
  7854. auto c = std::make_unique<Context>();
  7855. int rv = cache.CreateTransaction(&c->trans);
  7856. ASSERT_THAT(rv, IsOk());
  7857. rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
  7858. if (rv == ERR_IO_PENDING)
  7859. c->result = c->callback.WaitForResult();
  7860. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  7861. EXPECT_EQ(0, cache.disk_cache()->open_count());
  7862. EXPECT_EQ(1, cache.disk_cache()->create_count());
  7863. // Destroy the transaction. We only have the headers so we should delete this
  7864. // entry.
  7865. c.reset();
  7866. RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
  7867. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  7868. EXPECT_EQ(0, cache.disk_cache()->open_count());
  7869. EXPECT_EQ(2, cache.disk_cache()->create_count());
  7870. }
  7871. // Tests that we delete an entry when the request is cancelled if the response
  7872. // does not have content-length and strong validators.
  7873. TEST_F(HttpCacheTest, DoomOnDestruction2) {
  7874. MockHttpCache cache;
  7875. MockHttpRequest request(kSimpleGET_Transaction);
  7876. auto c = std::make_unique<Context>();
  7877. int rv = cache.CreateTransaction(&c->trans);
  7878. ASSERT_THAT(rv, IsOk());
  7879. rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
  7880. if (rv == ERR_IO_PENDING)
  7881. rv = c->callback.WaitForResult();
  7882. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  7883. EXPECT_EQ(0, cache.disk_cache()->open_count());
  7884. EXPECT_EQ(1, cache.disk_cache()->create_count());
  7885. // Make sure that the entry has some data stored.
  7886. scoped_refptr<IOBufferWithSize> buf =
  7887. base::MakeRefCounted<IOBufferWithSize>(10);
  7888. rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback());
  7889. if (rv == ERR_IO_PENDING)
  7890. rv = c->callback.WaitForResult();
  7891. EXPECT_EQ(buf->size(), rv);
  7892. // Destroy the transaction.
  7893. c.reset();
  7894. RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
  7895. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  7896. EXPECT_EQ(0, cache.disk_cache()->open_count());
  7897. EXPECT_EQ(2, cache.disk_cache()->create_count());
  7898. }
  7899. // Tests that we delete an entry when the request is cancelled if the response
  7900. // has an "Accept-Ranges: none" header.
  7901. TEST_F(HttpCacheTest, DoomOnDestruction3) {
  7902. MockHttpCache cache;
  7903. MockTransaction transaction(kSimpleGET_Transaction);
  7904. transaction.response_headers =
  7905. "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n"
  7906. "Content-Length: 22\n"
  7907. "Accept-Ranges: none\n"
  7908. "Etag: \"foopy\"\n";
  7909. AddMockTransaction(&transaction);
  7910. MockHttpRequest request(transaction);
  7911. auto c = std::make_unique<Context>();
  7912. int rv = cache.CreateTransaction(&c->trans);
  7913. ASSERT_THAT(rv, IsOk());
  7914. rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
  7915. if (rv == ERR_IO_PENDING)
  7916. rv = c->callback.WaitForResult();
  7917. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  7918. EXPECT_EQ(0, cache.disk_cache()->open_count());
  7919. EXPECT_EQ(1, cache.disk_cache()->create_count());
  7920. // Make sure that the entry has some data stored.
  7921. scoped_refptr<IOBufferWithSize> buf =
  7922. base::MakeRefCounted<IOBufferWithSize>(10);
  7923. rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback());
  7924. if (rv == ERR_IO_PENDING)
  7925. rv = c->callback.WaitForResult();
  7926. EXPECT_EQ(buf->size(), rv);
  7927. // Destroy the transaction.
  7928. c.reset();
  7929. RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
  7930. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  7931. EXPECT_EQ(0, cache.disk_cache()->open_count());
  7932. EXPECT_EQ(2, cache.disk_cache()->create_count());
  7933. RemoveMockTransaction(&transaction);
  7934. }
  7935. // Tests that we mark an entry as incomplete when the request is cancelled.
  7936. TEST_F(HttpCacheTest, SetTruncatedFlag) {
  7937. MockHttpCache cache;
  7938. ScopedMockTransaction transaction(kSimpleGET_Transaction);
  7939. transaction.response_headers =
  7940. "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n"
  7941. "Content-Length: 22\n"
  7942. "Etag: \"foopy\"\n";
  7943. MockHttpRequest request(transaction);
  7944. auto c = std::make_unique<Context>();
  7945. int rv = cache.CreateTransaction(&c->trans);
  7946. ASSERT_THAT(rv, IsOk());
  7947. rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
  7948. if (rv == ERR_IO_PENDING)
  7949. rv = c->callback.WaitForResult();
  7950. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  7951. EXPECT_EQ(0, cache.disk_cache()->open_count());
  7952. EXPECT_EQ(1, cache.disk_cache()->create_count());
  7953. // Make sure that the entry has some data stored.
  7954. scoped_refptr<IOBufferWithSize> buf =
  7955. base::MakeRefCounted<IOBufferWithSize>(10);
  7956. rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback());
  7957. if (rv == ERR_IO_PENDING)
  7958. rv = c->callback.WaitForResult();
  7959. EXPECT_EQ(buf->size(), rv);
  7960. // We want to cancel the request when the transaction is busy.
  7961. rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback());
  7962. EXPECT_THAT(rv, IsError(ERR_IO_PENDING));
  7963. EXPECT_FALSE(c->callback.have_result());
  7964. // Destroy the transaction.
  7965. c->trans.reset();
  7966. // Make sure that we don't invoke the callback. We may have an issue if the
  7967. // UrlRequestJob is killed directly (without cancelling the UrlRequest) so we
  7968. // could end up with the transaction being deleted twice if we send any
  7969. // notification from the transaction destructor (see http://crbug.com/31723).
  7970. EXPECT_FALSE(c->callback.have_result());
  7971. base::RunLoop().RunUntilIdle();
  7972. VerifyTruncatedFlag(&cache, request.CacheKey(), true, 0);
  7973. }
  7974. // Tests that we do not mark an entry as truncated when the request is
  7975. // cancelled.
  7976. TEST_F(HttpCacheTest, DontSetTruncatedFlagForGarbledResponseCode) {
  7977. MockHttpCache cache;
  7978. ScopedMockTransaction transaction(kSimpleGET_Transaction);
  7979. transaction.response_headers =
  7980. "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n"
  7981. "Content-Length: 22\n"
  7982. "Etag: \"foopy\"\n";
  7983. transaction.status = "HTTP/1.1 2";
  7984. MockHttpRequest request(transaction);
  7985. auto c = std::make_unique<Context>();
  7986. int rv = cache.CreateTransaction(&c->trans);
  7987. ASSERT_THAT(rv, IsOk());
  7988. rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
  7989. if (rv == ERR_IO_PENDING)
  7990. rv = c->callback.WaitForResult();
  7991. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  7992. EXPECT_EQ(0, cache.disk_cache()->open_count());
  7993. EXPECT_EQ(1, cache.disk_cache()->create_count());
  7994. // Make sure that the entry has some data stored.
  7995. scoped_refptr<IOBufferWithSize> buf =
  7996. base::MakeRefCounted<IOBufferWithSize>(10);
  7997. rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback());
  7998. if (rv == ERR_IO_PENDING)
  7999. rv = c->callback.WaitForResult();
  8000. EXPECT_EQ(buf->size(), rv);
  8001. // We want to cancel the request when the transaction is busy.
  8002. rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback());
  8003. EXPECT_THAT(rv, IsError(ERR_IO_PENDING));
  8004. EXPECT_FALSE(c->callback.have_result());
  8005. MockHttpCache::SetTestMode(TEST_MODE_SYNC_ALL);
  8006. // Destroy the transaction.
  8007. c->trans.reset();
  8008. MockHttpCache::SetTestMode(0);
  8009. // Make sure that we don't invoke the callback. We may have an issue if the
  8010. // UrlRequestJob is killed directly (without cancelling the UrlRequest) so we
  8011. // could end up with the transaction being deleted twice if we send any
  8012. // notification from the transaction destructor (see http://crbug.com/31723).
  8013. EXPECT_FALSE(c->callback.have_result());
  8014. // Verify that the entry is deleted as well, since the response status is
  8015. // garbled. Note that the entry will be deleted after the pending Read is
  8016. // complete.
  8017. base::RunLoop().RunUntilIdle();
  8018. disk_cache::Entry* entry;
  8019. ASSERT_FALSE(cache.OpenBackendEntry(request.CacheKey(), &entry));
  8020. }
  8021. // Tests that we don't mark an entry as truncated when we read everything.
  8022. TEST_F(HttpCacheTest, DontSetTruncatedFlag) {
  8023. MockHttpCache cache;
  8024. ScopedMockTransaction transaction(kSimpleGET_Transaction);
  8025. transaction.response_headers =
  8026. "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n"
  8027. "Content-Length: 22\n"
  8028. "Etag: \"foopy\"\n";
  8029. MockHttpRequest request(transaction);
  8030. auto c = std::make_unique<Context>();
  8031. int rv = cache.CreateTransaction(&c->trans);
  8032. ASSERT_THAT(rv, IsOk());
  8033. rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
  8034. EXPECT_THAT(c->callback.GetResult(rv), IsOk());
  8035. // Read everything.
  8036. scoped_refptr<IOBufferWithSize> buf =
  8037. base::MakeRefCounted<IOBufferWithSize>(22);
  8038. rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback());
  8039. EXPECT_EQ(buf->size(), c->callback.GetResult(rv));
  8040. // Destroy the transaction.
  8041. c->trans.reset();
  8042. // Verify that the entry is not marked as truncated.
  8043. VerifyTruncatedFlag(&cache, request.CacheKey(), false, 0);
  8044. }
  8045. // Tests that sparse entries don't set the truncate flag.
  8046. TEST_F(HttpCacheTest, RangeGET_DontTruncate) {
  8047. MockHttpCache cache;
  8048. ScopedMockTransaction transaction(kRangeGET_TransactionOK);
  8049. transaction.request_headers = "Range: bytes = 0-19\r\n" EXTRA_HEADER;
  8050. auto request = std::make_unique<MockHttpRequest>(transaction);
  8051. std::unique_ptr<HttpTransaction> trans;
  8052. int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans);
  8053. EXPECT_THAT(rv, IsOk());
  8054. TestCompletionCallback cb;
  8055. rv = trans->Start(request.get(), cb.callback(), NetLogWithSource());
  8056. EXPECT_EQ(0, cb.GetResult(rv));
  8057. scoped_refptr<IOBuffer> buf = base::MakeRefCounted<IOBuffer>(10);
  8058. rv = trans->Read(buf.get(), 10, cb.callback());
  8059. EXPECT_EQ(10, cb.GetResult(rv));
  8060. // Should not trigger any DCHECK.
  8061. trans.reset();
  8062. VerifyTruncatedFlag(&cache, request->CacheKey(), false, 0);
  8063. }
  8064. // Tests that sparse entries don't set the truncate flag (when the byte range
  8065. // starts after 0).
  8066. TEST_F(HttpCacheTest, RangeGET_DontTruncate2) {
  8067. MockHttpCache cache;
  8068. ScopedMockTransaction transaction(kRangeGET_TransactionOK);
  8069. transaction.request_headers = "Range: bytes = 30-49\r\n" EXTRA_HEADER;
  8070. auto request = std::make_unique<MockHttpRequest>(transaction);
  8071. std::unique_ptr<HttpTransaction> trans;
  8072. int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans);
  8073. EXPECT_THAT(rv, IsOk());
  8074. TestCompletionCallback cb;
  8075. rv = trans->Start(request.get(), cb.callback(), NetLogWithSource());
  8076. EXPECT_EQ(0, cb.GetResult(rv));
  8077. scoped_refptr<IOBuffer> buf = base::MakeRefCounted<IOBuffer>(10);
  8078. rv = trans->Read(buf.get(), 10, cb.callback());
  8079. EXPECT_EQ(10, cb.GetResult(rv));
  8080. // Should not trigger any DCHECK.
  8081. trans.reset();
  8082. VerifyTruncatedFlag(&cache, request->CacheKey(), false, 0);
  8083. }
  8084. // Tests that we can continue with a request that was interrupted.
  8085. TEST_F(HttpCacheTest, GET_IncompleteResource) {
  8086. MockHttpCache cache;
  8087. ScopedMockTransaction transaction(kRangeGET_TransactionOK);
  8088. std::string raw_headers("HTTP/1.1 200 OK\n"
  8089. "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
  8090. "ETag: \"foo\"\n"
  8091. "Accept-Ranges: bytes\n"
  8092. "Content-Length: 80\n");
  8093. CreateTruncatedEntry(raw_headers, &cache);
  8094. // Now make a regular request.
  8095. std::string headers;
  8096. transaction.request_headers = EXTRA_HEADER;
  8097. transaction.data = kFullRangeData;
  8098. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  8099. // We update the headers with the ones received while revalidating.
  8100. std::string expected_headers(
  8101. "HTTP/1.1 200 OK\n"
  8102. "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
  8103. "Accept-Ranges: bytes\n"
  8104. "ETag: \"foo\"\n"
  8105. "Content-Length: 80\n");
  8106. EXPECT_EQ(expected_headers, headers);
  8107. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  8108. EXPECT_EQ(1, cache.disk_cache()->open_count());
  8109. EXPECT_EQ(1, cache.disk_cache()->create_count());
  8110. // Verify that the disk entry was updated.
  8111. MockHttpRequest request(transaction);
  8112. VerifyTruncatedFlag(&cache, request.CacheKey(), false, 80);
  8113. }
  8114. // Tests the handling of no-store when revalidating a truncated entry.
  8115. TEST_F(HttpCacheTest, GET_IncompleteResource_NoStore) {
  8116. MockHttpCache cache;
  8117. AddMockTransaction(&kRangeGET_TransactionOK);
  8118. std::string raw_headers("HTTP/1.1 200 OK\n"
  8119. "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
  8120. "ETag: \"foo\"\n"
  8121. "Accept-Ranges: bytes\n"
  8122. "Content-Length: 80\n");
  8123. CreateTruncatedEntry(raw_headers, &cache);
  8124. RemoveMockTransaction(&kRangeGET_TransactionOK);
  8125. // Now make a regular request.
  8126. MockTransaction transaction(kRangeGET_TransactionOK);
  8127. transaction.request_headers = EXTRA_HEADER;
  8128. std::string response_headers(transaction.response_headers);
  8129. response_headers += ("Cache-Control: no-store\n");
  8130. transaction.response_headers = response_headers.c_str();
  8131. transaction.data = kFullRangeData;
  8132. AddMockTransaction(&transaction);
  8133. std::string headers;
  8134. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  8135. // We update the headers with the ones received while revalidating.
  8136. std::string expected_headers(
  8137. "HTTP/1.1 200 OK\n"
  8138. "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
  8139. "Accept-Ranges: bytes\n"
  8140. "Cache-Control: no-store\n"
  8141. "ETag: \"foo\"\n"
  8142. "Content-Length: 80\n");
  8143. EXPECT_EQ(expected_headers, headers);
  8144. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  8145. EXPECT_EQ(1, cache.disk_cache()->open_count());
  8146. EXPECT_EQ(1, cache.disk_cache()->create_count());
  8147. // Verify that the disk entry was deleted.
  8148. disk_cache::Entry* entry;
  8149. MockHttpRequest request(transaction);
  8150. EXPECT_FALSE(cache.OpenBackendEntry(request.CacheKey(), &entry));
  8151. RemoveMockTransaction(&transaction);
  8152. }
  8153. // Tests cancelling a request after the server sent no-store.
  8154. TEST_F(HttpCacheTest, GET_IncompleteResource_Cancel) {
  8155. MockHttpCache cache;
  8156. AddMockTransaction(&kRangeGET_TransactionOK);
  8157. std::string raw_headers("HTTP/1.1 200 OK\n"
  8158. "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
  8159. "ETag: \"foo\"\n"
  8160. "Accept-Ranges: bytes\n"
  8161. "Content-Length: 80\n");
  8162. CreateTruncatedEntry(raw_headers, &cache);
  8163. RemoveMockTransaction(&kRangeGET_TransactionOK);
  8164. // Now make a regular request.
  8165. MockTransaction transaction(kRangeGET_TransactionOK);
  8166. transaction.request_headers = EXTRA_HEADER;
  8167. std::string response_headers(transaction.response_headers);
  8168. response_headers += ("Cache-Control: no-store\n");
  8169. transaction.response_headers = response_headers.c_str();
  8170. transaction.data = kFullRangeData;
  8171. AddMockTransaction(&transaction);
  8172. MockHttpRequest request(transaction);
  8173. auto c = std::make_unique<Context>();
  8174. int rv = cache.CreateTransaction(&c->trans);
  8175. ASSERT_THAT(rv, IsOk());
  8176. // Queue another request to this transaction. We have to start this request
  8177. // before the first one gets the response from the server and dooms the entry,
  8178. // otherwise it will just create a new entry without being queued to the first
  8179. // request.
  8180. auto pending = std::make_unique<Context>();
  8181. ASSERT_THAT(cache.CreateTransaction(&pending->trans), IsOk());
  8182. rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
  8183. EXPECT_EQ(ERR_IO_PENDING,
  8184. pending->trans->Start(&request, pending->callback.callback(),
  8185. NetLogWithSource()));
  8186. EXPECT_THAT(c->callback.GetResult(rv), IsOk());
  8187. // Make sure that the entry has some data stored.
  8188. scoped_refptr<IOBufferWithSize> buf =
  8189. base::MakeRefCounted<IOBufferWithSize>(5);
  8190. rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback());
  8191. EXPECT_EQ(5, c->callback.GetResult(rv));
  8192. // Since |pending| is currently validating the already written headers
  8193. // it will be restarted as well.
  8194. c.reset();
  8195. pending.reset();
  8196. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  8197. EXPECT_EQ(1, cache.disk_cache()->open_count());
  8198. EXPECT_EQ(1, cache.disk_cache()->create_count());
  8199. base::RunLoop().RunUntilIdle();
  8200. RemoveMockTransaction(&transaction);
  8201. }
  8202. // Tests that we delete truncated entries if the server changes its mind midway.
  8203. TEST_F(HttpCacheTest, GET_IncompleteResource2) {
  8204. MockHttpCache cache;
  8205. AddMockTransaction(&kRangeGET_TransactionOK);
  8206. // Content-length will be intentionally bad.
  8207. std::string raw_headers("HTTP/1.1 200 OK\n"
  8208. "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
  8209. "ETag: \"foo\"\n"
  8210. "Accept-Ranges: bytes\n"
  8211. "Content-Length: 50\n");
  8212. CreateTruncatedEntry(raw_headers, &cache);
  8213. // Now make a regular request. We expect the code to fail the validation and
  8214. // retry the request without using byte ranges.
  8215. std::string headers;
  8216. MockTransaction transaction(kRangeGET_TransactionOK);
  8217. transaction.request_headers = EXTRA_HEADER;
  8218. transaction.data = "Not a range";
  8219. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  8220. // The server will return 200 instead of a byte range.
  8221. std::string expected_headers(
  8222. "HTTP/1.1 200 OK\n"
  8223. "Date: Wed, 28 Nov 2007 09:40:09 GMT\n");
  8224. EXPECT_EQ(expected_headers, headers);
  8225. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  8226. EXPECT_EQ(1, cache.disk_cache()->open_count());
  8227. EXPECT_EQ(1, cache.disk_cache()->create_count());
  8228. // Verify that the disk entry was deleted.
  8229. disk_cache::Entry* entry;
  8230. MockHttpRequest request(transaction);
  8231. ASSERT_FALSE(cache.OpenBackendEntry(request.CacheKey(), &entry));
  8232. RemoveMockTransaction(&kRangeGET_TransactionOK);
  8233. }
  8234. // Tests that we always validate a truncated request.
  8235. TEST_F(HttpCacheTest, GET_IncompleteResource3) {
  8236. MockHttpCache cache;
  8237. AddMockTransaction(&kRangeGET_TransactionOK);
  8238. // This should not require validation for 10 hours.
  8239. std::string raw_headers("HTTP/1.1 200 OK\n"
  8240. "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n"
  8241. "ETag: \"foo\"\n"
  8242. "Cache-Control: max-age= 36000\n"
  8243. "Accept-Ranges: bytes\n"
  8244. "Content-Length: 80\n");
  8245. CreateTruncatedEntry(raw_headers, &cache);
  8246. // Now make a regular request.
  8247. std::string headers;
  8248. MockTransaction transaction(kRangeGET_TransactionOK);
  8249. transaction.request_headers = EXTRA_HEADER;
  8250. transaction.data = kFullRangeData;
  8251. auto c = std::make_unique<Context>();
  8252. int rv = cache.CreateTransaction(&c->trans);
  8253. ASSERT_THAT(rv, IsOk());
  8254. MockHttpRequest request(transaction);
  8255. rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
  8256. EXPECT_THAT(c->callback.GetResult(rv), IsOk());
  8257. // We should have checked with the server before finishing Start().
  8258. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  8259. EXPECT_EQ(1, cache.disk_cache()->open_count());
  8260. EXPECT_EQ(1, cache.disk_cache()->create_count());
  8261. RemoveMockTransaction(&kRangeGET_TransactionOK);
  8262. }
  8263. // Tests that we handle 401s for truncated resources.
  8264. TEST_F(HttpCacheTest, GET_IncompleteResourceWithAuth) {
  8265. MockHttpCache cache;
  8266. AddMockTransaction(&kRangeGET_TransactionOK);
  8267. std::string raw_headers("HTTP/1.1 200 OK\n"
  8268. "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
  8269. "ETag: \"foo\"\n"
  8270. "Accept-Ranges: bytes\n"
  8271. "Content-Length: 80\n");
  8272. CreateTruncatedEntry(raw_headers, &cache);
  8273. // Now make a regular request.
  8274. MockTransaction transaction(kRangeGET_TransactionOK);
  8275. transaction.request_headers = "X-Require-Mock-Auth: dummy\r\n"
  8276. EXTRA_HEADER;
  8277. transaction.data = kFullRangeData;
  8278. RangeTransactionServer handler;
  8279. auto c = std::make_unique<Context>();
  8280. int rv = cache.CreateTransaction(&c->trans);
  8281. ASSERT_THAT(rv, IsOk());
  8282. MockHttpRequest request(transaction);
  8283. rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
  8284. EXPECT_THAT(c->callback.GetResult(rv), IsOk());
  8285. const HttpResponseInfo* response = c->trans->GetResponseInfo();
  8286. ASSERT_TRUE(response);
  8287. ASSERT_EQ(401, response->headers->response_code());
  8288. rv = c->trans->RestartWithAuth(AuthCredentials(), c->callback.callback());
  8289. EXPECT_THAT(c->callback.GetResult(rv), IsOk());
  8290. response = c->trans->GetResponseInfo();
  8291. ASSERT_TRUE(response);
  8292. ASSERT_EQ(200, response->headers->response_code());
  8293. ReadAndVerifyTransaction(c->trans.get(), transaction);
  8294. c.reset(); // The destructor could delete the entry.
  8295. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  8296. // Verify that the entry was deleted.
  8297. disk_cache::Entry* entry;
  8298. ASSERT_TRUE(cache.OpenBackendEntry(request.CacheKey(), &entry));
  8299. entry->Close();
  8300. RemoveMockTransaction(&kRangeGET_TransactionOK);
  8301. }
  8302. // Test that the transaction won't retry failed partial requests
  8303. // after it starts reading data. http://crbug.com/474835
  8304. TEST_F(HttpCacheTest, TransactionRetryLimit) {
  8305. MockHttpCache cache;
  8306. // Cache 0-9, so that we have data to read before failing.
  8307. ScopedMockTransaction transaction(kRangeGET_TransactionOK);
  8308. transaction.request_headers = "Range: bytes = 0-9\r\n" EXTRA_HEADER;
  8309. transaction.data = "rg: 00-09 ";
  8310. // Write to the cache.
  8311. RunTransactionTest(cache.http_cache(), transaction);
  8312. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  8313. // And now read from the cache and the network. 10-19 will get a
  8314. // 401, but will have already returned 0-9.
  8315. // We do not set X-Require-Mock-Auth because that causes the mock
  8316. // network transaction to become IsReadyToRestartForAuth().
  8317. transaction.request_headers =
  8318. "Range: bytes = 0-79\r\n"
  8319. "X-Require-Mock-Auth-Alt: dummy\r\n" EXTRA_HEADER;
  8320. auto c = std::make_unique<Context>();
  8321. int rv = cache.CreateTransaction(&c->trans);
  8322. ASSERT_THAT(rv, IsOk());
  8323. MockHttpRequest request(transaction);
  8324. rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
  8325. if (rv == ERR_IO_PENDING)
  8326. rv = c->callback.WaitForResult();
  8327. std::string content;
  8328. rv = ReadTransaction(c->trans.get(), &content);
  8329. EXPECT_THAT(rv, IsError(ERR_CACHE_AUTH_FAILURE_AFTER_READ));
  8330. }
  8331. // Tests that we cache a 200 response to the validation request.
  8332. TEST_F(HttpCacheTest, GET_IncompleteResource4) {
  8333. MockHttpCache cache;
  8334. ScopedMockTransaction transaction(kRangeGET_TransactionOK);
  8335. std::string raw_headers("HTTP/1.1 200 OK\n"
  8336. "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n"
  8337. "ETag: \"foo\"\n"
  8338. "Accept-Ranges: bytes\n"
  8339. "Content-Length: 80\n");
  8340. CreateTruncatedEntry(raw_headers, &cache);
  8341. // Now make a regular request.
  8342. std::string headers;
  8343. transaction.request_headers = EXTRA_HEADER;
  8344. transaction.data = "Not a range";
  8345. RangeTransactionServer handler;
  8346. handler.set_bad_200(true);
  8347. RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
  8348. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  8349. EXPECT_EQ(1, cache.disk_cache()->open_count());
  8350. EXPECT_EQ(1, cache.disk_cache()->create_count());
  8351. // Verify that the disk entry was updated.
  8352. MockHttpRequest request(transaction);
  8353. VerifyTruncatedFlag(&cache, request.CacheKey(), false, 11);
  8354. }
  8355. // Tests that when we cancel a request that was interrupted, we mark it again
  8356. // as truncated.
  8357. TEST_F(HttpCacheTest, GET_CancelIncompleteResource) {
  8358. MockHttpCache cache;
  8359. ScopedMockTransaction transaction(kRangeGET_TransactionOK);
  8360. std::string raw_headers("HTTP/1.1 200 OK\n"
  8361. "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n"
  8362. "ETag: \"foo\"\n"
  8363. "Accept-Ranges: bytes\n"
  8364. "Content-Length: 80\n");
  8365. CreateTruncatedEntry(raw_headers, &cache);
  8366. // Now make a regular request.
  8367. transaction.request_headers = EXTRA_HEADER;
  8368. MockHttpRequest request(transaction);
  8369. auto c = std::make_unique<Context>();
  8370. int rv = cache.CreateTransaction(&c->trans);
  8371. ASSERT_THAT(rv, IsOk());
  8372. rv = c->trans->Start(&request, c->callback.callback(), NetLogWithSource());
  8373. EXPECT_THAT(c->callback.GetResult(rv), IsOk());
  8374. // Read 20 bytes from the cache, and 10 from the net.
  8375. scoped_refptr<IOBuffer> buf = base::MakeRefCounted<IOBuffer>(100);
  8376. rv = c->trans->Read(buf.get(), 20, c->callback.callback());
  8377. EXPECT_EQ(20, c->callback.GetResult(rv));
  8378. rv = c->trans->Read(buf.get(), 10, c->callback.callback());
  8379. EXPECT_EQ(10, c->callback.GetResult(rv));
  8380. // At this point, we are already reading so canceling the request should leave
  8381. // a truncated one.
  8382. c.reset();
  8383. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  8384. EXPECT_EQ(1, cache.disk_cache()->open_count());
  8385. EXPECT_EQ(1, cache.disk_cache()->create_count());
  8386. // Verify that the disk entry was updated: now we have 30 bytes.
  8387. VerifyTruncatedFlag(&cache, request.CacheKey(), true, 30);
  8388. }
  8389. // Tests that we can handle range requests when we have a truncated entry.
  8390. TEST_F(HttpCacheTest, RangeGET_IncompleteResource) {
  8391. MockHttpCache cache;
  8392. AddMockTransaction(&kRangeGET_TransactionOK);
  8393. // Content-length will be intentionally bogus.
  8394. std::string raw_headers("HTTP/1.1 200 OK\n"
  8395. "Last-Modified: something\n"
  8396. "ETag: \"foo\"\n"
  8397. "Accept-Ranges: bytes\n"
  8398. "Content-Length: 10\n");
  8399. CreateTruncatedEntry(raw_headers, &cache);
  8400. // Now make a range request.
  8401. std::string headers;
  8402. RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK,
  8403. &headers);
  8404. Verify206Response(headers, 40, 49);
  8405. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  8406. EXPECT_EQ(1, cache.disk_cache()->open_count());
  8407. EXPECT_EQ(2, cache.disk_cache()->create_count());
  8408. RemoveMockTransaction(&kRangeGET_TransactionOK);
  8409. }
  8410. TEST_F(HttpCacheTest, SyncRead) {
  8411. MockHttpCache cache;
  8412. // This test ensures that a read that completes synchronously does not cause
  8413. // any problems.
  8414. ScopedMockTransaction transaction(kSimpleGET_Transaction);
  8415. transaction.test_mode |= (TEST_MODE_SYNC_CACHE_START |
  8416. TEST_MODE_SYNC_CACHE_READ |
  8417. TEST_MODE_SYNC_CACHE_WRITE);
  8418. MockHttpRequest r1(transaction),
  8419. r2(transaction),
  8420. r3(transaction);
  8421. TestTransactionConsumer c1(DEFAULT_PRIORITY, cache.http_cache()),
  8422. c2(DEFAULT_PRIORITY, cache.http_cache()),
  8423. c3(DEFAULT_PRIORITY, cache.http_cache());
  8424. c1.Start(&r1, NetLogWithSource());
  8425. r2.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
  8426. c2.Start(&r2, NetLogWithSource());
  8427. r3.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
  8428. c3.Start(&r3, NetLogWithSource());
  8429. base::RunLoop().Run();
  8430. EXPECT_TRUE(c1.is_done());
  8431. EXPECT_TRUE(c2.is_done());
  8432. EXPECT_TRUE(c3.is_done());
  8433. EXPECT_THAT(c1.error(), IsOk());
  8434. EXPECT_THAT(c2.error(), IsOk());
  8435. EXPECT_THAT(c3.error(), IsOk());
  8436. }
  8437. TEST_F(HttpCacheTest, ValidationResultsIn200) {
  8438. MockHttpCache cache;
  8439. // This test ensures that a conditional request, which results in a 200
  8440. // instead of a 304, properly truncates the existing response data.
  8441. // write to the cache
  8442. RunTransactionTest(cache.http_cache(), kETagGET_Transaction);
  8443. // force this transaction to validate the cache
  8444. MockTransaction transaction(kETagGET_Transaction);
  8445. transaction.load_flags |= LOAD_VALIDATE_CACHE;
  8446. RunTransactionTest(cache.http_cache(), transaction);
  8447. // read from the cache
  8448. RunTransactionTest(cache.http_cache(), kETagGET_Transaction);
  8449. }
  8450. TEST_F(HttpCacheTest, CachedRedirect) {
  8451. MockHttpCache cache;
  8452. ScopedMockTransaction kTestTransaction(kSimpleGET_Transaction);
  8453. kTestTransaction.status = "HTTP/1.1 301 Moved Permanently";
  8454. kTestTransaction.response_headers = "Location: http://www.bar.com/\n";
  8455. MockHttpRequest request(kTestTransaction);
  8456. TestCompletionCallback callback;
  8457. // Write to the cache.
  8458. {
  8459. std::unique_ptr<HttpTransaction> trans;
  8460. ASSERT_THAT(cache.CreateTransaction(&trans), IsOk());
  8461. int rv = trans->Start(&request, callback.callback(), NetLogWithSource());
  8462. if (rv == ERR_IO_PENDING)
  8463. rv = callback.WaitForResult();
  8464. ASSERT_THAT(rv, IsOk());
  8465. const HttpResponseInfo* info = trans->GetResponseInfo();
  8466. ASSERT_TRUE(info);
  8467. EXPECT_EQ(info->headers->response_code(), 301);
  8468. std::string location;
  8469. info->headers->EnumerateHeader(nullptr, "Location", &location);
  8470. EXPECT_EQ(location, "http://www.bar.com/");
  8471. // Mark the transaction as completed so it is cached.
  8472. trans->DoneReading();
  8473. // Destroy transaction when going out of scope. We have not actually
  8474. // read the response body -- want to test that it is still getting cached.
  8475. }
  8476. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  8477. EXPECT_EQ(0, cache.disk_cache()->open_count());
  8478. EXPECT_EQ(1, cache.disk_cache()->create_count());
  8479. // Active entries in the cache are not retired synchronously. Make
  8480. // sure the next run hits the MockHttpCache and open_count is
  8481. // correct.
  8482. base::RunLoop().RunUntilIdle();
  8483. // Read from the cache.
  8484. {
  8485. std::unique_ptr<HttpTransaction> trans;
  8486. ASSERT_THAT(cache.CreateTransaction(&trans), IsOk());
  8487. int rv = trans->Start(&request, callback.callback(), NetLogWithSource());
  8488. if (rv == ERR_IO_PENDING)
  8489. rv = callback.WaitForResult();
  8490. ASSERT_THAT(rv, IsOk());
  8491. const HttpResponseInfo* info = trans->GetResponseInfo();
  8492. ASSERT_TRUE(info);
  8493. EXPECT_EQ(info->headers->response_code(), 301);
  8494. std::string location;
  8495. info->headers->EnumerateHeader(nullptr, "Location", &location);
  8496. EXPECT_EQ(location, "http://www.bar.com/");
  8497. // Mark the transaction as completed so it is cached.
  8498. trans->DoneReading();
  8499. // Destroy transaction when going out of scope. We have not actually
  8500. // read the response body -- want to test that it is still getting cached.
  8501. }
  8502. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  8503. EXPECT_EQ(1, cache.disk_cache()->open_count());
  8504. EXPECT_EQ(1, cache.disk_cache()->create_count());
  8505. }
  8506. // Verify that no-cache resources are stored in cache, but are not fetched from
  8507. // cache during normal loads.
  8508. TEST_F(HttpCacheTest, CacheControlNoCacheNormalLoad) {
  8509. for (bool use_memory_entry_data : {false, true}) {
  8510. MockHttpCache cache;
  8511. cache.disk_cache()->set_support_in_memory_entry_data(use_memory_entry_data);
  8512. ScopedMockTransaction transaction(kSimpleGET_Transaction);
  8513. transaction.response_headers = "cache-control: no-cache\n";
  8514. // Initial load.
  8515. RunTransactionTest(cache.http_cache(), transaction);
  8516. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  8517. EXPECT_EQ(0, cache.disk_cache()->open_count());
  8518. EXPECT_EQ(1, cache.disk_cache()->create_count());
  8519. // Try loading again; it should result in a network fetch.
  8520. RunTransactionTest(cache.http_cache(), transaction);
  8521. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  8522. if (use_memory_entry_data) {
  8523. EXPECT_EQ(0, cache.disk_cache()->open_count());
  8524. EXPECT_EQ(2, cache.disk_cache()->create_count());
  8525. } else {
  8526. EXPECT_EQ(1, cache.disk_cache()->open_count());
  8527. EXPECT_EQ(1, cache.disk_cache()->create_count());
  8528. }
  8529. disk_cache::Entry* entry;
  8530. MockHttpRequest request(transaction);
  8531. EXPECT_TRUE(cache.OpenBackendEntry(request.CacheKey(), &entry));
  8532. entry->Close();
  8533. }
  8534. }
  8535. // Verify that no-cache resources are stored in cache and fetched from cache
  8536. // when the LOAD_SKIP_CACHE_VALIDATION flag is set.
  8537. TEST_F(HttpCacheTest, CacheControlNoCacheHistoryLoad) {
  8538. MockHttpCache cache;
  8539. ScopedMockTransaction transaction(kSimpleGET_Transaction);
  8540. transaction.response_headers = "cache-control: no-cache\n";
  8541. // Initial load.
  8542. RunTransactionTest(cache.http_cache(), transaction);
  8543. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  8544. EXPECT_EQ(0, cache.disk_cache()->open_count());
  8545. EXPECT_EQ(1, cache.disk_cache()->create_count());
  8546. // Try loading again with LOAD_SKIP_CACHE_VALIDATION.
  8547. transaction.load_flags = LOAD_SKIP_CACHE_VALIDATION;
  8548. RunTransactionTest(cache.http_cache(), transaction);
  8549. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  8550. EXPECT_EQ(1, cache.disk_cache()->open_count());
  8551. EXPECT_EQ(1, cache.disk_cache()->create_count());
  8552. disk_cache::Entry* entry;
  8553. MockHttpRequest request(transaction);
  8554. EXPECT_TRUE(cache.OpenBackendEntry(request.CacheKey(), &entry));
  8555. entry->Close();
  8556. }
  8557. TEST_F(HttpCacheTest, CacheControlNoStore) {
  8558. MockHttpCache cache;
  8559. ScopedMockTransaction transaction(kSimpleGET_Transaction);
  8560. transaction.response_headers = "cache-control: no-store\n";
  8561. // initial load
  8562. RunTransactionTest(cache.http_cache(), transaction);
  8563. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  8564. EXPECT_EQ(0, cache.disk_cache()->open_count());
  8565. EXPECT_EQ(1, cache.disk_cache()->create_count());
  8566. // try loading again; it should result in a network fetch
  8567. RunTransactionTest(cache.http_cache(), transaction);
  8568. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  8569. EXPECT_EQ(0, cache.disk_cache()->open_count());
  8570. EXPECT_EQ(2, cache.disk_cache()->create_count());
  8571. disk_cache::Entry* entry;
  8572. MockHttpRequest request(transaction);
  8573. EXPECT_FALSE(cache.OpenBackendEntry(request.CacheKey(), &entry));
  8574. }
  8575. TEST_F(HttpCacheTest, CacheControlNoStore2) {
  8576. // this test is similar to the above test, except that the initial response
  8577. // is cachable, but when it is validated, no-store is received causing the
  8578. // cached document to be deleted.
  8579. MockHttpCache cache;
  8580. ScopedMockTransaction transaction(kETagGET_Transaction);
  8581. // initial load
  8582. RunTransactionTest(cache.http_cache(), transaction);
  8583. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  8584. EXPECT_EQ(0, cache.disk_cache()->open_count());
  8585. EXPECT_EQ(1, cache.disk_cache()->create_count());
  8586. // try loading again; it should result in a network fetch
  8587. transaction.load_flags = LOAD_VALIDATE_CACHE;
  8588. transaction.response_headers = "cache-control: no-store\n";
  8589. RunTransactionTest(cache.http_cache(), transaction);
  8590. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  8591. EXPECT_EQ(1, cache.disk_cache()->open_count());
  8592. EXPECT_EQ(1, cache.disk_cache()->create_count());
  8593. disk_cache::Entry* entry;
  8594. MockHttpRequest request(transaction);
  8595. EXPECT_FALSE(cache.OpenBackendEntry(request.CacheKey(), &entry));
  8596. }
  8597. TEST_F(HttpCacheTest, CacheControlNoStore3) {
  8598. // this test is similar to the above test, except that the response is a 304
  8599. // instead of a 200. this should never happen in practice, but it seems like
  8600. // a good thing to verify that we still destroy the cache entry.
  8601. MockHttpCache cache;
  8602. ScopedMockTransaction transaction(kETagGET_Transaction);
  8603. // initial load
  8604. RunTransactionTest(cache.http_cache(), transaction);
  8605. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  8606. EXPECT_EQ(0, cache.disk_cache()->open_count());
  8607. EXPECT_EQ(1, cache.disk_cache()->create_count());
  8608. // try loading again; it should result in a network fetch
  8609. transaction.load_flags = LOAD_VALIDATE_CACHE;
  8610. transaction.response_headers = "cache-control: no-store\n";
  8611. transaction.status = "HTTP/1.1 304 Not Modified";
  8612. RunTransactionTest(cache.http_cache(), transaction);
  8613. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  8614. EXPECT_EQ(1, cache.disk_cache()->open_count());
  8615. EXPECT_EQ(1, cache.disk_cache()->create_count());
  8616. disk_cache::Entry* entry;
  8617. MockHttpRequest request(transaction);
  8618. EXPECT_FALSE(cache.OpenBackendEntry(request.CacheKey(), &entry));
  8619. }
  8620. // Ensure that we don't cache requests served over bad HTTPS.
  8621. TEST_F(HttpCacheTest, SimpleGET_SSLError) {
  8622. MockHttpCache cache;
  8623. MockTransaction transaction = kSimpleGET_Transaction;
  8624. transaction.cert_status = CERT_STATUS_REVOKED;
  8625. ScopedMockTransaction scoped_transaction(transaction);
  8626. // write to the cache
  8627. RunTransactionTest(cache.http_cache(), transaction);
  8628. // Test that it was not cached.
  8629. transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
  8630. MockHttpRequest request(transaction);
  8631. TestCompletionCallback callback;
  8632. std::unique_ptr<HttpTransaction> trans;
  8633. ASSERT_THAT(cache.CreateTransaction(&trans), IsOk());
  8634. int rv = trans->Start(&request, callback.callback(), NetLogWithSource());
  8635. if (rv == ERR_IO_PENDING)
  8636. rv = callback.WaitForResult();
  8637. ASSERT_THAT(rv, IsError(ERR_CACHE_MISS));
  8638. }
  8639. // Ensure that we don't crash by if left-behind transactions.
  8640. TEST_F(HttpCacheTest, OutlivedTransactions) {
  8641. auto cache = std::make_unique<MockHttpCache>();
  8642. std::unique_ptr<HttpTransaction> trans;
  8643. EXPECT_THAT(cache->CreateTransaction(&trans), IsOk());
  8644. cache.reset();
  8645. trans.reset();
  8646. }
  8647. // Test that the disabled mode works.
  8648. TEST_F(HttpCacheTest, CacheDisabledMode) {
  8649. MockHttpCache cache;
  8650. // write to the cache
  8651. RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
  8652. // go into disabled mode
  8653. cache.http_cache()->set_mode(HttpCache::DISABLE);
  8654. // force this transaction to write to the cache again
  8655. MockTransaction transaction(kSimpleGET_Transaction);
  8656. RunTransactionTest(cache.http_cache(), transaction);
  8657. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  8658. EXPECT_EQ(0, cache.disk_cache()->open_count());
  8659. EXPECT_EQ(1, cache.disk_cache()->create_count());
  8660. }
  8661. // Other tests check that the response headers of the cached response
  8662. // get updated on 304. Here we specifically check that the
  8663. // HttpResponseHeaders::request_time and HttpResponseHeaders::response_time
  8664. // fields also gets updated.
  8665. // http://crbug.com/20594.
  8666. TEST_F(HttpCacheTest, UpdatesRequestResponseTimeOn304) {
  8667. MockHttpCache cache;
  8668. const char kUrl[] = "http://foobar";
  8669. const char kData[] = "body";
  8670. MockTransaction mock_network_response = {nullptr};
  8671. mock_network_response.url = kUrl;
  8672. AddMockTransaction(&mock_network_response);
  8673. // Request |kUrl|, causing |kNetResponse1| to be written to the cache.
  8674. MockTransaction request = {nullptr};
  8675. request.url = kUrl;
  8676. request.method = "GET";
  8677. request.request_headers = "\r\n";
  8678. request.data = kData;
  8679. static const Response kNetResponse1 = {
  8680. "HTTP/1.1 200 OK",
  8681. "Date: Fri, 12 Jun 2009 21:46:42 GMT\n"
  8682. "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
  8683. kData
  8684. };
  8685. kNetResponse1.AssignTo(&mock_network_response);
  8686. RunTransactionTest(cache.http_cache(), request);
  8687. // Request |kUrl| again, this time validating the cache and getting
  8688. // a 304 back.
  8689. request.load_flags = LOAD_VALIDATE_CACHE;
  8690. static const Response kNetResponse2 = {
  8691. "HTTP/1.1 304 Not Modified",
  8692. "Date: Wed, 22 Jul 2009 03:15:26 GMT\n",
  8693. ""
  8694. };
  8695. kNetResponse2.AssignTo(&mock_network_response);
  8696. base::Time request_time = base::Time() + base::Hours(1234);
  8697. base::Time response_time = base::Time() + base::Hours(1235);
  8698. mock_network_response.request_time = request_time;
  8699. mock_network_response.response_time = response_time;
  8700. HttpResponseInfo response;
  8701. RunTransactionTestWithResponseInfo(cache.http_cache(), request, &response);
  8702. // The request and response times should have been updated.
  8703. EXPECT_EQ(request_time.ToInternalValue(),
  8704. response.request_time.ToInternalValue());
  8705. EXPECT_EQ(response_time.ToInternalValue(),
  8706. response.response_time.ToInternalValue());
  8707. EXPECT_EQ("HTTP/1.1 200 OK\n"
  8708. "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
  8709. "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
  8710. ToSimpleString(response.headers));
  8711. RemoveMockTransaction(&mock_network_response);
  8712. }
  8713. TEST_F(HttpCacheTest, SplitCacheWithNetworkIsolationKey) {
  8714. base::test::ScopedFeatureList feature_list;
  8715. feature_list.InitAndEnableFeature(
  8716. net::features::kSplitCacheByNetworkIsolationKey);
  8717. base::HistogramTester histograms;
  8718. MockHttpCache cache;
  8719. HttpResponseInfo response;
  8720. SchemefulSite site_a(GURL("http://a.com"));
  8721. SchemefulSite site_b(GURL("http://b.com"));
  8722. SchemefulSite site_data(GURL("data:text/html,<body>Hello World</body>"));
  8723. MockHttpRequest trans_info = MockHttpRequest(kSimpleGET_Transaction);
  8724. // Request with a.com as the top frame and subframe origins. It shouldn't be
  8725. // cached.
  8726. trans_info.network_isolation_key = NetworkIsolationKey(site_a, site_a);
  8727. RunTransactionTestWithRequest(cache.http_cache(), kSimpleGET_Transaction,
  8728. trans_info, &response);
  8729. EXPECT_FALSE(response.was_cached);
  8730. histograms.ExpectBucketCount(
  8731. "HttpCache.NetworkIsolationKeyPresent2",
  8732. HttpCache::Transaction::NetworkIsolationKeyPresent::kPresent, 1);
  8733. histograms.ExpectTotalCount("HttpCache.NetworkIsolationKeyPresent2", 1);
  8734. // The second request should be cached.
  8735. RunTransactionTestWithRequest(cache.http_cache(), kSimpleGET_Transaction,
  8736. trans_info, &response);
  8737. EXPECT_TRUE(response.was_cached);
  8738. // Now request with b.com as the subframe origin. It shouldn't be cached.
  8739. trans_info.network_isolation_key = NetworkIsolationKey(site_a, site_b);
  8740. RunTransactionTestWithRequest(cache.http_cache(), kSimpleGET_Transaction,
  8741. trans_info, &response);
  8742. EXPECT_FALSE(response.was_cached);
  8743. // The second request should be cached.
  8744. RunTransactionTestWithRequest(cache.http_cache(), kSimpleGET_Transaction,
  8745. trans_info, &response);
  8746. EXPECT_TRUE(response.was_cached);
  8747. // a.com should still be cached.
  8748. trans_info.network_isolation_key = NetworkIsolationKey(site_a, site_a);
  8749. RunTransactionTestWithRequest(cache.http_cache(), kSimpleGET_Transaction,
  8750. trans_info, &response);
  8751. EXPECT_TRUE(response.was_cached);
  8752. // Now make a request with an opaque subframe site. It shouldn't be
  8753. // cached.
  8754. trans_info.network_isolation_key = NetworkIsolationKey(site_a, site_data);
  8755. EXPECT_EQ(absl::nullopt, trans_info.network_isolation_key.ToCacheKeyString());
  8756. RunTransactionTestWithRequest(cache.http_cache(), kSimpleGET_Transaction,
  8757. trans_info, &response);
  8758. EXPECT_FALSE(response.was_cached);
  8759. // On the second request, it still shouldn't be cached.
  8760. RunTransactionTestWithRequest(cache.http_cache(), kSimpleGET_Transaction,
  8761. trans_info, &response);
  8762. EXPECT_FALSE(response.was_cached);
  8763. // Verify that a post transaction with a data stream uses a separate key.
  8764. const int64_t kUploadId = 1; // Just a dummy value.
  8765. std::vector<std::unique_ptr<UploadElementReader>> element_readers;
  8766. element_readers.push_back(
  8767. std::make_unique<UploadBytesElementReader>("hello", 5));
  8768. ElementsUploadDataStream upload_data_stream(std::move(element_readers),
  8769. kUploadId);
  8770. MockHttpRequest post_info = MockHttpRequest(kSimplePOST_Transaction);
  8771. post_info.network_isolation_key = NetworkIsolationKey(site_a, site_a);
  8772. post_info.upload_data_stream = &upload_data_stream;
  8773. RunTransactionTestWithRequest(cache.http_cache(), kSimplePOST_Transaction,
  8774. post_info, &response);
  8775. EXPECT_FALSE(response.was_cached);
  8776. }
  8777. TEST_F(HttpCacheTest, HttpCacheProfileThirdPartyCSS) {
  8778. base::HistogramTester histograms;
  8779. MockHttpCache cache;
  8780. HttpResponseInfo response;
  8781. url::Origin origin_a = url::Origin::Create(GURL(kSimpleGET_Transaction.url));
  8782. url::Origin origin_b = url::Origin::Create(GURL("http://b.com"));
  8783. SchemefulSite site_a(origin_a);
  8784. SchemefulSite site_b(origin_b);
  8785. ScopedMockTransaction transaction(kSimpleGET_Transaction);
  8786. transaction.response_headers = "Content-Type: text/css\n";
  8787. MockHttpRequest trans_info = MockHttpRequest(transaction);
  8788. // Requesting with the same top-frame site should not count as third-party
  8789. // but should still be recorded as CSS
  8790. trans_info.network_isolation_key = NetworkIsolationKey(site_a, site_a);
  8791. trans_info.possibly_top_frame_origin = origin_a;
  8792. RunTransactionTestWithRequest(cache.http_cache(), transaction, trans_info,
  8793. &response);
  8794. histograms.ExpectTotalCount("HttpCache.Pattern", 1);
  8795. histograms.ExpectTotalCount("HttpCache.Pattern.CSS", 1);
  8796. histograms.ExpectTotalCount("HttpCache.Pattern.CSSThirdParty", 0);
  8797. // Requesting with a different top-frame site should count as third-party
  8798. // and recorded as CSS
  8799. trans_info.network_isolation_key = NetworkIsolationKey(site_b, site_b);
  8800. trans_info.possibly_top_frame_origin = origin_b;
  8801. RunTransactionTestWithRequest(cache.http_cache(), transaction, trans_info,
  8802. &response);
  8803. histograms.ExpectTotalCount("HttpCache.Pattern", 2);
  8804. histograms.ExpectTotalCount("HttpCache.Pattern.CSS", 2);
  8805. histograms.ExpectTotalCount("HttpCache.Pattern.CSSThirdParty", 1);
  8806. }
  8807. TEST_F(HttpCacheTest, HttpCacheProfileThirdPartyJavaScript) {
  8808. base::HistogramTester histograms;
  8809. MockHttpCache cache;
  8810. HttpResponseInfo response;
  8811. url::Origin origin_a = url::Origin::Create(GURL(kSimpleGET_Transaction.url));
  8812. url::Origin origin_b = url::Origin::Create(GURL("http://b.com"));
  8813. SchemefulSite site_a(origin_a);
  8814. SchemefulSite site_b(origin_b);
  8815. ScopedMockTransaction transaction(kSimpleGET_Transaction);
  8816. transaction.response_headers = "Content-Type: application/javascript\n";
  8817. MockHttpRequest trans_info = MockHttpRequest(transaction);
  8818. // Requesting with the same top-frame site should not count as third-party
  8819. // but should still be recorded as JavaScript
  8820. trans_info.network_isolation_key = NetworkIsolationKey(site_a, site_a);
  8821. trans_info.possibly_top_frame_origin = origin_a;
  8822. RunTransactionTestWithRequest(cache.http_cache(), transaction, trans_info,
  8823. &response);
  8824. histograms.ExpectTotalCount("HttpCache.Pattern", 1);
  8825. histograms.ExpectTotalCount("HttpCache.Pattern.JavaScript", 1);
  8826. histograms.ExpectTotalCount("HttpCache.Pattern.JavaScriptThirdParty", 0);
  8827. // Requesting with a different top-frame site should count as third-party
  8828. // and recorded as JavaScript
  8829. trans_info.network_isolation_key = NetworkIsolationKey(site_b, site_b);
  8830. trans_info.possibly_top_frame_origin = origin_b;
  8831. RunTransactionTestWithRequest(cache.http_cache(), transaction, trans_info,
  8832. &response);
  8833. histograms.ExpectTotalCount("HttpCache.Pattern", 2);
  8834. histograms.ExpectTotalCount("HttpCache.Pattern.JavaScript", 2);
  8835. histograms.ExpectTotalCount("HttpCache.Pattern.JavaScriptThirdParty", 1);
  8836. }
  8837. TEST_F(HttpCacheTest, HttpCacheProfileThirdPartyFont) {
  8838. base::HistogramTester histograms;
  8839. MockHttpCache cache;
  8840. HttpResponseInfo response;
  8841. url::Origin origin_a = url::Origin::Create(GURL(kSimpleGET_Transaction.url));
  8842. url::Origin origin_b = url::Origin::Create(GURL("http://b.com"));
  8843. SchemefulSite site_a(origin_a);
  8844. SchemefulSite site_b(origin_b);
  8845. ScopedMockTransaction transaction(kSimpleGET_Transaction);
  8846. transaction.response_headers = "Content-Type: font/otf\n";
  8847. MockHttpRequest trans_info = MockHttpRequest(transaction);
  8848. // Requesting with the same top-frame site should not count as third-party
  8849. // but should still be recorded as a font
  8850. trans_info.network_isolation_key = NetworkIsolationKey(site_a, site_a);
  8851. trans_info.possibly_top_frame_origin = origin_a;
  8852. RunTransactionTestWithRequest(cache.http_cache(), transaction, trans_info,
  8853. &response);
  8854. histograms.ExpectTotalCount("HttpCache.Pattern", 1);
  8855. histograms.ExpectTotalCount("HttpCache.Pattern.Font", 1);
  8856. histograms.ExpectTotalCount("HttpCache.Pattern.FontThirdParty", 0);
  8857. // Requesting with a different top-frame site should count as third-party
  8858. // and recorded as a font
  8859. trans_info.network_isolation_key = NetworkIsolationKey(site_b, site_b);
  8860. trans_info.possibly_top_frame_origin = origin_b;
  8861. RunTransactionTestWithRequest(cache.http_cache(), transaction, trans_info,
  8862. &response);
  8863. histograms.ExpectTotalCount("HttpCache.Pattern", 2);
  8864. histograms.ExpectTotalCount("HttpCache.Pattern.Font", 2);
  8865. histograms.ExpectTotalCount("HttpCache.Pattern.FontThirdParty", 1);
  8866. }
  8867. TEST_F(HttpCacheTest, SplitCache) {
  8868. base::test::ScopedFeatureList feature_list;
  8869. feature_list.InitAndEnableFeature(
  8870. net::features::kSplitCacheByNetworkIsolationKey);
  8871. base::HistogramTester histograms;
  8872. MockHttpCache cache;
  8873. HttpResponseInfo response;
  8874. SchemefulSite site_a(GURL("http://a.com"));
  8875. SchemefulSite site_b(GURL("http://b.com"));
  8876. SchemefulSite site_data(GURL("data:text/html,<body>Hello World</body>"));
  8877. // A request without a top frame origin is not cached at all.
  8878. MockHttpRequest trans_info = MockHttpRequest(kSimpleGET_Transaction);
  8879. trans_info.network_isolation_key = net::NetworkIsolationKey();
  8880. RunTransactionTestWithRequest(cache.http_cache(), kSimpleGET_Transaction,
  8881. trans_info, &response);
  8882. EXPECT_FALSE(response.was_cached);
  8883. histograms.ExpectUniqueSample(
  8884. "HttpCache.NetworkIsolationKeyPresent2",
  8885. HttpCache::Transaction::NetworkIsolationKeyPresent::
  8886. kNotPresentNonCacheableRequest,
  8887. 1);
  8888. RunTransactionTestWithRequest(cache.http_cache(), kSimpleGET_Transaction,
  8889. trans_info, &response);
  8890. EXPECT_FALSE(response.was_cached);
  8891. // Now request with a.com as the top frame origin. It shouldn't be cached
  8892. // since the cached resource has a different top frame origin.
  8893. net::NetworkIsolationKey key_a(site_a, site_a);
  8894. trans_info.network_isolation_key = key_a;
  8895. RunTransactionTestWithRequest(cache.http_cache(), kSimpleGET_Transaction,
  8896. trans_info, &response);
  8897. EXPECT_FALSE(response.was_cached);
  8898. histograms.ExpectBucketCount(
  8899. "HttpCache.NetworkIsolationKeyPresent2",
  8900. HttpCache::Transaction::NetworkIsolationKeyPresent::kPresent, 1);
  8901. histograms.ExpectTotalCount("HttpCache.NetworkIsolationKeyPresent2", 3);
  8902. // The second request should be cached.
  8903. RunTransactionTestWithRequest(cache.http_cache(), kSimpleGET_Transaction,
  8904. trans_info, &response);
  8905. EXPECT_TRUE(response.was_cached);
  8906. // If the same resource with the same NIK is for a subframe document resource,
  8907. // it should not be a cache hit.
  8908. MockHttpRequest subframe_document_trans_info = trans_info;
  8909. subframe_document_trans_info.is_subframe_document_resource = true;
  8910. RunTransactionTestWithRequest(cache.http_cache(), kSimpleGET_Transaction,
  8911. subframe_document_trans_info, &response);
  8912. EXPECT_FALSE(response.was_cached);
  8913. // Same request again should be a cache hit.
  8914. RunTransactionTestWithRequest(cache.http_cache(), kSimpleGET_Transaction,
  8915. subframe_document_trans_info, &response);
  8916. EXPECT_TRUE(response.was_cached);
  8917. // Now request with b.com as the top frame origin. It shouldn't be cached.
  8918. trans_info.network_isolation_key = NetworkIsolationKey(site_b, site_b);
  8919. RunTransactionTestWithRequest(cache.http_cache(), kSimpleGET_Transaction,
  8920. trans_info, &response);
  8921. EXPECT_FALSE(response.was_cached);
  8922. // The second request should be cached.
  8923. RunTransactionTestWithRequest(cache.http_cache(), kSimpleGET_Transaction,
  8924. trans_info, &response);
  8925. EXPECT_TRUE(response.was_cached);
  8926. // a.com should still be cached.
  8927. trans_info.network_isolation_key = key_a;
  8928. RunTransactionTestWithRequest(cache.http_cache(), kSimpleGET_Transaction,
  8929. trans_info, &response);
  8930. EXPECT_TRUE(response.was_cached);
  8931. // Now make a request with an opaque top frame origin. It shouldn't be
  8932. // cached.
  8933. trans_info.network_isolation_key = NetworkIsolationKey(site_data, site_data);
  8934. EXPECT_EQ(absl::nullopt, trans_info.network_isolation_key.ToCacheKeyString());
  8935. RunTransactionTestWithRequest(cache.http_cache(), kSimpleGET_Transaction,
  8936. trans_info, &response);
  8937. EXPECT_FALSE(response.was_cached);
  8938. // On the second request, it still shouldn't be cached.
  8939. RunTransactionTestWithRequest(cache.http_cache(), kSimpleGET_Transaction,
  8940. trans_info, &response);
  8941. EXPECT_FALSE(response.was_cached);
  8942. // Verify that a post transaction with a data stream uses a separate key.
  8943. const int64_t kUploadId = 1; // Just a dummy value.
  8944. std::vector<std::unique_ptr<UploadElementReader>> element_readers;
  8945. element_readers.push_back(
  8946. std::make_unique<UploadBytesElementReader>("hello", 5));
  8947. ElementsUploadDataStream upload_data_stream(std::move(element_readers),
  8948. kUploadId);
  8949. MockHttpRequest post_info = MockHttpRequest(kSimplePOST_Transaction);
  8950. post_info.network_isolation_key = NetworkIsolationKey(site_a, site_a);
  8951. post_info.upload_data_stream = &upload_data_stream;
  8952. RunTransactionTestWithRequest(cache.http_cache(), kSimplePOST_Transaction,
  8953. post_info, &response);
  8954. EXPECT_FALSE(response.was_cached);
  8955. }
  8956. TEST_F(HttpCacheTest, SplitCacheEnabledByDefault) {
  8957. HttpCache::ClearGlobalsForTesting();
  8958. HttpCache::SplitCacheFeatureEnableByDefault();
  8959. EXPECT_TRUE(HttpCache::IsSplitCacheEnabled());
  8960. MockHttpCache cache;
  8961. HttpResponseInfo response;
  8962. SchemefulSite site_a(GURL("http://a.com"));
  8963. SchemefulSite site_b(GURL("http://b.com"));
  8964. MockHttpRequest trans_info = MockHttpRequest(kSimpleGET_Transaction);
  8965. net::NetworkIsolationKey key_a(site_a, site_a);
  8966. trans_info.network_isolation_key = key_a;
  8967. RunTransactionTestWithRequest(cache.http_cache(), kSimpleGET_Transaction,
  8968. trans_info, &response);
  8969. EXPECT_FALSE(response.was_cached);
  8970. // Subsequent requests with the same NIK and different NIK will be a cache hit
  8971. // and miss respectively.
  8972. RunTransactionTestWithRequest(cache.http_cache(), kSimpleGET_Transaction,
  8973. trans_info, &response);
  8974. EXPECT_TRUE(response.was_cached);
  8975. net::NetworkIsolationKey key_b(site_b, site_b);
  8976. trans_info.network_isolation_key = key_b;
  8977. RunTransactionTestWithRequest(cache.http_cache(), kSimpleGET_Transaction,
  8978. trans_info, &response);
  8979. EXPECT_FALSE(response.was_cached);
  8980. }
  8981. TEST_F(HttpCacheTest, SplitCacheEnabledByDefaultButOverridden) {
  8982. HttpCache::ClearGlobalsForTesting();
  8983. base::test::ScopedFeatureList feature_list;
  8984. feature_list.InitAndDisableFeature(
  8985. net::features::kSplitCacheByNetworkIsolationKey);
  8986. // Enabling it here should have no effect as it is already overridden.
  8987. HttpCache::SplitCacheFeatureEnableByDefault();
  8988. EXPECT_FALSE(HttpCache::IsSplitCacheEnabled());
  8989. }
  8990. TEST_F(HttpCacheTest, SplitCacheUsesRegistrableDomain) {
  8991. base::test::ScopedFeatureList feature_list;
  8992. feature_list.InitAndEnableFeature(
  8993. net::features::kSplitCacheByNetworkIsolationKey);
  8994. base::HistogramTester histograms;
  8995. MockHttpCache cache;
  8996. HttpResponseInfo response;
  8997. MockHttpRequest trans_info = MockHttpRequest(kSimpleGET_Transaction);
  8998. SchemefulSite site_a(GURL("http://a.foo.com"));
  8999. SchemefulSite site_b(GURL("http://b.foo.com"));
  9000. net::NetworkIsolationKey key_a(site_a, site_a);
  9001. trans_info.network_isolation_key = key_a;
  9002. RunTransactionTestWithRequest(cache.http_cache(), kSimpleGET_Transaction,
  9003. trans_info, &response);
  9004. EXPECT_FALSE(response.was_cached);
  9005. histograms.ExpectBucketCount(
  9006. "HttpCache.NetworkIsolationKeyPresent2",
  9007. HttpCache::Transaction::NetworkIsolationKeyPresent::kPresent, 1);
  9008. // The second request with a different origin but the same registrable domain
  9009. // should be a cache hit.
  9010. net::NetworkIsolationKey key_b(site_b, site_b);
  9011. trans_info.network_isolation_key = key_b;
  9012. RunTransactionTestWithRequest(cache.http_cache(), kSimpleGET_Transaction,
  9013. trans_info, &response);
  9014. EXPECT_TRUE(response.was_cached);
  9015. // Request with a different registrable domain. It should be a cache miss.
  9016. SchemefulSite new_site_a(GURL("http://a.bar.com"));
  9017. net::NetworkIsolationKey new_key_a(new_site_a, new_site_a);
  9018. trans_info.network_isolation_key = new_key_a;
  9019. RunTransactionTestWithRequest(cache.http_cache(), kSimpleGET_Transaction,
  9020. trans_info, &response);
  9021. EXPECT_FALSE(response.was_cached);
  9022. }
  9023. TEST_F(HttpCacheTest, NonSplitCache) {
  9024. base::test::ScopedFeatureList feature_list;
  9025. feature_list.InitAndDisableFeature(
  9026. net::features::kSplitCacheByNetworkIsolationKey);
  9027. base::HistogramTester histograms;
  9028. MockHttpCache cache;
  9029. HttpResponseInfo response;
  9030. // A request without a top frame is cached normally.
  9031. MockHttpRequest trans_info = MockHttpRequest(kSimpleGET_Transaction);
  9032. trans_info.network_isolation_key = NetworkIsolationKey();
  9033. RunTransactionTestWithRequest(cache.http_cache(), kSimpleGET_Transaction,
  9034. trans_info, &response);
  9035. EXPECT_FALSE(response.was_cached);
  9036. // The second request comes from cache.
  9037. RunTransactionTestWithRequest(cache.http_cache(), kSimpleGET_Transaction,
  9038. trans_info, &response);
  9039. EXPECT_TRUE(response.was_cached);
  9040. // Now request with a.com as the top frame origin. It should use the same
  9041. // cached object.
  9042. const SchemefulSite kSiteA(GURL("http://a.com/"));
  9043. trans_info.network_isolation_key = NetworkIsolationKey(kSiteA, kSiteA);
  9044. RunTransactionTestWithRequest(cache.http_cache(), kSimpleGET_Transaction,
  9045. trans_info, &response);
  9046. EXPECT_TRUE(response.was_cached);
  9047. histograms.ExpectBucketCount(
  9048. "HttpCache.NetworkIsolationKeyPresent2",
  9049. HttpCache::Transaction::NetworkIsolationKeyPresent::kPresent, 1);
  9050. histograms.ExpectTotalCount("HttpCache.NetworkIsolationKeyPresent2", 3);
  9051. }
  9052. TEST_F(HttpCacheTest, SkipVaryCheck) {
  9053. MockHttpCache cache;
  9054. // Write a simple vary transaction to the cache.
  9055. HttpResponseInfo response;
  9056. ScopedMockTransaction transaction(kSimpleGET_Transaction);
  9057. transaction.request_headers = "accept-encoding: gzip\r\n";
  9058. transaction.response_headers =
  9059. "Vary: accept-encoding\n"
  9060. "Cache-Control: max-age=10000\n";
  9061. RunTransactionTest(cache.http_cache(), transaction);
  9062. // Change the request headers so that the request doesn't match due to vary.
  9063. // The request should fail.
  9064. transaction.load_flags = LOAD_ONLY_FROM_CACHE;
  9065. transaction.request_headers = "accept-encoding: foo\r\n";
  9066. transaction.start_return_code = ERR_CACHE_MISS;
  9067. RunTransactionTest(cache.http_cache(), transaction);
  9068. // Change the load flags to ignore vary checks, the request should now hit.
  9069. transaction.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_VARY_CHECK;
  9070. transaction.start_return_code = OK;
  9071. RunTransactionTest(cache.http_cache(), transaction);
  9072. }
  9073. TEST_F(HttpCacheTest, SkipVaryCheckStar) {
  9074. MockHttpCache cache;
  9075. // Write a simple vary:* transaction to the cache.
  9076. HttpResponseInfo response;
  9077. ScopedMockTransaction transaction(kSimpleGET_Transaction);
  9078. transaction.request_headers = "accept-encoding: gzip\r\n";
  9079. transaction.response_headers =
  9080. "Vary: *\n"
  9081. "Cache-Control: max-age=10000\n";
  9082. RunTransactionTest(cache.http_cache(), transaction);
  9083. // The request shouldn't match even with the same request headers due to the
  9084. // Vary: *. The request should fail.
  9085. transaction.load_flags = LOAD_ONLY_FROM_CACHE;
  9086. transaction.start_return_code = ERR_CACHE_MISS;
  9087. RunTransactionTest(cache.http_cache(), transaction);
  9088. // Change the load flags to ignore vary checks, the request should now hit.
  9089. transaction.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_VARY_CHECK;
  9090. transaction.start_return_code = OK;
  9091. RunTransactionTest(cache.http_cache(), transaction);
  9092. }
  9093. // Tests that we only return valid entries with LOAD_ONLY_FROM_CACHE
  9094. // transactions unless LOAD_SKIP_CACHE_VALIDATION is set.
  9095. TEST_F(HttpCacheTest, ValidLoadOnlyFromCache) {
  9096. MockHttpCache cache;
  9097. base::SimpleTestClock clock;
  9098. cache.http_cache()->SetClockForTesting(&clock);
  9099. cache.network_layer()->SetClock(&clock);
  9100. // Write a resource that will expire in 100 seconds.
  9101. ScopedMockTransaction transaction(kSimpleGET_Transaction);
  9102. transaction.response_headers = "Cache-Control: max-age=100\n";
  9103. RunTransactionTest(cache.http_cache(), transaction);
  9104. // Move forward in time such that the cached response is no longer valid.
  9105. clock.Advance(base::Seconds(101));
  9106. // Skipping cache validation should still return a response.
  9107. transaction.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
  9108. RunTransactionTest(cache.http_cache(), transaction);
  9109. // If the cache entry is checked for validitiy, it should fail.
  9110. transaction.load_flags = LOAD_ONLY_FROM_CACHE;
  9111. transaction.start_return_code = ERR_CACHE_MISS;
  9112. RunTransactionTest(cache.http_cache(), transaction);
  9113. }
  9114. TEST_F(HttpCacheTest, InvalidLoadFlagCombination) {
  9115. MockHttpCache cache;
  9116. // Put the resource in the cache.
  9117. RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
  9118. // Now try to fetch it again, but with a flag combination disallowing both
  9119. // cache and network access.
  9120. ScopedMockTransaction transaction(kSimpleGET_Transaction);
  9121. // DevTools relies on this combination of flags for "disable cache" mode
  9122. // when a resource is only supposed to be loaded from cache.
  9123. transaction.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_BYPASS_CACHE;
  9124. transaction.start_return_code = ERR_CACHE_MISS;
  9125. RunTransactionTest(cache.http_cache(), transaction);
  9126. }
  9127. // Tests that we don't mark entries as truncated when a filter detects the end
  9128. // of the stream.
  9129. TEST_F(HttpCacheTest, FilterCompletion) {
  9130. MockHttpCache cache;
  9131. TestCompletionCallback callback;
  9132. {
  9133. MockHttpRequest request(kSimpleGET_Transaction);
  9134. std::unique_ptr<HttpTransaction> trans;
  9135. ASSERT_THAT(cache.CreateTransaction(&trans), IsOk());
  9136. int rv = trans->Start(&request, callback.callback(), NetLogWithSource());
  9137. EXPECT_THAT(callback.GetResult(rv), IsOk());
  9138. scoped_refptr<IOBuffer> buf = base::MakeRefCounted<IOBuffer>(256);
  9139. rv = trans->Read(buf.get(), 256, callback.callback());
  9140. EXPECT_GT(callback.GetResult(rv), 0);
  9141. // Now make sure that the entry is preserved.
  9142. trans->DoneReading();
  9143. }
  9144. // Make sure that the ActiveEntry is gone.
  9145. base::RunLoop().RunUntilIdle();
  9146. // Read from the cache.
  9147. RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
  9148. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  9149. EXPECT_EQ(1, cache.disk_cache()->open_count());
  9150. EXPECT_EQ(1, cache.disk_cache()->create_count());
  9151. }
  9152. // Tests that we don't mark entries as truncated and release the cache
  9153. // entry when DoneReading() is called before any Read() calls, such as
  9154. // for a redirect.
  9155. TEST_F(HttpCacheTest, DoneReading) {
  9156. MockHttpCache cache;
  9157. TestCompletionCallback callback;
  9158. ScopedMockTransaction transaction(kSimpleGET_Transaction);
  9159. transaction.data = "";
  9160. MockHttpRequest request(transaction);
  9161. std::unique_ptr<HttpTransaction> trans;
  9162. ASSERT_THAT(cache.CreateTransaction(&trans), IsOk());
  9163. int rv = trans->Start(&request, callback.callback(), NetLogWithSource());
  9164. EXPECT_THAT(callback.GetResult(rv), IsOk());
  9165. trans->DoneReading();
  9166. // Leave the transaction around.
  9167. // Make sure that the ActiveEntry is gone.
  9168. base::RunLoop().RunUntilIdle();
  9169. // Read from the cache. This should not deadlock.
  9170. RunTransactionTest(cache.http_cache(), transaction);
  9171. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  9172. EXPECT_EQ(1, cache.disk_cache()->open_count());
  9173. EXPECT_EQ(1, cache.disk_cache()->create_count());
  9174. }
  9175. // Tests that we stop caching when told.
  9176. TEST_F(HttpCacheTest, StopCachingDeletesEntry) {
  9177. MockHttpCache cache;
  9178. TestCompletionCallback callback;
  9179. MockHttpRequest request(kSimpleGET_Transaction);
  9180. {
  9181. std::unique_ptr<HttpTransaction> trans;
  9182. ASSERT_THAT(cache.CreateTransaction(&trans), IsOk());
  9183. int rv = trans->Start(&request, callback.callback(), NetLogWithSource());
  9184. EXPECT_THAT(callback.GetResult(rv), IsOk());
  9185. scoped_refptr<IOBuffer> buf = base::MakeRefCounted<IOBuffer>(256);
  9186. rv = trans->Read(buf.get(), 10, callback.callback());
  9187. EXPECT_EQ(10, callback.GetResult(rv));
  9188. trans->StopCaching();
  9189. // We should be able to keep reading.
  9190. rv = trans->Read(buf.get(), 256, callback.callback());
  9191. EXPECT_GT(callback.GetResult(rv), 0);
  9192. rv = trans->Read(buf.get(), 256, callback.callback());
  9193. EXPECT_EQ(0, callback.GetResult(rv));
  9194. }
  9195. // Make sure that the ActiveEntry is gone.
  9196. base::RunLoop().RunUntilIdle();
  9197. // Verify that the entry is gone.
  9198. RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
  9199. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  9200. EXPECT_EQ(0, cache.disk_cache()->open_count());
  9201. EXPECT_EQ(2, cache.disk_cache()->create_count());
  9202. }
  9203. // Tests that we stop caching when told, even if DoneReading is called
  9204. // after StopCaching.
  9205. TEST_F(HttpCacheTest, StopCachingThenDoneReadingDeletesEntry) {
  9206. MockHttpCache cache;
  9207. TestCompletionCallback callback;
  9208. MockHttpRequest request(kSimpleGET_Transaction);
  9209. {
  9210. std::unique_ptr<HttpTransaction> trans;
  9211. ASSERT_THAT(cache.CreateTransaction(&trans), IsOk());
  9212. int rv = trans->Start(&request, callback.callback(), NetLogWithSource());
  9213. EXPECT_THAT(callback.GetResult(rv), IsOk());
  9214. scoped_refptr<IOBuffer> buf = base::MakeRefCounted<IOBuffer>(256);
  9215. rv = trans->Read(buf.get(), 10, callback.callback());
  9216. EXPECT_EQ(10, callback.GetResult(rv));
  9217. trans->StopCaching();
  9218. // We should be able to keep reading.
  9219. rv = trans->Read(buf.get(), 256, callback.callback());
  9220. EXPECT_GT(callback.GetResult(rv), 0);
  9221. rv = trans->Read(buf.get(), 256, callback.callback());
  9222. EXPECT_EQ(0, callback.GetResult(rv));
  9223. // We should be able to call DoneReading.
  9224. trans->DoneReading();
  9225. }
  9226. // Make sure that the ActiveEntry is gone.
  9227. base::RunLoop().RunUntilIdle();
  9228. // Verify that the entry is gone.
  9229. RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
  9230. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  9231. EXPECT_EQ(0, cache.disk_cache()->open_count());
  9232. EXPECT_EQ(2, cache.disk_cache()->create_count());
  9233. }
  9234. // Tests that we stop caching when told, when using auth.
  9235. TEST_F(HttpCacheTest, StopCachingWithAuthDeletesEntry) {
  9236. MockHttpCache cache;
  9237. TestCompletionCallback callback;
  9238. MockTransaction mock_transaction(kSimpleGET_Transaction);
  9239. mock_transaction.status = "HTTP/1.1 401 Unauthorized";
  9240. AddMockTransaction(&mock_transaction);
  9241. MockHttpRequest request(mock_transaction);
  9242. {
  9243. std::unique_ptr<HttpTransaction> trans;
  9244. ASSERT_THAT(cache.CreateTransaction(&trans), IsOk());
  9245. int rv = trans->Start(&request, callback.callback(), NetLogWithSource());
  9246. EXPECT_THAT(callback.GetResult(rv), IsOk());
  9247. trans->StopCaching();
  9248. }
  9249. RemoveMockTransaction(&mock_transaction);
  9250. // Make sure that the ActiveEntry is gone.
  9251. base::RunLoop().RunUntilIdle();
  9252. // Verify that the entry is gone.
  9253. RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
  9254. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  9255. EXPECT_EQ(0, cache.disk_cache()->open_count());
  9256. EXPECT_EQ(2, cache.disk_cache()->create_count());
  9257. }
  9258. // Tests that when we are told to stop caching we don't throw away valid data.
  9259. TEST_F(HttpCacheTest, StopCachingSavesEntry) {
  9260. MockHttpCache cache;
  9261. TestCompletionCallback callback;
  9262. MockHttpRequest request(kSimpleGET_Transaction);
  9263. {
  9264. std::unique_ptr<HttpTransaction> trans;
  9265. ASSERT_THAT(cache.CreateTransaction(&trans), IsOk());
  9266. // Force a response that can be resumed.
  9267. ScopedMockTransaction mock_transaction(kSimpleGET_Transaction);
  9268. AddMockTransaction(&mock_transaction);
  9269. mock_transaction.response_headers = "Cache-Control: max-age=10000\n"
  9270. "Content-Length: 42\n"
  9271. "Etag: \"foo\"\n";
  9272. int rv = trans->Start(&request, callback.callback(), NetLogWithSource());
  9273. EXPECT_THAT(callback.GetResult(rv), IsOk());
  9274. scoped_refptr<IOBuffer> buf = base::MakeRefCounted<IOBuffer>(256);
  9275. rv = trans->Read(buf.get(), 10, callback.callback());
  9276. EXPECT_EQ(callback.GetResult(rv), 10);
  9277. trans->StopCaching();
  9278. // We should be able to keep reading.
  9279. rv = trans->Read(buf.get(), 256, callback.callback());
  9280. EXPECT_GT(callback.GetResult(rv), 0);
  9281. rv = trans->Read(buf.get(), 256, callback.callback());
  9282. EXPECT_EQ(callback.GetResult(rv), 0);
  9283. }
  9284. // Verify that the entry is marked as incomplete.
  9285. // VerifyTruncatedFlag(&cache, kSimpleGET_Transaction.url, true, 0);
  9286. // Verify that the entry is doomed.
  9287. cache.disk_cache()->IsDiskEntryDoomed(request.CacheKey());
  9288. }
  9289. // Tests that we handle truncated enries when StopCaching is called.
  9290. TEST_F(HttpCacheTest, StopCachingTruncatedEntry) {
  9291. MockHttpCache cache;
  9292. TestCompletionCallback callback;
  9293. MockHttpRequest request(kRangeGET_TransactionOK);
  9294. request.extra_headers.Clear();
  9295. request.extra_headers.AddHeaderFromString(EXTRA_HEADER_LINE);
  9296. AddMockTransaction(&kRangeGET_TransactionOK);
  9297. std::string raw_headers("HTTP/1.1 200 OK\n"
  9298. "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
  9299. "ETag: \"foo\"\n"
  9300. "Accept-Ranges: bytes\n"
  9301. "Content-Length: 80\n");
  9302. CreateTruncatedEntry(raw_headers, &cache);
  9303. {
  9304. // Now make a regular request.
  9305. std::unique_ptr<HttpTransaction> trans;
  9306. ASSERT_THAT(cache.CreateTransaction(&trans), IsOk());
  9307. int rv = trans->Start(&request, callback.callback(), NetLogWithSource());
  9308. EXPECT_THAT(callback.GetResult(rv), IsOk());
  9309. scoped_refptr<IOBuffer> buf = base::MakeRefCounted<IOBuffer>(256);
  9310. rv = trans->Read(buf.get(), 10, callback.callback());
  9311. EXPECT_EQ(callback.GetResult(rv), 10);
  9312. // This is actually going to do nothing.
  9313. trans->StopCaching();
  9314. // We should be able to keep reading.
  9315. rv = trans->Read(buf.get(), 256, callback.callback());
  9316. EXPECT_GT(callback.GetResult(rv), 0);
  9317. rv = trans->Read(buf.get(), 256, callback.callback());
  9318. EXPECT_GT(callback.GetResult(rv), 0);
  9319. rv = trans->Read(buf.get(), 256, callback.callback());
  9320. EXPECT_EQ(callback.GetResult(rv), 0);
  9321. }
  9322. // Verify that the disk entry was updated.
  9323. VerifyTruncatedFlag(&cache, request.CacheKey(), false, 80);
  9324. RemoveMockTransaction(&kRangeGET_TransactionOK);
  9325. }
  9326. namespace {
  9327. enum class TransactionPhase {
  9328. BEFORE_FIRST_READ,
  9329. AFTER_FIRST_READ,
  9330. AFTER_NETWORK_READ
  9331. };
  9332. using CacheInitializer = void (*)(MockHttpCache*);
  9333. using HugeCacheTestConfiguration =
  9334. std::pair<TransactionPhase, CacheInitializer>;
  9335. class HttpCacheHugeResourceTest
  9336. : public ::testing::TestWithParam<HugeCacheTestConfiguration>,
  9337. public WithTaskEnvironment {
  9338. public:
  9339. static std::list<HugeCacheTestConfiguration> GetTestModes();
  9340. static std::list<HugeCacheTestConfiguration> kTestModes;
  9341. // CacheInitializer callbacks. These are used to initialize the cache
  9342. // depending on the test run configuration.
  9343. // Initializes a cache containing a truncated entry containing the first 20
  9344. // bytes of the reponse body.
  9345. static void SetupTruncatedCacheEntry(MockHttpCache* cache);
  9346. // Initializes a cache containing a sparse entry. The first 10 bytes are
  9347. // present in the cache.
  9348. static void SetupPrefixSparseCacheEntry(MockHttpCache* cache);
  9349. // Initializes a cache containing a sparse entry. The 10 bytes at offset
  9350. // 99990 are present in the cache.
  9351. static void SetupInfixSparseCacheEntry(MockHttpCache* cache);
  9352. protected:
  9353. static void LargeResourceTransactionHandler(
  9354. const net::HttpRequestInfo* request,
  9355. std::string* response_status,
  9356. std::string* response_headers,
  9357. std::string* response_data);
  9358. static int LargeBufferReader(int64_t content_length,
  9359. int64_t offset,
  9360. net::IOBuffer* buf,
  9361. int buf_len);
  9362. static void SetFlagOnBeforeNetworkStart(bool* started, bool* /* defer */);
  9363. // Size of resource to be tested.
  9364. static const int64_t kTotalSize = 5000LL * 1000 * 1000;
  9365. };
  9366. const int64_t HttpCacheHugeResourceTest::kTotalSize;
  9367. // static
  9368. void HttpCacheHugeResourceTest::LargeResourceTransactionHandler(
  9369. const net::HttpRequestInfo* request,
  9370. std::string* response_status,
  9371. std::string* response_headers,
  9372. std::string* response_data) {
  9373. std::string if_range;
  9374. if (!request->extra_headers.GetHeader(net::HttpRequestHeaders::kIfRange,
  9375. &if_range)) {
  9376. // If there were no range headers in the request, we are going to just
  9377. // return the entire response body.
  9378. *response_status = "HTTP/1.1 200 Success";
  9379. *response_headers = base::StringPrintf("Content-Length: %" PRId64
  9380. "\n"
  9381. "ETag: \"foo\"\n"
  9382. "Accept-Ranges: bytes\n",
  9383. kTotalSize);
  9384. return;
  9385. }
  9386. // From this point on, we should be processing a valid byte-range request.
  9387. EXPECT_EQ("\"foo\"", if_range);
  9388. std::string range_header;
  9389. EXPECT_TRUE(request->extra_headers.GetHeader(net::HttpRequestHeaders::kRange,
  9390. &range_header));
  9391. std::vector<net::HttpByteRange> ranges;
  9392. EXPECT_TRUE(net::HttpUtil::ParseRangeHeader(range_header, &ranges));
  9393. ASSERT_EQ(1u, ranges.size());
  9394. net::HttpByteRange range = ranges[0];
  9395. EXPECT_TRUE(range.HasFirstBytePosition());
  9396. int64_t last_byte_position =
  9397. range.HasLastBytePosition() ? range.last_byte_position() : kTotalSize - 1;
  9398. *response_status = "HTTP/1.1 206 Partial";
  9399. *response_headers = base::StringPrintf(
  9400. "Content-Range: bytes %" PRId64 "-%" PRId64 "/%" PRId64
  9401. "\n"
  9402. "Content-Length: %" PRId64 "\n",
  9403. range.first_byte_position(), last_byte_position, kTotalSize,
  9404. last_byte_position - range.first_byte_position() + 1);
  9405. }
  9406. // static
  9407. int HttpCacheHugeResourceTest::LargeBufferReader(int64_t content_length,
  9408. int64_t offset,
  9409. net::IOBuffer* buf,
  9410. int buf_len) {
  9411. // This test involves reading multiple gigabytes of data. To make it run in a
  9412. // reasonable amount of time, we are going to skip filling the buffer with
  9413. // data. Instead the test relies on verifying that the count of bytes expected
  9414. // at the end is correct.
  9415. EXPECT_LT(0, content_length);
  9416. EXPECT_LE(offset, content_length);
  9417. int num = std::min(static_cast<int64_t>(buf_len), content_length - offset);
  9418. return num;
  9419. }
  9420. // static
  9421. void HttpCacheHugeResourceTest::SetFlagOnBeforeNetworkStart(bool* started,
  9422. bool* /* defer */) {
  9423. *started = true;
  9424. }
  9425. // static
  9426. void HttpCacheHugeResourceTest::SetupTruncatedCacheEntry(MockHttpCache* cache) {
  9427. ScopedMockTransaction scoped_transaction(kRangeGET_TransactionOK);
  9428. std::string cached_headers = base::StringPrintf(
  9429. "HTTP/1.1 200 OK\n"
  9430. "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
  9431. "ETag: \"foo\"\n"
  9432. "Accept-Ranges: bytes\n"
  9433. "Content-Length: %" PRId64 "\n",
  9434. kTotalSize);
  9435. CreateTruncatedEntry(cached_headers, cache);
  9436. }
  9437. // static
  9438. void HttpCacheHugeResourceTest::SetupPrefixSparseCacheEntry(
  9439. MockHttpCache* cache) {
  9440. MockTransaction transaction(kRangeGET_TransactionOK);
  9441. transaction.handler = nullptr;
  9442. transaction.request_headers = "Range: bytes = 0-9\r\n" EXTRA_HEADER;
  9443. transaction.response_headers =
  9444. "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
  9445. "ETag: \"foo\"\n"
  9446. "Accept-Ranges: bytes\n"
  9447. "Content-Range: bytes 0-9/5000000000\n"
  9448. "Content-Length: 10\n";
  9449. AddMockTransaction(&transaction);
  9450. std::string headers;
  9451. RunTransactionTestWithResponse(cache->http_cache(), transaction, &headers);
  9452. RemoveMockTransaction(&transaction);
  9453. }
  9454. // static
  9455. void HttpCacheHugeResourceTest::SetupInfixSparseCacheEntry(
  9456. MockHttpCache* cache) {
  9457. MockTransaction transaction(kRangeGET_TransactionOK);
  9458. transaction.handler = nullptr;
  9459. transaction.request_headers = "Range: bytes = 99990-99999\r\n" EXTRA_HEADER;
  9460. transaction.response_headers =
  9461. "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
  9462. "ETag: \"foo\"\n"
  9463. "Accept-Ranges: bytes\n"
  9464. "Content-Range: bytes 99990-99999/5000000000\n"
  9465. "Content-Length: 10\n";
  9466. AddMockTransaction(&transaction);
  9467. std::string headers;
  9468. RunTransactionTestWithResponse(cache->http_cache(), transaction, &headers);
  9469. RemoveMockTransaction(&transaction);
  9470. }
  9471. // static
  9472. std::list<HugeCacheTestConfiguration>
  9473. HttpCacheHugeResourceTest::GetTestModes() {
  9474. std::list<HugeCacheTestConfiguration> test_modes;
  9475. const TransactionPhase kTransactionPhases[] = {
  9476. TransactionPhase::BEFORE_FIRST_READ, TransactionPhase::AFTER_FIRST_READ,
  9477. TransactionPhase::AFTER_NETWORK_READ};
  9478. const CacheInitializer kInitializers[] = {&SetupTruncatedCacheEntry,
  9479. &SetupPrefixSparseCacheEntry,
  9480. &SetupInfixSparseCacheEntry};
  9481. for (const auto phase : kTransactionPhases)
  9482. for (const auto initializer : kInitializers)
  9483. test_modes.emplace_back(phase, initializer);
  9484. return test_modes;
  9485. }
  9486. // static
  9487. std::list<HugeCacheTestConfiguration> HttpCacheHugeResourceTest::kTestModes =
  9488. HttpCacheHugeResourceTest::GetTestModes();
  9489. INSTANTIATE_TEST_SUITE_P(
  9490. _,
  9491. HttpCacheHugeResourceTest,
  9492. ::testing::ValuesIn(HttpCacheHugeResourceTest::kTestModes));
  9493. } // namespace
  9494. // Test what happens when StopCaching() is called while reading a huge resource
  9495. // fetched via GET. Various combinations of cache state and when StopCaching()
  9496. // is called is controlled by the parameter passed into the test via the
  9497. // INSTANTIATE_TEST_SUITE_P invocation above.
  9498. TEST_P(HttpCacheHugeResourceTest,
  9499. StopCachingFollowedByReadForHugeTruncatedResource) {
  9500. // This test is going to be repeated for all combinations of TransactionPhase
  9501. // and CacheInitializers returned by GetTestModes().
  9502. const TransactionPhase stop_caching_phase = GetParam().first;
  9503. const CacheInitializer cache_initializer = GetParam().second;
  9504. MockHttpCache cache;
  9505. (*cache_initializer)(&cache);
  9506. MockTransaction transaction(kSimpleGET_Transaction);
  9507. transaction.url = kRangeGET_TransactionOK.url;
  9508. transaction.handler = &LargeResourceTransactionHandler;
  9509. transaction.read_handler = &LargeBufferReader;
  9510. ScopedMockTransaction scoped_transaction(transaction);
  9511. MockHttpRequest request(transaction);
  9512. net::TestCompletionCallback callback;
  9513. std::unique_ptr<net::HttpTransaction> http_transaction;
  9514. int rv = cache.http_cache()->CreateTransaction(net::DEFAULT_PRIORITY,
  9515. &http_transaction);
  9516. ASSERT_EQ(net::OK, rv);
  9517. ASSERT_TRUE(http_transaction.get());
  9518. bool network_transaction_started = false;
  9519. if (stop_caching_phase == TransactionPhase::AFTER_NETWORK_READ) {
  9520. http_transaction->SetBeforeNetworkStartCallback(base::BindOnce(
  9521. &SetFlagOnBeforeNetworkStart, &network_transaction_started));
  9522. }
  9523. rv = http_transaction->Start(&request, callback.callback(),
  9524. NetLogWithSource());
  9525. rv = callback.GetResult(rv);
  9526. ASSERT_EQ(net::OK, rv);
  9527. if (stop_caching_phase == TransactionPhase::BEFORE_FIRST_READ)
  9528. http_transaction->StopCaching();
  9529. int64_t total_bytes_received = 0;
  9530. EXPECT_EQ(kTotalSize,
  9531. http_transaction->GetResponseInfo()->headers->GetContentLength());
  9532. do {
  9533. // This test simulates reading gigabytes of data. Buffer size is set to 10MB
  9534. // to reduce the number of reads and speed up the test.
  9535. const int kBufferSize = 1024 * 1024 * 10;
  9536. scoped_refptr<net::IOBuffer> buf =
  9537. base::MakeRefCounted<net::IOBuffer>(kBufferSize);
  9538. rv = http_transaction->Read(buf.get(), kBufferSize, callback.callback());
  9539. rv = callback.GetResult(rv);
  9540. if (stop_caching_phase == TransactionPhase::AFTER_FIRST_READ &&
  9541. total_bytes_received == 0) {
  9542. http_transaction->StopCaching();
  9543. }
  9544. if (rv > 0)
  9545. total_bytes_received += rv;
  9546. if (network_transaction_started &&
  9547. stop_caching_phase == TransactionPhase::AFTER_NETWORK_READ) {
  9548. http_transaction->StopCaching();
  9549. network_transaction_started = false;
  9550. }
  9551. } while (rv > 0);
  9552. // The only verification we are going to do is that the received resource has
  9553. // the correct size. This is sufficient to verify that the state machine
  9554. // didn't terminate abruptly due to the StopCaching() call.
  9555. EXPECT_EQ(kTotalSize, total_bytes_received);
  9556. }
  9557. // Tests that we detect truncated resources from the net when there is
  9558. // a Content-Length header.
  9559. TEST_F(HttpCacheTest, TruncatedByContentLength) {
  9560. MockHttpCache cache;
  9561. TestCompletionCallback callback;
  9562. MockTransaction transaction(kSimpleGET_Transaction);
  9563. AddMockTransaction(&transaction);
  9564. transaction.response_headers = "Cache-Control: max-age=10000\n"
  9565. "Content-Length: 100\n";
  9566. RunTransactionTest(cache.http_cache(), transaction);
  9567. RemoveMockTransaction(&transaction);
  9568. // Read from the cache.
  9569. RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
  9570. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  9571. EXPECT_EQ(0, cache.disk_cache()->open_count());
  9572. EXPECT_EQ(2, cache.disk_cache()->create_count());
  9573. }
  9574. // Tests that we actually flag entries as truncated when we detect an error
  9575. // from the net.
  9576. TEST_F(HttpCacheTest, TruncatedByContentLength2) {
  9577. MockHttpCache cache;
  9578. TestCompletionCallback callback;
  9579. MockTransaction transaction(kSimpleGET_Transaction);
  9580. AddMockTransaction(&transaction);
  9581. transaction.response_headers = "Cache-Control: max-age=10000\n"
  9582. "Content-Length: 100\n"
  9583. "Etag: \"foo\"\n";
  9584. RunTransactionTest(cache.http_cache(), transaction);
  9585. RemoveMockTransaction(&transaction);
  9586. // Verify that the entry is marked as incomplete.
  9587. MockHttpRequest request(transaction);
  9588. VerifyTruncatedFlag(&cache, request.CacheKey(), true, 0);
  9589. }
  9590. // Make sure that calling SetPriority on a cache transaction passes on
  9591. // its priority updates to its underlying network transaction.
  9592. TEST_F(HttpCacheTest, SetPriority) {
  9593. MockHttpCache cache;
  9594. HttpRequestInfo info;
  9595. std::unique_ptr<HttpTransaction> trans;
  9596. ASSERT_THAT(cache.http_cache()->CreateTransaction(IDLE, &trans), IsOk());
  9597. // Shouldn't crash, but doesn't do anything either.
  9598. trans->SetPriority(LOW);
  9599. EXPECT_FALSE(cache.network_layer()->last_transaction());
  9600. EXPECT_EQ(DEFAULT_PRIORITY,
  9601. cache.network_layer()->last_create_transaction_priority());
  9602. info.url = GURL(kSimpleGET_Transaction.url);
  9603. TestCompletionCallback callback;
  9604. EXPECT_EQ(ERR_IO_PENDING,
  9605. trans->Start(&info, callback.callback(), NetLogWithSource()));
  9606. EXPECT_TRUE(cache.network_layer()->last_transaction());
  9607. if (cache.network_layer()->last_transaction()) {
  9608. EXPECT_EQ(LOW, cache.network_layer()->last_create_transaction_priority());
  9609. EXPECT_EQ(LOW, cache.network_layer()->last_transaction()->priority());
  9610. }
  9611. trans->SetPriority(HIGHEST);
  9612. if (cache.network_layer()->last_transaction()) {
  9613. EXPECT_EQ(LOW, cache.network_layer()->last_create_transaction_priority());
  9614. EXPECT_EQ(HIGHEST, cache.network_layer()->last_transaction()->priority());
  9615. }
  9616. EXPECT_THAT(callback.WaitForResult(), IsOk());
  9617. }
  9618. // Make sure that calling SetWebSocketHandshakeStreamCreateHelper on a cache
  9619. // transaction passes on its argument to the underlying network transaction.
  9620. TEST_F(HttpCacheTest, SetWebSocketHandshakeStreamCreateHelper) {
  9621. MockHttpCache cache;
  9622. HttpRequestInfo info;
  9623. FakeWebSocketHandshakeStreamCreateHelper create_helper;
  9624. std::unique_ptr<HttpTransaction> trans;
  9625. ASSERT_THAT(cache.http_cache()->CreateTransaction(IDLE, &trans), IsOk());
  9626. EXPECT_FALSE(cache.network_layer()->last_transaction());
  9627. info.url = GURL(kSimpleGET_Transaction.url);
  9628. TestCompletionCallback callback;
  9629. EXPECT_EQ(ERR_IO_PENDING,
  9630. trans->Start(&info, callback.callback(), NetLogWithSource()));
  9631. ASSERT_TRUE(cache.network_layer()->last_transaction());
  9632. EXPECT_FALSE(cache.network_layer()->last_transaction()->
  9633. websocket_handshake_stream_create_helper());
  9634. trans->SetWebSocketHandshakeStreamCreateHelper(&create_helper);
  9635. EXPECT_EQ(&create_helper,
  9636. cache.network_layer()->last_transaction()->
  9637. websocket_handshake_stream_create_helper());
  9638. EXPECT_THAT(callback.WaitForResult(), IsOk());
  9639. }
  9640. // Make sure that a cache transaction passes on its priority to
  9641. // newly-created network transactions.
  9642. TEST_F(HttpCacheTest, SetPriorityNewTransaction) {
  9643. MockHttpCache cache;
  9644. AddMockTransaction(&kRangeGET_TransactionOK);
  9645. std::string raw_headers("HTTP/1.1 200 OK\n"
  9646. "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
  9647. "ETag: \"foo\"\n"
  9648. "Accept-Ranges: bytes\n"
  9649. "Content-Length: 80\n");
  9650. CreateTruncatedEntry(raw_headers, &cache);
  9651. // Now make a regular request.
  9652. std::string headers;
  9653. MockTransaction transaction(kRangeGET_TransactionOK);
  9654. transaction.request_headers = EXTRA_HEADER;
  9655. transaction.data = kFullRangeData;
  9656. std::unique_ptr<HttpTransaction> trans;
  9657. ASSERT_THAT(cache.http_cache()->CreateTransaction(MEDIUM, &trans), IsOk());
  9658. EXPECT_EQ(DEFAULT_PRIORITY,
  9659. cache.network_layer()->last_create_transaction_priority());
  9660. MockHttpRequest info(transaction);
  9661. TestCompletionCallback callback;
  9662. EXPECT_EQ(ERR_IO_PENDING,
  9663. trans->Start(&info, callback.callback(), NetLogWithSource()));
  9664. EXPECT_THAT(callback.WaitForResult(), IsOk());
  9665. EXPECT_EQ(MEDIUM, cache.network_layer()->last_create_transaction_priority());
  9666. trans->SetPriority(HIGHEST);
  9667. // Should trigger a new network transaction and pick up the new
  9668. // priority.
  9669. ReadAndVerifyTransaction(trans.get(), transaction);
  9670. EXPECT_EQ(HIGHEST, cache.network_layer()->last_create_transaction_priority());
  9671. RemoveMockTransaction(&kRangeGET_TransactionOK);
  9672. }
  9673. namespace {
  9674. void RunTransactionAndGetNetworkBytes(MockHttpCache* cache,
  9675. const MockTransaction& trans_info,
  9676. int64_t* sent_bytes,
  9677. int64_t* received_bytes) {
  9678. RunTransactionTestBase(
  9679. cache->http_cache(), trans_info, MockHttpRequest(trans_info), nullptr,
  9680. NetLogWithSource(), nullptr, sent_bytes, received_bytes, nullptr);
  9681. }
  9682. } // namespace
  9683. TEST_F(HttpCacheTest, NetworkBytesCacheMissAndThenHit) {
  9684. MockHttpCache cache;
  9685. MockTransaction transaction(kSimpleGET_Transaction);
  9686. int64_t sent, received;
  9687. RunTransactionAndGetNetworkBytes(&cache, transaction, &sent, &received);
  9688. EXPECT_EQ(MockNetworkTransaction::kTotalSentBytes, sent);
  9689. EXPECT_EQ(MockNetworkTransaction::kTotalReceivedBytes, received);
  9690. RunTransactionAndGetNetworkBytes(&cache, transaction, &sent, &received);
  9691. EXPECT_EQ(0, sent);
  9692. EXPECT_EQ(0, received);
  9693. }
  9694. TEST_F(HttpCacheTest, NetworkBytesConditionalRequest304) {
  9695. MockHttpCache cache;
  9696. ScopedMockTransaction transaction(kETagGET_Transaction);
  9697. int64_t sent, received;
  9698. RunTransactionAndGetNetworkBytes(&cache, transaction, &sent, &received);
  9699. EXPECT_EQ(MockNetworkTransaction::kTotalSentBytes, sent);
  9700. EXPECT_EQ(MockNetworkTransaction::kTotalReceivedBytes, received);
  9701. transaction.load_flags = LOAD_VALIDATE_CACHE;
  9702. transaction.handler = ETagGet_ConditionalRequest_Handler;
  9703. RunTransactionAndGetNetworkBytes(&cache, transaction, &sent, &received);
  9704. EXPECT_EQ(MockNetworkTransaction::kTotalSentBytes, sent);
  9705. EXPECT_EQ(MockNetworkTransaction::kTotalReceivedBytes, received);
  9706. }
  9707. TEST_F(HttpCacheTest, NetworkBytesConditionalRequest200) {
  9708. MockHttpCache cache;
  9709. MockTransaction transaction(kTypicalGET_Transaction);
  9710. transaction.request_headers = "Foo: bar\r\n";
  9711. transaction.response_headers =
  9712. "Date: Wed, 28 Nov 2007 09:40:09 GMT\n"
  9713. "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n"
  9714. "Etag: \"foopy\"\n"
  9715. "Cache-Control: max-age=0\n"
  9716. "Vary: Foo\n";
  9717. AddMockTransaction(&transaction);
  9718. int64_t sent, received;
  9719. RunTransactionAndGetNetworkBytes(&cache, transaction, &sent, &received);
  9720. EXPECT_EQ(MockNetworkTransaction::kTotalSentBytes, sent);
  9721. EXPECT_EQ(MockNetworkTransaction::kTotalReceivedBytes, received);
  9722. RevalidationServer server;
  9723. transaction.handler = server.Handler;
  9724. transaction.request_headers = "Foo: none\r\n";
  9725. RunTransactionAndGetNetworkBytes(&cache, transaction, &sent, &received);
  9726. EXPECT_EQ(MockNetworkTransaction::kTotalSentBytes, sent);
  9727. EXPECT_EQ(MockNetworkTransaction::kTotalReceivedBytes, received);
  9728. RemoveMockTransaction(&transaction);
  9729. }
  9730. TEST_F(HttpCacheTest, NetworkBytesRange) {
  9731. MockHttpCache cache;
  9732. AddMockTransaction(&kRangeGET_TransactionOK);
  9733. MockTransaction transaction(kRangeGET_TransactionOK);
  9734. // Read bytes 40-49 from the network.
  9735. int64_t sent, received;
  9736. RunTransactionAndGetNetworkBytes(&cache, transaction, &sent, &received);
  9737. EXPECT_EQ(MockNetworkTransaction::kTotalSentBytes, sent);
  9738. EXPECT_EQ(MockNetworkTransaction::kTotalReceivedBytes, received);
  9739. // Read bytes 40-49 from the cache.
  9740. RunTransactionAndGetNetworkBytes(&cache, transaction, &sent, &received);
  9741. EXPECT_EQ(0, sent);
  9742. EXPECT_EQ(0, received);
  9743. base::RunLoop().RunUntilIdle();
  9744. // Read bytes 30-39 from the network.
  9745. transaction.request_headers = "Range: bytes = 30-39\r\n" EXTRA_HEADER;
  9746. transaction.data = "rg: 30-39 ";
  9747. RunTransactionAndGetNetworkBytes(&cache, transaction, &sent, &received);
  9748. EXPECT_EQ(MockNetworkTransaction::kTotalSentBytes, sent);
  9749. EXPECT_EQ(MockNetworkTransaction::kTotalReceivedBytes, received);
  9750. base::RunLoop().RunUntilIdle();
  9751. // Read bytes 20-29 and 50-59 from the network, bytes 30-49 from the cache.
  9752. transaction.request_headers = "Range: bytes = 20-59\r\n" EXTRA_HEADER;
  9753. transaction.data = "rg: 20-29 rg: 30-39 rg: 40-49 rg: 50-59 ";
  9754. RunTransactionAndGetNetworkBytes(&cache, transaction, &sent, &received);
  9755. EXPECT_EQ(MockNetworkTransaction::kTotalSentBytes * 2, sent);
  9756. EXPECT_EQ(MockNetworkTransaction::kTotalReceivedBytes * 2, received);
  9757. RemoveMockTransaction(&kRangeGET_TransactionOK);
  9758. }
  9759. class HttpCachePrefetchValidationTest : public TestWithTaskEnvironment {
  9760. protected:
  9761. static const int kNumSecondsPerMinute = 60;
  9762. static const int kMaxAgeSecs = 100;
  9763. static const int kRequireValidationSecs = kMaxAgeSecs + 1;
  9764. HttpCachePrefetchValidationTest() : transaction_(kSimpleGET_Transaction) {
  9765. DCHECK_LT(kMaxAgeSecs, prefetch_reuse_mins() * kNumSecondsPerMinute);
  9766. cache_.http_cache()->SetClockForTesting(&clock_);
  9767. cache_.network_layer()->SetClock(&clock_);
  9768. transaction_.response_headers = "Cache-Control: max-age=100\n";
  9769. }
  9770. bool TransactionRequiredNetwork(int load_flags) {
  9771. int pre_transaction_count = transaction_count();
  9772. transaction_.load_flags = load_flags;
  9773. RunTransactionTest(cache_.http_cache(), transaction_);
  9774. return pre_transaction_count != transaction_count();
  9775. }
  9776. void AdvanceTime(int seconds) { clock_.Advance(base::Seconds(seconds)); }
  9777. int prefetch_reuse_mins() { return HttpCache::kPrefetchReuseMins; }
  9778. // How many times this test has sent requests to the (fake) origin
  9779. // server. Every test case needs to make at least one request to initialise
  9780. // the cache.
  9781. int transaction_count() {
  9782. return cache_.network_layer()->transaction_count();
  9783. }
  9784. MockHttpCache cache_;
  9785. ScopedMockTransaction transaction_;
  9786. std::string response_headers_;
  9787. base::SimpleTestClock clock_;
  9788. };
  9789. TEST_F(HttpCachePrefetchValidationTest, SkipValidationShortlyAfterPrefetch) {
  9790. EXPECT_TRUE(TransactionRequiredNetwork(LOAD_PREFETCH));
  9791. AdvanceTime(kRequireValidationSecs);
  9792. EXPECT_FALSE(TransactionRequiredNetwork(LOAD_NORMAL));
  9793. }
  9794. TEST_F(HttpCachePrefetchValidationTest, ValidateLongAfterPrefetch) {
  9795. EXPECT_TRUE(TransactionRequiredNetwork(LOAD_PREFETCH));
  9796. AdvanceTime(prefetch_reuse_mins() * kNumSecondsPerMinute);
  9797. EXPECT_TRUE(TransactionRequiredNetwork(LOAD_NORMAL));
  9798. }
  9799. TEST_F(HttpCachePrefetchValidationTest, SkipValidationOnceOnly) {
  9800. EXPECT_TRUE(TransactionRequiredNetwork(LOAD_PREFETCH));
  9801. AdvanceTime(kRequireValidationSecs);
  9802. EXPECT_FALSE(TransactionRequiredNetwork(LOAD_NORMAL));
  9803. EXPECT_TRUE(TransactionRequiredNetwork(LOAD_NORMAL));
  9804. }
  9805. TEST_F(HttpCachePrefetchValidationTest, SkipValidationOnceReadOnly) {
  9806. EXPECT_TRUE(TransactionRequiredNetwork(LOAD_PREFETCH));
  9807. AdvanceTime(kRequireValidationSecs);
  9808. EXPECT_FALSE(TransactionRequiredNetwork(LOAD_ONLY_FROM_CACHE |
  9809. LOAD_SKIP_CACHE_VALIDATION));
  9810. EXPECT_TRUE(TransactionRequiredNetwork(LOAD_NORMAL));
  9811. }
  9812. TEST_F(HttpCachePrefetchValidationTest, BypassCacheOverwritesPrefetch) {
  9813. EXPECT_TRUE(TransactionRequiredNetwork(LOAD_PREFETCH));
  9814. AdvanceTime(kRequireValidationSecs);
  9815. EXPECT_TRUE(TransactionRequiredNetwork(LOAD_BYPASS_CACHE));
  9816. AdvanceTime(kRequireValidationSecs);
  9817. EXPECT_TRUE(TransactionRequiredNetwork(LOAD_NORMAL));
  9818. }
  9819. TEST_F(HttpCachePrefetchValidationTest,
  9820. SkipValidationOnExistingEntryThatNeedsValidation) {
  9821. EXPECT_TRUE(TransactionRequiredNetwork(LOAD_NORMAL));
  9822. AdvanceTime(kRequireValidationSecs);
  9823. EXPECT_TRUE(TransactionRequiredNetwork(LOAD_PREFETCH));
  9824. AdvanceTime(kRequireValidationSecs);
  9825. EXPECT_FALSE(TransactionRequiredNetwork(LOAD_NORMAL));
  9826. EXPECT_TRUE(TransactionRequiredNetwork(LOAD_NORMAL));
  9827. }
  9828. TEST_F(HttpCachePrefetchValidationTest,
  9829. SkipValidationOnExistingEntryThatDoesNotNeedValidation) {
  9830. EXPECT_TRUE(TransactionRequiredNetwork(LOAD_NORMAL));
  9831. EXPECT_FALSE(TransactionRequiredNetwork(LOAD_PREFETCH));
  9832. AdvanceTime(kRequireValidationSecs);
  9833. EXPECT_FALSE(TransactionRequiredNetwork(LOAD_NORMAL));
  9834. EXPECT_TRUE(TransactionRequiredNetwork(LOAD_NORMAL));
  9835. }
  9836. TEST_F(HttpCachePrefetchValidationTest, PrefetchMultipleTimes) {
  9837. EXPECT_TRUE(TransactionRequiredNetwork(LOAD_PREFETCH));
  9838. EXPECT_FALSE(TransactionRequiredNetwork(LOAD_PREFETCH));
  9839. AdvanceTime(kRequireValidationSecs);
  9840. EXPECT_FALSE(TransactionRequiredNetwork(LOAD_NORMAL));
  9841. }
  9842. TEST_F(HttpCachePrefetchValidationTest, ValidateOnDelayedSecondPrefetch) {
  9843. EXPECT_TRUE(TransactionRequiredNetwork(LOAD_PREFETCH));
  9844. AdvanceTime(kRequireValidationSecs);
  9845. EXPECT_TRUE(TransactionRequiredNetwork(LOAD_PREFETCH));
  9846. AdvanceTime(kRequireValidationSecs);
  9847. EXPECT_FALSE(TransactionRequiredNetwork(LOAD_NORMAL));
  9848. }
  9849. TEST_F(HttpCacheTest, StaleContentNotUsedWhenLoadFlagNotSet) {
  9850. MockHttpCache cache;
  9851. ScopedMockTransaction stale_while_revalidate_transaction(
  9852. kSimpleGET_Transaction);
  9853. stale_while_revalidate_transaction.response_headers =
  9854. "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
  9855. "Age: 10801\n"
  9856. "Cache-Control: max-age=0,stale-while-revalidate=86400\n";
  9857. // Write to the cache.
  9858. RunTransactionTest(cache.http_cache(), stale_while_revalidate_transaction);
  9859. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  9860. // Send the request again and check that it is sent to the network again.
  9861. HttpResponseInfo response_info;
  9862. RunTransactionTestWithResponseInfo(
  9863. cache.http_cache(), stale_while_revalidate_transaction, &response_info);
  9864. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  9865. EXPECT_FALSE(response_info.async_revalidation_requested);
  9866. }
  9867. TEST_F(HttpCacheTest, StaleContentUsedWhenLoadFlagSetAndUsableThenTimesout) {
  9868. MockHttpCache cache;
  9869. base::SimpleTestClock clock;
  9870. cache.http_cache()->SetClockForTesting(&clock);
  9871. cache.network_layer()->SetClock(&clock);
  9872. clock.Advance(base::Seconds(10));
  9873. ScopedMockTransaction stale_while_revalidate_transaction(
  9874. kSimpleGET_Transaction);
  9875. stale_while_revalidate_transaction.load_flags |=
  9876. LOAD_SUPPORT_ASYNC_REVALIDATION;
  9877. stale_while_revalidate_transaction.response_headers =
  9878. "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
  9879. "Age: 10801\n"
  9880. "Cache-Control: max-age=0,stale-while-revalidate=86400\n";
  9881. // Write to the cache.
  9882. RunTransactionTest(cache.http_cache(), stale_while_revalidate_transaction);
  9883. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  9884. // Send the request again and check that it is not sent to the network again.
  9885. HttpResponseInfo response_info;
  9886. RunTransactionTestWithResponseInfo(
  9887. cache.http_cache(), stale_while_revalidate_transaction, &response_info);
  9888. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  9889. EXPECT_TRUE(response_info.async_revalidation_requested);
  9890. EXPECT_FALSE(response_info.stale_revalidate_timeout.is_null());
  9891. // Move forward in time such that the stale response is no longer valid.
  9892. clock.SetNow(response_info.stale_revalidate_timeout);
  9893. clock.Advance(base::Seconds(1));
  9894. RunTransactionTestWithResponseInfo(
  9895. cache.http_cache(), stale_while_revalidate_transaction, &response_info);
  9896. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  9897. EXPECT_FALSE(response_info.async_revalidation_requested);
  9898. }
  9899. TEST_F(HttpCacheTest, StaleContentUsedWhenLoadFlagSetAndUsable) {
  9900. MockHttpCache cache;
  9901. base::SimpleTestClock clock;
  9902. cache.http_cache()->SetClockForTesting(&clock);
  9903. cache.network_layer()->SetClock(&clock);
  9904. clock.Advance(base::Seconds(10));
  9905. ScopedMockTransaction stale_while_revalidate_transaction(
  9906. kSimpleGET_Transaction);
  9907. stale_while_revalidate_transaction.load_flags |=
  9908. LOAD_SUPPORT_ASYNC_REVALIDATION;
  9909. stale_while_revalidate_transaction.response_headers =
  9910. "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
  9911. "Age: 10801\n"
  9912. "Cache-Control: max-age=0,stale-while-revalidate=86400\n";
  9913. // Write to the cache.
  9914. RunTransactionTest(cache.http_cache(), stale_while_revalidate_transaction);
  9915. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  9916. // Send the request again and check that it is not sent to the network again.
  9917. HttpResponseInfo response_info;
  9918. RunTransactionTestWithResponseInfo(
  9919. cache.http_cache(), stale_while_revalidate_transaction, &response_info);
  9920. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  9921. EXPECT_TRUE(response_info.async_revalidation_requested);
  9922. EXPECT_FALSE(response_info.stale_revalidate_timeout.is_null());
  9923. base::Time revalidation_timeout = response_info.stale_revalidate_timeout;
  9924. clock.Advance(base::Seconds(1));
  9925. EXPECT_TRUE(clock.Now() < revalidation_timeout);
  9926. // Fetch the resource again inside the revalidation timeout window.
  9927. RunTransactionTestWithResponseInfo(
  9928. cache.http_cache(), stale_while_revalidate_transaction, &response_info);
  9929. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  9930. EXPECT_TRUE(response_info.async_revalidation_requested);
  9931. EXPECT_FALSE(response_info.stale_revalidate_timeout.is_null());
  9932. // Expect that the original revalidation timeout hasn't changed.
  9933. EXPECT_TRUE(revalidation_timeout == response_info.stale_revalidate_timeout);
  9934. // mask of async revalidation flag.
  9935. stale_while_revalidate_transaction.load_flags &=
  9936. ~LOAD_SUPPORT_ASYNC_REVALIDATION;
  9937. stale_while_revalidate_transaction.status = "HTTP/1.1 304 Not Modified";
  9938. // Write 304 to the cache.
  9939. RunTransactionTestWithResponseInfo(
  9940. cache.http_cache(), stale_while_revalidate_transaction, &response_info);
  9941. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  9942. EXPECT_FALSE(response_info.async_revalidation_requested);
  9943. EXPECT_TRUE(response_info.stale_revalidate_timeout.is_null());
  9944. }
  9945. TEST_F(HttpCacheTest, StaleContentNotUsedWhenUnusable) {
  9946. MockHttpCache cache;
  9947. ScopedMockTransaction stale_while_revalidate_transaction(
  9948. kSimpleGET_Transaction);
  9949. stale_while_revalidate_transaction.load_flags |=
  9950. LOAD_SUPPORT_ASYNC_REVALIDATION;
  9951. stale_while_revalidate_transaction.response_headers =
  9952. "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
  9953. "Age: 10801\n"
  9954. "Cache-Control: max-age=0,stale-while-revalidate=1800\n";
  9955. // Write to the cache.
  9956. RunTransactionTest(cache.http_cache(), stale_while_revalidate_transaction);
  9957. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  9958. // Send the request again and check that it is sent to the network again.
  9959. HttpResponseInfo response_info;
  9960. RunTransactionTestWithResponseInfo(
  9961. cache.http_cache(), stale_while_revalidate_transaction, &response_info);
  9962. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  9963. EXPECT_FALSE(response_info.async_revalidation_requested);
  9964. }
  9965. TEST_F(HttpCacheTest, StaleContentWriteError) {
  9966. MockHttpCache cache;
  9967. base::SimpleTestClock clock;
  9968. cache.http_cache()->SetClockForTesting(&clock);
  9969. cache.network_layer()->SetClock(&clock);
  9970. clock.Advance(base::Seconds(10));
  9971. ScopedMockTransaction stale_while_revalidate_transaction(
  9972. kSimpleGET_Transaction);
  9973. stale_while_revalidate_transaction.load_flags |=
  9974. LOAD_SUPPORT_ASYNC_REVALIDATION;
  9975. stale_while_revalidate_transaction.response_headers =
  9976. "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
  9977. "Age: 10801\n"
  9978. "Cache-Control: max-age=0,stale-while-revalidate=86400\n";
  9979. // Write to the cache.
  9980. RunTransactionTest(cache.http_cache(), stale_while_revalidate_transaction);
  9981. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  9982. // Send the request again but inject a write fault. Should still work
  9983. // (and not dereference any null pointers).
  9984. cache.disk_cache()->set_soft_failures_mask(MockDiskEntry::FAIL_WRITE);
  9985. HttpResponseInfo response_info;
  9986. RunTransactionTestWithResponseInfo(
  9987. cache.http_cache(), stale_while_revalidate_transaction, &response_info);
  9988. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  9989. }
  9990. // Tests that we allow multiple simultaneous, non-overlapping transactions to
  9991. // take place on a sparse entry.
  9992. TEST_F(HttpCacheTest, RangeGET_MultipleRequests) {
  9993. MockHttpCache cache;
  9994. // Create a transaction for bytes 0-9.
  9995. MockHttpRequest request(kRangeGET_TransactionOK);
  9996. MockTransaction transaction(kRangeGET_TransactionOK);
  9997. transaction.request_headers = "Range: bytes = 0-9\r\n" EXTRA_HEADER;
  9998. transaction.data = "rg: 00-09 ";
  9999. AddMockTransaction(&transaction);
  10000. TestCompletionCallback callback;
  10001. std::unique_ptr<HttpTransaction> trans;
  10002. int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans);
  10003. EXPECT_THAT(rv, IsOk());
  10004. ASSERT_TRUE(trans.get());
  10005. // Start our transaction.
  10006. trans->Start(&request, callback.callback(), NetLogWithSource());
  10007. // A second transaction on a different part of the file (the default
  10008. // kRangeGET_TransactionOK requests 40-49) should not be blocked by
  10009. // the already pending transaction.
  10010. RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK);
  10011. // Let the first transaction complete.
  10012. callback.WaitForResult();
  10013. RemoveMockTransaction(&transaction);
  10014. }
  10015. // Makes sure that a request stops using the cache when the response headers
  10016. // with "Cache-Control: no-store" arrives. That means that another request for
  10017. // the same URL can be processed before the response body of the original
  10018. // request arrives.
  10019. TEST_F(HttpCacheTest, NoStoreResponseShouldNotBlockFollowingRequests) {
  10020. MockHttpCache cache;
  10021. ScopedMockTransaction mock_transaction(kSimpleGET_Transaction);
  10022. mock_transaction.response_headers = "Cache-Control: no-store\n";
  10023. MockHttpRequest request(mock_transaction);
  10024. auto first = std::make_unique<Context>();
  10025. first->result = cache.CreateTransaction(&first->trans);
  10026. ASSERT_THAT(first->result, IsOk());
  10027. EXPECT_EQ(LOAD_STATE_IDLE, first->trans->GetLoadState());
  10028. first->result = first->trans->Start(&request, first->callback.callback(),
  10029. NetLogWithSource());
  10030. EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, first->trans->GetLoadState());
  10031. base::RunLoop().RunUntilIdle();
  10032. EXPECT_EQ(LOAD_STATE_IDLE, first->trans->GetLoadState());
  10033. ASSERT_TRUE(first->trans->GetResponseInfo());
  10034. EXPECT_TRUE(first->trans->GetResponseInfo()->headers->HasHeaderValue(
  10035. "Cache-Control", "no-store"));
  10036. // Here we have read the response header but not read the response body yet.
  10037. // Let us create the second (read) transaction.
  10038. auto second = std::make_unique<Context>();
  10039. second->result = cache.CreateTransaction(&second->trans);
  10040. ASSERT_THAT(second->result, IsOk());
  10041. EXPECT_EQ(LOAD_STATE_IDLE, second->trans->GetLoadState());
  10042. second->result = second->trans->Start(&request, second->callback.callback(),
  10043. NetLogWithSource());
  10044. // Here the second transaction proceeds without reading the first body.
  10045. EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, second->trans->GetLoadState());
  10046. base::RunLoop().RunUntilIdle();
  10047. EXPECT_EQ(LOAD_STATE_IDLE, second->trans->GetLoadState());
  10048. ASSERT_TRUE(second->trans->GetResponseInfo());
  10049. EXPECT_TRUE(second->trans->GetResponseInfo()->headers->HasHeaderValue(
  10050. "Cache-Control", "no-store"));
  10051. ReadAndVerifyTransaction(second->trans.get(), kSimpleGET_Transaction);
  10052. }
  10053. // Tests that serving a response entirely from cache replays the previous
  10054. // SSLInfo.
  10055. TEST_F(HttpCacheTest, CachePreservesSSLInfo) {
  10056. static const uint16_t kTLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 = 0xc02f;
  10057. int status = 0;
  10058. SSLConnectionStatusSetCipherSuite(kTLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
  10059. &status);
  10060. SSLConnectionStatusSetVersion(SSL_CONNECTION_VERSION_TLS1_2, &status);
  10061. scoped_refptr<X509Certificate> cert =
  10062. ImportCertFromFile(GetTestCertsDirectory(), "ok_cert.pem");
  10063. MockHttpCache cache;
  10064. ScopedMockTransaction transaction(kSimpleGET_Transaction);
  10065. transaction.cert = cert;
  10066. transaction.ssl_connection_status = status;
  10067. // Fetch the resource.
  10068. HttpResponseInfo response_info;
  10069. RunTransactionTestWithResponseInfo(cache.http_cache(), transaction,
  10070. &response_info);
  10071. // The request should have hit the network and a cache entry created.
  10072. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  10073. EXPECT_EQ(0, cache.disk_cache()->open_count());
  10074. EXPECT_EQ(1, cache.disk_cache()->create_count());
  10075. // The expected SSL state was reported.
  10076. EXPECT_EQ(transaction.ssl_connection_status,
  10077. response_info.ssl_info.connection_status);
  10078. EXPECT_TRUE(cert->EqualsIncludingChain(response_info.ssl_info.cert.get()));
  10079. // Fetch the resource again.
  10080. RunTransactionTestWithResponseInfo(cache.http_cache(), transaction,
  10081. &response_info);
  10082. // The request should have been reused without hitting the network.
  10083. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  10084. EXPECT_EQ(1, cache.disk_cache()->open_count());
  10085. EXPECT_EQ(1, cache.disk_cache()->create_count());
  10086. // The SSL state was preserved.
  10087. EXPECT_EQ(status, response_info.ssl_info.connection_status);
  10088. EXPECT_TRUE(cert->EqualsIncludingChain(response_info.ssl_info.cert.get()));
  10089. }
  10090. // Tests that SSLInfo gets updated when revalidating a cached response.
  10091. TEST_F(HttpCacheTest, RevalidationUpdatesSSLInfo) {
  10092. static const uint16_t kTLS_RSA_WITH_RC4_128_MD5 = 0x0004;
  10093. static const uint16_t kTLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 = 0xc02f;
  10094. int status1 = 0;
  10095. SSLConnectionStatusSetCipherSuite(kTLS_RSA_WITH_RC4_128_MD5, &status1);
  10096. SSLConnectionStatusSetVersion(SSL_CONNECTION_VERSION_TLS1, &status1);
  10097. int status2 = 0;
  10098. SSLConnectionStatusSetCipherSuite(kTLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
  10099. &status2);
  10100. SSLConnectionStatusSetVersion(SSL_CONNECTION_VERSION_TLS1_2, &status2);
  10101. scoped_refptr<X509Certificate> cert1 =
  10102. ImportCertFromFile(GetTestCertsDirectory(), "expired_cert.pem");
  10103. scoped_refptr<X509Certificate> cert2 =
  10104. ImportCertFromFile(GetTestCertsDirectory(), "ok_cert.pem");
  10105. MockHttpCache cache;
  10106. ScopedMockTransaction transaction(kTypicalGET_Transaction);
  10107. transaction.cert = cert1;
  10108. transaction.ssl_connection_status = status1;
  10109. // Fetch the resource.
  10110. HttpResponseInfo response_info;
  10111. RunTransactionTestWithResponseInfo(cache.http_cache(), transaction,
  10112. &response_info);
  10113. // The request should have hit the network and a cache entry created.
  10114. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  10115. EXPECT_EQ(0, cache.disk_cache()->open_count());
  10116. EXPECT_EQ(1, cache.disk_cache()->create_count());
  10117. EXPECT_FALSE(response_info.was_cached);
  10118. // The expected SSL state was reported.
  10119. EXPECT_EQ(status1, response_info.ssl_info.connection_status);
  10120. EXPECT_TRUE(cert1->EqualsIncludingChain(response_info.ssl_info.cert.get()));
  10121. // The server deploys a more modern configuration but reports 304 on the
  10122. // revalidation attempt.
  10123. transaction.status = "HTTP/1.1 304 Not Modified";
  10124. transaction.cert = cert2;
  10125. transaction.ssl_connection_status = status2;
  10126. // Fetch the resource again, forcing a revalidation.
  10127. transaction.request_headers = "Cache-Control: max-age=0\r\n";
  10128. RunTransactionTestWithResponseInfo(cache.http_cache(), transaction,
  10129. &response_info);
  10130. // The request should have been successfully revalidated.
  10131. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  10132. EXPECT_EQ(1, cache.disk_cache()->open_count());
  10133. EXPECT_EQ(1, cache.disk_cache()->create_count());
  10134. EXPECT_TRUE(response_info.was_cached);
  10135. // The new SSL state is reported.
  10136. EXPECT_EQ(status2, response_info.ssl_info.connection_status);
  10137. EXPECT_TRUE(cert2->EqualsIncludingChain(response_info.ssl_info.cert.get()));
  10138. }
  10139. TEST_F(HttpCacheTest, CacheEntryStatusOther) {
  10140. MockHttpCache cache;
  10141. HttpResponseInfo response_info;
  10142. RunTransactionTestWithResponseInfo(cache.http_cache(), kRangeGET_Transaction,
  10143. &response_info);
  10144. EXPECT_FALSE(response_info.was_cached);
  10145. EXPECT_TRUE(response_info.network_accessed);
  10146. EXPECT_EQ(CacheEntryStatus::ENTRY_OTHER, response_info.cache_entry_status);
  10147. }
  10148. TEST_F(HttpCacheTest, CacheEntryStatusNotInCache) {
  10149. MockHttpCache cache;
  10150. HttpResponseInfo response_info;
  10151. RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction,
  10152. &response_info);
  10153. EXPECT_FALSE(response_info.was_cached);
  10154. EXPECT_TRUE(response_info.network_accessed);
  10155. EXPECT_EQ(CacheEntryStatus::ENTRY_NOT_IN_CACHE,
  10156. response_info.cache_entry_status);
  10157. }
  10158. TEST_F(HttpCacheTest, CacheEntryStatusUsed) {
  10159. MockHttpCache cache;
  10160. RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
  10161. HttpResponseInfo response_info;
  10162. RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction,
  10163. &response_info);
  10164. EXPECT_TRUE(response_info.was_cached);
  10165. EXPECT_FALSE(response_info.network_accessed);
  10166. EXPECT_EQ(CacheEntryStatus::ENTRY_USED, response_info.cache_entry_status);
  10167. }
  10168. TEST_F(HttpCacheTest, CacheEntryStatusValidated) {
  10169. MockHttpCache cache;
  10170. RunTransactionTest(cache.http_cache(), kETagGET_Transaction);
  10171. ScopedMockTransaction still_valid(kETagGET_Transaction);
  10172. still_valid.load_flags = LOAD_VALIDATE_CACHE; // Force a validation.
  10173. still_valid.handler = ETagGet_ConditionalRequest_Handler;
  10174. HttpResponseInfo response_info;
  10175. RunTransactionTestWithResponseInfo(cache.http_cache(), still_valid,
  10176. &response_info);
  10177. EXPECT_TRUE(response_info.was_cached);
  10178. EXPECT_TRUE(response_info.network_accessed);
  10179. EXPECT_EQ(CacheEntryStatus::ENTRY_VALIDATED,
  10180. response_info.cache_entry_status);
  10181. }
  10182. TEST_F(HttpCacheTest, CacheEntryStatusUpdated) {
  10183. MockHttpCache cache;
  10184. RunTransactionTest(cache.http_cache(), kETagGET_Transaction);
  10185. ScopedMockTransaction update(kETagGET_Transaction);
  10186. update.load_flags = LOAD_VALIDATE_CACHE; // Force a validation.
  10187. HttpResponseInfo response_info;
  10188. RunTransactionTestWithResponseInfo(cache.http_cache(), update,
  10189. &response_info);
  10190. EXPECT_FALSE(response_info.was_cached);
  10191. EXPECT_TRUE(response_info.network_accessed);
  10192. EXPECT_EQ(CacheEntryStatus::ENTRY_UPDATED, response_info.cache_entry_status);
  10193. }
  10194. TEST_F(HttpCacheTest, CacheEntryStatusCantConditionalize) {
  10195. MockHttpCache cache;
  10196. cache.FailConditionalizations();
  10197. RunTransactionTest(cache.http_cache(), kTypicalGET_Transaction);
  10198. HttpResponseInfo response_info;
  10199. RunTransactionTestWithResponseInfo(cache.http_cache(),
  10200. kTypicalGET_Transaction, &response_info);
  10201. EXPECT_FALSE(response_info.was_cached);
  10202. EXPECT_TRUE(response_info.network_accessed);
  10203. EXPECT_EQ(CacheEntryStatus::ENTRY_CANT_CONDITIONALIZE,
  10204. response_info.cache_entry_status);
  10205. }
  10206. TEST_F(HttpSplitCacheKeyTest, GetResourceURLFromHttpCacheKey) {
  10207. base::test::ScopedFeatureList feature_list;
  10208. feature_list.InitAndEnableFeature(
  10209. net::features::kSplitCacheByNetworkIsolationKey);
  10210. MockHttpCache cache;
  10211. std::string urls[] = {"http://www.a.com/", "https://b.com/example.html",
  10212. "http://example.com/Some Path/Some Leaf?some query"};
  10213. for (const std::string& url : urls) {
  10214. std::string key = ComputeCacheKey(url);
  10215. EXPECT_EQ(GURL(url).spec(), HttpCache::GetResourceURLFromHttpCacheKey(key));
  10216. }
  10217. }
  10218. TEST_F(HttpCacheTest, GetResourceURLFromHttpCacheKey) {
  10219. const struct {
  10220. std::string input;
  10221. std::string output;
  10222. } kTestCase[] = {
  10223. // Valid input:
  10224. {"0/0/https://a.com/", "https://a.com/"},
  10225. {"0/0/https://a.com/path", "https://a.com/path"},
  10226. {"0/0/https://a.com/?query", "https://a.com/?query"},
  10227. {"0/0/https://a.com/#fragment", "https://a.com/#fragment"},
  10228. {"0/0/_dk_s_ https://a.com/", "https://a.com/"},
  10229. {"0/0/_dk_https://a.com https://b.com https://c.com/", "https://c.com/"},
  10230. {"0/0/_dk_shttps://a.com https://b.com https://c.com/", "https://c.com/"},
  10231. // Invalid input, producing garbage, without crashing.
  10232. {"", ""},
  10233. {"0/a.com", "0/a.com"},
  10234. {"https://a.com/", "a.com/"},
  10235. {"0/https://a.com/", "/a.com/"},
  10236. };
  10237. for (const auto& test : kTestCase) {
  10238. EXPECT_EQ(test.output,
  10239. HttpCache::GetResourceURLFromHttpCacheKey(test.input));
  10240. }
  10241. }
  10242. class TestCompletionCallbackForHttpCache : public TestCompletionCallbackBase {
  10243. public:
  10244. TestCompletionCallbackForHttpCache() = default;
  10245. ~TestCompletionCallbackForHttpCache() override = default;
  10246. CompletionRepeatingCallback callback() {
  10247. return base::BindRepeating(&TestCompletionCallbackForHttpCache::SetResult,
  10248. base::Unretained(this));
  10249. }
  10250. const std::vector<int>& results() { return results_; }
  10251. private:
  10252. std::vector<int> results_;
  10253. protected:
  10254. void SetResult(int result) override {
  10255. results_.push_back(result);
  10256. DidSetResult();
  10257. }
  10258. };
  10259. TEST_F(HttpCacheIOCallbackTest, FailedDoomFollowedByOpen) {
  10260. MockHttpCache cache;
  10261. TestCompletionCallbackForHttpCache cb;
  10262. std::unique_ptr<Transaction> transaction =
  10263. std::make_unique<Transaction>(DEFAULT_PRIORITY, cache.http_cache());
  10264. transaction->SetIOCallBackForTest(cb.callback());
  10265. // Create the backend here as our direct calls to DoomEntry and OpenEntry
  10266. // below require that it exists.
  10267. cache.backend();
  10268. // Need a mock transaction in order to use some of MockHttpCache's
  10269. // functions.
  10270. ScopedMockTransaction m_transaction(kSimpleGET_Transaction);
  10271. ActiveEntry* entry1 = nullptr;
  10272. cache.disk_cache()->set_force_fail_callback_later(true);
  10273. // Queue up our operations.
  10274. int rv = DoomEntry(cache.http_cache(), m_transaction.url, transaction.get());
  10275. ASSERT_EQ(rv, ERR_IO_PENDING);
  10276. cache.disk_cache()->set_force_fail_callback_later(false);
  10277. rv = OpenEntry(cache.http_cache(), m_transaction.url, &entry1,
  10278. transaction.get());
  10279. ASSERT_EQ(rv, ERR_IO_PENDING);
  10280. // Wait for all the results to arrive.
  10281. cb.GetResult(rv);
  10282. ASSERT_EQ(cb.results().size(), 2u);
  10283. // Verify that DoomEntry failed correctly.
  10284. ASSERT_EQ(cb.results()[0], ERR_CACHE_DOOM_FAILURE);
  10285. // Verify that OpenEntry fails with the same code.
  10286. ASSERT_EQ(cb.results()[1], ERR_CACHE_DOOM_FAILURE);
  10287. ASSERT_EQ(entry1, nullptr);
  10288. }
  10289. TEST_F(HttpCacheIOCallbackTest, FailedDoomFollowedByCreate) {
  10290. MockHttpCache cache;
  10291. TestCompletionCallbackForHttpCache cb;
  10292. std::unique_ptr<Transaction> transaction =
  10293. std::make_unique<Transaction>(DEFAULT_PRIORITY, cache.http_cache());
  10294. transaction->SetIOCallBackForTest(cb.callback());
  10295. // Create the backend here as our direct calls to DoomEntry and CreateEntry
  10296. // below require that it exists.
  10297. cache.backend();
  10298. // Need a mock transaction in order to use some of MockHttpCache's
  10299. // functions.
  10300. ScopedMockTransaction m_transaction(kSimpleGET_Transaction);
  10301. ActiveEntry* entry1 = nullptr;
  10302. cache.disk_cache()->set_force_fail_callback_later(true);
  10303. // Queue up our operations.
  10304. int rv = DoomEntry(cache.http_cache(), m_transaction.url, transaction.get());
  10305. ASSERT_EQ(rv, ERR_IO_PENDING);
  10306. cache.disk_cache()->set_force_fail_callback_later(false);
  10307. rv = CreateEntry(cache.http_cache(), m_transaction.url, &entry1,
  10308. transaction.get());
  10309. ASSERT_EQ(rv, ERR_IO_PENDING);
  10310. // Wait for all the results to arrive.
  10311. cb.GetResult(rv);
  10312. ASSERT_EQ(cb.results().size(), 2u);
  10313. // Verify that DoomEntry failed correctly.
  10314. ASSERT_EQ(cb.results()[0], ERR_CACHE_DOOM_FAILURE);
  10315. // Verify that CreateEntry requests a restart (CACHE_RACE).
  10316. ASSERT_EQ(cb.results()[1], ERR_CACHE_RACE);
  10317. ASSERT_EQ(entry1, nullptr);
  10318. }
  10319. TEST_F(HttpCacheIOCallbackTest, FailedDoomFollowedByDoom) {
  10320. MockHttpCache cache;
  10321. TestCompletionCallbackForHttpCache cb;
  10322. std::unique_ptr<Transaction> transaction =
  10323. std::make_unique<Transaction>(DEFAULT_PRIORITY, cache.http_cache());
  10324. transaction->SetIOCallBackForTest(cb.callback());
  10325. // Create the backend here as our direct calls to DoomEntry below require that
  10326. // it exists.
  10327. cache.backend();
  10328. // Need a mock transaction in order to use some of MockHttpCache's
  10329. // functions.
  10330. ScopedMockTransaction m_transaction(kSimpleGET_Transaction);
  10331. cache.disk_cache()->set_force_fail_callback_later(true);
  10332. // Queue up our operations.
  10333. int rv = DoomEntry(cache.http_cache(), m_transaction.url, transaction.get());
  10334. ASSERT_EQ(rv, ERR_IO_PENDING);
  10335. cache.disk_cache()->set_force_fail_callback_later(false);
  10336. rv = DoomEntry(cache.http_cache(), m_transaction.url, transaction.get());
  10337. ASSERT_EQ(rv, ERR_IO_PENDING);
  10338. // Wait for all the results to arrive.
  10339. cb.GetResult(rv);
  10340. ASSERT_EQ(cb.results().size(), 2u);
  10341. // Verify that DoomEntry failed correctly.
  10342. ASSERT_EQ(cb.results()[0], ERR_CACHE_DOOM_FAILURE);
  10343. // Verify that the second DoomEntry requests a restart (CACHE_RACE).
  10344. ASSERT_EQ(cb.results()[1], ERR_CACHE_RACE);
  10345. }
  10346. TEST_F(HttpCacheIOCallbackTest, FailedOpenFollowedByCreate) {
  10347. MockHttpCache cache;
  10348. TestCompletionCallbackForHttpCache cb;
  10349. std::unique_ptr<Transaction> transaction =
  10350. std::make_unique<Transaction>(DEFAULT_PRIORITY, cache.http_cache());
  10351. transaction->SetIOCallBackForTest(cb.callback());
  10352. // Create the backend here as our direct calls to OpenEntry and CreateEntry
  10353. // below require that it exists.
  10354. cache.backend();
  10355. // Need a mock transaction in order to use some of MockHttpCache's
  10356. // functions.
  10357. ScopedMockTransaction m_transaction(kSimpleGET_Transaction);
  10358. ActiveEntry* entry1 = nullptr;
  10359. ActiveEntry* entry2 = nullptr;
  10360. cache.disk_cache()->set_force_fail_callback_later(true);
  10361. // Queue up our operations.
  10362. int rv = OpenEntry(cache.http_cache(), m_transaction.url, &entry1,
  10363. transaction.get());
  10364. ASSERT_EQ(rv, ERR_IO_PENDING);
  10365. cache.disk_cache()->set_force_fail_callback_later(false);
  10366. rv = CreateEntry(cache.http_cache(), m_transaction.url, &entry2,
  10367. transaction.get());
  10368. ASSERT_EQ(rv, ERR_IO_PENDING);
  10369. // Wait for all the results to arrive.
  10370. cb.GetResult(rv);
  10371. ASSERT_EQ(cb.results().size(), 2u);
  10372. // Verify that OpenEntry failed correctly.
  10373. ASSERT_EQ(cb.results()[0], ERR_CACHE_OPEN_FAILURE);
  10374. ASSERT_EQ(entry1, nullptr);
  10375. // Verify that the CreateEntry requests a restart (CACHE_RACE).
  10376. ASSERT_EQ(cb.results()[1], ERR_CACHE_RACE);
  10377. ASSERT_EQ(entry2, nullptr);
  10378. }
  10379. TEST_F(HttpCacheIOCallbackTest, FailedCreateFollowedByOpen) {
  10380. MockHttpCache cache;
  10381. TestCompletionCallbackForHttpCache cb;
  10382. std::unique_ptr<Transaction> transaction =
  10383. std::make_unique<Transaction>(DEFAULT_PRIORITY, cache.http_cache());
  10384. transaction->SetIOCallBackForTest(cb.callback());
  10385. // Create the backend here as our direct calls to CreateEntry and OpenEntry
  10386. // below require that it exists.
  10387. cache.backend();
  10388. // Need a mock transaction in order to use some of MockHttpCache's
  10389. // functions.
  10390. ScopedMockTransaction m_transaction(kSimpleGET_Transaction);
  10391. ActiveEntry* entry1 = nullptr;
  10392. ActiveEntry* entry2 = nullptr;
  10393. cache.disk_cache()->set_force_fail_callback_later(true);
  10394. // Queue up our operations.
  10395. int rv = CreateEntry(cache.http_cache(), m_transaction.url, &entry1,
  10396. transaction.get());
  10397. ASSERT_EQ(rv, ERR_IO_PENDING);
  10398. cache.disk_cache()->set_force_fail_callback_later(false);
  10399. rv = OpenEntry(cache.http_cache(), m_transaction.url, &entry2,
  10400. transaction.get());
  10401. ASSERT_EQ(rv, ERR_IO_PENDING);
  10402. // Wait for all the results to arrive.
  10403. cb.GetResult(rv);
  10404. ASSERT_EQ(cb.results().size(), 2u);
  10405. // Verify that CreateEntry failed correctly.
  10406. ASSERT_EQ(cb.results()[0], ERR_CACHE_CREATE_FAILURE);
  10407. ASSERT_EQ(entry1, nullptr);
  10408. // Verify that the OpenEntry requests a restart (CACHE_RACE).
  10409. ASSERT_EQ(cb.results()[1], ERR_CACHE_RACE);
  10410. ASSERT_EQ(entry2, nullptr);
  10411. }
  10412. TEST_F(HttpCacheIOCallbackTest, FailedCreateFollowedByCreate) {
  10413. MockHttpCache cache;
  10414. TestCompletionCallbackForHttpCache cb;
  10415. std::unique_ptr<Transaction> transaction =
  10416. std::make_unique<Transaction>(DEFAULT_PRIORITY, cache.http_cache());
  10417. transaction->SetIOCallBackForTest(cb.callback());
  10418. // Create the backend here as our direct calls to CreateEntry below require
  10419. // that it exists.
  10420. cache.backend();
  10421. // Need a mock transaction in order to use some of MockHttpCache's
  10422. // functions.
  10423. ScopedMockTransaction m_transaction(kSimpleGET_Transaction);
  10424. ActiveEntry* entry1 = nullptr;
  10425. ActiveEntry* entry2 = nullptr;
  10426. cache.disk_cache()->set_force_fail_callback_later(true);
  10427. // Queue up our operations.
  10428. int rv = CreateEntry(cache.http_cache(), m_transaction.url, &entry1,
  10429. transaction.get());
  10430. ASSERT_EQ(rv, ERR_IO_PENDING);
  10431. cache.disk_cache()->set_force_fail_callback_later(false);
  10432. rv = CreateEntry(cache.http_cache(), m_transaction.url, &entry2,
  10433. transaction.get());
  10434. ASSERT_EQ(rv, ERR_IO_PENDING);
  10435. // Wait for all the results to arrive.
  10436. cb.GetResult(rv);
  10437. ASSERT_EQ(cb.results().size(), 2u);
  10438. // Verify the CreateEntry(s) failed.
  10439. ASSERT_EQ(cb.results()[0], ERR_CACHE_CREATE_FAILURE);
  10440. ASSERT_EQ(entry1, nullptr);
  10441. ASSERT_EQ(cb.results()[1], ERR_CACHE_CREATE_FAILURE);
  10442. ASSERT_EQ(entry2, nullptr);
  10443. }
  10444. TEST_F(HttpCacheIOCallbackTest, CreateFollowedByCreate) {
  10445. MockHttpCache cache;
  10446. TestCompletionCallbackForHttpCache cb;
  10447. std::unique_ptr<Transaction> transaction =
  10448. std::make_unique<Transaction>(DEFAULT_PRIORITY, cache.http_cache());
  10449. transaction->SetIOCallBackForTest(cb.callback());
  10450. // Create the backend here as our direct calls to CreateEntry below require
  10451. // that it exists.
  10452. cache.backend();
  10453. // Need a mock transaction in order to use some of MockHttpCache's
  10454. // functions.
  10455. ScopedMockTransaction m_transaction(kSimpleGET_Transaction);
  10456. ActiveEntry* entry1 = nullptr;
  10457. ActiveEntry* entry2 = nullptr;
  10458. // Queue up our operations.
  10459. int rv = CreateEntry(cache.http_cache(), m_transaction.url, &entry1,
  10460. transaction.get());
  10461. ASSERT_EQ(rv, ERR_IO_PENDING);
  10462. rv = CreateEntry(cache.http_cache(), m_transaction.url, &entry2,
  10463. transaction.get());
  10464. ASSERT_EQ(rv, ERR_IO_PENDING);
  10465. // Wait for all the results to arrive.
  10466. cb.GetResult(rv);
  10467. ASSERT_EQ(cb.results().size(), 2u);
  10468. // Verify that the first CreateEntry succeeded.
  10469. ASSERT_EQ(cb.results()[0], OK);
  10470. ASSERT_NE(entry1, nullptr);
  10471. // Verify that the second CreateEntry failed.
  10472. ASSERT_EQ(cb.results()[1], ERR_CACHE_CREATE_FAILURE);
  10473. ASSERT_EQ(entry2, nullptr);
  10474. }
  10475. TEST_F(HttpCacheIOCallbackTest, OperationFollowedByDoom) {
  10476. MockHttpCache cache;
  10477. TestCompletionCallbackForHttpCache cb;
  10478. std::unique_ptr<Transaction> transaction =
  10479. std::make_unique<Transaction>(DEFAULT_PRIORITY, cache.http_cache());
  10480. transaction->SetIOCallBackForTest(cb.callback());
  10481. // Create the backend here as our direct calls to CreateEntry and DoomEntry
  10482. // below require that it exists.
  10483. cache.backend();
  10484. // Need a mock transaction in order to use some of MockHttpCache's
  10485. // functions.
  10486. ScopedMockTransaction m_transaction(kSimpleGET_Transaction);
  10487. ActiveEntry* entry1 = nullptr;
  10488. // Queue up our operations.
  10489. // For this test all we need is some operation followed by a doom, a create
  10490. // fulfills that requirement.
  10491. int rv = CreateEntry(cache.http_cache(), m_transaction.url, &entry1,
  10492. transaction.get());
  10493. ASSERT_EQ(rv, ERR_IO_PENDING);
  10494. rv = DoomEntry(cache.http_cache(), m_transaction.url, transaction.get());
  10495. ASSERT_EQ(rv, ERR_IO_PENDING);
  10496. // Wait for all the results to arrive.
  10497. cb.GetResult(rv);
  10498. ASSERT_EQ(cb.results().size(), 2u);
  10499. // Verify that the CreateEntry succeeded.
  10500. ASSERT_EQ(cb.results()[0], OK);
  10501. // Verify that the DoomEntry requests a restart (CACHE_RACE).
  10502. ASSERT_EQ(cb.results()[1], ERR_CACHE_RACE);
  10503. }
  10504. TEST_F(HttpCacheIOCallbackTest, CreateFollowedByOpenOrCreate) {
  10505. MockHttpCache cache;
  10506. TestCompletionCallbackForHttpCache cb;
  10507. std::unique_ptr<Transaction> transaction =
  10508. std::make_unique<Transaction>(DEFAULT_PRIORITY, cache.http_cache());
  10509. transaction->SetIOCallBackForTest(cb.callback());
  10510. // Create the backend here as our direct calls to CreateEntry and
  10511. // OpenOrCreateEntry below require that it exists.
  10512. cache.backend();
  10513. // Need a mock transaction in order to use some of MockHttpCache's
  10514. // functions.
  10515. ScopedMockTransaction m_transaction(kSimpleGET_Transaction);
  10516. ActiveEntry* entry1 = nullptr;
  10517. ActiveEntry* entry2 = nullptr;
  10518. // Queue up our operations.
  10519. int rv = CreateEntry(cache.http_cache(), m_transaction.url, &entry1,
  10520. transaction.get());
  10521. ASSERT_EQ(rv, ERR_IO_PENDING);
  10522. rv = OpenOrCreateEntry(cache.http_cache(), m_transaction.url, &entry2,
  10523. transaction.get());
  10524. ASSERT_EQ(rv, ERR_IO_PENDING);
  10525. // Wait for all the results to arrive.
  10526. cb.GetResult(rv);
  10527. ASSERT_EQ(cb.results().size(), 2u);
  10528. // Verify that the CreateEntry succeeded.
  10529. ASSERT_EQ(cb.results()[0], OK);
  10530. ASSERT_NE(entry1, nullptr);
  10531. // Verify that OpenOrCreateEntry succeeded.
  10532. ASSERT_EQ(cb.results()[1], OK);
  10533. ASSERT_NE(entry2, nullptr);
  10534. ASSERT_EQ(entry1->disk_entry, entry2->disk_entry);
  10535. }
  10536. TEST_F(HttpCacheIOCallbackTest, FailedCreateFollowedByOpenOrCreate) {
  10537. MockHttpCache cache;
  10538. TestCompletionCallbackForHttpCache cb;
  10539. std::unique_ptr<Transaction> transaction =
  10540. std::make_unique<Transaction>(DEFAULT_PRIORITY, cache.http_cache());
  10541. transaction->SetIOCallBackForTest(cb.callback());
  10542. // Create the backend here as our direct calls to CreateEntry and
  10543. // OpenOrCreateEntry below require that it exists.
  10544. cache.backend();
  10545. // Need a mock transaction in order to use some of MockHttpCache's
  10546. // functions.
  10547. ScopedMockTransaction m_transaction(kSimpleGET_Transaction);
  10548. ActiveEntry* entry1 = nullptr;
  10549. ActiveEntry* entry2 = nullptr;
  10550. cache.disk_cache()->set_force_fail_callback_later(true);
  10551. // Queue up our operations.
  10552. int rv = CreateEntry(cache.http_cache(), m_transaction.url, &entry1,
  10553. transaction.get());
  10554. ASSERT_EQ(rv, ERR_IO_PENDING);
  10555. cache.disk_cache()->set_force_fail_callback_later(false);
  10556. rv = OpenOrCreateEntry(cache.http_cache(), m_transaction.url, &entry2,
  10557. transaction.get());
  10558. ASSERT_EQ(rv, ERR_IO_PENDING);
  10559. // Wait for all the results to arrive.
  10560. cb.GetResult(rv);
  10561. ASSERT_EQ(cb.results().size(), 2u);
  10562. // Verify that CreateEntry failed correctly.
  10563. ASSERT_EQ(cb.results()[0], ERR_CACHE_CREATE_FAILURE);
  10564. ASSERT_EQ(entry1, nullptr);
  10565. // Verify that the OpenOrCreateEntry requests a restart (CACHE_RACE).
  10566. ASSERT_EQ(cb.results()[1], ERR_CACHE_RACE);
  10567. ASSERT_EQ(entry2, nullptr);
  10568. }
  10569. TEST_F(HttpCacheIOCallbackTest, OpenFollowedByOpenOrCreate) {
  10570. MockHttpCache cache;
  10571. TestCompletionCallbackForHttpCache cb;
  10572. std::unique_ptr<Transaction> transaction =
  10573. std::make_unique<Transaction>(DEFAULT_PRIORITY, cache.http_cache());
  10574. transaction->SetIOCallBackForTest(cb.callback());
  10575. // Create the backend here as our direct calls to OpenEntry and
  10576. // OpenOrCreateEntry below require that it exists.
  10577. cache.backend();
  10578. // Need a mock transaction in order to use some of MockHttpCache's
  10579. // functions.
  10580. ScopedMockTransaction m_transaction(kSimpleGET_Transaction);
  10581. ActiveEntry* entry0 = nullptr;
  10582. ActiveEntry* entry1 = nullptr;
  10583. ActiveEntry* entry2 = nullptr;
  10584. // First need to create and entry so we can open it.
  10585. int rv = CreateEntry(cache.http_cache(), m_transaction.url, &entry0,
  10586. transaction.get());
  10587. ASSERT_EQ(rv, ERR_IO_PENDING);
  10588. cb.GetResult(rv);
  10589. ASSERT_EQ(cb.results().size(), static_cast<size_t>(1));
  10590. ASSERT_EQ(cb.results()[0], OK);
  10591. ASSERT_NE(entry0, nullptr);
  10592. // Manually DeactivateEntry() because OpenEntry() fails if there is an
  10593. // existing active entry.
  10594. DeactivateEntry(cache.http_cache(), entry0);
  10595. // Queue up our operations.
  10596. rv = OpenEntry(cache.http_cache(), m_transaction.url, &entry1,
  10597. transaction.get());
  10598. ASSERT_EQ(rv, ERR_IO_PENDING);
  10599. rv = OpenOrCreateEntry(cache.http_cache(), m_transaction.url, &entry2,
  10600. transaction.get());
  10601. ASSERT_EQ(rv, ERR_IO_PENDING);
  10602. // Wait for all the results to arrive.
  10603. cb.GetResult(rv);
  10604. ASSERT_EQ(cb.results().size(), 3u);
  10605. // Verify that the OpenEntry succeeded.
  10606. ASSERT_EQ(cb.results()[1], OK);
  10607. ASSERT_NE(entry1, nullptr);
  10608. // Verify that OpenOrCreateEntry succeeded.
  10609. ASSERT_EQ(cb.results()[2], OK);
  10610. ASSERT_NE(entry2, nullptr);
  10611. ASSERT_EQ(entry1->disk_entry, entry2->disk_entry);
  10612. }
  10613. TEST_F(HttpCacheIOCallbackTest, FailedOpenFollowedByOpenOrCreate) {
  10614. MockHttpCache cache;
  10615. TestCompletionCallbackForHttpCache cb;
  10616. std::unique_ptr<Transaction> transaction =
  10617. std::make_unique<Transaction>(DEFAULT_PRIORITY, cache.http_cache());
  10618. transaction->SetIOCallBackForTest(cb.callback());
  10619. // Create the backend here as our direct calls to OpenEntry and
  10620. // OpenOrCreateEntry below require that it exists.
  10621. cache.backend();
  10622. // Need a mock transaction in order to use some of MockHttpCache's
  10623. // functions.
  10624. ScopedMockTransaction m_transaction(kSimpleGET_Transaction);
  10625. ActiveEntry* entry1 = nullptr;
  10626. ActiveEntry* entry2 = nullptr;
  10627. cache.disk_cache()->set_force_fail_callback_later(true);
  10628. // Queue up our operations.
  10629. int rv = OpenEntry(cache.http_cache(), m_transaction.url, &entry1,
  10630. transaction.get());
  10631. ASSERT_EQ(rv, ERR_IO_PENDING);
  10632. cache.disk_cache()->set_force_fail_callback_later(false);
  10633. rv = OpenOrCreateEntry(cache.http_cache(), m_transaction.url, &entry2,
  10634. transaction.get());
  10635. ASSERT_EQ(rv, ERR_IO_PENDING);
  10636. // Wait for all the results to arrive.
  10637. cb.GetResult(rv);
  10638. ASSERT_EQ(cb.results().size(), 2u);
  10639. // Verify that OpenEntry failed correctly.
  10640. ASSERT_EQ(cb.results()[0], ERR_CACHE_OPEN_FAILURE);
  10641. ASSERT_EQ(entry1, nullptr);
  10642. // Verify that the OpenOrCreateEntry requests a restart (CACHE_RACE).
  10643. ASSERT_EQ(cb.results()[1], ERR_CACHE_RACE);
  10644. ASSERT_EQ(entry2, nullptr);
  10645. }
  10646. TEST_F(HttpCacheIOCallbackTest, OpenOrCreateFollowedByCreate) {
  10647. MockHttpCache cache;
  10648. TestCompletionCallbackForHttpCache cb;
  10649. std::unique_ptr<Transaction> transaction =
  10650. std::make_unique<Transaction>(DEFAULT_PRIORITY, cache.http_cache());
  10651. transaction->SetIOCallBackForTest(cb.callback());
  10652. // Create the backend here as our direct calls to OpenOrCreateEntry and
  10653. // CreateEntry below require that it exists.
  10654. cache.backend();
  10655. // Need a mock transaction in order to use some of MockHttpCache's
  10656. // functions.
  10657. ScopedMockTransaction m_transaction(kSimpleGET_Transaction);
  10658. ActiveEntry* entry1 = nullptr;
  10659. ActiveEntry* entry2 = nullptr;
  10660. // Queue up our operations.
  10661. int rv = OpenOrCreateEntry(cache.http_cache(), m_transaction.url, &entry1,
  10662. transaction.get());
  10663. ASSERT_EQ(rv, ERR_IO_PENDING);
  10664. rv = CreateEntry(cache.http_cache(), m_transaction.url, &entry2,
  10665. transaction.get());
  10666. ASSERT_EQ(rv, ERR_IO_PENDING);
  10667. // Wait for all the results to arrive.
  10668. cb.GetResult(rv);
  10669. ASSERT_EQ(cb.results().size(), 2u);
  10670. // Verify that the OpenOrCreateEntry succeeded.
  10671. ASSERT_EQ(cb.results()[0], OK);
  10672. ASSERT_NE(entry1, nullptr);
  10673. // Verify that CreateEntry failed.
  10674. ASSERT_EQ(cb.results()[1], ERR_CACHE_CREATE_FAILURE);
  10675. ASSERT_EQ(entry2, nullptr);
  10676. }
  10677. TEST_F(HttpCacheIOCallbackTest, OpenOrCreateFollowedByOpenOrCreate) {
  10678. MockHttpCache cache;
  10679. TestCompletionCallbackForHttpCache cb;
  10680. std::unique_ptr<Transaction> transaction =
  10681. std::make_unique<Transaction>(DEFAULT_PRIORITY, cache.http_cache());
  10682. transaction->SetIOCallBackForTest(cb.callback());
  10683. // Create the backend here as our direct calls to OpenOrCreateEntry below
  10684. // require that it exists.
  10685. cache.backend();
  10686. // Need a mock transaction in order to use some of MockHttpCache's
  10687. // functions.
  10688. ScopedMockTransaction m_transaction(kSimpleGET_Transaction);
  10689. ActiveEntry* entry1 = nullptr;
  10690. ActiveEntry* entry2 = nullptr;
  10691. // Queue up our operations.
  10692. int rv = OpenOrCreateEntry(cache.http_cache(), m_transaction.url, &entry1,
  10693. transaction.get());
  10694. ASSERT_EQ(rv, ERR_IO_PENDING);
  10695. rv = OpenOrCreateEntry(cache.http_cache(), m_transaction.url, &entry2,
  10696. transaction.get());
  10697. ASSERT_EQ(rv, ERR_IO_PENDING);
  10698. // Wait for all the results to arrive.
  10699. cb.GetResult(rv);
  10700. ASSERT_EQ(cb.results().size(), 2u);
  10701. // Verify that the OpenOrCreateEntry succeeded.
  10702. ASSERT_EQ(cb.results()[0], OK);
  10703. ASSERT_NE(entry1, nullptr);
  10704. // Verify that the other succeeded.
  10705. ASSERT_EQ(cb.results()[1], OK);
  10706. ASSERT_NE(entry2, nullptr);
  10707. }
  10708. TEST_F(HttpCacheIOCallbackTest, FailedOpenOrCreateFollowedByOpenOrCreate) {
  10709. MockHttpCache cache;
  10710. TestCompletionCallbackForHttpCache cb;
  10711. std::unique_ptr<Transaction> transaction =
  10712. std::make_unique<Transaction>(DEFAULT_PRIORITY, cache.http_cache());
  10713. transaction->SetIOCallBackForTest(cb.callback());
  10714. // Create the backend here as our direct calls to OpenOrCreateEntry below
  10715. // require that it exists.
  10716. cache.backend();
  10717. // Need a mock transaction in order to use some of MockHttpCache's
  10718. // functions.
  10719. ScopedMockTransaction m_transaction(kSimpleGET_Transaction);
  10720. ActiveEntry* entry1 = nullptr;
  10721. ActiveEntry* entry2 = nullptr;
  10722. cache.disk_cache()->set_force_fail_callback_later(true);
  10723. // Queue up our operations.
  10724. int rv = OpenOrCreateEntry(cache.http_cache(), m_transaction.url, &entry1,
  10725. transaction.get());
  10726. ASSERT_EQ(rv, ERR_IO_PENDING);
  10727. cache.disk_cache()->set_force_fail_callback_later(false);
  10728. rv = OpenOrCreateEntry(cache.http_cache(), m_transaction.url, &entry2,
  10729. transaction.get());
  10730. ASSERT_EQ(rv, ERR_IO_PENDING);
  10731. // Wait for all the results to arrive.
  10732. cb.GetResult(rv);
  10733. ASSERT_EQ(cb.results().size(), 2u);
  10734. // Verify that the OpenOrCreateEntry failed.
  10735. ASSERT_EQ(cb.results()[0], ERR_CACHE_OPEN_OR_CREATE_FAILURE);
  10736. ASSERT_EQ(entry1, nullptr);
  10737. // Verify that the other failed.
  10738. ASSERT_EQ(cb.results()[1], ERR_CACHE_OPEN_OR_CREATE_FAILURE);
  10739. ASSERT_EQ(entry2, nullptr);
  10740. }
  10741. TEST_F(HttpCacheTest, DnsAliasesNoRevalidation) {
  10742. MockHttpCache cache;
  10743. HttpResponseInfo response;
  10744. ScopedMockTransaction transaction(kSimpleGET_Transaction);
  10745. transaction.dns_aliases = {"alias1", "alias2"};
  10746. RunTransactionTestWithResponseInfo(cache.http_cache(), transaction,
  10747. &response);
  10748. EXPECT_FALSE(response.was_cached);
  10749. EXPECT_THAT(response.dns_aliases, testing::ElementsAre("alias1", "alias2"));
  10750. // The second request should be cached, and the response used without
  10751. // revalidation. Set the transaction alias list to empty to verify that the
  10752. // cached aliases are being used.
  10753. transaction.dns_aliases = {};
  10754. RunTransactionTestWithResponseInfo(cache.http_cache(), transaction,
  10755. &response);
  10756. EXPECT_TRUE(response.was_cached);
  10757. EXPECT_THAT(response.dns_aliases, testing::ElementsAre("alias1", "alias2"));
  10758. }
  10759. TEST_F(HttpCacheTest, NoDnsAliasesNoRevalidation) {
  10760. MockHttpCache cache;
  10761. HttpResponseInfo response;
  10762. ScopedMockTransaction transaction(kSimpleGET_Transaction);
  10763. transaction.dns_aliases = {};
  10764. RunTransactionTestWithResponseInfo(cache.http_cache(), transaction,
  10765. &response);
  10766. EXPECT_FALSE(response.was_cached);
  10767. EXPECT_TRUE(response.dns_aliases.empty());
  10768. // The second request should be cached, and the response used without
  10769. // revalidation. Set the transaction alias list to nonempty to verify that the
  10770. // cached aliases are being used.
  10771. transaction.dns_aliases = {"alias"};
  10772. RunTransactionTestWithResponseInfo(cache.http_cache(), transaction,
  10773. &response);
  10774. EXPECT_TRUE(response.was_cached);
  10775. EXPECT_TRUE(response.dns_aliases.empty());
  10776. }
  10777. TEST_F(HttpCacheTest, DnsAliasesRevalidation) {
  10778. MockHttpCache cache;
  10779. HttpResponseInfo response;
  10780. ScopedMockTransaction transaction(kTypicalGET_Transaction);
  10781. transaction.response_headers =
  10782. "Date: Wed, 28 Nov 2007 09:40:09 GMT\n"
  10783. "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n"
  10784. "Cache-Control: max-age=0\n";
  10785. transaction.dns_aliases = {"alias1", "alias2"};
  10786. RunTransactionTestWithResponseInfo(cache.http_cache(), transaction,
  10787. &response);
  10788. EXPECT_FALSE(response.was_cached);
  10789. EXPECT_THAT(response.dns_aliases, testing::ElementsAre("alias1", "alias2"));
  10790. // On the second request, the cache should be revalidated. Change the aliases
  10791. // to be sure that the new aliases are being used, and have the response be
  10792. // cached for next time.
  10793. transaction.response_headers = "Cache-Control: max-age=10000\n";
  10794. transaction.dns_aliases = {"alias3", "alias4"};
  10795. RunTransactionTestWithResponseInfo(cache.http_cache(), transaction,
  10796. &response);
  10797. EXPECT_FALSE(response.was_cached);
  10798. EXPECT_THAT(response.dns_aliases, testing::ElementsAre("alias3", "alias4"));
  10799. transaction.dns_aliases = {"alias5", "alias6"};
  10800. RunTransactionTestWithResponseInfo(cache.http_cache(), transaction,
  10801. &response);
  10802. EXPECT_TRUE(response.was_cached);
  10803. EXPECT_THAT(response.dns_aliases, testing::ElementsAre("alias3", "alias4"));
  10804. }
  10805. TEST_F(HttpCacheTest, SecurityHeadersAreCopiedToConditionalizedResponse) {
  10806. MockHttpCache cache;
  10807. HttpResponseInfo response;
  10808. ScopedMockTransaction transaction(kSimpleGET_Transaction);
  10809. static const Response kNetResponse1 = {
  10810. "HTTP/1.1 200 OK",
  10811. "Date: Fri, 12 Jun 2009 21:46:42 GMT\n"
  10812. "Server: server1\n"
  10813. "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n"
  10814. "Cross-Origin-Resource-Policy: cross-origin\n",
  10815. "body1"};
  10816. static const Response kNetResponse2 = {
  10817. "HTTP/1.1 304 Not Modified",
  10818. "Date: Wed, 22 Jul 2009 03:15:26 GMT\n"
  10819. "Server: server2\n"
  10820. "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n",
  10821. ""};
  10822. kNetResponse1.AssignTo(&transaction);
  10823. RunTransactionTestWithResponseInfo(cache.http_cache(), transaction,
  10824. &response);
  10825. // On the second request, the cache is revalidated.
  10826. const char kExtraRequestHeaders[] =
  10827. "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n";
  10828. transaction.request_headers = kExtraRequestHeaders;
  10829. kNetResponse2.AssignTo(&transaction);
  10830. RunTransactionTestWithResponseInfo(cache.http_cache(), transaction,
  10831. &response);
  10832. // Verify that the CORP header was carried over to the response.
  10833. std::string response_corp_header;
  10834. response.headers->GetNormalizedHeader("Cross-Origin-Resource-Policy",
  10835. &response_corp_header);
  10836. EXPECT_EQ(304, response.headers->response_code());
  10837. EXPECT_EQ("cross-origin", response_corp_header);
  10838. }
  10839. class HttpCacheSingleKeyedCacheTest : public HttpCacheTest {
  10840. public:
  10841. void SetUp() override {
  10842. // The single-keyed cache feature is meaningless when the split cache is not
  10843. // enabled. The //net layer doesn't care whether or not the
  10844. // "CacheTransparency" feature is enabled.
  10845. feature_list_.InitWithFeatureState(
  10846. net::features::kSplitCacheByNetworkIsolationKey, true);
  10847. HttpCacheTest::SetUp();
  10848. }
  10849. void RunTransactionTestForSingleKeyedCache(
  10850. HttpCache* cache,
  10851. const MockTransaction& trans_info,
  10852. const NetworkIsolationKey& network_isolation_key,
  10853. const std::string& checksum) {
  10854. ScopedMockTransaction transaction(trans_info);
  10855. MockHttpRequest request(transaction);
  10856. request.network_isolation_key = network_isolation_key;
  10857. request.checksum = checksum;
  10858. HttpResponseInfo response_info;
  10859. RunTransactionTestWithRequest(cache, transaction, request, &response_info);
  10860. }
  10861. void RunSimpleTransactionTestForSingleKeyedCache(
  10862. HttpCache* cache,
  10863. const NetworkIsolationKey& network_isolation_key,
  10864. const std::string& checksum) {
  10865. RunTransactionTestForSingleKeyedCache(cache, kSimpleGET_Transaction,
  10866. network_isolation_key, checksum);
  10867. }
  10868. private:
  10869. base::test::ScopedFeatureList feature_list_;
  10870. };
  10871. constexpr char kChecksumForSimpleGET[] =
  10872. "80B4C37CEF5CFE69B4A90830282AA2BB772DC4CBC00491A219CE5F2AD75C7B58";
  10873. TEST_F(HttpCacheSingleKeyedCacheTest, SuccessfulGET) {
  10874. MockHttpCache cache;
  10875. // The first request adds the item to the cache.
  10876. {
  10877. const auto site_a = SchemefulSite(GURL("https://a.com/"));
  10878. RunSimpleTransactionTestForSingleKeyedCache(
  10879. cache.http_cache(), NetworkIsolationKey(site_a, site_a),
  10880. kChecksumForSimpleGET);
  10881. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  10882. EXPECT_EQ(0, cache.disk_cache()->open_count());
  10883. EXPECT_EQ(1, cache.disk_cache()->create_count());
  10884. }
  10885. // The second request verifies that the same cache entry is used with a
  10886. // different NetworkIsolationKey
  10887. {
  10888. const auto site_b = SchemefulSite(GURL("https://b.com/"));
  10889. RunSimpleTransactionTestForSingleKeyedCache(
  10890. cache.http_cache(), NetworkIsolationKey(site_b, site_b),
  10891. kChecksumForSimpleGET);
  10892. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  10893. EXPECT_EQ(1, cache.disk_cache()->open_count());
  10894. EXPECT_EQ(1, cache.disk_cache()->create_count());
  10895. }
  10896. }
  10897. TEST_F(HttpCacheSingleKeyedCacheTest, GETWithChecksumMismatch) {
  10898. MockHttpCache cache;
  10899. const auto site_a = SchemefulSite(GURL("https://a.com/"));
  10900. // The first request adds the item to the cache.
  10901. {
  10902. RunSimpleTransactionTestForSingleKeyedCache(
  10903. cache.http_cache(), NetworkIsolationKey(site_a, site_a),
  10904. "000000000000000000000000000000000000000000000000000000000000000");
  10905. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  10906. EXPECT_EQ(0, cache.disk_cache()->open_count());
  10907. EXPECT_EQ(1, cache.disk_cache()->create_count());
  10908. }
  10909. // The second request doesn't use the item that was added to the single-keyed
  10910. // cache, but adds it to the split cache instead.
  10911. {
  10912. RunSimpleTransactionTestForSingleKeyedCache(
  10913. cache.http_cache(), NetworkIsolationKey(site_a, site_a),
  10914. "000000000000000000000000000000000000000000000000000000000000000");
  10915. // Fetches from the network again, this time into the split cache.
  10916. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  10917. EXPECT_EQ(1, cache.disk_cache()->open_count());
  10918. EXPECT_EQ(2, cache.disk_cache()->create_count());
  10919. }
  10920. // The third request uses the split cache.
  10921. {
  10922. RunSimpleTransactionTestForSingleKeyedCache(
  10923. cache.http_cache(), NetworkIsolationKey(site_a, site_a),
  10924. "000000000000000000000000000000000000000000000000000000000000000");
  10925. // Fetches from the split cache.
  10926. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  10927. EXPECT_EQ(3, cache.disk_cache()->open_count()); // opens both cache entries
  10928. EXPECT_EQ(2, cache.disk_cache()->create_count());
  10929. }
  10930. }
  10931. TEST_F(HttpCacheSingleKeyedCacheTest, GETWithBadResponseCode) {
  10932. MockHttpCache cache;
  10933. MockTransaction transaction = kSimpleGET_Transaction;
  10934. transaction.status = "HTTP/1.1 404 Not Found";
  10935. const auto site_a = SchemefulSite(GURL("https://a.com/"));
  10936. // The first request adds the item to the single-keyed cache.
  10937. {
  10938. RunTransactionTestForSingleKeyedCache(cache.http_cache(), transaction,
  10939. NetworkIsolationKey(site_a, site_a),
  10940. kChecksumForSimpleGET);
  10941. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  10942. EXPECT_EQ(0, cache.disk_cache()->open_count());
  10943. EXPECT_EQ(1, cache.disk_cache()->create_count());
  10944. }
  10945. // The second request verifies that the cache entry is not re-used
  10946. // but a new one is created in the split cache.
  10947. {
  10948. RunTransactionTestForSingleKeyedCache(cache.http_cache(), transaction,
  10949. NetworkIsolationKey(site_a, site_a),
  10950. kChecksumForSimpleGET);
  10951. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  10952. EXPECT_EQ(1, cache.disk_cache()->open_count());
  10953. EXPECT_EQ(2, cache.disk_cache()->create_count());
  10954. }
  10955. }
  10956. // This is identical to GETWithBadResponseCode but with a different response
  10957. // code. It's not very realistic as it doesn't call DoneReading(), but it covers
  10958. // the relevant code path.
  10959. TEST_F(HttpCacheSingleKeyedCacheTest, RedirectUnusable) {
  10960. MockHttpCache cache;
  10961. MockTransaction transaction = kSimpleGET_Transaction;
  10962. transaction.status = "HTTP/1.1 301 Moved Permanently";
  10963. const auto site_a = SchemefulSite(GURL("https://a.com/"));
  10964. // The first request adds the item to the single-keyed cache.
  10965. {
  10966. RunTransactionTestForSingleKeyedCache(cache.http_cache(), transaction,
  10967. NetworkIsolationKey(site_a, site_a),
  10968. kChecksumForSimpleGET);
  10969. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  10970. EXPECT_EQ(0, cache.disk_cache()->open_count());
  10971. EXPECT_EQ(1, cache.disk_cache()->create_count());
  10972. }
  10973. // The second request verifies that the cache entry is not re-used
  10974. // but a new one is created in the split cache.
  10975. {
  10976. RunTransactionTestForSingleKeyedCache(cache.http_cache(), transaction,
  10977. NetworkIsolationKey(site_a, site_a),
  10978. kChecksumForSimpleGET);
  10979. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  10980. EXPECT_EQ(1, cache.disk_cache()->open_count());
  10981. EXPECT_EQ(2, cache.disk_cache()->create_count());
  10982. }
  10983. }
  10984. TEST_F(HttpCacheSingleKeyedCacheTest, SuccessfulRevalidation) {
  10985. MockHttpCache cache;
  10986. MockTransaction transaction = kSimpleGET_Transaction;
  10987. // Add a cache control header to permit the entry to be cached, with max-age 0
  10988. // to force relatidation next time. Add Etag to permit it to be revalidated.
  10989. transaction.response_headers =
  10990. "Etag: \"foo\"\n"
  10991. "Cache-Control: max-age=0\n";
  10992. {
  10993. const auto site_a = SchemefulSite(GURL("https://a.com/"));
  10994. RunTransactionTestForSingleKeyedCache(cache.http_cache(), transaction,
  10995. NetworkIsolationKey(site_a, site_a),
  10996. kChecksumForSimpleGET);
  10997. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  10998. EXPECT_EQ(0, cache.disk_cache()->open_count());
  10999. EXPECT_EQ(1, cache.disk_cache()->create_count());
  11000. }
  11001. // The second request revalidates the existing entry.
  11002. {
  11003. const auto site_b = SchemefulSite(GURL("https://b.com/"));
  11004. transaction.status = "HTTP/1.1 304 Not Modified";
  11005. // Allow it to be reused without validation next time by increasing max-age.
  11006. transaction.response_headers =
  11007. "Etag: \"foo\"\n"
  11008. "Cache-Control: max-age=10000\n";
  11009. RunTransactionTestForSingleKeyedCache(cache.http_cache(), transaction,
  11010. NetworkIsolationKey(site_b, site_b),
  11011. kChecksumForSimpleGET);
  11012. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  11013. EXPECT_EQ(1, cache.disk_cache()->open_count());
  11014. EXPECT_EQ(1, cache.disk_cache()->create_count());
  11015. }
  11016. // The third request re-uses the entry.
  11017. {
  11018. const auto site_c = SchemefulSite(GURL("https://c.com/"));
  11019. // Load from cache again.
  11020. RunTransactionTestForSingleKeyedCache(cache.http_cache(), transaction,
  11021. NetworkIsolationKey(site_c, site_c),
  11022. kChecksumForSimpleGET);
  11023. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  11024. EXPECT_EQ(1, cache.disk_cache()->open_count());
  11025. EXPECT_EQ(1, cache.disk_cache()->create_count());
  11026. }
  11027. }
  11028. TEST_F(HttpCacheSingleKeyedCacheTest, RevalidationChangingUncheckedHeader) {
  11029. MockHttpCache cache;
  11030. MockTransaction transaction = kSimpleGET_Transaction;
  11031. // Add a cache control header to permit the entry to be cached, with max-age 0
  11032. // to force relatidation next time. Add Etag to permit it to be revalidated.
  11033. transaction.response_headers =
  11034. "Etag: \"foo\"\n"
  11035. "Cache-Control: max-age=0\n";
  11036. {
  11037. const auto site_a = SchemefulSite(GURL("https://a.com/"));
  11038. RunTransactionTestForSingleKeyedCache(cache.http_cache(), transaction,
  11039. NetworkIsolationKey(site_a, site_a),
  11040. kChecksumForSimpleGET);
  11041. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  11042. EXPECT_EQ(0, cache.disk_cache()->open_count());
  11043. EXPECT_EQ(1, cache.disk_cache()->create_count());
  11044. }
  11045. // The second request revalidates the existing entry.
  11046. {
  11047. const auto site_b = SchemefulSite(GURL("https://b.com/"));
  11048. transaction.status = "HTTP/1.1 304 Not Modified";
  11049. // Add a response header. This is the only difference from the
  11050. // SuccessfulRevalidation test.
  11051. transaction.response_headers =
  11052. "Etag: \"foo\"\n"
  11053. "Cache-Control: max-age=10000\n"
  11054. "X-Unchecked-Header: 1\n";
  11055. RunTransactionTestForSingleKeyedCache(cache.http_cache(), transaction,
  11056. NetworkIsolationKey(site_b, site_b),
  11057. kChecksumForSimpleGET);
  11058. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  11059. EXPECT_EQ(1, cache.disk_cache()->open_count());
  11060. EXPECT_EQ(1, cache.disk_cache()->create_count());
  11061. }
  11062. // The third request re-uses the entry.
  11063. {
  11064. const auto site_c = SchemefulSite(GURL("https://c.com/"));
  11065. // Load from cache again.
  11066. RunTransactionTestForSingleKeyedCache(cache.http_cache(), transaction,
  11067. NetworkIsolationKey(site_c, site_c),
  11068. kChecksumForSimpleGET);
  11069. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  11070. EXPECT_EQ(1, cache.disk_cache()->open_count());
  11071. EXPECT_EQ(1, cache.disk_cache()->create_count());
  11072. }
  11073. }
  11074. TEST_F(HttpCacheSingleKeyedCacheTest, RevalidationChangingCheckedHeader) {
  11075. MockHttpCache cache;
  11076. MockTransaction transaction = kSimpleGET_Transaction;
  11077. // Add a cache control header to permit the entry to be cached, with max-age 0
  11078. // to force relatidation next time. Add Etag to permit it to be revalidated.
  11079. transaction.response_headers =
  11080. "Etag: \"foo\"\n"
  11081. "Cache-Control: max-age=0\n";
  11082. {
  11083. const auto site_a = SchemefulSite(GURL("https://a.com/"));
  11084. RunTransactionTestForSingleKeyedCache(cache.http_cache(), transaction,
  11085. NetworkIsolationKey(site_a, site_a),
  11086. kChecksumForSimpleGET);
  11087. EXPECT_EQ(1, cache.network_layer()->transaction_count());
  11088. EXPECT_EQ(0, cache.disk_cache()->open_count());
  11089. EXPECT_EQ(1, cache.disk_cache()->create_count());
  11090. }
  11091. // The second request marks the single-keyed cache entry unusable because the
  11092. // checksum no longer matches.
  11093. {
  11094. const auto site_b = SchemefulSite(GURL("https://b.com/"));
  11095. transaction.status = "HTTP/1.1 304 Not Modified";
  11096. // Add the "Vary" response header.
  11097. transaction.response_headers =
  11098. "Etag: \"foo\"\n"
  11099. "Cache-Control: max-age=10000\n"
  11100. "Vary: Cookie\n";
  11101. RunTransactionTestForSingleKeyedCache(cache.http_cache(), transaction,
  11102. NetworkIsolationKey(site_b, site_b),
  11103. kChecksumForSimpleGET);
  11104. EXPECT_EQ(2, cache.network_layer()->transaction_count());
  11105. EXPECT_EQ(1, cache.disk_cache()->open_count());
  11106. EXPECT_EQ(1, cache.disk_cache()->create_count());
  11107. }
  11108. // The third request has to go to the network because the single-keyed cache
  11109. // entry is unusable. It writes a new entry to the split cache.
  11110. {
  11111. const auto site_c = SchemefulSite(GURL("https://c.com/"));
  11112. RunTransactionTestForSingleKeyedCache(cache.http_cache(), transaction,
  11113. NetworkIsolationKey(site_c, site_c),
  11114. kChecksumForSimpleGET);
  11115. EXPECT_EQ(3, cache.network_layer()->transaction_count());
  11116. EXPECT_EQ(2, cache.disk_cache()->open_count());
  11117. EXPECT_EQ(2, cache.disk_cache()->create_count());
  11118. }
  11119. }
  11120. } // namespace net