- add sources.
[platform/framework/web/crosswalk.git] / src / net / disk_cache / storage_block_unittest.cc
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.
4
5 #include "base/files/file_path.h"
6 #include "net/disk_cache/disk_format.h"
7 #include "net/disk_cache/storage_block.h"
8 #include "net/disk_cache/storage_block-inl.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
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));
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
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));
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() != NULL);
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
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));
60
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();
67   delete entry1;
68
69   CacheEntryBlock entry2(file.get(), disk_cache::Addr(0xa0010003));
70   EXPECT_TRUE(entry2.Load());
71   EXPECT_TRUE(0x45687912 == entry2.Data()->hash);
72 }