summaryrefslogtreecommitdiffstats
path: root/chrome_frame/urlmon_upload_data_stream_unittest.cc
blob: 8bab45a9d19aa1bf2c691c26926a65c9244aa8f3 (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
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "gtest/gtest.h"

#include "base/memory/ref_counted.h"
#include "base/win/scoped_comptr.h"
#include "chrome_frame/urlmon_upload_data_stream.h"

TEST(UrlmonUploadDataStreamTest, TestBasicRead) {
  char random_string[] = "some random data, no really this totally random";
  int random_string_length = strlen(random_string);
  scoped_refptr<net::UploadData> upload_data = new net::UploadData();
  upload_data->AppendBytes(random_string, random_string_length);

  CComObject<UrlmonUploadDataStream>* upload_stream = NULL;
  HRESULT hr =
      CComObject<UrlmonUploadDataStream>::CreateInstance(&upload_stream);
  ASSERT_TRUE(SUCCEEDED(hr));

  upload_stream->Initialize(upload_data.get());
  base::win::ScopedComPtr<IStream> upload_istream(upload_stream);

  char buffer[500];
  memset(buffer, 0, 500);
  ULONG bytes_read = 0;
  hr = upload_istream->Read(buffer, 500, &bytes_read);

  EXPECT_TRUE(SUCCEEDED(hr));
  EXPECT_EQ(bytes_read, random_string_length);
  EXPECT_TRUE(strcmp(buffer, random_string) == 0);

  char buffer2[500];
  memset(buffer2, 0, 500);
  ULONG bytes_read2 = 0;
  hr = upload_istream->Read(buffer2, 500, &bytes_read2);

  EXPECT_EQ(S_FALSE, hr);
  EXPECT_EQ(bytes_read2, 0);
  EXPECT_FALSE(strcmp(buffer2, random_string) == 0);
}

TEST(UrlmonUploadDataStreamTest, TestBigRead) {
  const size_t kBigBufferLength = 100000;
  char big_buffer[kBigBufferLength];
  memset(big_buffer, 'a', kBigBufferLength);

  scoped_refptr<net::UploadData> upload_data = new net::UploadData();
  upload_data->AppendBytes(big_buffer, kBigBufferLength);

  CComObject<UrlmonUploadDataStream>* upload_stream = NULL;
  HRESULT hr =
      CComObject<UrlmonUploadDataStream>::CreateInstance(&upload_stream);
  ASSERT_TRUE(SUCCEEDED(hr));

  upload_stream->Initialize(upload_data.get());
  base::win::ScopedComPtr<IStream> upload_istream(upload_stream);

  char big_rcv_buffer[kBigBufferLength];
  int write_pos = 0;
  ULONG bytes_read = 0;
  hr = E_UNEXPECTED;

  while ((hr = upload_istream->Read(&big_rcv_buffer[write_pos],
                                    kBigBufferLength,
                                    &bytes_read)) != S_FALSE) {
    EXPECT_TRUE(SUCCEEDED(hr));
    EXPECT_GT(bytes_read, static_cast<ULONG>(0));

    write_pos += bytes_read;
    bytes_read = 0;
  }

  EXPECT_EQ(S_FALSE, hr);
  EXPECT_TRUE((write_pos + bytes_read) == kBigBufferLength);
  EXPECT_EQ(0, memcmp(big_buffer, big_rcv_buffer, kBigBufferLength));
}

TEST(UrlmonUploadDataStreamTest, TestStat) {
  char random_string[] = "some random data, no really this totally random";
  int random_string_length = strlen(random_string);
  scoped_refptr<net::UploadData> upload_data = new net::UploadData();
  upload_data->AppendBytes(random_string, random_string_length);

  CComObject<UrlmonUploadDataStream>* upload_stream = NULL;
  HRESULT hr =
      CComObject<UrlmonUploadDataStream>::CreateInstance(&upload_stream);
  ASSERT_TRUE(SUCCEEDED(hr));

  upload_stream->Initialize(upload_data.get());
  base::win::ScopedComPtr<IStream> upload_istream(upload_stream);

  STATSTG statstg;
  hr = upload_stream->Stat(&statstg, STATFLAG_NONAME);
  EXPECT_TRUE(SUCCEEDED(hr));
  EXPECT_EQ(static_cast<LONGLONG>(random_string_length),
            statstg.cbSize.QuadPart);
}

TEST(UrlmonUploadDataStreamTest, TestRepeatedRead) {
  char random_string[] = "some random data, no really this totally random";
  int random_string_length = strlen(random_string);
  scoped_refptr<net::UploadData> upload_data = new net::UploadData();
  upload_data->AppendBytes(random_string, random_string_length);

  CComObject<UrlmonUploadDataStream>* upload_stream = NULL;
  HRESULT hr =
      CComObject<UrlmonUploadDataStream>::CreateInstance(&upload_stream);
  ASSERT_TRUE(SUCCEEDED(hr));

  upload_stream->Initialize(upload_data.get());
  base::win::ScopedComPtr<IStream> upload_istream(upload_stream);

  char buffer[500];
  memset(buffer, 0, 500);
  ULONG bytes_read = 0;
  hr = upload_istream->Read(buffer, 500, &bytes_read);

  EXPECT_TRUE(SUCCEEDED(hr));
  EXPECT_EQ(bytes_read, random_string_length);
  EXPECT_EQ(0, strcmp(buffer, random_string));

  char buffer2[500];
  memset(buffer2, 0, 500);
  ULONG bytes_read2 = 0;

  for (int i = 0; i < 10; i++) {
    hr = upload_istream->Read(buffer2, 500, &bytes_read2);
    EXPECT_EQ(S_FALSE, hr);
    EXPECT_EQ(bytes_read2, 0);
    EXPECT_NE(0, strcmp(buffer2, random_string));
  }
}

TEST(UrlmonUploadDataStreamTest, TestZeroRead) {
  char random_string[] = "some random data, no really this totally random";
  int random_string_length = strlen(random_string);
  scoped_refptr<net::UploadData> upload_data = new net::UploadData();
  upload_data->AppendBytes(random_string, random_string_length);

  CComObject<UrlmonUploadDataStream>* upload_stream = NULL;
  HRESULT hr =
      CComObject<UrlmonUploadDataStream>::CreateInstance(&upload_stream);
  ASSERT_TRUE(SUCCEEDED(hr));

  upload_stream->Initialize(upload_data.get());
  base::win::ScopedComPtr<IStream> upload_istream(upload_stream);

  char buffer[500];
  memset(buffer, 0, 500);
  ULONG bytes_read = 42;
  hr = upload_istream->Read(&buffer[0], 0, &bytes_read);

  EXPECT_EQ(S_OK, hr);
  EXPECT_EQ(0, bytes_read);

  char buffer2[500];
  memset(&buffer2[0], 0, 500);
  EXPECT_EQ(0, memcmp(buffer, buffer2, 500));
}