OLD | NEW |
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 "crypto/openssl_bio_string.h" | 5 #include "crypto/openssl_bio_string.h" |
6 | 6 |
7 #include <openssl/bio.h> | 7 #include <openssl/bio.h> |
8 | 8 |
9 #include "crypto/scoped_openssl_types.h" | 9 #include "crypto/scoped_openssl_types.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 | 11 |
12 namespace crypto { | 12 namespace crypto { |
13 | 13 |
14 TEST(OpenSSLBIOString, TestWrite) { | 14 TEST(OpenSSLBIOString, TestWrite) { |
15 std::string s; | 15 std::string s; |
16 const std::string expected1("a one\nb 2\n"); | 16 const std::string expected1("a one\nb 2\n"); |
17 const std::string expected2("c d e f"); | 17 const std::string expected2("c d e f"); |
18 const std::string expected3("g h i"); | 18 const std::string expected3("g h i"); |
19 { | 19 { |
20 ScopedBIO bio(BIO_new_string(&s)); | 20 ScopedBIO bio(BIO_new_string(&s)); |
21 ASSERT_TRUE(bio.get()); | 21 ASSERT_TRUE(bio.get()); |
22 | 22 |
23 EXPECT_EQ(static_cast<int>(expected1.size()), | 23 EXPECT_EQ(static_cast<int>(expected1.size()), |
24 BIO_printf(bio.get(), "a %s\nb %i\n", "one", 2)); | 24 BIO_printf(bio.get(), "a %s\nb %i\n", "one", 2)); |
25 EXPECT_EQ(expected1, s); | 25 EXPECT_EQ(expected1, s); |
26 EXPECT_EQ(static_cast<int>(expected1.size()), BIO_tell(bio.get())); | |
27 | 26 |
28 EXPECT_EQ(1, BIO_flush(bio.get())); | 27 EXPECT_EQ(1, BIO_flush(bio.get())); |
29 EXPECT_EQ(-1, BIO_seek(bio.get(), 0)); | |
30 EXPECT_EQ(expected1, s); | 28 EXPECT_EQ(expected1, s); |
31 | 29 |
32 EXPECT_EQ(static_cast<int>(expected2.size()), | 30 EXPECT_EQ(static_cast<int>(expected2.size()), |
33 BIO_write(bio.get(), expected2.data(), expected2.size())); | 31 BIO_write(bio.get(), expected2.data(), expected2.size())); |
34 EXPECT_EQ(expected1 + expected2, s); | 32 EXPECT_EQ(expected1 + expected2, s); |
35 EXPECT_EQ(static_cast<int>(expected1.size() + expected2.size()), | |
36 BIO_tell(bio.get())); | |
37 | 33 |
38 EXPECT_EQ(static_cast<int>(expected3.size()), | 34 EXPECT_EQ(static_cast<int>(expected3.size()), |
39 BIO_puts(bio.get(), expected3.c_str())); | 35 BIO_puts(bio.get(), expected3.c_str())); |
40 EXPECT_EQ(expected1 + expected2 + expected3, s); | 36 EXPECT_EQ(expected1 + expected2 + expected3, s); |
41 EXPECT_EQ(static_cast<int>(expected1.size() + expected2.size() + | |
42 expected3.size()), | |
43 BIO_tell(bio.get())); | |
44 } | 37 } |
45 EXPECT_EQ(expected1 + expected2 + expected3, s); | 38 EXPECT_EQ(expected1 + expected2 + expected3, s); |
46 } | 39 } |
47 | 40 |
48 TEST(OpenSSLBIOString, TestReset) { | 41 TEST(OpenSSLBIOString, TestReset) { |
49 std::string s; | 42 std::string s; |
50 const std::string expected1("a b c\n"); | 43 const std::string expected1("a b c\n"); |
51 const std::string expected2("d e f g\n"); | 44 const std::string expected2("d e f g\n"); |
52 { | 45 { |
53 ScopedBIO bio(BIO_new_string(&s)); | 46 ScopedBIO bio(BIO_new_string(&s)); |
54 ASSERT_TRUE(bio.get()); | 47 ASSERT_TRUE(bio.get()); |
55 | 48 |
56 EXPECT_EQ(static_cast<int>(expected1.size()), | 49 EXPECT_EQ(static_cast<int>(expected1.size()), |
57 BIO_write(bio.get(), expected1.data(), expected1.size())); | 50 BIO_write(bio.get(), expected1.data(), expected1.size())); |
58 EXPECT_EQ(expected1, s); | 51 EXPECT_EQ(expected1, s); |
59 | 52 |
60 EXPECT_EQ(1, BIO_reset(bio.get())); | 53 EXPECT_EQ(1, BIO_reset(bio.get())); |
61 EXPECT_EQ(std::string(), s); | 54 EXPECT_EQ(std::string(), s); |
62 | 55 |
63 EXPECT_EQ(static_cast<int>(expected2.size()), | 56 EXPECT_EQ(static_cast<int>(expected2.size()), |
64 BIO_write(bio.get(), expected2.data(), expected2.size())); | 57 BIO_write(bio.get(), expected2.data(), expected2.size())); |
65 EXPECT_EQ(expected2, s); | 58 EXPECT_EQ(expected2, s); |
66 } | 59 } |
67 EXPECT_EQ(expected2, s); | 60 EXPECT_EQ(expected2, s); |
68 } | 61 } |
69 | 62 |
70 } // namespace crypto | 63 } // namespace crypto |
OLD | NEW |