Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1341)

Unified Diff: webrtc/base/file_unittest.cc

Issue 2214763002: Initial version of new file wrapper (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Test random access better Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: webrtc/base/file_unittest.cc
diff --git a/webrtc/base/file_unittest.cc b/webrtc/base/file_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..dde7e2505f7fcd25a8681bfd23f0bdef206d1c13
--- /dev/null
+++ b/webrtc/base/file_unittest.cc
@@ -0,0 +1,225 @@
+/*
+ * Copyright 2015 The WebRTC Project Authors. All rights reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#include <limits>
+#include <memory>
+#include <string>
+
+#include "webrtc/base/gunit.h"
+#include "webrtc/base/file.h"
+#include "webrtc/test/testsupport/fileutils.h"
+
+#if defined(WEBRTC_WIN)
+#include <windows.h>
+
+#include <type_traits>
+
+#include "webrtc/system_wrappers/include/utf_util_win.h"
+#include "webrtc/base/checks.h"
+
+#else
sprang_webrtc 2016/08/15 09:43:07 I usually like #else // if defined(WEBRTC_WIN) f
palmkvist 2016/08/16 08:55:35 Done.
+
+#include <errno.h>
+
+#endif
+
+namespace rtc {
+
+rtc::PlatformFile OpenFile(const std::string& path) {
+#if defined(WEBRTC_WIN)
+ HANDLE handle =
+ ::CreateFile(ToUtf16(path).c_str(), GENERIC_READ | GENERIC_WRITE, 0,
+ nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
+ return handle;
+#else
+ int fd = ::open(path.c_str(), O_RDWR);
+ return fd;
+#endif
+}
+
+void RemoveFile(const std::string& path) {
+#if defined(WEBRTC_WIN)
+ ::DeleteFile(ToUtf16(path).c_str());
+#else
+ ::unlink(path.c_str());
+#endif
+}
+
+bool VerifyBuffer(uint8_t* buffer, size_t length, uint8_t start_value) {
+ for (size_t i = 0; i < length; ++i) {
+ uint8_t val = start_value++;
+ EXPECT_EQ(val, buffer[i]);
+ if (buffer[i] != val)
+ return false;
+ }
+ // Prevent the same buffer from being verified multiple times simply
+ // because some operation that should have written to it failed
+ memset(buffer, 0, length);
+ return true;
+}
+
+class FileTest : public ::testing::Test {
+ protected:
+ std::string path_;
+ void SetUp() {
+ path_ = webrtc::test::TempFilename(webrtc::test::OutputPath(), "test_file");
+ ASSERT_FALSE(path_.empty());
+ }
+ rtc::PlatformFile OpenTempFile() { return OpenFile(path_); }
+ void TearDown() { RemoveFile(path_); }
+};
+
+TEST_F(FileTest, SimpleReadWrite) {
+ rtc::PlatformFile p_file = OpenTempFile();
+#if defined(WEBRTC_WIN)
+ ASSERT_NE(rtc::kInvalidPlatformFileValue, p_file)
+ << "Error code: " << ::GetLastError() << " Path: " << path_;
+#else
+ ASSERT_NE(rtc::kInvalidPlatformFileValue, p_file) << "Errno: " << errno
+ << " Path: " << path_;
+#endif
+ File file(p_file);
+
+ uint8_t data[100] = {0};
+ uint8_t out[100] = {0};
+ for (int i = 0; i < 100; ++i) {
+ data[i] = i;
+ }
+
+ EXPECT_EQ(10u, file.Write(data, 10));
+
+ EXPECT_TRUE(file.Seek(0));
+ EXPECT_EQ(10u, file.Read(out, 10));
+ EXPECT_TRUE(VerifyBuffer(out, 10, 0));
+
+ EXPECT_TRUE(file.Seek(0));
+ EXPECT_EQ(100u, file.Write(data, 100));
+
+ EXPECT_TRUE(file.Seek(0));
+ EXPECT_EQ(100u, file.Read(out, 100));
+ EXPECT_TRUE(VerifyBuffer(out, 100, 0));
+
+ EXPECT_TRUE(file.Seek(1));
+ EXPECT_EQ(50u, file.Write(data, 50));
+ EXPECT_EQ(50u, file.Write(data + 50, 50));
+
+ EXPECT_TRUE(file.Seek(1));
+ EXPECT_EQ(100u, file.Read(out, 100));
+ EXPECT_TRUE(VerifyBuffer(out, 100, 0));
+}
+
+TEST_F(FileTest, ReadWriteClose) {
+ rtc::PlatformFile p_file = OpenTempFile();
+#if defined(WEBRTC_WIN)
+ ASSERT_NE(rtc::kInvalidPlatformFileValue, p_file) << "Error code: "
+ << ::GetLastError();
+#else
+ ASSERT_NE(rtc::kInvalidPlatformFileValue, p_file) << "Errno: " << errno;
+#endif
sprang_webrtc 2016/08/15 09:43:08 Looks like a LastError() method in FileTest could
+ File file(p_file);
+
+ uint8_t data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
+ uint8_t out[10] = {0};
+ EXPECT_EQ(10u, file.Write(data, 10));
+ EXPECT_TRUE(file.Close());
+
+ p_file = OpenTempFile();
+#if defined(WEBRTC_WIN)
+ ASSERT_NE(rtc::kInvalidPlatformFileValue, p_file) << "Error code: "
+ << ::GetLastError();
+#else
+ ASSERT_NE(rtc::kInvalidPlatformFileValue, p_file) << "Errno: " << errno;
+#endif
+ File file2(p_file);
+ EXPECT_EQ(10u, file2.Read(out, 10));
+ EXPECT_TRUE(VerifyBuffer(out, 10, 0));
+}
+
+TEST_F(FileTest, RandomAccessRead) {
+ rtc::PlatformFile p_file = OpenTempFile();
+#if defined(WEBRTC_WIN)
+ ASSERT_NE(rtc::kInvalidPlatformFileValue, p_file) << "Error code: "
+ << ::GetLastError();
+#else
+ ASSERT_NE(rtc::kInvalidPlatformFileValue, p_file) << "Errno: " << errno;
+#endif
+ File file(p_file);
+
+ uint8_t data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
+ uint8_t out[10] = {0};
+ EXPECT_EQ(10u, file.Write(data, 10));
+
+ EXPECT_EQ(4u, file.ReadAt(out, 4, 0));
+ EXPECT_TRUE(VerifyBuffer(out, 4, 0));
+
+ EXPECT_EQ(4u, file.ReadAt(out, 4, 4));
+ EXPECT_TRUE(VerifyBuffer(out, 4, 4));
+
+ EXPECT_EQ(5u, file.ReadAt(out, 5, 5));
+ EXPECT_TRUE(VerifyBuffer(out, 5, 5));
+}
+
+TEST_F(FileTest, RandomAccessReadWrite) {
+ rtc::PlatformFile p_file = OpenTempFile();
+#if defined(WEBRTC_WIN)
+ ASSERT_NE(rtc::kInvalidPlatformFileValue, p_file) << "Error code: "
+ << ::GetLastError();
+#else
+ ASSERT_NE(rtc::kInvalidPlatformFileValue, p_file) << "Errno: " << errno;
+#endif
+ File file(p_file);
+
+ uint8_t data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
+ uint8_t out[10] = {0};
+ EXPECT_EQ(10u, file.Write(data, 10));
+ EXPECT_TRUE(file.Seek(4));
+
+ EXPECT_EQ(4u, file.WriteAt(data, 4, 4));
+ EXPECT_EQ(4u, file.Read(out, 4));
+ EXPECT_TRUE(VerifyBuffer(out, 4, 0));
+
+ EXPECT_EQ(2u, file.WriteAt(data, 2, 8));
+ EXPECT_EQ(2u, file.ReadAt(out, 2, 8));
+ EXPECT_TRUE(VerifyBuffer(out, 2, 0));
+}
+
+TEST_F(FileTest, RandomAccessDoesNotMovePosition) {
+ rtc::PlatformFile p_file = OpenTempFile();
+#if defined(WEBRTC_WIN)
+ ASSERT_NE(rtc::kInvalidPlatformFileValue, p_file) << "Error code: "
+ << ::GetLastError();
+#else
+ ASSERT_NE(rtc::kInvalidPlatformFileValue, p_file) << "Errno: " << errno;
+#endif
+ File file(p_file);
+
+ uint8_t data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
+ uint8_t out[10] = {0};
+ EXPECT_EQ(10u, file.Write(data, 10));
+
+ EXPECT_TRUE(file.Seek(0));
+ EXPECT_EQ(2u, file.ReadAt(out, 2, 5));
+ EXPECT_EQ(4u, file.Read(out, 4));
+ EXPECT_TRUE(VerifyBuffer(out, 4, 0));
+
+ EXPECT_EQ(6u, file.WriteAt(data + 1, 6, 0));
+ EXPECT_EQ(2u, file.Read(out, 2));
+ EXPECT_TRUE(VerifyBuffer(out, 2, 5));
+
+ EXPECT_EQ(4u, file.WriteAt(data, 4, 6));
+ EXPECT_EQ(4u, file.Read(out, 4));
+ EXPECT_TRUE(VerifyBuffer(out, 4, 0));
+}
+
+} // namespace rtc

Powered by Google App Engine
This is Rietveld 408576698