xref: /aosp_15_r20/external/tink/cc/subtle/test_util.cc (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1 // Copyright 2019 Google Inc.
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/subtle/test_util.h"
18 
19 #include <algorithm>
20 #include <string>
21 
22 #include "absl/status/status.h"
23 
24 namespace crypto {
25 namespace tink {
26 namespace subtle {
27 namespace test {
28 
29 const int DummyStreamSegmentEncrypter::kSegmentTagSize;
30 const char DummyStreamSegmentEncrypter::kLastSegment;
31 const char DummyStreamSegmentEncrypter::kNotLastSegment;
32 
WriteToStream(OutputStream * output_stream,absl::string_view contents,bool close_stream)33 util::Status WriteToStream(OutputStream* output_stream,
34                            absl::string_view contents, bool close_stream) {
35   void* buffer;
36   int pos = 0;
37   int remaining = contents.length();
38   int available_space = 0;
39   int available_bytes = 0;
40   while (remaining > 0) {
41     auto next_result = output_stream->Next(&buffer);
42     if (!next_result.ok()) return next_result.status();
43     available_space = next_result.value();
44     available_bytes = std::min(available_space, remaining);
45     memcpy(buffer, contents.data() + pos, available_bytes);
46     remaining -= available_bytes;
47     pos += available_bytes;
48   }
49   if (available_space > available_bytes) {
50     output_stream->BackUp(available_space - available_bytes);
51   }
52   return close_stream ? output_stream->Close() : util::OkStatus();
53 }
54 
ReadFromStream(InputStream * input_stream,std::string * output)55 util::Status ReadFromStream(InputStream* input_stream, std::string* output) {
56   if (input_stream == nullptr || output == nullptr) {
57     return util::Status(absl::StatusCode::kInternal,
58                         "Illegal read from a stream");
59   }
60   const void* buffer;
61   output->clear();
62   while (true) {
63     auto next_result = input_stream->Next(&buffer);
64     if (next_result.status().code() == absl::StatusCode::kOutOfRange) {
65       // End of stream.
66       return util::OkStatus();
67     }
68     if (!next_result.ok()) return next_result.status();
69     auto read_bytes = next_result.value();
70     if (read_bytes > 0) {
71       output->append(
72           std::string(reinterpret_cast<const char*>(buffer), read_bytes));
73     }
74   }
75   return util::OkStatus();
76 }
77 
78 }  // namespace test
79 }  // namespace subtle
80 }  // namespace tink
81 }  // namespace crypto
82