Home | History | Annotate | Download | only in chrome_frame
      1 // Copyright (c) 2011 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.
      4 
      5 #include "gtest/gtest.h"
      6 
      7 #include "base/memory/ref_counted.h"
      8 #include "base/win/scoped_comptr.h"
      9 #include "chrome_frame/urlmon_upload_data_stream.h"
     10 
     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);
     16 
     17   CComObject<UrlmonUploadDataStream>* upload_stream = NULL;
     18   HRESULT hr =
     19       CComObject<UrlmonUploadDataStream>::CreateInstance(&upload_stream);
     20   ASSERT_TRUE(SUCCEEDED(hr));
     21 
     22   upload_stream->Initialize(upload_data.get());
     23   base::win::ScopedComPtr<IStream> upload_istream(upload_stream);
     24 
     25   char buffer[500];
     26   memset(buffer, 0, 500);
     27   ULONG bytes_read = 0;
     28   hr = upload_istream->Read(buffer, 500, &bytes_read);
     29 
     30   EXPECT_TRUE(SUCCEEDED(hr));
     31   EXPECT_EQ(bytes_read, random_string_length);
     32   EXPECT_TRUE(strcmp(buffer, random_string) == 0);
     33 
     34   char buffer2[500];
     35   memset(buffer2, 0, 500);
     36   ULONG bytes_read2 = 0;
     37   hr = upload_istream->Read(buffer2, 500, &bytes_read2);
     38 
     39   EXPECT_EQ(S_FALSE, hr);
     40   EXPECT_EQ(bytes_read2, 0);
     41   EXPECT_FALSE(strcmp(buffer2, random_string) == 0);
     42 }
     43 
     44 TEST(UrlmonUploadDataStreamTest, TestBigRead) {
     45   const size_t kBigBufferLength = 100000;
     46   char big_buffer[kBigBufferLength];
     47   memset(big_buffer, 'a', kBigBufferLength);
     48 
     49   scoped_refptr<net::UploadData> upload_data = new net::UploadData();
     50   upload_data->AppendBytes(big_buffer, kBigBufferLength);
     51 
     52   CComObject<UrlmonUploadDataStream>* upload_stream = NULL;
     53   HRESULT hr =
     54       CComObject<UrlmonUploadDataStream>::CreateInstance(&upload_stream);
     55   ASSERT_TRUE(SUCCEEDED(hr));
     56 
     57   upload_stream->Initialize(upload_data.get());
     58   base::win::ScopedComPtr<IStream> upload_istream(upload_stream);
     59 
     60   char big_rcv_buffer[kBigBufferLength];
     61   int write_pos = 0;
     62   ULONG bytes_read = 0;
     63   hr = E_UNEXPECTED;
     64 
     65   while ((hr = upload_istream->Read(&big_rcv_buffer[write_pos],
     66                                     kBigBufferLength,
     67                                     &bytes_read)) != S_FALSE) {
     68     EXPECT_TRUE(SUCCEEDED(hr));
     69     EXPECT_GT(bytes_read, static_cast<ULONG>(0));
     70 
     71     write_pos += bytes_read;
     72     bytes_read = 0;
     73   }
     74 
     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));
     78 }
     79 
     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);
     85 
     86   CComObject<UrlmonUploadDataStream>* upload_stream = NULL;
     87   HRESULT hr =
     88       CComObject<UrlmonUploadDataStream>::CreateInstance(&upload_stream);
     89   ASSERT_TRUE(SUCCEEDED(hr));
     90 
     91   upload_stream->Initialize(upload_data.get());
     92   base::win::ScopedComPtr<IStream> upload_istream(upload_stream);
     93 
     94   STATSTG statstg;
     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);
     99 }
    100 
    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);
    106 
    107   CComObject<UrlmonUploadDataStream>* upload_stream = NULL;
    108   HRESULT hr =
    109       CComObject<UrlmonUploadDataStream>::CreateInstance(&upload_stream);
    110   ASSERT_TRUE(SUCCEEDED(hr));
    111 
    112   upload_stream->Initialize(upload_data.get());
    113   base::win::ScopedComPtr<IStream> upload_istream(upload_stream);
    114 
    115   char buffer[500];
    116   memset(buffer, 0, 500);
    117   ULONG bytes_read = 0;
    118   hr = upload_istream->Read(buffer, 500, &bytes_read);
    119 
    120   EXPECT_TRUE(SUCCEEDED(hr));
    121   EXPECT_EQ(bytes_read, random_string_length);
    122   EXPECT_EQ(0, strcmp(buffer, random_string));
    123 
    124   char buffer2[500];
    125   memset(buffer2, 0, 500);
    126   ULONG bytes_read2 = 0;
    127 
    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));
    133   }
    134 }
    135 
    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);
    141 
    142   CComObject<UrlmonUploadDataStream>* upload_stream = NULL;
    143   HRESULT hr =
    144       CComObject<UrlmonUploadDataStream>::CreateInstance(&upload_stream);
    145   ASSERT_TRUE(SUCCEEDED(hr));
    146 
    147   upload_stream->Initialize(upload_data.get());
    148   base::win::ScopedComPtr<IStream> upload_istream(upload_stream);
    149 
    150   char buffer[500];
    151   memset(buffer, 0, 500);
    152   ULONG bytes_read = 42;
    153   hr = upload_istream->Read(&buffer[0], 0, &bytes_read);
    154 
    155   EXPECT_EQ(S_OK, hr);
    156   EXPECT_EQ(0, bytes_read);
    157 
    158   char buffer2[500];
    159   memset(&buffer2[0], 0, 500);
    160   EXPECT_EQ(0, memcmp(buffer, buffer2, 500));
    161 }
    162 
    163