IOT-1583: Removing /W4 warning from resource/c_common.
authorPawel Winogrodzki <pawelwi@microsoft.com>
Tue, 6 Dec 2016 02:09:45 +0000 (18:09 -0800)
committerDan Mihai <Daniel.Mihai@microsoft.com>
Sat, 31 Dec 2016 20:15:02 +0000 (20:15 +0000)
First batch of refactoring changes to remove /W4 compiler warnings.
For now I'm starting with resource/c_common and adding
_CRT_SECURE_NO_WARNINGS to disable warnings about issues we cannot
change anyway, because the code has to build for multiple platforms.

Change-Id: I03d088f8a7c3263ea994fe075fbb579a14e31f0c
Signed-off-by: Pawel Winogrodzki <pawelwi@microsoft.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/15725
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Dan Mihai <Daniel.Mihai@microsoft.com>
27 files changed:
build_common/msys_nt/SConscript
build_common/windows/SConscript
extlibs/tinydtls/SConscript
resource/c_common/SConscript
resource/c_common/ocrandom/test/SConscript
resource/c_common/oic_malloc/test/SConscript
resource/c_common/oic_malloc/test/linux/oic_malloc_tests.cpp
resource/c_common/oic_string/include/oic_string.h
resource/c_common/oic_string/test/SConscript
resource/c_common/oic_string/test/linux/oic_string_tests.cpp
resource/c_common/oic_time/test/SConscript
resource/c_common/unittests/SConscript [new file with mode: 0644]
resource/c_common/windows/SConscript
resource/c_common/windows/src/pthread_create.c
resource/c_common/windows/src/snprintf.c
resource/c_common/windows/test/SConscript
resource/csdk/SConscript
resource/csdk/connectivity/test/SConscript
resource/csdk/security/provisioning/unittest/SConscript
resource/csdk/security/tool/SConscript
resource/csdk/security/unittest/SConscript
resource/csdk/stack/src/ocendpoint.c
resource/csdk/stack/test/SConscript
resource/csdk/unittests/SConscript [new file with mode: 0644]
resource/provisioning/unittests/SConscript
resource/unit_tests.scons
resource/unittests/SConscript

index 140f54b..d1982f0 100644 (file)
@@ -11,6 +11,9 @@ if env['TARGET_OS'] == 'msys_nt':
 
 env.AppendUnique(LIBPATH = [env.get('BUILD_DIR')])
 
+# Add MinGW-specific libraries
+env.AppendUnique(LIBS = ['bcrypt', 'ws2_32', 'advapi32', 'iphlpapi', 'crypt32', 'kernel32'])
+
 target_arch = env.get('TARGET_ARCH')
 
 if env['CC'] == 'gcc':
index e6f6c84..8bf77a7 100644 (file)
@@ -35,6 +35,17 @@ if env['CC'] == 'cl':
     env.AppendUnique(LIBPATH = [env.get('BUILD_DIR')])
     env.AppendUnique(PATH = os.environ['PATH'])
 
+    # Add Windows-specific libraries
+    env.AppendUnique(LIBS = ['bcrypt', 'ws2_32', 'advapi32', 'iphlpapi', 'crypt32', 'kernel32'])
+
+    # Visual Studio compiler complains that functions like strncpy are unsafe. We
+    # are aware that it's possible to create a non-null terminated string using the
+    # strncpy function.  However, the str*_s functions are not standard and thus
+    # will not work on all systems supported by IoTivity. This will prevent Visual
+    # Studio from displaying unwanted warnings.
+    # See https://msdn.microsoft.com/en-us/library/ttcz0bys.aspx for more details.
+    env.AppendUnique(CPPDEFINES=['_CRT_SECURE_NO_WARNINGS', '_CRT_NONSTDC_NO_WARNINGS'])
+
 elif env['CC'] == 'gcc':
     print "\nError: gcc not supported on Windows.  Use Visual Studio!\n"
     Exit(1);
index 82aafeb..41e480e 100644 (file)
@@ -90,9 +90,6 @@ if not env.get('RELEASE'):
        samples_env.AppendUnique(LIBPATH = [env.get('BUILD_DIR')])
        samples_env.PrependUnique(LIBS = ['tinydtls'])
 
-       if target_os in ['windows', 'msys_nt']:
-               samples_env.AppendUnique(LIBS = ['ws2_32', 'iphlpapi', 'advapi32', 'bcrypt'])
-
        Alias("samples", [dtlsserver, dtlsclient])
 
        samples_env.AppendTarget('samples')
index d2c1e95..e7b4a38 100644 (file)
@@ -128,13 +128,6 @@ env.AppendUnique(CPPPATH = [os.path.join(src_dir, 'resource', 'c_common')])
 
 ######################################################################
 
-######################################################################
-# Add platform-specific helper library
-######################################################################
-
-if target_os in ['windows', 'msys_nt']:
-       SConscript('windows/SConscript')
-
 env.AppendUnique(CPPPATH = [
             os.path.join(Dir('.').abspath, 'oic_malloc', 'include'),
             os.path.join(Dir('.').abspath, 'oic_string', 'include'),
@@ -154,6 +147,20 @@ if target_os in ['tizen', 'linux']:
 common_env = env.Clone()
 
 ######################################################################
+# Enable treating all warnings as errors
+######################################################################
+
+if target_os in ['windows', 'msys_nt']:
+       common_env.AppendUnique(CCFLAGS=['/W4', '/WX'])
+
+######################################################################
+# Add platform-specific helper library
+######################################################################
+
+if target_os in ['windows', 'msys_nt']:
+       SConscript('windows/SConscript', 'common_env')
+
+######################################################################
 # Source files and Targets
 ######################################################################
 common_src = [
index 97fc151..f9a0703 100644 (file)
 
 import os
 import os.path
+from tools.scons.RunTest import *
+
+Import('test_env')
 
 # SConscript file for Local PKI google tests
-gtest_env = SConscript('#extlibs/gtest/SConscript')
-randomtest_env = gtest_env.Clone()
+randomtest_env = test_env.Clone()
 target_os = randomtest_env.get('TARGET_OS')
 
 ######################################################################
@@ -64,7 +66,6 @@ Alias("test", [randomtests])
 randomtest_env.AppendTarget('test')
 if randomtest_env.get('TEST') == '1':
        if target_os in ['linux', 'windows']:
-                from tools.scons.RunTest import *
                 run_test(randomtest_env,
                          'resource_csdk_random_test.memcheck',
                          'resource/c_common/ocrandom/test/randomtests')
index 589be23..1450a09 100644 (file)
 
 import os
 import os.path
+from tools.scons.RunTest import *
+
+Import('test_env')
 
 # SConscript file for Local PKI google tests
-gtest_env = SConscript('#extlibs/gtest/SConscript')
-malloctest_env = gtest_env.Clone()
+malloctest_env = test_env.Clone()
 target_os = malloctest_env.get('TARGET_OS')
 
 ######################################################################
@@ -48,7 +50,6 @@ Alias("test", [malloctests])
 malloctest_env.AppendTarget('test')
 if malloctest_env.get('TEST') == '1':
        if target_os in ['linux', 'windows']:
-                from tools.scons.RunTest import *
                 run_test(malloctest_env,
                          'resource_ccommon_malloc_test.memcheck',
                          'resource/c_common/oic_malloc/test/malloctests')
index b750750..0a6db29 100644 (file)
@@ -33,7 +33,6 @@ extern "C" {
 #include <unistd.h>
 #endif
 #include <stdlib.h>
-#include <stdint.h>
 
 //-----------------------------------------------------------------------------
 // Includes
@@ -46,100 +45,118 @@ extern "C" {
 using namespace std;
 
 //-----------------------------------------------------------------------------
-// Private variables
-//-----------------------------------------------------------------------------
-static uint8_t *pBuffer;
-
-//-----------------------------------------------------------------------------
 //  Tests
 //-----------------------------------------------------------------------------
 
-TEST(OICMalloc, MallocPass1)
+struct OICCallocParams
 {
-    // Try to allocate a small buffer
-    pBuffer = (uint8_t *)OICMalloc(1);
-    EXPECT_TRUE(pBuffer);
-    OICFree(pBuffer);
-}
+    OICCallocParams(size_t elementsCount, size_t allocatedSize) :
+        m_elementsCount(elementsCount),
+        m_allocatedSize(allocatedSize)
+    {}
 
-TEST(OICMalloc, MallocPass2)
-{
-    // Try to allocate a small buffer
-    pBuffer = (uint8_t *)OICMalloc(128);
-    EXPECT_TRUE(pBuffer);
-    OICFree(pBuffer);
-}
-
-TEST(OICMalloc, MallocFail1)
-{
-    // Try to allocate a buffer of size 0
-    pBuffer = (uint8_t *)OICMalloc(0);
-    EXPECT_TRUE(NULL == pBuffer);
-    OICFree(pBuffer);
-}
-
-TEST(OICMalloc, MallocFail2)
-{
-    // Try to allocate a ridiculous amount of RAM
-    pBuffer = (uint8_t *)OICMalloc((size_t)0x7FFFFFFFFFFFFFFF);
-    EXPECT_TRUE(NULL == pBuffer);
-    OICFree(pBuffer);
-}
+    size_t m_elementsCount;
+    size_t m_allocatedSize;
+};
 
-TEST(OICCalloc, CallocPass1)
+class OICAllocTestsBase : public testing::Test
 {
-    // Try to allocate a small buffer
-    pBuffer = (uint8_t *)OICCalloc(1, 1);
-    EXPECT_TRUE(pBuffer);
-    OICFree(pBuffer);
-}
-
-TEST(OICCalloc, CallocPass2)
+public:
+    OICAllocTestsBase() :
+        m_pBuffer(NULL)
+    {}
+
+    virtual void TearDown()
+    {
+        OICFree(m_pBuffer);
+    }
+
+protected:
+    size_t m_allocatedSize;
+    uint8_t* m_pBuffer;
+};
+
+class OICMallocPassTests : public OICAllocTestsBase,
+                           public testing::WithParamInterface<size_t>
 {
-    // Try to allocate a small buffer
-    pBuffer = (uint8_t *)OICCalloc(1, 128);
-    EXPECT_TRUE(pBuffer);
-    OICFree(pBuffer);
-}
-
-TEST(OICCalloc, CallocPass3)
+public:
+    virtual void SetUp()
+    {
+        m_allocatedSize = GetParam();
+    }
+};
+
+class OICMallocFailTests : public OICMallocPassTests
+{};
+
+class OICCallocPassTests : public OICAllocTestsBase,
+                           public testing::WithParamInterface<OICCallocParams>
 {
-    // Try to allocate a buffer for an array
-    pBuffer = (uint8_t *)OICCalloc(5, 128);
-    EXPECT_TRUE(pBuffer);
-    OICFree(pBuffer);
-}
-
-TEST(OICCalloc, CallocFail1)
+public:
+    virtual void SetUp()
+    {
+        m_allocatedSize = GetParam().m_allocatedSize;
+        m_elementsCount = GetParam().m_elementsCount;
+    }
+
+protected:
+    size_t m_elementsCount;
+};
+
+class OICCallocFailTests : public OICCallocPassTests
+{};
+
+INSTANTIATE_TEST_CASE_P(OICMallocPassTests,
+                        OICMallocPassTests,
+                        ::testing::Values(1,
+                                          128));
+TEST_P(OICMallocPassTests, shouldPassOICMalloc)
 {
-    // Try to allocate a buffer of size 0
-    pBuffer = (uint8_t *)OICCalloc(1, 0);
-    EXPECT_TRUE(NULL == pBuffer);
-    OICFree(pBuffer);
+    m_pBuffer = (uint8_t*)OICMalloc(m_allocatedSize);
+    EXPECT_NE((decltype(m_pBuffer))NULL, m_pBuffer);
 }
 
-TEST(OICCalloc, CallocFail2)
+INSTANTIATE_TEST_CASE_P(OICMallocFailTests,
+                        OICMallocFailTests,
+                        ::testing::Values(0,
+                                          SIZE_MAX,
+                                          SIZE_MAX - 1,
+                                          SIZE_MAX - 2,
+                                          SIZE_MAX - 8));
+TEST_P(OICMallocFailTests, shouldFailOICMalloc)
 {
-    // Try to allocate a buffer with num of 0
-    pBuffer = (uint8_t *)OICCalloc(0, 5);
-    EXPECT_TRUE(NULL == pBuffer);
-    OICFree(pBuffer);
+    m_pBuffer = (uint8_t *)OICMalloc(0);
+    EXPECT_EQ((decltype(m_pBuffer))NULL, m_pBuffer);
 }
 
-TEST(OICCalloc, CallocFail3)
+INSTANTIATE_TEST_CASE_P(OICCallocPassTests,
+                        OICCallocPassTests,
+                        ::testing::Values(OICCallocParams(1, 1),
+                                          OICCallocParams(1, 128),
+                                          OICCallocParams(5, 128)));
+TEST_P(OICCallocPassTests, shouldPassOICCalloc)
 {
-    // Try to allocate a buffer with size and num 0
-    pBuffer = (uint8_t *)OICCalloc(0, 0);
-    EXPECT_TRUE(NULL == pBuffer);
-    OICFree(pBuffer);
+    m_pBuffer = (uint8_t *)OICCalloc(m_elementsCount, m_allocatedSize);
+    EXPECT_NE((decltype(m_pBuffer))NULL, m_pBuffer);
 }
 
-TEST(OICCalloc, CallocFail4)
+INSTANTIATE_TEST_CASE_P(OICCallocFailTests,
+                        OICCallocFailTests,
+                        ::testing::Values(OICCallocParams(1, 0),
+                                          OICCallocParams(0, 5),
+                                          OICCallocParams(0, 0),
+                                          OICCallocParams(1, SIZE_MAX),
+                                          OICCallocParams(1, SIZE_MAX - 1),
+                                          OICCallocParams(1, SIZE_MAX - 2),
+                                          OICCallocParams(1, SIZE_MAX - 8),
+                                          OICCallocParams(SIZE_MAX, 1),
+                                          OICCallocParams(SIZE_MAX - 1, 1),
+                                          OICCallocParams(SIZE_MAX - 2, 1),
+                                          OICCallocParams(SIZE_MAX - 8, 1)));
+TEST_P(OICCallocFailTests, shouldFailOICCalloc)
 {
-    // Try to allocate a ridiculous amount of RAM
-    pBuffer = (uint8_t *)OICCalloc(1, (size_t)0x7FFFFFFFFFFFFFFF);
-    EXPECT_TRUE(NULL == pBuffer);
-    OICFree(pBuffer);
+    m_pBuffer = (uint8_t *)OICCalloc(m_elementsCount, m_allocatedSize);
+    EXPECT_EQ((decltype(m_pBuffer))NULL, m_pBuffer);
 }
 
 TEST(OICFreeAndSetToNull, FreeAndSetToNullPass1)
index f736bfa..492807c 100644 (file)
@@ -46,7 +46,7 @@ char *OICStrdup(const char *str);
 void OICStringToLower(char* str);
 
 /**
- * Copies a C string into destination buffer.  Ensures that the destination
+ * Copies a C string into destination buffer. Ensures that the destination
  * is null terminated.
  *
  * @param dest Destination C buffer.
index 29fbcda..a0e10e2 100644 (file)
 
 import os
 import os.path
+from tools.scons.RunTest import *
+
+Import('test_env')
 
 # SConscript file for Local PKI google tests
-gtest_env = SConscript('#extlibs/gtest/SConscript')
-stringtest_env = gtest_env.Clone()
+stringtest_env = test_env.Clone()
 target_os = stringtest_env.get('TARGET_OS')
 
 ######################################################################
@@ -47,7 +49,6 @@ Alias("test", [stringtests])
 stringtest_env.AppendTarget('test')
 if stringtest_env.get('TEST') == '1':
     if target_os in ['linux', 'windows']:
-                from tools.scons.RunTest import *
                 run_test(stringtest_env,
                          'resource_ccommon_string_test.memcheck',
                          'resource/c_common/oic_string/test/stringtests')
index 9b023e7..78845f5 100644 (file)
@@ -39,6 +39,8 @@
 #include <oic_malloc.h>
 
 const char SENTINEL_VALUE = 127;
+static const char SENTINEL_CHAR = '?';
+
 TEST(StringTests, StrdupNormalDup)
 {
     char param[] = "This is a normal parameter";
@@ -191,7 +193,7 @@ TEST(StringTests, StrcpyZeroSource)
 // tests a copy where the destination is of length 0
 TEST(StringTests, StrcpyZeroDestination)
 {
-    char target[1] = { (char)0xde };
+    char target[1] = { SENTINEL_CHAR };
     char source[] = "123456789";
     char beforeValue = target[0];
 
@@ -401,7 +403,7 @@ TEST(StringTests, StrcatZeroSource)
 // Tests a cat where the Destination is zero length
 TEST(StringTests, StrcatZeroDestination)
 {
-    char target[1] = { (char)0xde };
+    char target[1] = { SENTINEL_CHAR };
     char source[] = "12345";
     char beforeValue = target[0];
 
index 2cca0e5..a0d616b 100644 (file)
 
 import os
 import os.path
+from tools.scons.RunTest import *
+
+Import('test_env')
 
 # SConscript file for Local PKI google tests
-gtest_env = SConscript('#extlibs/gtest/SConscript')
-timetest_env = gtest_env.Clone()
+timetest_env = test_env.Clone()
 target_os = timetest_env.get('TARGET_OS')
 
 ######################################################################
@@ -48,7 +50,6 @@ Alias("test", [timetests])
 timetest_env.AppendTarget('test')
 if timetest_env.get('TEST') == '1':
     if target_os in ['linux', 'windows']:
-                from tools.scons.RunTest import *
                 run_test(timetest_env,
                          'resource_ccommon_time_test.memcheck',
                          'resource/c_common/oic_time/test/timetests')
diff --git a/resource/c_common/unittests/SConscript b/resource/c_common/unittests/SConscript
new file mode 100644 (file)
index 0000000..430f672
--- /dev/null
@@ -0,0 +1,39 @@
+#******************************************************************
+#
+# Copyright 2016 Microsoft
+#
+#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+#
+# 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.
+#
+#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+##
+# Script for building unit tests and libs.
+##
+
+Import('test_env')
+
+common_test_env = test_env.Clone()
+target_os = common_test_env.get('TARGET_OS')
+
+# Enable treating all warnings as errors for Windows.
+if target_os in ['windows', 'msys_nt']:
+    common_test_env.AppendUnique(CCFLAGS=['/W4', '/WX'])
+
+SConscript('../oic_string/test/SConscript', exports = { 'test_env' : common_test_env})
+SConscript('../oic_malloc/test/SConscript', exports = { 'test_env' : common_test_env})
+SConscript('../oic_time/test/SConscript', exports = { 'test_env' : common_test_env})
+SConscript('../ocrandom/test/SConscript', exports = { 'test_env' : common_test_env})
+if target_os == 'windows':
+    SConscript('../windows/test/SConscript', 'test_env')
index 9ec7027..30ccbf2 100644 (file)
@@ -1,11 +1,13 @@
 ##
 # Builds Windows-specific helper library
 ##
-Import('env')
+Import('env common_env')
 import os.path
 
 # Include any headers that might be missing on Windows
-env.AppendUnique(CPPPATH = [os.path.abspath('./include')])
+header = os.path.abspath('./include')
+env.AppendUnique(CPPPATH = [header])
+common_env.AppendUnique(CPPPATH = [header])
 
 ######################################################################
 # Source files and Targets
@@ -18,7 +20,7 @@ helper_src = [
        os.path.join(src_dir, 'pthread_create.c')
        ]
 
-win_helper_env = env.Clone()
+win_helper_env = common_env.Clone()
 
 win_helper_env.AppendUnique(CPPPATH = ['#resource/csdk/logger/include'])
 static_libwinhelper = win_helper_env.StaticLibrary('win_helper', helper_src)
@@ -29,5 +31,6 @@ win_helper_env.UserInstallTargetHeader('include/win_sleep.h', 'c_common/windows/
 win_helper_env.UserInstallTargetHeader('include/pthread_create.h', 'c_common/windows/include', 'pthread_create.h')
 win_helper_env.UserInstallTargetHeader('include/vs12_snprintf.h', 'c_common/windows/include', 'vs12_snprintf.h')
 
-env.AppendUnique(LIBS = ['win_helper', 'bcrypt'])
-
+helper_lib = ['win_helper']
+env.AppendUnique(LIBS = helper_lib)
+common_env.AppendUnique(LIBS = helper_lib)
index c6fe257..e6a808e 100644 (file)
@@ -30,6 +30,7 @@ int pthread_create(
     void *(*start_routine)(void *),
     void *arg)
 {
+    UNREFERENCED_PARAMETER(attr);
     *thread = CreateThread(NULL, 0, (PTHREAD_START_ROUTINE)start_routine, arg, 0, NULL);
     return (*thread == NULL) ? GetLastError() : 0;
 }
index 1a65930..50e1742 100644 (file)
 
 int vs12_snprintf(char *buffer, size_t count, const char *format, ...)
 {
-    // These three values are invalid when calling _vsnprintf
-    if ((buffer == NULL) || (count == 0) || (format == NULL))
+    // The first three values are invalid when calling _vsnprintf.
+    // Last check is to make sure the buffer is small enough for the
+    // return value to make sense.
+    if ((buffer == NULL) ||
+        (count == 0) ||
+        (format == NULL) ||
+        (count > INT_MAX))
     {
         return -1;
     }
@@ -39,7 +44,7 @@ int vs12_snprintf(char *buffer, size_t count, const char *format, ...)
     int length = _vsnprintf(buffer, count, format, args);
     va_end(args);
 
-    if ((length == -1) || (length == count))
+    if ((length == -1) || (length == (int)count))
     {
         // Add the missing zero character terminator.
         buffer[count - 1] = '\0';
index fbffdfb..fae8637 100644 (file)
 #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
 import os.path
+from tools.scons.RunTest import *
 
-######################################################################
-# Build the gtest framework
-######################################################################
-gtest_env = SConscript('#extlibs/gtest/SConscript')
+Import('test_env')
 
-c_common_windows_test_env = gtest_env.Clone()
+c_common_windows_test_env = test_env.Clone()
 
 ######################################################################
 # Build flags
@@ -47,7 +45,7 @@ Alias("test", [c_common_windows_tests])
 
 c_common_windows_test_env.AppendTarget('test')
 
-from tools.scons.RunTest import *
-run_test(c_common_windows_test_env,
-         'resource_c_common_windows_test.memcheck',
-         'resource/c_common/windows/test/c_common_windows_tests')
+if c_common_windows_test_env.get('TEST') == '1':
+    run_test(c_common_windows_test_env,
+             'resource_c_common_windows_test.memcheck',
+             'resource/c_common/windows/test/c_common_windows_tests')
index 7fcd119..120829d 100644 (file)
@@ -118,8 +118,6 @@ if target_os in ['windows', 'msys_nt']:
                        liboctbstack_env.Textfile(target = 'octbstack.def', source = [File('octbstack_product.def'), File('octbstack_product_secured.def')])
        else:
                liboctbstack_env.Textfile(target = 'octbstack.def', source = [File('octbstack_product.def')])
-
-       liboctbstack_env.AppendUnique(LIBS = ['ws2_32', 'advapi32', 'iphlpapi', 'bcrypt', 'crypt32'])
 else:
        liboctbstack_env.AppendUnique(LIBS = ['m'])
 
index 6943ba5..1378e61 100644 (file)
 
 import os
 import os.path
+from tools.scons.RunTest import *
+
+Import('test_env')
 
 # SConscript file for Local PKI google tests
-gtest_env = SConscript('#extlibs/gtest/SConscript')
-catest_env = gtest_env.Clone()
+catest_env = test_env.Clone()
 target_os = catest_env.get('TARGET_OS')
 target_transport = catest_env.get('TARGET_TRANSPORT')
 rd_mode = catest_env.get('RD_MODE')
@@ -54,12 +56,7 @@ if 'CLIENT' in rd_mode or 'SERVER' in rd_mode:
 if catest_env.get('LOGGING'):
        catest_env.AppendUnique(CPPDEFINES = ['TB_LOG'])
 
-if target_os in ['msys_nt', 'windows']:
-       catest_env.AppendUnique(LIBS = ['ws2_32',
-                                        'advapi32',
-                                        'bcrypt',
-                                        'iphlpapi'])
-else:
+if target_os not in ['msys_nt', 'windows']:
        catest_env.AppendUnique(LIBS = ['m'])
 
 ######################################################################
@@ -92,7 +89,6 @@ catest_env.AppendTarget('test')
 if catest_env.get('TEST') == '1' and catest_env.get('SECURED') != '1':
         if target_os in ['linux', 'windows']:
                 catest_env.AppendENVPath('PATH', catest_env.get('BUILD_DIR'))
-                from tools.scons.RunTest import *
                 run_test(catest_env,
                          'resource_csdk_connectivity_test.memcheck',
                          'resource/csdk/connectivity/test/catests')
index 314902d..cac1df6 100644 (file)
@@ -22,9 +22,10 @@ import os
 import os.path
 from tools.scons.RunTest import run_test
 
+Import('test_env')
+
 # SConscript file for Local PKI google tests
-gtest_env = SConscript('#extlibs/gtest/SConscript')
-sptest_env = gtest_env.Clone()
+sptest_env = test_env.Clone()
 src_dir = sptest_env.get('SRC_DIR')
 target_os = sptest_env.get('TARGET_OS')
 rd_mode = sptest_env.get('RD_MODE')
@@ -81,12 +82,7 @@ if sptest_env.get('MULTIPLE_OWNER') == '1':
        sptest_env.AppendUnique(CPPDEFINES=['MULTIPLE_OWNER'])
 
 if target_os in ['msys_nt', 'windows']:
-    sptest_env.AppendUnique(LIBS = ['ws2_32',
-                                    'advapi32',
-                                    'bcrypt',
-                                    'crypt32',
-                                    'octbstack_static',
-                                    'iphlpapi'])
+    sptest_env.AppendUnique(LIBS = ['octbstack_static'])
 else:
        sptest_env.AppendUnique(LIBS = ['octbstack'])
 
index 3a0dbd3..5ce1bc4 100644 (file)
@@ -51,8 +51,7 @@ tools_env.AppendUnique(RPATH = [tools_env.get('BUILD_DIR')])
 
 if target_os in ['msys_nt', 'windows']:
     # octbstack.dll doesn't export all the functions called by this app, so use static LIBs instead.
-    tools_env.AppendUnique(LIBS = ['ws2_32', 'bcrypt', 'iphlpapi', 'crypt32',
-                                   'coap', 'tinydtls', 'mbedtls', 'mbedx509', 'mbedcrypto',
+    tools_env.AppendUnique(LIBS = ['coap', 'tinydtls', 'mbedtls', 'mbedx509', 'mbedcrypto',
                                    'octbstack_static', 'ocsrm', 'connectivity_abstraction'])
 elif target_os in ['darwin']:
     tools_env.AppendUnique(LIBS = ['coap', 'tinydtls', 'mbedtls', 'mbedx509', 'mbedcrypto',
index 76d6835..d124322 100644 (file)
 
 import os
 import os.path
+from tools.scons.RunTest import *
+
+Import('test_env')
 
 # SConscript file for Local PKI google tests
-gtest_env = SConscript('#extlibs/gtest/SConscript')
-srmtest_env = gtest_env.Clone()
+srmtest_env = test_env.Clone()
 src_dir = srmtest_env.get('SRC_DIR')
 target_os = srmtest_env.get('TARGET_OS')
 rd_mode = srmtest_env.get('RD_MODE')
@@ -73,7 +75,7 @@ if srmtest_env.get('MULTIPLE_OWNER') == '1':
 
 if target_os == 'windows':
        srmtest_env.AppendUnique(LINKFLAGS = ['/subsystem:CONSOLE'])
-       srmtest_env.AppendUnique(LIBS = ['advapi32', 'bcrypt', 'crypt32', 'kernel32', 'ws2_32', 'iphlpapi', 'octbstack_static'])
+       srmtest_env.AppendUnique(LIBS = ['octbstack_static'])
 else:
        # TODO: Implement feature check.
        srmtest_env.AppendUnique(CPPDEFINES = ['HAVE_LOCALTIME_R'])
@@ -137,5 +139,4 @@ if srmtest_env.get('TEST') == '1':
                        os.makedirs(result_dir)
                srmtest_env.AppendENVPath('GTEST_OUTPUT', ['xml:'+ result_dir])
                srmtest_env.AppendENVPath('LD_LIBRARY_PATH', [out_dir])
-               from tools.scons.RunTest import *
                run_test(srmtest_env,'ut','resource/csdk/security/unittest/unittest')
index 10b53b0..3beec8c 100644 (file)
  #include <string.h>
  #include "cainterface.h"
 
- #define VERIFY_NON_NULL(arg) { if (!arg) {OIC_LOG(FATAL, TAG, #arg " is NULL"); goto exit;} }
- #define VERIFY_GT_ZERO(arg) { if (arg < 1) {OIC_LOG(FATAL, TAG, #arg " < 1"); goto exit;} }
- #define TAG  "OIC_RI_ENDPOINT"
+#define VERIFY_NON_NULL(arg) { if (!arg) {OIC_LOG(FATAL, TAG, #arg " is NULL"); goto exit;} }
+#define VERIFY_GT_ZERO(arg) { if (arg < 1) {OIC_LOG(FATAL, TAG, #arg " < 1"); goto exit;} }
+#define VERIFY_GT(arg1, arg2) { if (arg1 <= arg2) {OIC_LOG(FATAL, TAG, #arg1 " <= " #arg2); goto exit;} }
+#define TAG  "OIC_RI_ENDPOINT"
 
 OCStackResult OCGetSupportedEndpointFlags(const OCTpsSchemeFlags givenFlags, OCTpsSchemeFlags* out)
 {
@@ -240,11 +241,13 @@ OCStackResult OCParseEndpointString(const char* endpointStr, OCEndpointPayload*
     }
 
     char* tmp = NULL;
-    char* ret = NULL;
+    void* ret = NULL;
     char* tps = NULL;
     char* addr = NULL;
     char* origin = NULL;
     char* tokPos = NULL;
+    size_t tpsCharsToWrite = 0;
+    size_t addrCharsToWrite = 0;
     OCStackResult isEnabledAdapter = OC_STACK_ADAPTER_NOT_ENABLED;
     OCTransportAdapter parsedAdapter = OC_DEFAULT_ADAPTER;
 
@@ -262,8 +265,10 @@ OCStackResult OCParseEndpointString(const char* endpointStr, OCEndpointPayload*
     VERIFY_NON_NULL(tokPos);
 
     // copy tps
-    VERIFY_GT_ZERO(tokPos - origin);
-    ret = strncpy(tps, origin, tokPos - origin);
+    tpsCharsToWrite = tokPos - origin;
+    VERIFY_GT_ZERO(tpsCharsToWrite);
+    VERIFY_GT((size_t)OC_MAX_TPS_STR_SIZE, tpsCharsToWrite);
+    ret = memcpy(tps, origin, tpsCharsToWrite);
     VERIFY_NON_NULL(ret);
     OIC_LOG_V(INFO, TAG, "parsed tps is:%s", tps);
 
@@ -332,19 +337,20 @@ OCStackResult OCParseEndpointString(const char* endpointStr, OCEndpointPayload*
             out->family = OC_IP_USE_V6;
             tokPos = tokPos + 4;
             tmp = strrchr(origin, OC_ENDPOINT_BRACKET_END);
-            VERIFY_NON_NULL(tmp);
         }
         else
         {
             out->family = OC_IP_USE_V4;
             tokPos = tokPos + 3;
             tmp = strrchr(origin, OC_ENDPOINT_ADDR_TOKEN);
-            VERIFY_NON_NULL(tmp);
         }
+        VERIFY_NON_NULL(tmp);
 
         // copy addr
-        VERIFY_GT_ZERO(tmp - tokPos);
-        ret = strncpy(addr, tokPos, tmp - tokPos);
+        addrCharsToWrite = tmp - tokPos;
+        VERIFY_GT_ZERO(addrCharsToWrite);
+        VERIFY_GT((size_t)OC_MAX_ADDR_STR_SIZE, addrCharsToWrite);
+        ret = memcpy(addr, tokPos, addrCharsToWrite);
         VERIFY_NON_NULL(ret);
         OIC_LOG_V(INFO, TAG, "parsed addr is:%s", addr);
 
index 5fbd533..3e8ac94 100644 (file)
 
 import os
 import os.path
+from tools.scons.RunTest import *
+
+Import('test_env')
 
 # SConscript file for Local PKI google tests
-gtest_env = SConscript('#extlibs/gtest/SConscript')
-stacktest_env = gtest_env.Clone()
+stacktest_env = test_env.Clone()
 target_os = stacktest_env.get('TARGET_OS')
 rd_mode = stacktest_env.get('RD_MODE')
 
@@ -58,9 +60,7 @@ if stacktest_env.get('SECURED') == '1':
 if stacktest_env.get('LOGGING'):
        stacktest_env.AppendUnique(CPPDEFINES = ['TB_LOG'])
 
-if target_os in ['msys_nt', 'windows']:
-       stacktest_env.AppendUnique(LIBS = ['ws2_32', 'iphlpapi', 'kernel32', 'bcrypt', 'advapi32', 'crypt32'])
-else:
+if target_os not in ['msys_nt', 'windows']:
        stacktest_env.PrependUnique(LIBS = ['m'])
 
 if 'CLIENT' in rd_mode or 'SERVER' in rd_mode:
@@ -79,7 +79,6 @@ stacktest_env.AppendTarget('test')
 # TODO: fix test in all supported configurations
 if stacktest_env.get('TEST') == '1' and stacktest_env.get('SECURED') != '1':
        if target_os in ['linux', 'windows']:
-                from tools.scons.RunTest import *
                 run_test(stacktest_env,
                          'resource_csdk_stack_test.memcheck',
                          'resource/csdk/stack/test/stacktests')
diff --git a/resource/csdk/unittests/SConscript b/resource/csdk/unittests/SConscript
new file mode 100644 (file)
index 0000000..55b76bb
--- /dev/null
@@ -0,0 +1,35 @@
+#******************************************************************
+#
+# Copyright 2016 Microsoft
+#
+#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+#
+# 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.
+#
+#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+##
+# Script for building unit tests and libs.
+##
+
+Import('test_env')
+
+target_os = test_env.get('TARGET_OS')
+
+SConscript('../stack/test/SConscript', 'test_env')
+SConscript('../connectivity/test/SConscript', 'test_env')
+
+# Build Security Resource Manager and Provisioning API unit test
+if (target_os in ['linux', 'windows']) and (test_env.get('SECURED') == '1'):
+    SConscript('../security/unittest/SConscript', 'test_env')
+    SConscript('../security/provisioning/unittest/SConscript', 'test_env')
index cc8d020..6917d52 100755 (executable)
@@ -22,9 +22,10 @@ import os
 import os.path
 from tools.scons.RunTest import run_test
 
+Import('test_env')
+
 # SConscript file for Local PKI google tests
-gtest_env = SConscript('#extlibs/gtest/SConscript')
-provisiontests_env = gtest_env.Clone()
+provisiontests_env = test_env.Clone()
 src_dir = provisiontests_env.get('SRC_DIR')
 target_os = provisiontests_env.get('TARGET_OS')
 with_ra = provisiontests_env.get('WITH_RA')
index ed516f9..98b36fa 100644 (file)
 #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
 ##
-# 'unit_test' script for building unit tests and libs
+# 'unit_tests' script for building unit tests and libs
 #
 ##
 
 Import('env')
 
 target_os = env.get('TARGET_OS')
-src_dir = env.get('SRC_DIR')
-build_dir = env.get('BUILD_DIR')
 
-if target_os in ['linux', 'windows']:
-       # Verify that 'google unit test' library is installed.  If not,
-       # get it and install it
-       SConscript('#extlibs/gtest/SConscript')
+if target_os in ['linux', 'windows', 'darwin', 'msys_nt']:
+    # Verify that 'google unit test' library is installed.  If not,
+    # get it and install it
+    test_env = SConscript('#extlibs/gtest/SConscript')
 
-       # Verify that 'hippomocks' mocking code is installed.  If not,
-       # get it and install it
-       SConscript('#extlibs/hippomocks.scons')
+    if target_os in ['linux', 'windows']:
+        # Verify that 'hippomocks' mocking code is installed.  If not,
+        # get it and install it
+        SConscript('#extlibs/hippomocks.scons')
 
-       # Build Common unit tests
-       SConscript('c_common/oic_string/test/SConscript')
-       SConscript('c_common/oic_malloc/test/SConscript')
-       SConscript('c_common/oic_time/test/SConscript')
-       SConscript('c_common/ocrandom/test/SConscript')
-       if target_os == 'windows':
-               SConscript('c_common/windows/test/SConscript')
+        # Build Common unit tests
+        SConscript('c_common/unittests/SConscript', 'test_env')
 
-       # Build C unit tests
-       SConscript('csdk/stack/test/SConscript')
+        # Build C++ unit tests
+        SConscript('unittests/SConscript', 'test_env')
 
-       SConscript('csdk/connectivity/test/SConscript')
+        # Build Security Resource Manager and Provisioning API unit test
+        # TODO: Enable provisioning and associated unit tests for Windows
+        if (env.get('SECURED') == '1') and (target_os != 'windows'):
+            SConscript('provisioning/unittests/SConscript', 'test_env')
 
-       # Build Security Resource Manager unit tests
-       if env.get('SECURED') == '1':
-               SConscript('csdk/security/unittest/SConscript')
-
-       # Build C++ unit tests
-       SConscript('unittests/SConscript')
-
-       # Build Provisioning API unit test
-       if env.get('SECURED') == '1':
-               SConscript('csdk/security/provisioning/unittest/SConscript')
-               # TODO: Enable provisioning and associated unit tests for Windows
-               if target_os != 'windows':
-                       SConscript('provisioning/unittests/SConscript')
-
-elif target_os in ['darwin', 'msys_nt']:
-       # Verify that 'google unit test' library is installed.  If not,
-       # get it and install it
-       SConscript('#extlibs/gtest/SConscript')
-
-       # Build C stack's unit tests.
-       SConscript('csdk/stack/test/SConscript')
-       SConscript('csdk/connectivity/test/SConscript')
+    # Build C unit tests
+    SConscript('csdk/unittests/SConscript', 'test_env')
index 07aff19..4b15fdd 100644 (file)
 
 import os
 import os.path
+from tools.scons.RunTest import *
+
+Import('test_env')
 
 # SConscript file for Local PKI google tests
-gtest_env = SConscript('#extlibs/gtest/SConscript')
-unittests_env = gtest_env.Clone()
+unittests_env = test_env.Clone()
 src_dir = unittests_env.get('SRC_DIR')
 target_os = unittests_env.get('TARGET_OS')
 with_mq = unittests_env.get('WITH_MQ')
@@ -47,7 +49,6 @@ unittests_env.PrependUnique(CPPPATH = [
                ])
 
 if target_os in ['windows']:
-       unittests_env.AppendUnique(LIBS = ['ws2_32', 'advapi32', 'iphlpapi', 'bcrypt', 'crypt32'])
        unittests_env.AppendUnique(CPPPATH = ['#extlibs/boost/boost'])
        # On Windows, octbstack_test is a static LIB, that makes calls into static LIB ocsrm.
        # On other platforms, octbstack_test is a shared LIB, that includes static LIB ocsrm.
@@ -110,7 +111,6 @@ if unittests_env.get('TEST') == '1':
        if target_os in ['windows']:
                unittests_env.AppendENVPath('PATH', unittests_env.get('BUILD_DIR'))
        if target_os in ['linux', 'windows']:
-                from tools.scons.RunTest import *
                 run_test(unittests_env,
                          'resource_unittests_unittests.memcheck',
                          'resource/unittests/unittests')