diff options
author | mattm <mattm@chromium.org> | 2015-05-11 04:01:07 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-05-11 11:01:29 +0000 |
commit | bac89e25f8378ba23452dd96a96f329564908da7 (patch) | |
tree | fa328afb040d156808daa3c62c824f214b0a1500 /net/der | |
parent | 547dcf23cd4b129b83bc6667f69bdac4843c2284 (diff) | |
download | chromium_src-bac89e25f8378ba23452dd96a96f329564908da7.zip chromium_src-bac89e25f8378ba23452dd96a96f329564908da7.tar.gz chromium_src-bac89e25f8378ba23452dd96a96f329564908da7.tar.bz2 |
Trivial implementation of distinguished name comparison.
BUG=410574
Review URL: https://codereview.chromium.org/1126023002
Cr-Commit-Position: refs/heads/master@{#329140}
Diffstat (limited to 'net/der')
-rw-r--r-- | net/der/input.cc | 8 | ||||
-rw-r--r-- | net/der/input.h | 3 | ||||
-rw-r--r-- | net/der/input_unittest.cc | 15 |
3 files changed, 26 insertions, 0 deletions
diff --git a/net/der/input.cc b/net/der/input.cc index f896bb8..004034e 100644 --- a/net/der/input.cc +++ b/net/der/input.cc @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include <string.h> + #include "base/logging.h" #include "net/der/input.h" @@ -19,6 +21,12 @@ Input::Input(const std::string& s) : data_(reinterpret_cast<const uint8_t*>(s.data())), len_(s.size()) { } +bool Input::Equals(const Input& other) const { + if (len_ != other.len_) + return false; + return memcmp(data_, other.data_, len_) == 0; +} + ByteReader::ByteReader(const Input& in) : data_(in.UnsafeData()), len_(in.Length()) { } diff --git a/net/der/input.h b/net/der/input.h index 667bd89..5814375 100644 --- a/net/der/input.h +++ b/net/der/input.h @@ -50,6 +50,9 @@ class NET_EXPORT_PRIVATE Input { // Returns the length in bytes of an Input's data. size_t Length() const { return len_; } + // Return true if the Input's data and |other|'s data are byte-wise equal. + bool Equals(const Input& other) const; + // Returns a pointer to the Input's data. This method is marked as "unsafe" // because access to the Input's data should be done through ByteReader // instead. This method should only be used where using a ByteReader truly diff --git a/net/der/input_unittest.cc b/net/der/input_unittest.cc index a6a9b8c..e8145b5 100644 --- a/net/der/input_unittest.cc +++ b/net/der/input_unittest.cc @@ -12,6 +12,21 @@ namespace test { const uint8_t kInput[] = {'t', 'e', 's', 't'}; +TEST(InputTest, Equals) { + Input test(kInput, arraysize(kInput)); + Input test2(kInput, arraysize(kInput)); + EXPECT_TRUE(test.Equals(test2)); + + std::string input_copy(reinterpret_cast<const char*>(kInput), + arraysize(kInput)); + Input test_copy(input_copy); + EXPECT_TRUE(test.Equals(test_copy)); + + Input test_truncated(kInput, arraysize(kInput) - 1); + EXPECT_FALSE(test.Equals(test_truncated)); + EXPECT_FALSE(test_truncated.Equals(test)); +} + TEST(ByteReaderTest, NoReadPastEnd) { ByteReader reader(Input(nullptr, 0)); uint8_t data; |