xref: /aosp_15_r20/external/cronet/net/disk_cache/blockfile/storage_block_unittest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2011 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
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"
12 
13 typedef disk_cache::StorageBlock<disk_cache::EntryStore> CacheEntryBlock;
14 
TEST_F(DiskCacheTest,StorageBlock_LoadStore)15 TEST_F(DiskCacheTest, StorageBlock_LoadStore) {
16   base::FilePath filename = cache_path_.AppendASCII("a_test");
17   auto file = base::MakeRefCounted<disk_cache::MappedFile>();
18   ASSERT_TRUE(CreateCacheTestFile(filename));
19   ASSERT_TRUE(file->Init(filename, 8192));
20 
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;
25 
26   EXPECT_TRUE(entry1.Store());
27   entry1.Data()->hash = 0x88118811;
28   entry1.Data()->rankings_node = 0xa0040009;
29 
30   EXPECT_TRUE(entry1.Load());
31   EXPECT_EQ(0xaa5555aa, entry1.Data()->hash);
32   EXPECT_EQ(0xa0010002, entry1.Data()->rankings_node);
33 }
34 
TEST_F(DiskCacheTest,StorageBlock_SetData)35 TEST_F(DiskCacheTest, StorageBlock_SetData) {
36   base::FilePath filename = cache_path_.AppendASCII("a_test");
37   auto file = base::MakeRefCounted<disk_cache::MappedFile>();
38   ASSERT_TRUE(CreateCacheTestFile(filename));
39   ASSERT_TRUE(file->Init(filename, 8192));
40 
41   CacheEntryBlock entry1(file.get(), disk_cache::Addr(0xa0010001));
42   entry1.Data()->hash = 0xaa5555aa;
43 
44   CacheEntryBlock entry2(file.get(), disk_cache::Addr(0xa0010002));
45   EXPECT_TRUE(entry2.Load());
46   EXPECT_TRUE(entry2.Data() != nullptr);
47   EXPECT_TRUE(0 == entry2.Data()->hash);
48 
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());
53 }
54 
TEST_F(DiskCacheTest,StorageBlock_SetModified)55 TEST_F(DiskCacheTest, StorageBlock_SetModified) {
56   base::FilePath filename = cache_path_.AppendASCII("a_test");
57   auto file = base::MakeRefCounted<disk_cache::MappedFile>();
58   ASSERT_TRUE(CreateCacheTestFile(filename));
59   ASSERT_TRUE(file->Init(filename, 8192));
60 
61   auto entry1 = std::make_unique<CacheEntryBlock>(file.get(),
62                                                   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();
67   entry1.reset();
68 
69   CacheEntryBlock entry2(file.get(), disk_cache::Addr(0xa0010003));
70   EXPECT_TRUE(entry2.Load());
71   EXPECT_TRUE(0x45687912 == entry2.Data()->hash);
72 }
73 
TEST_F(DiskCacheTest,StorageBlock_DifferentNumBuffers)74 TEST_F(DiskCacheTest, StorageBlock_DifferentNumBuffers) {
75   base::FilePath filename = cache_path_.AppendASCII("a_test");
76   auto file = base::MakeRefCounted<disk_cache::MappedFile>();
77   ASSERT_TRUE(CreateCacheTestFile(filename));
78   ASSERT_TRUE(file->Init(filename, 8192));
79 
80   // 2 buffers at index 1.
81   CacheEntryBlock entry1(file.get(), disk_cache::Addr(0xa1010001));
82   EXPECT_TRUE(entry1.Load());
83 
84   // 1 buffer at index 3.
85   CacheEntryBlock entry2(file.get(), disk_cache::Addr(0xa0010003));
86   EXPECT_TRUE(entry2.Load());
87 
88   // Now specify 2 buffers at index 1.
89   entry2.CopyFrom(&entry1);
90   EXPECT_TRUE(entry2.Load());
91 }
92