1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "gtest/gtest.h"
7 #include "base/ref_counted.h"
8 #include "base/scoped_comptr_win.h"
9 #include "chrome_frame/urlmon_upload_data_stream.h"
11 TEST(UrlmonUploadDataStreamTest
, TestBasicRead
) {
12 char random_string
[] = "some random data, no really this totally random";
13 int random_string_length
= strlen(random_string
);
14 scoped_refptr
<net::UploadData
> upload_data
= new net::UploadData();
15 upload_data
->AppendBytes(random_string
, random_string_length
);
17 CComObject
<UrlmonUploadDataStream
>* upload_stream
= NULL
;
19 CComObject
<UrlmonUploadDataStream
>::CreateInstance(&upload_stream
);
20 ASSERT_TRUE(SUCCEEDED(hr
));
22 upload_stream
->Initialize(upload_data
.get());
23 ScopedComPtr
<IStream
> upload_istream(upload_stream
);
26 memset(buffer
, 0, 500);
28 hr
= upload_istream
->Read(buffer
, 500, &bytes_read
);
30 EXPECT_TRUE(SUCCEEDED(hr
));
31 EXPECT_EQ(bytes_read
, random_string_length
);
32 EXPECT_TRUE(strcmp(buffer
, random_string
) == 0);
35 memset(buffer2
, 0, 500);
36 ULONG bytes_read2
= 0;
37 hr
= upload_istream
->Read(buffer2
, 500, &bytes_read2
);
39 EXPECT_EQ(S_FALSE
, hr
);
40 EXPECT_EQ(bytes_read2
, 0);
41 EXPECT_FALSE(strcmp(buffer2
, random_string
) == 0);
44 TEST(UrlmonUploadDataStreamTest
, TestBigRead
) {
45 const size_t kBigBufferLength
= 100000;
46 char big_buffer
[kBigBufferLength
];
47 memset(big_buffer
, 'a', kBigBufferLength
);
49 scoped_refptr
<net::UploadData
> upload_data
= new net::UploadData();
50 upload_data
->AppendBytes(big_buffer
, kBigBufferLength
);
52 CComObject
<UrlmonUploadDataStream
>* upload_stream
= NULL
;
54 CComObject
<UrlmonUploadDataStream
>::CreateInstance(&upload_stream
);
55 ASSERT_TRUE(SUCCEEDED(hr
));
57 upload_stream
->Initialize(upload_data
.get());
58 ScopedComPtr
<IStream
> upload_istream(upload_stream
);
60 char big_rcv_buffer
[kBigBufferLength
];
65 while ((hr
= upload_istream
->Read(&big_rcv_buffer
[write_pos
],
67 &bytes_read
)) != S_FALSE
) {
68 EXPECT_TRUE(SUCCEEDED(hr
));
69 EXPECT_GT(bytes_read
, static_cast<ULONG
>(0));
71 write_pos
+= bytes_read
;
75 EXPECT_EQ(S_FALSE
, hr
);
76 EXPECT_TRUE((write_pos
+ bytes_read
) == kBigBufferLength
);
77 EXPECT_EQ(0, memcmp(big_buffer
, big_rcv_buffer
, kBigBufferLength
));
80 TEST(UrlmonUploadDataStreamTest
, TestStat
) {
81 char random_string
[] = "some random data, no really this totally random";
82 int random_string_length
= strlen(random_string
);
83 scoped_refptr
<net::UploadData
> upload_data
= new net::UploadData();
84 upload_data
->AppendBytes(random_string
, random_string_length
);
86 CComObject
<UrlmonUploadDataStream
>* upload_stream
= NULL
;
88 CComObject
<UrlmonUploadDataStream
>::CreateInstance(&upload_stream
);
89 ASSERT_TRUE(SUCCEEDED(hr
));
91 upload_stream
->Initialize(upload_data
.get());
92 ScopedComPtr
<IStream
> upload_istream(upload_stream
);
95 hr
= upload_stream
->Stat(&statstg
, STATFLAG_NONAME
);
96 EXPECT_TRUE(SUCCEEDED(hr
));
97 EXPECT_EQ(static_cast<LONGLONG
>(random_string_length
),
98 statstg
.cbSize
.QuadPart
);
101 TEST(UrlmonUploadDataStreamTest
, TestRepeatedRead
) {
102 char random_string
[] = "some random data, no really this totally random";
103 int random_string_length
= strlen(random_string
);
104 scoped_refptr
<net::UploadData
> upload_data
= new net::UploadData();
105 upload_data
->AppendBytes(random_string
, random_string_length
);
107 CComObject
<UrlmonUploadDataStream
>* upload_stream
= NULL
;
109 CComObject
<UrlmonUploadDataStream
>::CreateInstance(&upload_stream
);
110 ASSERT_TRUE(SUCCEEDED(hr
));
112 upload_stream
->Initialize(upload_data
.get());
113 ScopedComPtr
<IStream
> upload_istream(upload_stream
);
116 memset(buffer
, 0, 500);
117 ULONG bytes_read
= 0;
118 hr
= upload_istream
->Read(buffer
, 500, &bytes_read
);
120 EXPECT_TRUE(SUCCEEDED(hr
));
121 EXPECT_EQ(bytes_read
, random_string_length
);
122 EXPECT_EQ(0, strcmp(buffer
, random_string
));
125 memset(buffer2
, 0, 500);
126 ULONG bytes_read2
= 0;
128 for (int i
= 0; i
< 10; i
++) {
129 hr
= upload_istream
->Read(buffer2
, 500, &bytes_read2
);
130 EXPECT_EQ(S_FALSE
, hr
);
131 EXPECT_EQ(bytes_read2
, 0);
132 EXPECT_NE(0, strcmp(buffer2
, random_string
));
136 TEST(UrlmonUploadDataStreamTest
, TestZeroRead
) {
137 char random_string
[] = "some random data, no really this totally random";
138 int random_string_length
= strlen(random_string
);
139 scoped_refptr
<net::UploadData
> upload_data
= new net::UploadData();
140 upload_data
->AppendBytes(random_string
, random_string_length
);
142 CComObject
<UrlmonUploadDataStream
>* upload_stream
= NULL
;
144 CComObject
<UrlmonUploadDataStream
>::CreateInstance(&upload_stream
);
145 ASSERT_TRUE(SUCCEEDED(hr
));
147 upload_stream
->Initialize(upload_data
.get());
148 ScopedComPtr
<IStream
> upload_istream(upload_stream
);
151 memset(buffer
, 0, 500);
152 ULONG bytes_read
= 42;
153 hr
= upload_istream
->Read(&buffer
[0], 0, &bytes_read
);
156 EXPECT_EQ(0, bytes_read
);
159 memset(&buffer2
[0], 0, 500);
160 EXPECT_EQ(0, memcmp(buffer
, buffer2
, 500));