xref: /aosp_15_r20/external/tink/cc/streamingaead/shared_random_access_stream_test.cc (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1 // Copyright 2019 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 
17 #include "tink/streamingaead/shared_random_access_stream.h"
18 
19 #include <string>
20 #include <utility>
21 
22 #include "gtest/gtest.h"
23 #include "absl/memory/memory.h"
24 #include "absl/strings/str_cat.h"
25 #include "tink/internal/test_random_access_stream.h"
26 #include "tink/random_access_stream.h"
27 #include "tink/subtle/random.h"
28 
29 namespace crypto {
30 namespace tink {
31 namespace streamingaead {
32 namespace {
33 
TEST(SharedRandomAccessStreamTest,ReadingStreams)34 TEST(SharedRandomAccessStreamTest, ReadingStreams) {
35   for (auto stream_size : {0, 10, 100, 1000, 10000, 1000000}) {
36     SCOPED_TRACE(absl::StrCat("stream_size = ", stream_size));
37     std::string stream_content = subtle::Random::GetRandomBytes(stream_size);
38     auto ra_stream =
39         absl::make_unique<internal::TestRandomAccessStream>(stream_content);
40     SharedRandomAccessStream shared_stream(ra_stream.get());
41     std::string stream_contents;
42     auto status = internal::ReadAllFromRandomAccessStream(
43         &shared_stream, stream_contents, /*chunk_size=*/1 + (stream_size / 10));
44     EXPECT_EQ(absl::StatusCode::kOutOfRange, status.code());
45     EXPECT_EQ("EOF", status.message());
46     EXPECT_EQ(stream_content, stream_contents);
47     EXPECT_EQ(stream_size, shared_stream.size().value());
48   }
49 }
50 
51 
52 }  // namespace
53 }  // namespace streamingaead
54 }  // namespace tink
55 }  // namespace crypto
56