-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic tests for fileops functions
Summary: Add basic tests for `folly::fileops::{open,read,write,close,pipe}`. These are implemented for windows to handle sockets and /dev/null. They are simple aliases on non-windows. Reviewed By: Orvid Differential Revision: D66838378 fbshipit-source-id: feee6f1f14deebab58d928dc8b31d0012b7d834a
- Loading branch information
1 parent
f0155ae
commit 30fc8cb
Showing
4 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include <folly/portability/Fcntl.h> | ||
#include <folly/portability/Unistd.h> | ||
|
||
#include <gtest/gtest.h> | ||
|
||
using namespace ::testing; | ||
|
||
/// Test that open supports "/dev/null" on Windows. | ||
/// | ||
/// Windows doesn't have a /dev/null, but it does have | ||
/// NUL, which achieves the same result. | ||
TEST(FcntlTest, OpenDevNull) { | ||
int fd = folly::fileops::open("/dev/null", O_RDWR); | ||
ASSERT_GE(fd, 0); | ||
|
||
ASSERT_EQ(3, folly::fileops::write(fd, "abc", 3)); | ||
|
||
{ | ||
char buf[4] = {1, 2, 3, 4}; | ||
// Reads from /dev/null always return 0 for end-of-file (EOF). | ||
ASSERT_EQ(0, folly::fileops::read(fd, buf, 3)); | ||
ASSERT_EQ(buf[0], 1); | ||
ASSERT_EQ(buf[1], 2); | ||
ASSERT_EQ(buf[2], 3); | ||
ASSERT_EQ(buf[3], 4); | ||
} | ||
|
||
ASSERT_EQ(0, folly::fileops::close(fd)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include <folly/portability/Unistd.h> | ||
|
||
#include <gtest/gtest.h> | ||
|
||
using namespace ::testing; | ||
|
||
/// Simple smoke test of `folly::fileops::{pipe, read, write, close}` functions. | ||
/// | ||
/// On windows, the file descriptors created will be unix sockets, which is | ||
/// normally unsupported by the windows UCRT version of these functions. | ||
/// | ||
/// This tests pipe's ability to perform a write, read, and close. | ||
TEST(UnistdTest, FileOpsSmokeTest) { | ||
int fds[2] = {-1}; | ||
ASSERT_EQ(0, folly::fileops::pipe(fds)); | ||
int readEnd = fds[0]; | ||
int writeEnd = fds[1]; | ||
ASSERT_GE(readEnd, 0); | ||
ASSERT_GE(writeEnd, 0); | ||
|
||
ASSERT_EQ(4, folly::fileops::write(writeEnd, "pika", 4)); | ||
|
||
{ | ||
char actual[5] = {0}; | ||
ASSERT_EQ(4, folly::fileops::read(readEnd, actual, 4)); | ||
ASSERT_STREQ("pika", actual); | ||
} | ||
|
||
ASSERT_EQ(0, folly::fileops::close(readEnd)); | ||
ASSERT_EQ(0, folly::fileops::close(writeEnd)); | ||
} |