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
3 // found in the LICENSE file.
5 #include "crypto/openssl_bio_string.h"
7 #include <openssl/bio.h>
9 #include "crypto/openssl_util.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 TEST(OpenSSLBIOString
, TestWrite
) {
14 const std::string
expected1("a one\nb 2\n");
15 const std::string
expected2("c d e f");
16 const std::string
expected3("g h i");
18 crypto::ScopedOpenSSL
<BIO
, BIO_free_all
> bio(crypto::BIO_new_string(&s
));
19 ASSERT_TRUE(bio
.get());
21 EXPECT_EQ(static_cast<int>(expected1
.size()),
22 BIO_printf(bio
.get(), "a %s\nb %i\n", "one", 2));
23 EXPECT_EQ(expected1
, s
);
24 EXPECT_EQ(static_cast<int>(expected1
.size()), BIO_tell(bio
.get()));
26 EXPECT_EQ(1, BIO_flush(bio
.get()));
27 EXPECT_EQ(-1, BIO_seek(bio
.get(), 0));
28 EXPECT_EQ(expected1
, s
);
30 EXPECT_EQ(static_cast<int>(expected2
.size()),
31 BIO_write(bio
.get(), expected2
.data(), expected2
.size()));
32 EXPECT_EQ(expected1
+ expected2
, s
);
33 EXPECT_EQ(static_cast<int>(expected1
.size() + expected2
.size()),
36 EXPECT_EQ(static_cast<int>(expected3
.size()),
37 BIO_puts(bio
.get(), expected3
.c_str()));
38 EXPECT_EQ(expected1
+ expected2
+ expected3
, s
);
39 EXPECT_EQ(static_cast<int>(expected1
.size() + expected2
.size() +
43 EXPECT_EQ(expected1
+ expected2
+ expected3
, s
);
46 TEST(OpenSSLBIOString
, TestReset
) {
48 const std::string
expected1("a b c\n");
49 const std::string
expected2("d e f g\n");
51 crypto::ScopedOpenSSL
<BIO
, BIO_free_all
> bio(crypto::BIO_new_string(&s
));
52 ASSERT_TRUE(bio
.get());
54 EXPECT_EQ(static_cast<int>(expected1
.size()),
55 BIO_write(bio
.get(), expected1
.data(), expected1
.size()));
56 EXPECT_EQ(expected1
, s
);
58 EXPECT_EQ(1, BIO_reset(bio
.get()));
59 EXPECT_EQ(std::string(), s
);
61 EXPECT_EQ(static_cast<int>(expected2
.size()),
62 BIO_write(bio
.get(), expected2
.data(), expected2
.size()));
63 EXPECT_EQ(expected2
, s
);
65 EXPECT_EQ(expected2
, s
);