1 // Copyright (c) 2011 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.
5 #include "base/files/file_path.h"
6 #include "net/disk_cache/blockfile/disk_format.h"
7 #include "net/disk_cache/blockfile/storage_block-inl.h"
8 #include "net/disk_cache/blockfile/storage_block.h"
9 #include "net/disk_cache/disk_cache_test_base.h"
10 #include "net/disk_cache/disk_cache_test_util.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 typedef disk_cache::StorageBlock
<disk_cache::EntryStore
> CacheEntryBlock
;
15 TEST_F(DiskCacheTest
, StorageBlock_LoadStore
) {
16 base::FilePath filename
= cache_path_
.AppendASCII("a_test");
17 scoped_refptr
<disk_cache::MappedFile
> file(new disk_cache::MappedFile
);
18 ASSERT_TRUE(CreateCacheTestFile(filename
));
19 ASSERT_TRUE(file
->Init(filename
, 8192));
21 CacheEntryBlock
entry1(file
.get(), disk_cache::Addr(0xa0010001));
22 memset(entry1
.Data(), 0, sizeof(disk_cache::EntryStore
));
23 entry1
.Data()->hash
= 0xaa5555aa;
24 entry1
.Data()->rankings_node
= 0xa0010002;
26 EXPECT_TRUE(entry1
.Store());
27 entry1
.Data()->hash
= 0x88118811;
28 entry1
.Data()->rankings_node
= 0xa0040009;
30 EXPECT_TRUE(entry1
.Load());
31 EXPECT_EQ(0xaa5555aa, entry1
.Data()->hash
);
32 EXPECT_EQ(0xa0010002, entry1
.Data()->rankings_node
);
35 TEST_F(DiskCacheTest
, StorageBlock_SetData
) {
36 base::FilePath filename
= cache_path_
.AppendASCII("a_test");
37 scoped_refptr
<disk_cache::MappedFile
> file(new disk_cache::MappedFile
);
38 ASSERT_TRUE(CreateCacheTestFile(filename
));
39 ASSERT_TRUE(file
->Init(filename
, 8192));
41 CacheEntryBlock
entry1(file
.get(), disk_cache::Addr(0xa0010001));
42 entry1
.Data()->hash
= 0xaa5555aa;
44 CacheEntryBlock
entry2(file
.get(), disk_cache::Addr(0xa0010002));
45 EXPECT_TRUE(entry2
.Load());
46 EXPECT_TRUE(entry2
.Data() != NULL
);
47 EXPECT_TRUE(0 == entry2
.Data()->hash
);
49 EXPECT_TRUE(entry2
.Data() != entry1
.Data());
50 entry2
.SetData(entry1
.Data());
51 EXPECT_EQ(0xaa5555aa, entry2
.Data()->hash
);
52 EXPECT_TRUE(entry2
.Data() == entry1
.Data());
55 TEST_F(DiskCacheTest
, StorageBlock_SetModified
) {
56 base::FilePath filename
= cache_path_
.AppendASCII("a_test");
57 scoped_refptr
<disk_cache::MappedFile
> file(new disk_cache::MappedFile
);
58 ASSERT_TRUE(CreateCacheTestFile(filename
));
59 ASSERT_TRUE(file
->Init(filename
, 8192));
61 CacheEntryBlock
* entry1
=
62 new CacheEntryBlock(file
.get(), disk_cache::Addr(0xa0010003));
63 EXPECT_TRUE(entry1
->Load());
64 EXPECT_TRUE(0 == entry1
->Data()->hash
);
65 entry1
->Data()->hash
= 0x45687912;
66 entry1
->set_modified();
69 CacheEntryBlock
entry2(file
.get(), disk_cache::Addr(0xa0010003));
70 EXPECT_TRUE(entry2
.Load());
71 EXPECT_TRUE(0x45687912 == entry2
.Data()->hash
);