aboutsummaryrefslogtreecommitdiff
path: root/cc/streamingaead/shared_input_stream_test.cc
blob: aef6638ad092e20743aabe3a72fa69e07c4c64e2 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright 2019 Google Inc.
//
// 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_input_stream.h"

#include <algorithm>
#include <memory>
#include <sstream>
#include <string>
#include <utility>

#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/memory/memory.h"
#include "absl/status/status.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "tink/input_stream.h"
#include "tink/streamingaead/buffered_input_stream.h"
#include "tink/subtle/random.h"
#include "tink/subtle/test_util.h"
#include "tink/util/istream_input_stream.h"
#include "tink/util/status.h"
#include "tink/util/test_matchers.h"

namespace crypto {
namespace tink {
namespace streamingaead {
namespace {

using crypto::tink::test::IsOk;
using crypto::tink::test::StatusIs;
using subtle::test::ReadFromStream;

static int kBufferSize = 4096;

// Creates an InputStream with the specified contents.
std::unique_ptr<InputStream> GetInputStream(absl::string_view contents) {
  // Prepare ciphertext source stream.
  auto string_stream =
      absl::make_unique<std::stringstream>(std::string(contents));
  std::unique_ptr<InputStream> input_stream(
      absl::make_unique<util::IstreamInputStream>(
          std::move(string_stream), kBufferSize));
  return input_stream;
}

// Attempts to read 'count' bytes from 'input_stream', and writes the read
// bytes to 'output'.
util::Status ReadFromStream(InputStream* input_stream, int count,
                            std::string* output) {
  if (input_stream == nullptr || output == nullptr || count < 0) {
    return util::Status(absl::StatusCode::kInternal,
                        "Illegal read from a stream");
  }
  const void* buffer;
  output->clear();
  int bytes_to_read = count;
  while (bytes_to_read > 0) {
    auto next_result = input_stream->Next(&buffer);
    if (next_result.status().code() == absl::StatusCode::kOutOfRange) {
      // End of stream.
      return util::OkStatus();
    }
    if (!next_result.ok()) return next_result.status();
    auto read_bytes = next_result.value();
    auto used_bytes = std::min(read_bytes, bytes_to_read);
    if (used_bytes > 0) {
      output->append(
          std::string(reinterpret_cast<const char*>(buffer), used_bytes));
      bytes_to_read -= used_bytes;
      if (bytes_to_read == 0) input_stream->BackUp(read_bytes - used_bytes);
    }
  }
  return util::OkStatus();
}

TEST(SharedInputStreamTest, BasicOperations) {
  for (auto input_size : {0, 1, 10, 100, 1000, 10000, 100000}) {
    std::string contents = subtle::Random::GetRandomBytes(input_size);
    auto input_stream = GetInputStream(contents);
    auto buffered_stream =
        std::make_shared<BufferedInputStream>(std::move(input_stream));
    for (auto read_size : {0, 1, 10, 123, 300}) {
      SCOPED_TRACE(absl::StrCat("input_size = ", input_size,
                                ", read_size = ", read_size));
      {
        auto shared_stream =
            absl::make_unique<SharedInputStream>(buffered_stream.get());

        // Read a prefix of the stream.
        std::string prefix;
        auto status = ReadFromStream(shared_stream.get(), read_size, &prefix);
        EXPECT_THAT(status, IsOk());
        EXPECT_EQ(std::min(read_size, input_size), shared_stream->Position());
        EXPECT_EQ(contents.substr(0, read_size), prefix);
        EXPECT_EQ(buffered_stream->Position(), shared_stream->Position());

        // Read the rest of the stream.
        std::string rest;
        status = ReadFromStream(shared_stream.get(), &rest);
        EXPECT_THAT(status, IsOk());
        EXPECT_EQ(input_size, shared_stream->Position());
        EXPECT_EQ(contents, prefix + rest);
        EXPECT_EQ(buffered_stream->Position(), shared_stream->Position());

        // Try reading again, should get an empty string.
        status = ReadFromStream(shared_stream.get(), &rest);
        EXPECT_THAT(status, IsOk());
        EXPECT_EQ("", rest);
        EXPECT_EQ(buffered_stream->Position(), shared_stream->Position());
      }

      // Now that shared_stream is out of scope, we rewind the underlying
      // buffered_stream, so that the next read iteration starts from
      // the beginning.
      auto status = buffered_stream->Rewind();
      EXPECT_THAT(status, IsOk());
      EXPECT_EQ(0, buffered_stream->Position());
    }
  }
}


TEST(SharedInputStreamTest, SingleBackup) {
  for (auto input_size : {0, 1, 10, 100, 1000, 10000, 100000}) {
    std::string contents = subtle::Random::GetRandomBytes(input_size);
    auto input_stream = GetInputStream(contents);
    auto buffered_stream =
        std::make_shared<BufferedInputStream>(std::move(input_stream));
    for (auto read_size : {0, 1, 10, 123, 300, 1024}) {
      SCOPED_TRACE(absl::StrCat("input_size = ", input_size,
                                ", read_size = ", read_size));
      {
        auto shared_stream = absl::make_unique<SharedInputStream>(
            buffered_stream.get());

        // Read a part of the stream.
        std::string prefix;
        auto status = ReadFromStream(shared_stream.get(), read_size, &prefix);
        EXPECT_THAT(status, IsOk());
        EXPECT_EQ(std::min(read_size, input_size), shared_stream->Position());
        EXPECT_EQ(contents.substr(0, read_size), prefix);

        // Read the next block of the stream, and then back it up.
        const void* buf;
        int pos = shared_stream->Position();
        auto next_result = shared_stream->Next(&buf);
        if (read_size < input_size) {
          EXPECT_THAT(next_result, IsOk());
          auto next_size = next_result.value();
          EXPECT_EQ(pos + next_size, shared_stream->Position());
          shared_stream->BackUp(next_size);
          EXPECT_EQ(pos, shared_stream->Position());
          shared_stream->BackUp(input_size);
          EXPECT_EQ(pos, shared_stream->Position());
        } else {
          EXPECT_THAT(next_result.status(),
                      StatusIs(absl::StatusCode::kOutOfRange));
        }

        // Read the rest of the input.
        std::string rest;
        status = ReadFromStream(shared_stream.get(), &rest);
        EXPECT_THAT(status, IsOk());
        EXPECT_EQ(input_size, shared_stream->Position());
        EXPECT_EQ(contents, prefix + rest);
      }
      // Now that shared_stream is out of scope, we rewind the underlying
      // buffered_stream, so that the next read iteration starts from
      // the beginning.
      auto status = buffered_stream->Rewind();
      EXPECT_THAT(status, IsOk());
      EXPECT_EQ(0, buffered_stream->Position());
    }
  }
}

TEST(SharedInputStreamTest, MultipleBackups) {
  int input_size = 70000;
  std::string contents = subtle::Random::GetRandomBytes(input_size);
  auto input_stream = GetInputStream(contents);
  auto buffered_stream =
      std::make_shared<BufferedInputStream>(std::move(input_stream));

  for (int i = 0; i < 2; i++) {  // Two rounds, to test with Rewind.
    auto status = buffered_stream->Rewind();
    EXPECT_THAT(status, IsOk());
    EXPECT_EQ(0, buffered_stream->Position());

    auto shared_stream = absl::make_unique<SharedInputStream>(
        buffered_stream.get());
    EXPECT_EQ(0, shared_stream->Position());

    const void* buffer;
    auto next_result = shared_stream->Next(&buffer);
    EXPECT_THAT(next_result, IsOk());
    auto next_size = next_result.value();
    EXPECT_EQ(contents.substr(0, next_size),
              std::string(static_cast<const char*>(buffer), next_size));

    // BackUp several times, but in total fewer bytes than returned by Next().
    int total_backup_size = 0;
    for (auto backup_size : {0, 1, 5, 0, 10, 100, -42, 400, 20, -100}) {
      shared_stream->BackUp(backup_size);
      total_backup_size += std::max(0, backup_size);
      EXPECT_EQ(next_size - total_backup_size, shared_stream->Position());
      EXPECT_EQ(buffered_stream->Position(), shared_stream->Position());
    }
    EXPECT_GT(next_size, total_backup_size);

    // Call Next(), it should return exactly the backed up bytes.
    next_result = shared_stream->Next(&buffer);
    EXPECT_THAT(next_result, IsOk());
    EXPECT_EQ(total_backup_size, next_result.value());
    EXPECT_EQ(next_size, shared_stream->Position());
    EXPECT_EQ(buffered_stream->Position(), shared_stream->Position());
    EXPECT_EQ(contents.substr(next_size - total_backup_size, total_backup_size),
              std::string(static_cast<const char*>(buffer), total_backup_size));
  }
}


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