aboutsummaryrefslogtreecommitdiff
path: root/cc/streamingaead/shared_random_access_stream_test.cc
blob: ddbb5b39ff9aa1f52fd0b69a545dffa29643b451 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///////////////////////////////////////////////////////////////////////////////

#include "tink/streamingaead/shared_random_access_stream.h"

#include <string>
#include <utility>

#include "gtest/gtest.h"
#include "absl/memory/memory.h"
#include "absl/strings/str_cat.h"
#include "tink/internal/test_random_access_stream.h"
#include "tink/random_access_stream.h"
#include "tink/subtle/random.h"

namespace crypto {
namespace tink {
namespace streamingaead {
namespace {

TEST(SharedRandomAccessStreamTest, ReadingStreams) {
  for (auto stream_size : {0, 10, 100, 1000, 10000, 1000000}) {
    SCOPED_TRACE(absl::StrCat("stream_size = ", stream_size));
    std::string stream_content = subtle::Random::GetRandomBytes(stream_size);
    auto ra_stream =
        absl::make_unique<internal::TestRandomAccessStream>(stream_content);
    SharedRandomAccessStream shared_stream(ra_stream.get());
    std::string stream_contents;
    auto status = internal::ReadAllFromRandomAccessStream(
        &shared_stream, stream_contents, /*chunk_size=*/1 + (stream_size / 10));
    EXPECT_EQ(absl::StatusCode::kOutOfRange, status.code());
    EXPECT_EQ("EOF", status.message());
    EXPECT_EQ(stream_content, stream_contents);
    EXPECT_EQ(stream_size, shared_stream.size().value());
  }
}


}  // namespace
}  // namespace streamingaead
}  // namespace tink
}  // namespace crypto