1eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Copyright 2013 The Chromium Authors. All rights reserved. 2eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Use of this source code is governed by a BSD-style license that can be 3eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// found in the LICENSE file. 4eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 57dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include <string> 6eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 7eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/files/file_path.h" 8eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/memory/singleton.h" 9eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/path_service.h" 107dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "base/strings/string_number_conversions.h" 117dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "base/strings/string_piece.h" 124e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "net/tools/balsa/balsa_headers.h" 137dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "net/tools/quic/quic_in_memory_cache.h" 144e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "net/tools/quic/test_tools/quic_in_memory_cache_peer.h" 15eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "testing/gtest/include/gtest/gtest.h" 16eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 177dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochusing base::IntToString; 187dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochusing base::StringPiece; 197dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch 20eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochnamespace net { 21eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochnamespace tools { 22eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochnamespace test { 23eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 24eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochclass QuicInMemoryCacheTest : public ::testing::Test { 25eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch protected: 26eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch QuicInMemoryCacheTest() { 27eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch base::FilePath path; 28eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch PathService::Get(base::DIR_SOURCE_ROOT, &path); 29eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch path = path.AppendASCII("net").AppendASCII("data") 30eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch .AppendASCII("quic_in_memory_cache_data"); 31eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // The file path is known to be an ascii string. 32eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch FLAGS_quic_in_memory_cache_dir = path.MaybeAsASCII(); 33eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch } 34eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 35eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch void CreateRequest(std::string host, 36eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch std::string path, 37eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch net::BalsaHeaders* headers) { 38eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch headers->SetRequestFirstlineFromStringPieces("GET", path, "HTTP/1.1"); 39eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch headers->ReplaceOrAppendHeader("host", host); 40eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch } 417dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch 427dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch virtual void SetUp() { 434e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) QuicInMemoryCachePeer::ResetForTests(); 447dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch } 457dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch 467dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch // This method was copied from end_to_end_test.cc in this directory. 477dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch void AddToCache(const StringPiece& method, 487dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch const StringPiece& path, 497dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch const StringPiece& version, 507dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch const StringPiece& response_code, 517dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch const StringPiece& response_detail, 527dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch const StringPiece& body) { 537dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch BalsaHeaders request_headers, response_headers; 547dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch request_headers.SetRequestFirstlineFromStringPieces(method, 557dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch path, 567dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch version); 577dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch response_headers.SetRequestFirstlineFromStringPieces(version, 587dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch response_code, 597dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch response_detail); 607dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch response_headers.AppendHeader("content-length", 617dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch base::IntToString(body.length())); 627dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch 637dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch // Check if response already exists and matches. 647dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch QuicInMemoryCache* cache = QuicInMemoryCache::GetInstance(); 657dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch const QuicInMemoryCache::Response* cached_response = 667dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch cache->GetResponse(request_headers); 677dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch if (cached_response != NULL) { 687dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch std::string cached_response_headers_str, response_headers_str; 697dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch cached_response->headers().DumpToString(&cached_response_headers_str); 707dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch response_headers.DumpToString(&response_headers_str); 717dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch CHECK_EQ(cached_response_headers_str, response_headers_str); 727dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch CHECK_EQ(cached_response->body(), body); 737dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch return; 747dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch } 757dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch cache->AddResponse(request_headers, response_headers, body); 767dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch } 77eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}; 78eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 797dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochTEST_F(QuicInMemoryCacheTest, AddResponseGetResponse) { 807dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch std::string response_body("hello response"); 817dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch AddToCache("GET", "https://www.google.com/bar", 827dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch "HTTP/1.1", "200", "OK", response_body); 837dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch net::BalsaHeaders request_headers; 847dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch CreateRequest("www.google.com", "/bar", &request_headers); 857dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch QuicInMemoryCache* cache = QuicInMemoryCache::GetInstance(); 867dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch const QuicInMemoryCache::Response* response = 877dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch cache->GetResponse(request_headers); 887dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch ASSERT_TRUE(response); 897dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch EXPECT_EQ("200", response->headers().response_code()); 907dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch EXPECT_EQ(response_body.size(), response->body().length()); 917dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch 927dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch CreateRequest("", "https://www.google.com/bar", &request_headers); 937dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch response = cache->GetResponse(request_headers); 947dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch ASSERT_TRUE(response); 957dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch EXPECT_EQ("200", response->headers().response_code()); 967dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch EXPECT_EQ(response_body.size(), response->body().length()); 977dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch} 987dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch 99eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen MurdochTEST_F(QuicInMemoryCacheTest, ReadsCacheDir) { 100eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch net::BalsaHeaders request_headers; 101eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch CreateRequest("quic.test.url", "/index.html", &request_headers); 102eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 103eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch const QuicInMemoryCache::Response* response = 104eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch QuicInMemoryCache::GetInstance()->GetResponse(request_headers); 105eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch ASSERT_TRUE(response); 106eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch std::string value; 107eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch response->headers().GetAllOfHeaderAsString("Connection", &value); 108eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch EXPECT_EQ("200", response->headers().response_code()); 109eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch EXPECT_EQ("Keep-Alive", value); 110eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch EXPECT_LT(0U, response->body().length()); 111eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch} 112eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 113eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen MurdochTEST_F(QuicInMemoryCacheTest, ReadsCacheDirHttp) { 114eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch net::BalsaHeaders request_headers; 1157dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch CreateRequest("", "http://quic.test.url/index.html", &request_headers); 116eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 117eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch const QuicInMemoryCache::Response* response = 118eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch QuicInMemoryCache::GetInstance()->GetResponse(request_headers); 119eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch ASSERT_TRUE(response); 120eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch std::string value; 121eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch response->headers().GetAllOfHeaderAsString("Connection", &value); 122eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch EXPECT_EQ("200", response->headers().response_code()); 123eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch EXPECT_EQ("Keep-Alive", value); 124eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch EXPECT_LT(0U, response->body().length()); 125eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch} 126eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 127eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen MurdochTEST_F(QuicInMemoryCacheTest, GetResponseNoMatch) { 128eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch net::BalsaHeaders request_headers; 129eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch CreateRequest("www.google.com", "/index.html", &request_headers); 130eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 131eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch const QuicInMemoryCache::Response* response = 132eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch QuicInMemoryCache::GetInstance()->GetResponse(request_headers); 133eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch ASSERT_FALSE(response); 134eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch} 135eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 136eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch} // namespace test 137eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch} // namespace tools 138eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch} // namespace net 139