File: upload_bytes_element_reader_unittest.cc

package info (click to toggle)
chromium 145.0.7632.159-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,976,224 kB
  • sloc: cpp: 36,198,469; ansic: 7,634,080; javascript: 3,564,060; python: 1,649,622; xml: 838,470; asm: 717,087; pascal: 185,708; sh: 88,786; perl: 88,718; objc: 79,984; sql: 59,811; cs: 42,452; fortran: 24,101; makefile: 21,144; tcl: 15,277; php: 14,022; yacc: 9,066; ruby: 7,553; awk: 3,720; lisp: 3,233; lex: 1,328; ada: 727; jsp: 228; sed: 36
file content (99 lines) | stat: -rw-r--r-- 3,534 bytes parent folder | download | duplicates (8)
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
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/base/upload_bytes_element_reader.h"

#include <memory>

#include "base/containers/span.h"
#include "net/base/completion_once_callback.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/test/gtest_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"

using net::test::IsOk;

namespace net {

class UploadBytesElementReaderTest : public PlatformTest {
 protected:
  void SetUp() override {
    bytes_.assign({'1', '2', '3', 'a', 'b', 'c'});
    reader_ =
        std::make_unique<UploadBytesElementReader>(base::as_byte_span(bytes_));
    ASSERT_THAT(reader_->Init(CompletionOnceCallback()), IsOk());
    EXPECT_EQ(bytes_.size(), reader_->GetContentLength());
    EXPECT_EQ(bytes_.size(), reader_->BytesRemaining());
    EXPECT_TRUE(reader_->IsInMemory());
  }

  std::vector<char> bytes_;
  std::unique_ptr<UploadElementReader> reader_;
};

TEST_F(UploadBytesElementReaderTest, ReadPartially) {
  const size_t kHalfSize = bytes_.size() / 2;
  std::vector<char> buf(kHalfSize);
  auto wrapped_buffer = base::MakeRefCounted<WrappedIOBuffer>(buf);
  EXPECT_EQ(static_cast<int>(buf.size()),
            reader_->Read(wrapped_buffer.get(), buf.size(),
                          CompletionOnceCallback()));
  EXPECT_EQ(bytes_.size() - buf.size(), reader_->BytesRemaining());
  bytes_.resize(kHalfSize);  // Resize to compare.
  EXPECT_EQ(bytes_, buf);
}

TEST_F(UploadBytesElementReaderTest, ReadAll) {
  std::vector<char> buf(bytes_.size());
  auto wrapped_buffer = base::MakeRefCounted<WrappedIOBuffer>(buf);
  EXPECT_EQ(static_cast<int>(buf.size()),
            reader_->Read(wrapped_buffer.get(), buf.size(),
                          CompletionOnceCallback()));
  EXPECT_EQ(0U, reader_->BytesRemaining());
  EXPECT_EQ(bytes_, buf);
  // Try to read again.
  EXPECT_EQ(0, reader_->Read(wrapped_buffer.get(), buf.size(),
                             CompletionOnceCallback()));
}

TEST_F(UploadBytesElementReaderTest, ReadTooMuch) {
  const size_t kTooLargeSize = bytes_.size() * 2;
  std::vector<char> buf(kTooLargeSize);
  auto wrapped_buffer = base::MakeRefCounted<WrappedIOBuffer>(buf);
  EXPECT_EQ(static_cast<int>(bytes_.size()),
            reader_->Read(wrapped_buffer.get(), buf.size(),
                          CompletionOnceCallback()));
  EXPECT_EQ(0U, reader_->BytesRemaining());
  buf.resize(bytes_.size());  // Resize to compare.
  EXPECT_EQ(bytes_, buf);
}

TEST_F(UploadBytesElementReaderTest, MultipleInit) {
  std::vector<char> buf(bytes_.size());
  auto wrapped_buffer = base::MakeRefCounted<WrappedIOBuffer>(buf);

  // Read all.
  EXPECT_EQ(static_cast<int>(buf.size()),
            reader_->Read(wrapped_buffer.get(), buf.size(),
                          CompletionOnceCallback()));
  EXPECT_EQ(0U, reader_->BytesRemaining());
  EXPECT_EQ(bytes_, buf);

  // Call Init() again to reset the state.
  ASSERT_THAT(reader_->Init(CompletionOnceCallback()), IsOk());
  EXPECT_EQ(bytes_.size(), reader_->GetContentLength());
  EXPECT_EQ(bytes_.size(), reader_->BytesRemaining());

  // Read again.
  EXPECT_EQ(static_cast<int>(buf.size()),
            reader_->Read(wrapped_buffer.get(), buf.size(),
                          CompletionOnceCallback()));
  EXPECT_EQ(0U, reader_->BytesRemaining());
  EXPECT_EQ(bytes_, buf);
}

}  // namespace net