Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(352)

Side by Side Diff: content/browser/indexed_db/leveldb/leveldb_unittest.cc

Issue 16256014: IndexedDB: Convert decoding functions to pass StringPieces vs. pointers (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Correct bogus iterator dereference in unit test Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « content/browser/indexed_db/leveldb/leveldb_transaction.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <algorithm> 5 #include <algorithm>
6 #include <cstring> 6 #include <cstring>
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/files/file_path.h" 9 #include "base/files/file_path.h"
10 #include "base/files/scoped_temp_dir.h" 10 #include "base/files/scoped_temp_dir.h"
(...skipping 26 matching lines...) Expand all
37 ret[i] = s[i]; 37 ret[i] = s[i];
38 return ret; 38 return ret;
39 } 39 }
40 40
41 TEST(LevelDBDatabaseTest, CorruptionTest) { 41 TEST(LevelDBDatabaseTest, CorruptionTest) {
42 base::ScopedTempDir temp_directory; 42 base::ScopedTempDir temp_directory;
43 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); 43 ASSERT_TRUE(temp_directory.CreateUniqueTempDir());
44 44
45 const std::vector<char> key = EncodeString("key"); 45 const std::vector<char> key = EncodeString("key");
46 const std::vector<char> put_value = EncodeString("value"); 46 const std::vector<char> put_value = EncodeString("value");
47 std::vector<char> got_value; 47 std::string got_value;
48 SimpleComparator comparator; 48 SimpleComparator comparator;
49 49
50 scoped_ptr<LevelDBDatabase> leveldb = 50 scoped_ptr<LevelDBDatabase> leveldb =
51 LevelDBDatabase::Open(temp_directory.path(), &comparator); 51 LevelDBDatabase::Open(temp_directory.path(), &comparator);
52 EXPECT_TRUE(leveldb); 52 EXPECT_TRUE(leveldb);
53 bool success = leveldb->Put(LevelDBSlice(key), put_value); 53 bool success = leveldb->Put(LevelDBSlice(key), put_value);
54 EXPECT_TRUE(success); 54 EXPECT_TRUE(success);
55 leveldb.Pass(); 55 leveldb.Pass();
56 EXPECT_FALSE(leveldb); 56 EXPECT_FALSE(leveldb);
57 57
58 leveldb = LevelDBDatabase::Open(temp_directory.path(), &comparator); 58 leveldb = LevelDBDatabase::Open(temp_directory.path(), &comparator);
59 EXPECT_TRUE(leveldb); 59 EXPECT_TRUE(leveldb);
60 bool found = false; 60 bool found = false;
61 success = leveldb->Get(LevelDBSlice(key), got_value, found); 61 success = leveldb->Get(LevelDBSlice(key), &got_value, found);
62 EXPECT_TRUE(success); 62 EXPECT_TRUE(success);
63 EXPECT_TRUE(found); 63 EXPECT_TRUE(found);
64 EXPECT_EQ(put_value, got_value); 64 EXPECT_EQ(put_value, std::vector<char>(got_value.begin(), got_value.end()));
65 leveldb.Pass(); 65 leveldb.Pass();
66 EXPECT_FALSE(leveldb); 66 EXPECT_FALSE(leveldb);
67 67
68 base::FilePath file_path = temp_directory.path().AppendASCII("CURRENT"); 68 base::FilePath file_path = temp_directory.path().AppendASCII("CURRENT");
69 base::PlatformFile handle = base::CreatePlatformFile( 69 base::PlatformFile handle = base::CreatePlatformFile(
70 file_path, 70 file_path,
71 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE, 71 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
72 NULL, 72 NULL,
73 NULL); 73 NULL);
74 base::TruncatePlatformFile(handle, 0); 74 base::TruncatePlatformFile(handle, 0);
75 base::ClosePlatformFile(handle); 75 base::ClosePlatformFile(handle);
76 76
77 leveldb = LevelDBDatabase::Open(temp_directory.path(), &comparator); 77 leveldb = LevelDBDatabase::Open(temp_directory.path(), &comparator);
78 EXPECT_FALSE(leveldb); 78 EXPECT_FALSE(leveldb);
79 79
80 bool destroyed = LevelDBDatabase::Destroy(temp_directory.path()); 80 bool destroyed = LevelDBDatabase::Destroy(temp_directory.path());
81 EXPECT_TRUE(destroyed); 81 EXPECT_TRUE(destroyed);
82 82
83 leveldb = LevelDBDatabase::Open(temp_directory.path(), &comparator); 83 leveldb = LevelDBDatabase::Open(temp_directory.path(), &comparator);
84 EXPECT_TRUE(leveldb); 84 EXPECT_TRUE(leveldb);
85 success = leveldb->Get(LevelDBSlice(key), got_value, found); 85 success = leveldb->Get(LevelDBSlice(key), &got_value, found);
86 EXPECT_TRUE(success); 86 EXPECT_TRUE(success);
87 EXPECT_FALSE(found); 87 EXPECT_FALSE(found);
88 } 88 }
89 89
90 TEST(LevelDBDatabaseTest, Transaction) { 90 TEST(LevelDBDatabaseTest, Transaction) {
91 base::ScopedTempDir temp_directory; 91 base::ScopedTempDir temp_directory;
92 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); 92 ASSERT_TRUE(temp_directory.CreateUniqueTempDir());
93 93
94 const std::vector<char> key = EncodeString("key"); 94 const std::vector<char> key = EncodeString("key");
95 std::vector<char> got_value; 95 std::string got_value;
96 SimpleComparator comparator; 96 SimpleComparator comparator;
97 97
98 scoped_ptr<LevelDBDatabase> leveldb = 98 scoped_ptr<LevelDBDatabase> leveldb =
99 LevelDBDatabase::Open(temp_directory.path(), &comparator); 99 LevelDBDatabase::Open(temp_directory.path(), &comparator);
100 EXPECT_TRUE(leveldb); 100 EXPECT_TRUE(leveldb);
101 101
102 const std::vector<char> old_value = EncodeString("value"); 102 const std::vector<char> old_value = EncodeString("value");
103 bool success = leveldb->Put(LevelDBSlice(key), old_value); 103 bool success = leveldb->Put(LevelDBSlice(key), old_value);
104 EXPECT_TRUE(success); 104 EXPECT_TRUE(success);
105 105
106 scoped_refptr<LevelDBTransaction> transaction = 106 scoped_refptr<LevelDBTransaction> transaction =
107 LevelDBTransaction::Create(leveldb.get()); 107 LevelDBTransaction::Create(leveldb.get());
108 108
109 const std::vector<char> new_value = EncodeString("new value"); 109 const std::vector<char> new_value = EncodeString("new value");
110 success = leveldb->Put(LevelDBSlice(key), new_value); 110 success = leveldb->Put(LevelDBSlice(key), new_value);
111 EXPECT_TRUE(success); 111 EXPECT_TRUE(success);
112 112
113 bool found = false; 113 bool found = false;
114 success = transaction->Get(LevelDBSlice(key), got_value, found); 114 success = transaction->Get(LevelDBSlice(key), &got_value, found);
115 EXPECT_TRUE(success); 115 EXPECT_TRUE(success);
116 EXPECT_TRUE(found); 116 EXPECT_TRUE(found);
117 EXPECT_EQ( 117 EXPECT_EQ(
118 comparator.Compare(LevelDBSlice(got_value), LevelDBSlice(old_value)), 0); 118 comparator.Compare(LevelDBSlice(got_value), LevelDBSlice(old_value)), 0);
119 119
120 found = false; 120 found = false;
121 success = leveldb->Get(LevelDBSlice(key), got_value, found); 121 success = leveldb->Get(LevelDBSlice(key), &got_value, found);
122 EXPECT_TRUE(success); 122 EXPECT_TRUE(success);
123 EXPECT_TRUE(found); 123 EXPECT_TRUE(found);
124 EXPECT_EQ( 124 EXPECT_EQ(
125 comparator.Compare(LevelDBSlice(got_value), LevelDBSlice(new_value)), 0); 125 comparator.Compare(LevelDBSlice(got_value), LevelDBSlice(new_value)), 0);
126 126
127 const std::vector<char> added_key = EncodeString("added key"); 127 const std::vector<char> added_key = EncodeString("added key");
128 const std::vector<char> added_value = EncodeString("added value"); 128 const std::vector<char> added_value = EncodeString("added value");
129 success = leveldb->Put(LevelDBSlice(added_key), added_value); 129 success = leveldb->Put(LevelDBSlice(added_key), added_value);
130 EXPECT_TRUE(success); 130 EXPECT_TRUE(success);
131 131
132 success = leveldb->Get(LevelDBSlice(added_key), got_value, found); 132 success = leveldb->Get(LevelDBSlice(added_key), &got_value, found);
133 EXPECT_TRUE(success); 133 EXPECT_TRUE(success);
134 EXPECT_TRUE(found); 134 EXPECT_TRUE(found);
135 EXPECT_EQ( 135 EXPECT_EQ(
136 comparator.Compare(LevelDBSlice(got_value), LevelDBSlice(added_value)), 136 comparator.Compare(LevelDBSlice(got_value), LevelDBSlice(added_value)),
137 0); 137 0);
138 138
139 success = transaction->Get(LevelDBSlice(added_key), got_value, found); 139 success = transaction->Get(LevelDBSlice(added_key), &got_value, found);
140 EXPECT_TRUE(success); 140 EXPECT_TRUE(success);
141 EXPECT_FALSE(found); 141 EXPECT_FALSE(found);
142
143 const std::vector<char> another_key = EncodeString("another key");
144 const std::vector<char> another_value = EncodeString("another value");
145 transaction->Put(LevelDBSlice(another_key), another_value);
146
147 success = transaction->Get(LevelDBSlice(another_key), &got_value, found);
148 EXPECT_TRUE(success);
149 EXPECT_TRUE(found);
150 EXPECT_EQ(
151 comparator.Compare(LevelDBSlice(got_value), LevelDBSlice(another_value)),
152 0);
142 } 153 }
143 154
144 TEST(LevelDBDatabaseTest, TransactionIterator) { 155 TEST(LevelDBDatabaseTest, TransactionIterator) {
145 base::ScopedTempDir temp_directory; 156 base::ScopedTempDir temp_directory;
146 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); 157 ASSERT_TRUE(temp_directory.CreateUniqueTempDir());
147 158
148 const std::vector<char> key1 = EncodeString("key1"); 159 const std::vector<char> key1 = EncodeString("key1");
149 const std::vector<char> value1 = EncodeString("value1"); 160 const std::vector<char> value1 = EncodeString("value1");
150 const std::vector<char> key2 = EncodeString("key2"); 161 const std::vector<char> key2 = EncodeString("key2");
151 const std::vector<char> value2 = EncodeString("value2"); 162 const std::vector<char> value2 = EncodeString("value2");
(...skipping 11 matching lines...) Expand all
163 EXPECT_TRUE(success); 174 EXPECT_TRUE(success);
164 175
165 scoped_refptr<LevelDBTransaction> transaction = 176 scoped_refptr<LevelDBTransaction> transaction =
166 LevelDBTransaction::Create(leveldb.get()); 177 LevelDBTransaction::Create(leveldb.get());
167 178
168 success = leveldb->Remove(LevelDBSlice(key2)); 179 success = leveldb->Remove(LevelDBSlice(key2));
169 EXPECT_TRUE(success); 180 EXPECT_TRUE(success);
170 181
171 scoped_ptr<LevelDBIterator> it = transaction->CreateIterator(); 182 scoped_ptr<LevelDBIterator> it = transaction->CreateIterator();
172 183
173 const char empty[] = {0}; 184 it->Seek(LevelDBSlice(std::string()));
174 it->Seek(LevelDBSlice(empty, empty));
175 185
176 EXPECT_TRUE(it->IsValid()); 186 EXPECT_TRUE(it->IsValid());
177 EXPECT_EQ(comparator.Compare(LevelDBSlice(it->Key()), LevelDBSlice(key1)), 0); 187 EXPECT_EQ(comparator.Compare(LevelDBSlice(it->Key()), LevelDBSlice(key1)), 0);
178 EXPECT_EQ(comparator.Compare(it->Value(), LevelDBSlice(value1)), 0); 188 EXPECT_EQ(comparator.Compare(it->Value(), LevelDBSlice(value1)), 0);
179 189
180 it->Next(); 190 it->Next();
181 191
182 EXPECT_TRUE(it->IsValid()); 192 EXPECT_TRUE(it->IsValid());
183 EXPECT_EQ(comparator.Compare(it->Key(), LevelDBSlice(key2)), 0); 193 EXPECT_EQ(comparator.Compare(it->Key(), LevelDBSlice(key2)), 0);
184 EXPECT_EQ(comparator.Compare(it->Value(), LevelDBSlice(value2)), 0); 194 EXPECT_EQ(comparator.Compare(it->Value(), LevelDBSlice(value2)), 0);
185 195
186 it->Next(); 196 it->Next();
187 197
188 EXPECT_FALSE(it->IsValid()); 198 EXPECT_FALSE(it->IsValid());
189 } 199 }
190 200
191 } // namespace 201 } // namespace
192 202
193 } // namespace content 203 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/indexed_db/leveldb/leveldb_transaction.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698