summaryrefslogtreecommitdiffstats
path: root/crypto/openssl_bio_string_unittest.cc
diff options
context:
space:
mode:
authormattm@chromium.org <mattm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-05-22 04:36:07 +0000
committermattm@chromium.org <mattm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-05-22 04:36:07 +0000
commitd46baaac9c1fe58e8a28845731ae7b28780f247c (patch)
treee642ddca456b0d7adcb430f0c11b2239da1bf210 /crypto/openssl_bio_string_unittest.cc
parent22a80ae1d50ada14e6b204978169417d2cebcaea (diff)
downloadchromium_src-d46baaac9c1fe58e8a28845731ae7b28780f247c.zip
chromium_src-d46baaac9c1fe58e8a28845731ae7b28780f247c.tar.gz
chromium_src-d46baaac9c1fe58e8a28845731ae7b28780f247c.tar.bz2
Add OpenSSL BIO method that writes to a std::string.
BUG=none Review URL: https://codereview.chromium.org/286263006 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@272100 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'crypto/openssl_bio_string_unittest.cc')
-rw-r--r--crypto/openssl_bio_string_unittest.cc66
1 files changed, 66 insertions, 0 deletions
diff --git a/crypto/openssl_bio_string_unittest.cc b/crypto/openssl_bio_string_unittest.cc
new file mode 100644
index 0000000..39d3a9a
--- /dev/null
+++ b/crypto/openssl_bio_string_unittest.cc
@@ -0,0 +1,66 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "crypto/openssl_bio_string.h"
+
+#include <openssl/bio.h>
+
+#include "crypto/openssl_util.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+TEST(OpenSSLBIOString, TestWrite) {
+ std::string s;
+ const std::string expected1("a one\nb 2\n");
+ const std::string expected2("c d e f");
+ const std::string expected3("g h i");
+ {
+ crypto::ScopedOpenSSL<BIO, BIO_free_all> bio(crypto::BIO_new_string(&s));
+ ASSERT_TRUE(bio.get());
+
+ EXPECT_EQ(static_cast<int>(expected1.size()),
+ BIO_printf(bio.get(), "a %s\nb %i\n", "one", 2));
+ EXPECT_EQ(expected1, s);
+ EXPECT_EQ(static_cast<int>(expected1.size()), BIO_tell(bio.get()));
+
+ EXPECT_EQ(1, BIO_flush(bio.get()));
+ EXPECT_EQ(-1, BIO_seek(bio.get(), 0));
+ EXPECT_EQ(expected1, s);
+
+ EXPECT_EQ(static_cast<int>(expected2.size()),
+ BIO_write(bio.get(), expected2.data(), expected2.size()));
+ EXPECT_EQ(expected1 + expected2, s);
+ EXPECT_EQ(static_cast<int>(expected1.size() + expected2.size()),
+ BIO_tell(bio.get()));
+
+ EXPECT_EQ(static_cast<int>(expected3.size()),
+ BIO_puts(bio.get(), expected3.c_str()));
+ EXPECT_EQ(expected1 + expected2 + expected3, s);
+ EXPECT_EQ(static_cast<int>(expected1.size() + expected2.size() +
+ expected3.size()),
+ BIO_tell(bio.get()));
+ }
+ EXPECT_EQ(expected1 + expected2 + expected3, s);
+}
+
+TEST(OpenSSLBIOString, TestReset) {
+ std::string s;
+ const std::string expected1("a b c\n");
+ const std::string expected2("d e f g\n");
+ {
+ crypto::ScopedOpenSSL<BIO, BIO_free_all> bio(crypto::BIO_new_string(&s));
+ ASSERT_TRUE(bio.get());
+
+ EXPECT_EQ(static_cast<int>(expected1.size()),
+ BIO_write(bio.get(), expected1.data(), expected1.size()));
+ EXPECT_EQ(expected1, s);
+
+ EXPECT_EQ(1, BIO_reset(bio.get()));
+ EXPECT_EQ(std::string(), s);
+
+ EXPECT_EQ(static_cast<int>(expected2.size()),
+ BIO_write(bio.get(), expected2.data(), expected2.size()));
+ EXPECT_EQ(expected2, s);
+ }
+ EXPECT_EQ(expected2, s);
+}