xref: /aosp_15_r20/external/tink/cc/internal/err_util_test.cc (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1 // Copyright 2021 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 ///////////////////////////////////////////////////////////////////////////////
16 #include "tink/internal/err_util.h"
17 
18 #include <string>
19 #include <vector>
20 
21 #include "gmock/gmock.h"
22 #include "gtest/gtest.h"
23 #include "absl/strings/str_split.h"
24 #include "absl/strings/string_view.h"
25 #include "openssl/bio.h"
26 #include "openssl/err.h"
27 
28 namespace crypto {
29 namespace tink {
30 namespace internal {
31 namespace {
32 
33 using ::testing::AllOf;
34 using ::testing::HasSubstr;
35 using ::testing::IsEmpty;
36 using ::testing::SizeIs;
37 
TEST(GetSslErrorsTest,ReturnsExpectedErrorrs)38 TEST(GetSslErrorsTest, ReturnsExpectedErrorrs) {
39   // Artificially add some errors to OpenSSL/BoringSSL's error queue.
40 #ifdef OPENSSL_IS_BORINGSSL
41   OPENSSL_PUT_ERROR(BIO, BIO_R_UNINITIALIZED);
42   OPENSSL_PUT_ERROR(BIO, BIO_R_WRITE_TO_READ_ONLY_BIO);
43   OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD);
44 #else
45   BIOerr(BIO_F_BIO_READ_INTERN, BIO_R_UNINITIALIZED);
46   BIOerr(BIO_F_MEM_WRITE, BIO_R_WRITE_TO_READ_ONLY_BIO);
47   BIOerr(BIO_F_BIO_CTRL, BIO_R_UNSUPPORTED_METHOD);
48 #endif
49   std::string error = GetSslErrors();
50 
51   // OpenSSL/BoringSSL returns each error as a null terminated char*; since we
52   // accumulate each of them into a std::string, the resulting string will be
53   // "double null terminated". So we ignore the last char, and split by \n.
54   auto errors_without_last_char = absl::string_view(error);
55   errors_without_last_char.remove_suffix(1);
56   std::vector<std::string> lines =
57       absl::StrSplit(errors_without_last_char, '\n');
58   ASSERT_THAT(lines, SizeIs(3));
59 
60 #ifdef OPENSSL_IS_BORINGSSL
61   std::string uninitialized_str = "UNINITIALIZED";
62   std::string write_to_read_only_bio_str = "WRITE_TO_READ_ONLY_BIO";
63   std::string unsupported_method_str = "UNSUPPORTED_METHOD";
64 #else
65   std::string uninitialized_str = "uninitialized";
66   std::string write_to_read_only_bio_str = "write to read only BIO";
67   std::string unsupported_method_str = "unsupported method";
68 #endif
69 
70   EXPECT_THAT(lines[0], AllOf(HasSubstr("BIO"), HasSubstr(uninitialized_str)));
71   EXPECT_THAT(lines[1],
72               AllOf(HasSubstr("BIO"), HasSubstr(write_to_read_only_bio_str)));
73   EXPECT_THAT(lines[2],
74               AllOf(HasSubstr("BIO"), HasSubstr(unsupported_method_str)));
75   // A second call to GetSslErrors() returns an empty string because the
76   // OpenSSL/BoringSSL error queue is empty.
77   EXPECT_THAT(GetSslErrors(), IsEmpty());
78 }
79 
80 }  // namespace
81 }  // namespace internal
82 }  // namespace tink
83 }  // namespace crypto
84