-
Notifications
You must be signed in to change notification settings - Fork 1.7k
AVRO-4185: [C++] Use a temp file in StreamTests #3496
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ | |
| #include <boost/test/included/unit_test.hpp> | ||
| #include <boost/test/parameterized_test.hpp> | ||
|
|
||
| #include <cstdio> | ||
| #include <filesystem> | ||
|
|
||
| namespace avro { | ||
|
|
@@ -134,36 +135,34 @@ void testNonEmpty2(const TestData &td) { | |
| Verify1()(*is, td.dataSize); | ||
| } | ||
|
|
||
| static const char filename[] = "test_str.bin"; | ||
|
|
||
| struct FileRemover { | ||
| const std::filesystem::path file; | ||
| explicit FileRemover(const char *fn) : file(fn) {} | ||
| ~FileRemover() { std::filesystem::remove(file); } | ||
| struct FileGuard { | ||
| const std::filesystem::path path{ std::tmpnam(nullptr) }; | ||
| ~FileGuard() { std::filesystem::remove(path); } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we care about failures in
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would not care, and I wouldn't know how to recover from such an error.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not recover, but log it.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we fail to
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A bug, or some temporary condition. |
||
| const char* filename() const { return path.c_str(); } | ||
| }; | ||
|
|
||
| template<typename V> | ||
| void testEmpty_fileStream() { | ||
| FileRemover fr(filename); | ||
| FileGuard fg; | ||
| { | ||
| std::unique_ptr<OutputStream> os = fileOutputStream(filename); | ||
| std::unique_ptr<OutputStream> os = fileOutputStream(fg.filename()); | ||
| } | ||
| std::unique_ptr<InputStream> is = fileInputStream(filename); | ||
| std::unique_ptr<InputStream> is = fileInputStream(fg.filename()); | ||
| V() | ||
| (*is); | ||
| } | ||
|
|
||
| template<typename F, typename V> | ||
| void testNonEmpty_fileStream(const TestData &td) { | ||
| FileRemover fr(filename); | ||
| FileGuard fg; | ||
| { | ||
| std::unique_ptr<OutputStream> os = fileOutputStream(filename, | ||
| std::unique_ptr<OutputStream> os = fileOutputStream(fg.filename(), | ||
| td.chunkSize); | ||
| F() | ||
| (*os, td.dataSize); | ||
| } | ||
|
|
||
| std::unique_ptr<InputStream> is = fileInputStream(filename, td.chunkSize); | ||
| std::unique_ptr<InputStream> is = fileInputStream(fg.filename(), td.chunkSize); | ||
| V() | ||
| (*is, td.dataSize); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using std::tmpnam(nullptr) is unsafe as it can cause race conditions and security vulnerabilities. Consider using std::filesystem::temp_directory_path() with a unique filename generator or platform-specific secure temporary file functions instead.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think these concerns apply to this test code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't
tmpnamdeprecated ?Avro uses C++17, so we can use something like the following instead:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't find references to its deprecation.
I don't think using
std::random_deviceis a good idea, as it can be implemented using a PRNG, i.e., two tests running at the same time would produce the same number.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/hs3e7355(v=vs.100)
In the example code there is
// Note: tmpnam is deprecated; consider using tmpnam_s insteadAlso in the build logs we can see:
https://productionresultssa3.blob.core.windows.net/actions-results/abfc1b86-599c-48bd-a249-acda8f187020/workflow-job-run-670f0204-095a-5c23-a7d3-1d13fabc5e69/logs/job/job-logs.txt?rsct=text%2Fplain&se=2025-10-23T08%3A12%3A13Z&sig=9G%2FOy2gomdBnbYY%2FXTGGg%2Fza%2BjuKKXvqJVAL4xpemfg%3D&ske=2025-10-23T17%3A45%3A39Z&skoid=ca7593d4-ee42-46cd-af88-8b886a2f84eb&sks=b&skt=2025-10-23T05%3A45%3A39Z&sktid=398a6654-997b-47e9-b12b-9515b896b4de&skv=2025-11-05&sp=r&spr=https&sr=b&st=2025-10-23T08%3A02%3A08Z&sv=2025-11-05
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have a more standard way, sorry.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since
random_device()is meant to be used for seeding a PNRG and not directly for generating random numbers, we can do something like:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't address my concern: two processes can still generate the same sequence.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remember the code is in test. The conflict happens when two people run the run the exactly at the same sub-microsecond. How likely is it? Even if happens, who cares? Just rerun the test. It is anyway better than what we have now.
If we are still paranoid, let's
xorthe value ofrandom_device()withpid.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is the spec that say that random_device must be implemented in such a way? In the standard I read it can generate the same sequence each time.