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

Side by Side Diff: content/child/webcrypto/test/test_helpers.cc

Issue 510583002: Improve a poor implementation of "find duplicates". (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 6 years, 3 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
« no previous file with comments | « content/child/webcrypto/test/test_helpers.h ('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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "content/child/webcrypto/test/test_helpers.h" 5 #include "content/child/webcrypto/test/test_helpers.h"
6 6
7 #include <algorithm>
8
7 #include "base/file_util.h" 9 #include "base/file_util.h"
8 #include "base/json/json_reader.h" 10 #include "base/json/json_reader.h"
9 #include "base/json/json_writer.h" 11 #include "base/json/json_writer.h"
10 #include "base/logging.h" 12 #include "base/logging.h"
11 #include "base/path_service.h" 13 #include "base/path_service.h"
12 #include "base/stl_util.h" 14 #include "base/stl_util.h"
13 #include "base/strings/string_number_conversions.h" 15 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_util.h" 16 #include "base/strings/string_util.h"
15 #include "base/values.h" 17 #include "base/values.h"
16 #include "content/child/webcrypto/algorithm_dispatch.h" 18 #include "content/child/webcrypto/algorithm_dispatch.h"
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 }; 234 };
233 235
234 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kDigestNameToId); ++i) { 236 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kDigestNameToId); ++i) {
235 if (kDigestNameToId[i].name == algorithm_name) 237 if (kDigestNameToId[i].name == algorithm_name)
236 return CreateAlgorithm(kDigestNameToId[i].id); 238 return CreateAlgorithm(kDigestNameToId[i].id);
237 } 239 }
238 240
239 return blink::WebCryptoAlgorithm::createNull(); 241 return blink::WebCryptoAlgorithm::createNull();
240 } 242 }
241 243
242 // Returns true if any of the vectors in the input list have identical content. 244 // Creates a comparator for |bufs| which operates on indices rather than values.
243 // Dumb O(n^2) implementation but should be fast enough for the input sizes that 245 class CompareUsingIndex {
244 // are used. 246 public:
247 explicit CompareUsingIndex(const std::vector<std::vector<uint8_t> >* bufs)
248 : bufs_(bufs) {}
249
250 bool operator()(size_t i1, size_t i2) { return (*bufs_)[i1] < (*bufs_)[i2]; }
251
252 private:
253 const std::vector<std::vector<uint8_t> >* bufs_;
254 };
255
245 bool CopiesExist(const std::vector<std::vector<uint8_t> >& bufs) { 256 bool CopiesExist(const std::vector<std::vector<uint8_t> >& bufs) {
246 for (size_t i = 0; i < bufs.size(); ++i) { 257 // Sort the indices of |bufs| into a separate vector. This reduces the amount
247 for (size_t j = i + 1; j < bufs.size(); ++j) { 258 // of data copied versus sorting |bufs| directly.
248 if (CryptoData(bufs[i]) == CryptoData(bufs[j])) 259 std::vector<size_t> sorted_indices(bufs.size());
249 return true; 260 for (size_t i = 0; i < sorted_indices.size(); ++i)
250 } 261 sorted_indices[i] = i;
262 std::sort(
263 sorted_indices.begin(), sorted_indices.end(), CompareUsingIndex(&bufs));
264
265 // Scan for adjacent duplicates.
266 for (size_t i = 1; i < sorted_indices.size(); ++i) {
267 if (bufs[sorted_indices[i]] == bufs[sorted_indices[i - 1]])
268 return true;
251 } 269 }
252 return false; 270 return false;
253 } 271 }
254 272
255 blink::WebCryptoAlgorithm CreateAesKeyGenAlgorithm( 273 blink::WebCryptoAlgorithm CreateAesKeyGenAlgorithm(
256 blink::WebCryptoAlgorithmId aes_alg_id, 274 blink::WebCryptoAlgorithmId aes_alg_id,
257 unsigned short length) { 275 unsigned short length) {
258 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( 276 return blink::WebCryptoAlgorithm::adoptParamsAndCreate(
259 aes_alg_id, new blink::WebCryptoAesKeyGenParams(length)); 277 aes_alg_id, new blink::WebCryptoAesKeyGenParams(length));
260 } 278 }
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after
557 // Export the key in raw format and compare to the original. 575 // Export the key in raw format and compare to the original.
558 std::vector<uint8_t> key_raw_out; 576 std::vector<uint8_t> key_raw_out;
559 ASSERT_EQ(Status::Success(), 577 ASSERT_EQ(Status::Success(),
560 ExportKey(blink::WebCryptoKeyFormatRaw, key, &key_raw_out)); 578 ExportKey(blink::WebCryptoKeyFormatRaw, key, &key_raw_out));
561 EXPECT_BYTES_EQ_HEX(key_hex, key_raw_out); 579 EXPECT_BYTES_EQ_HEX(key_hex, key_raw_out);
562 } 580 }
563 581
564 } // namespace webcrypto 582 } // namespace webcrypto
565 583
566 } // namesapce content 584 } // namesapce content
OLDNEW
« no previous file with comments | « content/child/webcrypto/test/test_helpers.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698