OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/base/upload_data.h" | 5 #include "net/base/upload_data.h" |
6 | 6 |
| 7 #include "base/bind.h" |
7 #include "base/file_path.h" | 8 #include "base/file_path.h" |
| 9 #include "base/file_util.h" |
| 10 #include "base/message_loop.h" |
| 11 #include "base/scoped_temp_dir.h" |
8 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/threading/thread_restrictions.h" |
9 #include "base/time.h" | 14 #include "base/time.h" |
10 #include "googleurl/src/gurl.h" | 15 #include "googleurl/src/gurl.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "testing/platform_test.h" |
12 | 18 |
13 namespace net { | 19 namespace net { |
14 | 20 |
15 TEST(UploadDataTest, IsInMemory_Empty) { | 21 // Simplified version of TestCompletionCallback for ContentLengthCallback, |
16 scoped_refptr<UploadData> upload_data = new UploadData; | 22 // that handles uint64 rather than int. |
17 ASSERT_TRUE(upload_data->IsInMemory()); | 23 class TestContentLengthCallback { |
| 24 public: |
| 25 TestContentLengthCallback() |
| 26 : result_(0), |
| 27 callback_(ALLOW_THIS_IN_INITIALIZER_LIST( |
| 28 base::Bind(&TestContentLengthCallback::SetResult, |
| 29 base::Unretained(this)))) { |
| 30 } |
| 31 |
| 32 ~TestContentLengthCallback() {} |
| 33 |
| 34 const UploadData::ContentLengthCallback& callback() const { |
| 35 return callback_; |
| 36 } |
| 37 |
| 38 // Waits for the result and returns it. |
| 39 uint64 WaitForResult() { |
| 40 MessageLoop::current()->Run(); |
| 41 return result_; |
| 42 } |
| 43 |
| 44 private: |
| 45 // Sets the result and stops the message loop. |
| 46 void SetResult(uint64 result) { |
| 47 result_ = result; |
| 48 MessageLoop::current()->Quit(); |
| 49 } |
| 50 |
| 51 uint64 result_; |
| 52 const UploadData::ContentLengthCallback callback_; |
| 53 |
| 54 DISALLOW_COPY_AND_ASSIGN(TestContentLengthCallback); |
| 55 }; |
| 56 |
| 57 class UploadDataTest : public PlatformTest { |
| 58 protected: |
| 59 virtual void SetUp() { |
| 60 upload_data_= new UploadData; |
| 61 // To ensure that file IO is not performed on the main thread in the |
| 62 // test (i.e. GetContentLengthSync() will fail if file IO is performed). |
| 63 base::ThreadRestrictions::SetIOAllowed(false); |
| 64 } |
| 65 |
| 66 virtual void TearDown() { |
| 67 base::ThreadRestrictions::SetIOAllowed(true); |
| 68 } |
| 69 |
| 70 // Creates a temporary file with the given data. The temporary file is |
| 71 // deleted by temp_dir_. Returns true on success. |
| 72 bool CreateTemporaryFile(const std::string& data, |
| 73 FilePath* temp_file_path) { |
| 74 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 75 if (!temp_dir_.CreateUniqueTempDir()) |
| 76 return false; |
| 77 if (!file_util::CreateTemporaryFileInDir(temp_dir_.path(), temp_file_path)) |
| 78 return false; |
| 79 if (static_cast<int>(data.size()) != |
| 80 file_util::WriteFile(*temp_file_path, data.data(), data.size())) |
| 81 return false; |
| 82 |
| 83 return true; |
| 84 } |
| 85 |
| 86 ScopedTempDir temp_dir_; |
| 87 scoped_refptr<UploadData> upload_data_; |
| 88 }; |
| 89 |
| 90 TEST_F(UploadDataTest, IsInMemory_Empty) { |
| 91 ASSERT_TRUE(upload_data_->IsInMemory()); |
18 } | 92 } |
19 | 93 |
20 TEST(UploadDataTest, IsInMemory_Bytes) { | 94 TEST_F(UploadDataTest, IsInMemory_Bytes) { |
21 scoped_refptr<UploadData> upload_data = new UploadData; | 95 upload_data_->AppendBytes("123", 3); |
22 upload_data->AppendBytes("123", 3); | 96 ASSERT_TRUE(upload_data_->IsInMemory()); |
23 ASSERT_TRUE(upload_data->IsInMemory()); | |
24 } | 97 } |
25 | 98 |
26 TEST(UploadDataTest, IsInMemory_File) { | 99 TEST_F(UploadDataTest, IsInMemory_File) { |
27 scoped_refptr<UploadData> upload_data = new UploadData; | 100 upload_data_->AppendFileRange( |
28 upload_data->AppendFileRange( | |
29 FilePath(FILE_PATH_LITERAL("random_file_name.txt")), | 101 FilePath(FILE_PATH_LITERAL("random_file_name.txt")), |
30 0, 0, base::Time()); | 102 0, 0, base::Time()); |
31 ASSERT_FALSE(upload_data->IsInMemory()); | 103 ASSERT_FALSE(upload_data_->IsInMemory()); |
32 } | 104 } |
33 | 105 |
34 TEST(UploadDataTest, IsInMemory_Blob) { | 106 TEST_F(UploadDataTest, IsInMemory_Blob) { |
35 scoped_refptr<UploadData> upload_data = new UploadData; | 107 upload_data_->AppendBlob(GURL("blog:internal:12345")); |
36 upload_data->AppendBlob(GURL("blog:internal:12345")); | |
37 // Until it's resolved, we don't know what blob contains. | 108 // Until it's resolved, we don't know what blob contains. |
38 ASSERT_FALSE(upload_data->IsInMemory()); | 109 ASSERT_FALSE(upload_data_->IsInMemory()); |
39 } | 110 } |
40 | 111 |
41 TEST(UploadDataTest, IsInMemory_Chunk) { | 112 TEST_F(UploadDataTest, IsInMemory_Chunk) { |
42 scoped_refptr<UploadData> upload_data = new UploadData; | 113 upload_data_->set_is_chunked(true); |
43 upload_data->set_is_chunked(true); | 114 ASSERT_FALSE(upload_data_->IsInMemory()); |
44 ASSERT_FALSE(upload_data->IsInMemory()); | |
45 } | 115 } |
46 | 116 |
47 TEST(UploadDataTest, IsInMemory_Mixed) { | 117 TEST_F(UploadDataTest, IsInMemory_Mixed) { |
48 scoped_refptr<UploadData> upload_data = new UploadData; | 118 ASSERT_TRUE(upload_data_->IsInMemory()); |
49 ASSERT_TRUE(upload_data->IsInMemory()); | |
50 | 119 |
51 upload_data->AppendBytes("123", 3); | 120 upload_data_->AppendBytes("123", 3); |
52 upload_data->AppendBytes("abc", 3); | 121 upload_data_->AppendBytes("abc", 3); |
53 ASSERT_TRUE(upload_data->IsInMemory()); | 122 ASSERT_TRUE(upload_data_->IsInMemory()); |
54 | 123 |
55 upload_data->AppendFileRange( | 124 upload_data_->AppendFileRange( |
56 FilePath(FILE_PATH_LITERAL("random_file_name.txt")), | 125 FilePath(FILE_PATH_LITERAL("random_file_name.txt")), |
57 0, 0, base::Time()); | 126 0, 0, base::Time()); |
58 ASSERT_FALSE(upload_data->IsInMemory()); | 127 ASSERT_FALSE(upload_data_->IsInMemory()); |
| 128 } |
| 129 |
| 130 TEST_F(UploadDataTest, GetContentLength_Empty) { |
| 131 ASSERT_EQ(0U, upload_data_->GetContentLengthSync()); |
| 132 } |
| 133 |
| 134 TEST_F(UploadDataTest, GetContentLength_Bytes) { |
| 135 upload_data_->AppendBytes("123", 3); |
| 136 ASSERT_EQ(3U, upload_data_->GetContentLengthSync()); |
| 137 } |
| 138 |
| 139 TEST_F(UploadDataTest, GetContentLength_File) { |
| 140 // Create a temporary file with some data. |
| 141 const std::string kData = "hello"; |
| 142 FilePath temp_file_path; |
| 143 ASSERT_TRUE(CreateTemporaryFile(kData, &temp_file_path)); |
| 144 upload_data_->AppendFileRange(temp_file_path, 0, kuint64max, base::Time()); |
| 145 |
| 146 // The length is returned asynchronously. |
| 147 TestContentLengthCallback callback; |
| 148 upload_data_->GetContentLength(callback.callback()); |
| 149 ASSERT_EQ(kData.size(), callback.WaitForResult()); |
| 150 } |
| 151 |
| 152 TEST_F(UploadDataTest, GetContentLength_Blob) { |
| 153 upload_data_->AppendBlob(GURL("blog:internal:12345")); |
| 154 ASSERT_EQ(0U, upload_data_->GetContentLengthSync()); |
| 155 } |
| 156 |
| 157 TEST_F(UploadDataTest, GetContentLength_Chunk) { |
| 158 upload_data_->set_is_chunked(true); |
| 159 ASSERT_EQ(0U, upload_data_->GetContentLengthSync()); |
| 160 } |
| 161 |
| 162 TEST_F(UploadDataTest, GetContentLength_Mixed) { |
| 163 upload_data_->AppendBytes("123", 3); |
| 164 upload_data_->AppendBytes("abc", 3); |
| 165 |
| 166 const uint64 content_length = upload_data_->GetContentLengthSync(); |
| 167 ASSERT_EQ(6U, content_length); |
| 168 |
| 169 // Append a file. |
| 170 const std::string kData = "hello"; |
| 171 FilePath temp_file_path; |
| 172 ASSERT_TRUE(CreateTemporaryFile(kData, &temp_file_path)); |
| 173 upload_data_->AppendFileRange(temp_file_path, 0, kuint64max, base::Time()); |
| 174 |
| 175 TestContentLengthCallback callback; |
| 176 upload_data_->GetContentLength(callback.callback()); |
| 177 ASSERT_EQ(content_length + kData.size(), callback.WaitForResult()); |
59 } | 178 } |
60 | 179 |
61 } // namespace net | 180 } // namespace net |
OLD | NEW |