From e2ab3e0d29511d60529fd0ff22bd85be896902fe Mon Sep 17 00:00:00 2001 From: Jared Mulconry Date: Tue, 26 Sep 2017 21:32:11 +1000 Subject: [PATCH 1/5] Changed the method by which temporary files are created for unit the FileSizeTest. Will apply to other tests next. --- test/unit/utDefaultIOStream.cpp | 70 +++++++++++++++++++-------------- 1 file changed, 41 insertions(+), 29 deletions(-) diff --git a/test/unit/utDefaultIOStream.cpp b/test/unit/utDefaultIOStream.cpp index 9335007ba..c5f5ae760 100644 --- a/test/unit/utDefaultIOStream.cpp +++ b/test/unit/utDefaultIOStream.cpp @@ -43,30 +43,42 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include -#if defined(__GNUC__) || defined(__clang__) -#define TMP_PATH "/tmp/" -void MakeTmpFilePath(char* tmplate) -{ - auto err = mkstemp(tmplate); - ASSERT_NE(err, -1); -} -#elif defined(_MSC_VER) -#include -#define TMP_PATH "./" -void MakeTmpFilePath(char* tmplate) -{ - auto pathtemplate = _mktemp(tmplate); - ASSERT_NE(pathtemplate, nullptr); -} -#endif - using namespace ::Assimp; class utDefaultIOStream : public ::testing::Test { // empty }; - +#if defined(__GNUC__) || defined(__clang__) +#define TMP_PATH "/tmp/" +FILE* MakeTmpFilePath(char* tmplate) +{ + auto fd = mkstemp(tmplate); + EXPECT_NE(-1, fd); + if(fd == -1) + { + return nullptr; + } + auto fs = fdopen(fd, "w+"); + EXPECT_NE(nullptr, fs); + return fs; +} +#elif defined(_MSC_VER) +#include +#define TMP_PATH "./" +FILE* MakeTmpFilePath(char* tmplate) +{ + auto pathtemplate = _mktemp(tmplate); + EXPECT_NE(pathtemplate, nullptr); + if(pathtemplate == nullptr) + { + return nullptr; + } + auto* fs = std::fopen(pathtemplate, "w+"); + EXPECT_NE(fs, nullptr); + return fs; +} +#endif const char data[]{"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Qui\ sque luctus sem diam, ut eleifend arcu auctor eu. Vestibulum id est vel nulla l\ @@ -78,18 +90,18 @@ TEST_F( utDefaultIOStream, FileSizeTest ) { const auto dataCount = dataSize / sizeof(*data); char fpath[] = { TMP_PATH"rndfp.XXXXXX" }; - MakeTmpFilePath(fpath); + auto* fs = MakeTmpFilePath(fpath); + ASSERT_NE(nullptr, fs); + { + auto written = std::fwrite(data, sizeof(*data), dataCount, fs ); + EXPECT_NE( 0U, written ); - auto *fs = std::fopen(fpath, "w+" ); - ASSERT_NE(fs, nullptr); - auto written = std::fwrite(data, sizeof(*data), dataCount, fs ); - EXPECT_NE( 0U, written ); - auto vflush = std::fflush( fs ); - ASSERT_EQ(vflush, 0); + auto vflush = std::fflush( fs ); + ASSERT_EQ(vflush, 0); - TestDefaultIOStream myStream( fs, fpath); - size_t size = myStream.FileSize(); - EXPECT_EQ( size, dataSize); + TestDefaultIOStream myStream( fs, fpath); + size_t size = myStream.FileSize(); + EXPECT_EQ( size, dataSize); + } remove(fpath); - } From 980d2b0eee6981625d92bc01dfd24ab06f3068db Mon Sep 17 00:00:00 2001 From: Jared Mulconry Date: Tue, 26 Sep 2017 22:08:52 +1000 Subject: [PATCH 2/5] Added a header to hold the file generation code for unit testing purposes. --- test/unit/UnitTestFileGenerator.h | 76 +++++++++++++++++++++++++++++++ test/unit/utDefaultIOStream.cpp | 32 +------------ 2 files changed, 77 insertions(+), 31 deletions(-) create mode 100644 test/unit/UnitTestFileGenerator.h diff --git a/test/unit/UnitTestFileGenerator.h b/test/unit/UnitTestFileGenerator.h new file mode 100644 index 000000000..04f5fd409 --- /dev/null +++ b/test/unit/UnitTestFileGenerator.h @@ -0,0 +1,76 @@ +/* +--------------------------------------------------------------------------- +Open Asset Import Library (assimp) +--------------------------------------------------------------------------- + +Copyright (c) 2006-2017, assimp team + +All rights reserved. + +Redistribution and use of this software in source and binary forms, +with or without modification, are permitted provided that the following +conditions are met: + +* Redistributions of source code must retain the above +copyright notice, this list of conditions and the +following disclaimer. + +* Redistributions in binary form must reproduce the above +copyright notice, this list of conditions and the +following disclaimer in the documentation and/or other +materials provided with the distribution. + +* Neither the name of the assimp team, nor the names of its +contributors may be used to endorse or promote products +derived from this software without specific prior +written permission of the assimp team. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +--------------------------------------------------------------------------- +*/ +#pragma once + +#include +#include +#include + +#if defined(__GNUC__) || defined(__clang__) +#define TMP_PATH "/tmp/" +inline FILE* MakeTmpFilePath(char* tmplate) +{ + auto fd = mkstemp(tmplate); + EXPECT_NE(-1, fd); + if(fd == -1) + { + return nullptr; + } + auto fs = fdopen(fd, "w+"); + EXPECT_NE(nullptr, fs); + return fs; +} +#elif defined(_MSC_VER) +#include +#define TMP_PATH "./" +inline FILE* MakeTmpFilePath(char* tmplate) +{ + auto pathtemplate = _mktemp(tmplate); + EXPECT_NE(pathtemplate, nullptr); + if(pathtemplate == nullptr) + { + return nullptr; + } + auto* fs = std::fopen(pathtemplate, "w+"); + EXPECT_NE(fs, nullptr); + return fs; +} +#endif diff --git a/test/unit/utDefaultIOStream.cpp b/test/unit/utDefaultIOStream.cpp index c5f5ae760..1d78fb616 100644 --- a/test/unit/utDefaultIOStream.cpp +++ b/test/unit/utDefaultIOStream.cpp @@ -39,6 +39,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------*/ #include #include "TestIOStream.h" +#include "UnitTestFileGenerator.h" #include #include #include @@ -49,37 +50,6 @@ class utDefaultIOStream : public ::testing::Test { // empty }; -#if defined(__GNUC__) || defined(__clang__) -#define TMP_PATH "/tmp/" -FILE* MakeTmpFilePath(char* tmplate) -{ - auto fd = mkstemp(tmplate); - EXPECT_NE(-1, fd); - if(fd == -1) - { - return nullptr; - } - auto fs = fdopen(fd, "w+"); - EXPECT_NE(nullptr, fs); - return fs; -} -#elif defined(_MSC_VER) -#include -#define TMP_PATH "./" -FILE* MakeTmpFilePath(char* tmplate) -{ - auto pathtemplate = _mktemp(tmplate); - EXPECT_NE(pathtemplate, nullptr); - if(pathtemplate == nullptr) - { - return nullptr; - } - auto* fs = std::fopen(pathtemplate, "w+"); - EXPECT_NE(fs, nullptr); - return fs; -} -#endif - const char data[]{"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Qui\ sque luctus sem diam, ut eleifend arcu auctor eu. Vestibulum id est vel nulla l\ obortis malesuada ut sed turpis. Nulla a volutpat tortor. Nunc vestibulum portt\ From e77e89c8b7dd6391b300262670b884a407021a3b Mon Sep 17 00:00:00 2001 From: Jared Mulconry Date: Tue, 26 Sep 2017 22:41:20 +1000 Subject: [PATCH 3/5] Improved the naming of temporary file generator function. Replaced use of tmpnam in utIOStreamBuffer with this facility to addresssafety warning. --- test/unit/UnitTestFileGenerator.h | 4 +-- test/unit/utDefaultIOStream.cpp | 2 +- test/unit/utIOStreamBuffer.cpp | 56 +++++++++++++++++++++---------- 3 files changed, 41 insertions(+), 21 deletions(-) diff --git a/test/unit/UnitTestFileGenerator.h b/test/unit/UnitTestFileGenerator.h index 04f5fd409..2e4aede0f 100644 --- a/test/unit/UnitTestFileGenerator.h +++ b/test/unit/UnitTestFileGenerator.h @@ -46,7 +46,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #if defined(__GNUC__) || defined(__clang__) #define TMP_PATH "/tmp/" -inline FILE* MakeTmpFilePath(char* tmplate) +inline FILE* MakeTmpFile(char* tmplate) { auto fd = mkstemp(tmplate); EXPECT_NE(-1, fd); @@ -61,7 +61,7 @@ inline FILE* MakeTmpFilePath(char* tmplate) #elif defined(_MSC_VER) #include #define TMP_PATH "./" -inline FILE* MakeTmpFilePath(char* tmplate) +inline FILE* MakeTmpFile(char* tmplate) { auto pathtemplate = _mktemp(tmplate); EXPECT_NE(pathtemplate, nullptr); diff --git a/test/unit/utDefaultIOStream.cpp b/test/unit/utDefaultIOStream.cpp index 1d78fb616..c0d82b6dc 100644 --- a/test/unit/utDefaultIOStream.cpp +++ b/test/unit/utDefaultIOStream.cpp @@ -60,7 +60,7 @@ TEST_F( utDefaultIOStream, FileSizeTest ) { const auto dataCount = dataSize / sizeof(*data); char fpath[] = { TMP_PATH"rndfp.XXXXXX" }; - auto* fs = MakeTmpFilePath(fpath); + auto* fs = MakeTmpFile(fpath); ASSERT_NE(nullptr, fs); { auto written = std::fwrite(data, sizeof(*data), dataCount, fs ); diff --git a/test/unit/utIOStreamBuffer.cpp b/test/unit/utIOStreamBuffer.cpp index 0bf69bf00..a1b67da44 100644 --- a/test/unit/utIOStreamBuffer.cpp +++ b/test/unit/utIOStreamBuffer.cpp @@ -43,6 +43,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "UnitTestPCH.h" #include "IOStreamBuffer.h" #include "TestIOStream.h" +#include "UnitTestFileGenerator.h" class IOStreamBufferTest : public ::testing::Test { // empty @@ -68,40 +69,59 @@ TEST_F( IOStreamBufferTest, accessCacheSizeTest ) { EXPECT_EQ( 100U, myBuffer2.cacheSize() ); } +const char data[]{"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Qui\ +sque luctus sem diam, ut eleifend arcu auctor eu. Vestibulum id est vel nulla l\ +obortis malesuada ut sed turpis. Nulla a volutpat tortor. Nunc vestibulum portt\ +itor sapien ornare sagittis volutpat."}; + + TEST_F( IOStreamBufferTest, open_close_Test ) { IOStreamBuffer myBuffer; EXPECT_FALSE( myBuffer.open( nullptr ) ); EXPECT_FALSE( myBuffer.close() ); + + const auto dataSize = sizeof(data); + const auto dataCount = dataSize / sizeof(*data); - char buffer[ L_tmpnam ]; - tmpnam( buffer ); - std::FILE *fs( std::fopen( buffer, "w+" ) ); - size_t written( std::fwrite( buffer, 1, sizeof( char ) * L_tmpnam, fs ) ); + char fname[]={ "octest.XXXXXX" }; + auto* fs = MakeTmpFile(fname); + ASSERT_NE(nullptr, fs); + + auto written = std::fwrite( data, sizeof(*data), dataCount, fs ); EXPECT_NE( 0U, written ); std::fflush( fs ); + { + TestDefaultIOStream myStream( fs, fname ); - TestDefaultIOStream myStream( fs, buffer ); - - EXPECT_TRUE( myBuffer.open( &myStream ) ); - EXPECT_FALSE( myBuffer.open( &myStream ) ); - EXPECT_TRUE( myBuffer.close() ); + EXPECT_TRUE( myBuffer.open( &myStream ) ); + EXPECT_FALSE( myBuffer.open( &myStream ) ); + EXPECT_TRUE( myBuffer.close() ); + } + remove(fname); } TEST_F( IOStreamBufferTest, readlineTest ) { - char buffer[ L_tmpnam ]; - tmpnam( buffer ); - std::FILE *fs( std::fopen( buffer, "w+" ) ); - size_t written( std::fwrite( buffer, 1, sizeof( char ) * L_tmpnam, fs ) ); + + const auto dataSize = sizeof(data); + const auto dataCount = dataSize / sizeof(*data); + + char fname[]={ "readlinetest.XXXXXX" }; + auto* fs = MakeTmpFile(fname); + ASSERT_NE(nullptr, fs); + + auto written = std::fwrite( data, sizeof(*data), dataCount, fs ); EXPECT_NE( 0U, written ); std::fflush( fs ); - IOStreamBuffer myBuffer( 26 ); - EXPECT_EQ( 26U, myBuffer.cacheSize() ); + const auto tCacheSize = 26u; - TestDefaultIOStream myStream( fs, buffer ); - size_t size( myStream.FileSize() ); - size_t numBlocks( size / myBuffer.cacheSize() ); + IOStreamBuffer myBuffer( tCacheSize ); + EXPECT_EQ(tCacheSize, myBuffer.cacheSize() ); + + TestDefaultIOStream myStream( fs, fname ); + auto size = myStream.FileSize(); + auto numBlocks = size / myBuffer.cacheSize(); if ( size % myBuffer.cacheSize() > 0 ) { numBlocks++; } From 12b6895f7b682255f60dcd77dd26a75da63a4697 Mon Sep 17 00:00:00 2001 From: Jared Mulconry Date: Fri, 29 Sep 2017 01:29:11 +1000 Subject: [PATCH 4/5] Replaced unsigned long for the crc table to z_crc_t, to match what is returned by get-crc_table --- contrib/unzip/crypt.h | 2 +- contrib/unzip/unzip.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/contrib/unzip/crypt.h b/contrib/unzip/crypt.h index 622f4bc2e..5fd3dcd6c 100644 --- a/contrib/unzip/crypt.h +++ b/contrib/unzip/crypt.h @@ -45,7 +45,7 @@ static int decrypt_byte(unsigned long* pkeys, const unsigned long* pcrc_32_tab) /*********************************************************************** * Update the encryption keys with the next byte of plain text */ -static int update_keys(unsigned long* pkeys,const unsigned long* pcrc_32_tab,int c) +static int update_keys(unsigned long* pkeys,const z_crc_t* pcrc_32_tab,int c) { (*(pkeys+0)) = CRC32((*(pkeys+0)), c); (*(pkeys+1)) += (*(pkeys+0)) & 0xff; diff --git a/contrib/unzip/unzip.c b/contrib/unzip/unzip.c index 9ad4766d8..e9ad1ff99 100644 --- a/contrib/unzip/unzip.c +++ b/contrib/unzip/unzip.c @@ -147,7 +147,7 @@ typedef struct int encrypted; # ifndef NOUNCRYPT unsigned long keys[3]; /* keys defining the pseudo-random sequence */ - const unsigned long* pcrc_32_tab; + const z_crc_t* pcrc_32_tab; # endif } unz_s; From 4feac1b1f93c8f12df65b1592d64b06e6a048fc6 Mon Sep 17 00:00:00 2001 From: Jared Mulconry Date: Fri, 29 Sep 2017 01:40:05 +1000 Subject: [PATCH 5/5] Changed a couple more function interfaces to correct the input type. --- contrib/unzip/crypt.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contrib/unzip/crypt.h b/contrib/unzip/crypt.h index 5fd3dcd6c..2aa99c284 100644 --- a/contrib/unzip/crypt.h +++ b/contrib/unzip/crypt.h @@ -32,7 +32,7 @@ /*********************************************************************** * Return the next byte in the pseudo-random sequence */ -static int decrypt_byte(unsigned long* pkeys, const unsigned long* pcrc_32_tab) +static int decrypt_byte(unsigned long* pkeys, const z_crc_t* pcrc_32_tab) { unsigned temp; /* POTENTIAL BUG: temp*(temp^1) may overflow in an * unpredictable manner on 16-bit systems; not a problem @@ -62,7 +62,7 @@ static int update_keys(unsigned long* pkeys,const z_crc_t* pcrc_32_tab,int c) * Initialize the encryption keys and the random header according to * the given password. */ -static void init_keys(const char* passwd,unsigned long* pkeys,const unsigned long* pcrc_32_tab) +static void init_keys(const char* passwd,unsigned long* pkeys,const z_crc_t* pcrc_32_tab) { *(pkeys+0) = 305419896L; *(pkeys+1) = 591751049L;