Skip to content

Commit

Permalink
Windows tempfile creation is buggy
Browse files Browse the repository at this point in the history
  • Loading branch information
justinwsmith committed Mar 22, 2024
1 parent 1df69c7 commit 74b4021
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 8 deletions.
10 changes: 5 additions & 5 deletions crypto/bio/bio_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ TEST(BIOTest, CloseFlags) {

// Assert that CRLF line endings get inserted on write and translated back out
// on read for text mode.
TempFILE text_bio_file(tmpfile());
TempFILE text_bio_file = createTempFILE();
ASSERT_TRUE(text_bio_file);
bssl::UniquePtr<BIO> text_bio(
BIO_new_fp(text_bio_file.get(), BIO_NOCLOSE | BIO_FP_TEXT));
Expand Down Expand Up @@ -403,7 +403,7 @@ TEST(BIOTest, CloseFlags) {

// Assert that CRLF line endings don't get inserted on write for
// (default) binary mode.
TempFILE binary_bio_file(tmpfile());
TempFILE binary_bio_file = createTempFILE();
ASSERT_TRUE(binary_bio_file);
bssl::UniquePtr<BIO> binary_bio(
BIO_new_fp(binary_bio_file.get(), BIO_NOCLOSE));
Expand Down Expand Up @@ -432,7 +432,7 @@ TEST(BIOTest, CloseFlags) {

// Assert that BIO_CLOSE causes the underlying file to be closed on BIO free
// (ftell will return < 0)
FILE *tmp = tmpfile();
FILE *tmp = createRawTempFILE();
ASSERT_TRUE(tmp);
BIO *bio = BIO_new_fp(tmp, BIO_CLOSE);
EXPECT_EQ(0, BIO_tell(bio));
Expand All @@ -449,7 +449,7 @@ TEST(BIOTest, CloseFlags) {
#endif

// Assert that BIO_NOCLOSE does not close the underlying file on BIO free
tmp = tmpfile();
tmp = createRawTempFILE();
ASSERT_TRUE(tmp);
bio = BIO_new_fp(tmp, BIO_NOCLOSE);
EXPECT_EQ(0, BIO_tell(bio));
Expand Down Expand Up @@ -737,7 +737,7 @@ TEST(BIOTest, Gets) {
check_bio_gets(bio.get());
}

TempFILE file(tmpfile());
TempFILE file = createTempFILE();
#if defined(OPENSSL_ANDROID)
// On Android, when running from an APK, |tmpfile| does not work. See
// b/36991167#comment8.
Expand Down
2 changes: 1 addition & 1 deletion crypto/dsa/dsa_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ TEST(DSATest, DSAPrint) {
#if !defined(OPENSSL_ANDROID)
// On Android, when running from an APK, |tmpfile| does not work. See
// b/36991167#comment8.
TempFILE tmp(tmpfile());
TempFILE tmp = createTempFILE();
ASSERT_TRUE(tmp);
ASSERT_TRUE(DSA_print_fp(tmp.get(), dsa.get(), 4));
fseek(tmp.get(), 0, SEEK_END);
Expand Down
2 changes: 1 addition & 1 deletion crypto/pem/pem_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ TEST(PEMTest, WriteReadASN1IntegerPem) {
ASSERT_TRUE(ASN1_INTEGER_set(asn1_int.get(), original_value));

// Create buffer for writing
TempFILE pem_file(tmpfile());
TempFILE pem_file = createTempFILE();
ASSERT_TRUE(pem_file);

// Write the ASN1_INTEGER to a PEM-formatted string
Expand Down
2 changes: 1 addition & 1 deletion crypto/rsa_extra/rsa_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1450,7 +1450,7 @@ TEST(RSATest, PrintBio) {
#if !defined(OPENSSL_ANDROID)
// On Android, when running from an APK, |tmpfile| does not work. See
// b/36991167#comment8.
TempFILE tmp(tmpfile());
TempFILE tmp = createTempFILE();
ASSERT_TRUE(tmp);
ASSERT_TRUE(RSA_print_fp(tmp.get(), rsa.get(), 4));
fseek(tmp.get(), 0, SEEK_END);
Expand Down
28 changes: 28 additions & 0 deletions crypto/test/test_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,31 @@ bssl::UniquePtr<X509> CertFromPEM(const char *pem) {
return bssl::UniquePtr<X509>(
PEM_read_bio_X509(bio.get(), nullptr, nullptr, nullptr));
}

#if defined(OPENSSL_WINDOWS)
#include <windows.h>
#ifndef PATH_MAX
#define PATH_MAX MAX_PATH
#endif
#include <fileapi.h>
FILE* createRawTempFILE() {
char pathname[PATH_MAX];
if(0 == GetTempPathA(PATH_MAX, pathname)) {
return nullptr;
}
char filename[PATH_MAX];
if(0 == GetTempFileNameA(pathname, "awslctest", 0, filename)) {
return nullptr;
}
return fopen(filename, "w+b");
}
#else
#include <stdio.h>
FILE* createRawTempFILE() {
return tmpfile();
}
#endif

TempFILE createTempFILE() {
return TempFILE(createRawTempFILE());
}
3 changes: 3 additions & 0 deletions crypto/test/test_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,7 @@ struct FileCloser {

using TempFILE = std::unique_ptr<FILE, FileCloser>;

FILE* createRawTempFILE();
TempFILE createTempFILE();

#endif // OPENSSL_HEADER_CRYPTO_TEST_TEST_UTIL_H

0 comments on commit 74b4021

Please sign in to comment.