Enable unit tests on Windows
authorDaniel Ferguson <daniel.j.ferguson@intel.com>
Wed, 14 Sep 2016 22:13:06 +0000 (15:13 -0700)
committerDave Thaler <dthaler@microsoft.com>
Thu, 22 Sep 2016 21:52:24 +0000 (21:52 +0000)
* Add OCPlatformTest.cpp back into build process. Disabled specific tests
  that cause the delta in exception handling between Windows and Linux to
  be a problem. Replaced use of NULL with nullptr
* Add OCRepresentationEncodingTest.cpp back into build process
  only for cl.exe 14.0 (not 12.0)
* Add OCDevicePayloadCreate to octbstack_product.def
* Add OCByteStringCopy to octbstack_product.def
* Explanation:
On the Windows platform, certain exceptions are not being
caught by Gtests' EXPECT_ANY_THROW macro.
Specifically; on Windows, asynchronous structured exceptions
are not caught with C++'s try/catch exception handling. Unfortunately,
these tests raise asynchronous structured exceptions, and the result
is the unit tests crash instead of reporting a failure.

The two most common exception tests being disabled are:
1) Assigning a null pointer to a std::string
2) Indexing into an empty vector

For a specific example, where an asynchronous structured exception
is provoked, look at:
TEST(BindInterfaceToResourceTest, BindZeroResourceInterface)
inside of resource/unittests/OCPlatformTest.cpp

The tests could be refactored because both of
the operations causing the exceptions are not acceptable for the caller to
perform. Expecting anything afterward may be hazardous.
Furthermore, these tests just prove that the C++ documentation is
correct when it states that passing a null pointer to a std::string
constructor, or indexing into an empty vector is a bad idea.

"If s is a null pointer, if n == npos, or if the range specified by
[first,last) is not valid, it causes undefined behavior."
http://www.cplusplus.com/reference/string/string/string/#exceptions

"Portable programs should never call this function with an argument n
that is out of range, since this causes undefined behavior."
http://www.cplusplus.com/reference/vector/vector/operator[]/

Change-Id: If8dbdcb54d7a0fe22ca71fc8513eab413620beed
Signed-off-by: Daniel Ferguson <daniel.j.ferguson@intel.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/11783
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Dan Mihai <Daniel.Mihai@microsoft.com>
Reviewed-by: Dave Thaler <dthaler@microsoft.com>
resource/csdk/octbstack_test.def
resource/unittests/OCPlatformTest.cpp
resource/unittests/OCRepresentationEncodingTest.cpp
resource/unittests/SConscript

index b698b9f4051f884dfa30e4617787a96e4c5882a3..87e1853970441f9c562f7daab370c247998e79e2 100644 (file)
@@ -2,5 +2,7 @@
 
 encodeAddressForRFC6874
 
+OCByteStringCopy
 OCConvertPayload
+OCDevicePayloadCreate
 OCParsePayload
index 0fea345f907d24698cf6e24014702f23ed53fb89..8c8ef7531486ae8c2f3fd4977de9026d5179f7b7 100644 (file)
@@ -176,7 +176,7 @@ namespace OCPlatformTest
              OC::QualityOfService::LowQos
          };
          OCPlatform::Configure(cfg);
-         EXPECT_ANY_THROW(OCPlatform::setDefaultDeviceEntityHandler(NULL));
+         EXPECT_ANY_THROW(OCPlatform::setDefaultDeviceEntityHandler(nullptr));
      }
 
     // Enable it when the stack throw an exception
@@ -191,7 +191,7 @@ namespace OCPlatformTest
              OC::QualityOfService::LowQos
          };
          OCPlatform::Configure(cfg);
-         EXPECT_ANY_THROW(OCPlatform::setDefaultDeviceEntityHandler(NULL));
+         EXPECT_ANY_THROW(OCPlatform::setDefaultDeviceEntityHandler(nullptr));
      }
 
     // Enable it when the stack throw an exception
@@ -438,8 +438,12 @@ namespace OCPlatformTest
             BATCH_INTERFACE);
         EXPECT_EQ(OC_STACK_OK, result);
     }
-
+    
+#if defined (_MSC_VER)
+    TEST(BindInterfaceToResourceTest, DISABLED_BindZeroResourceInterface)
+#else
     TEST(BindInterfaceToResourceTest, BindZeroResourceInterface)
+#endif
     {
         OCResourceHandle resourceHandle = RegisterResource(std::string("/a/light1"),
             std::string("core.light"));
@@ -455,8 +459,12 @@ namespace OCPlatformTest
             "core.brightlight");
         EXPECT_EQ(OC_STACK_OK, result);
     }
-
+    
+#if defined (_MSC_VER)
+    TEST(BindTypeToResourceTest, DISABLED_BindZeroResourceType)
+#else
     TEST(BindTypeToResourceTest, BindZeroResourceType)
+#endif
     {
         OCResourceHandle resourceHandle = RegisterResource(std::string("/a/light4"),
             std::string("core.light"));
@@ -529,7 +537,11 @@ namespace OCPlatformTest
                 OC::QualityOfService::HighQos));
     }
 
+#if defined (_MSC_VER)
+    TEST(NotifyAllObserverTest, DISABLED_NotifyListOfObservers)
+#else
     TEST(NotifyAllObserverTest, NotifyListOfObservers)
+#endif
     {
         OCResourceHandle resourceHome = RegisterResource(std::string("/a/obs6"),
             std::string("core.obs"));
@@ -540,7 +552,11 @@ namespace OCPlatformTest
             interestedObservers, resourceResponse));
     }
 
+#if defined (_MSC_VER)
+    TEST(NotifyAllObserverTest, DISABLED_NotifyListOfObserversWithLowQos)
+#else
     TEST(NotifyAllObserverTest, NotifyListOfObserversWithLowQos)
+#endif
     {
         OCResourceHandle resourceHome = RegisterResource(std::string("/a/obs7"),
             std::string("core.obs"));
@@ -551,7 +567,11 @@ namespace OCPlatformTest
             interestedObservers, resourceResponse,OC::QualityOfService::LowQos));
     }
 
+#if defined (_MSC_VER)
+    TEST(NotifyAllObserverTest, DISABLED_NotifyListOfObserversWithMidQos)
+#else
     TEST(NotifyAllObserverTest, NotifyListOfObserversWithMidQos)
+#endif
     {
         OCResourceHandle resourceHome = RegisterResource(std::string("/a/obs8"),
             std::string("core.obs"));
@@ -562,7 +582,11 @@ namespace OCPlatformTest
             interestedObservers, resourceResponse,OC::QualityOfService::MidQos));
     }
 
+#if defined (_MSC_VER)
+    TEST(NotifyAllObserverTest, DISABLED_NotifyListOfObserversWithNaQos)
+#else
     TEST(NotifyAllObserverTest, NotifyListOfObserversWithNaQos)
+#endif
     {
         OCResourceHandle resourceHome = RegisterResource(std::string("/a/obs9"),
             std::string("core.obs"));
@@ -573,7 +597,11 @@ namespace OCPlatformTest
             interestedObservers, resourceResponse,OC::QualityOfService::NaQos));
     }
 
+#if defined (_MSC_VER)
+    TEST(NotifyAllObserverTest, DISABLED_NotifyListOfObserversWithHighQos)
+#else
     TEST(NotifyAllObserverTest, NotifyListOfObserversWithHighQos)
+#endif
     {
         OCResourceHandle resourceHome = RegisterResource(std::string("/a/obs10"),
             std::string("core.obs"));
@@ -588,7 +616,7 @@ namespace OCPlatformTest
     TEST(DeviceEntityHandlerTest, SetDefaultDeviceEntityHandler)
     {
         EXPECT_EQ(OC_STACK_OK, OCPlatform::setDefaultDeviceEntityHandler(entityHandler));
-        EXPECT_EQ(OC_STACK_OK, OCPlatform::setDefaultDeviceEntityHandler(NULL));
+        EXPECT_EQ(OC_STACK_OK, OCPlatform::setDefaultDeviceEntityHandler(nullptr));
     }
 
 
@@ -601,13 +629,22 @@ namespace OCPlatformTest
               CT_DEFAULT, &foundResource));
     }
 
+#if defined (_MSC_VER)
+    TEST(FindResourceTest, DISABLED_FindResourceNullResourceURI)
+#else
     TEST(FindResourceTest, FindResourceNullResourceURI)
+#endif
     {
       EXPECT_ANY_THROW(OCPlatform::findResource("", nullptr,
               CT_DEFAULT, &foundResource));
     }
 
+
+#if defined (_MSC_VER)
+    TEST(FindResourceTest, DISABLED_FindResourceNullResourceURI1)
+#else
     TEST(FindResourceTest, FindResourceNullResourceURI1)
+#endif
     {
       std::ostringstream requestURI;
       requestURI << OC_RSRVD_WELL_KNOWN_URI << "?rt=core.light";
@@ -615,7 +652,11 @@ namespace OCPlatformTest
               CT_DEFAULT, &foundResource));
     }
 
+#if defined (_MSC_VER)
+    TEST(FindResourceTest, DISABLED_FindResourceNullHost)
+#else
     TEST(FindResourceTest, FindResourceNullHost)
+#endif
     {
       std::ostringstream requestURI;
       requestURI << OC_RSRVD_WELL_KNOWN_URI << "?rt=core.light";
@@ -628,7 +669,7 @@ namespace OCPlatformTest
       std::ostringstream requestURI;
       requestURI << OC_RSRVD_WELL_KNOWN_URI << "?rt=core.light";
       EXPECT_THROW(OCPlatform::findResource("", requestURI.str(),
-              CT_DEFAULT, NULL), OC::OCException);
+              CT_DEFAULT, nullptr), OC::OCException);
     }
 
     TEST(FindResourceTest, DISABLED_FindResourceWithLowQoS)
@@ -679,7 +720,11 @@ namespace OCPlatformTest
                 OCPlatform::getDeviceInfo("", requestURI.str(), CT_DEFAULT, &receivedDeviceInfo));
     }
 
+#if defined (_MSC_VER)
+    TEST(GetDeviceInfoTest, DISABLED_GetDeviceInfoNullDeviceURI)
+#else
     TEST(GetDeviceInfoTest, GetDeviceInfoNullDeviceURI)
+#endif
     {
         PlatformConfig cfg;
         OCPlatform::Configure(cfg);
@@ -695,7 +740,7 @@ namespace OCPlatformTest
         std::ostringstream requestURI;
         requestURI << OC_MULTICAST_PREFIX << deviceDiscoveryURI;
         EXPECT_THROW(
-                OCPlatform::getDeviceInfo("", requestURI.str(), CT_DEFAULT, NULL),
+                OCPlatform::getDeviceInfo("", requestURI.str(), CT_DEFAULT, nullptr),
                 OC::OCException);
     }
 
@@ -778,7 +823,11 @@ namespace OCPlatformTest
                  CT_DEFAULT, &presenceHandler));
     }
 
+#if defined (_MSC_VER)
+    TEST(SubscribePresenceTest, DISABLED_SubscribePresenceWithNullHost)
+#else
     TEST(SubscribePresenceTest, SubscribePresenceWithNullHost)
+#endif
     {
         OCPlatform::OCPresenceHandle presenceHandle = nullptr;
 
@@ -786,12 +835,16 @@ namespace OCPlatformTest
                  CT_DEFAULT, &presenceHandler));
     }
 
+#if defined (_MSC_VER)
+    TEST(SubscribePresenceTest, DISABLED_SubscribePresenceWithNullPresenceHandler)
+#else
     TEST(SubscribePresenceTest, SubscribePresenceWithNullPresenceHandler)
+#endif
     {
         OCPlatform::OCPresenceHandle presenceHandle = nullptr;
 
         EXPECT_ANY_THROW(OCPlatform::subscribePresence(presenceHandle, nullptr,
-                 CT_DEFAULT, NULL));
+                 CT_DEFAULT, nullptr));
     }
 
     TEST(SubscribePresenceTest, DISABLED_SubscribePresenceWithResourceType)
@@ -802,7 +855,11 @@ namespace OCPlatformTest
                 OC_MULTICAST_IP, "core.light", CT_DEFAULT, &presenceHandler));
     }
 
+#if defined (_MSC_VER)
+    TEST(SubscribePresenceTest, DISABLED_SubscribePresenceWithNullResourceType)
+#else
     TEST(SubscribePresenceTest, SubscribePresenceWithNullResourceType)
+#endif
     {
         OCPlatform::OCPresenceHandle presenceHandle = nullptr;
 
@@ -862,7 +919,7 @@ namespace OCPlatformTest
         std::vector<std::string> di;
 
         EXPECT_ANY_THROW(OCPlatform::subscribeDevicePresence(presenceHandle,
-                        hostAddress, di, CT_DEFAULT, NULL));
+                        hostAddress, di, CT_DEFAULT, nullptr));
     }
 
     TEST(SubscribeDevicePresenceTest, DISABLED_UnsubscribePresenceWithValidHandle)
index 54fc76060ff4dbc2b61fc7b85ddaae3af05ccf03..c48a0369398b6e40862da1a61ee15bd3fd14f8bb 100644 (file)
@@ -373,8 +373,11 @@ namespace OCRepresentationEncodingTest
                 newSubRep.getValue<std::vector<uint8_t>>("BinaryAttr"));
         OCPayloadDestroy(cparsed);
     }
-
+#if defined (_MSC_VER)
+    TEST(RepresentationEncoding, DISABLED_OneDVectors)
+#else
     TEST(RepresentationEncoding, OneDVectors)
+#endif
     {
         // Setup
         OC::OCRepresentation startRep;
@@ -451,7 +454,11 @@ namespace OCRepresentationEncodingTest
         OCPayloadDestroy(cparsed);
     }
 
+#if defined (_MSC_VER)
+    TEST(RepresentationEncoding, DISABLED_TwoDVectors)
+#else
     TEST(RepresentationEncoding, TwoDVectors)
+#endif
     {
         // Setup
         OC::OCRepresentation startRep;
@@ -544,7 +551,11 @@ namespace OCRepresentationEncodingTest
         OCPayloadDestroy(cparsed);
     }
 
+#if defined (_MSC_VER)
+    TEST(RepresentationEncoding, DISABLED_TwoDVectorsJagged)
+#else
     TEST(RepresentationEncoding, TwoDVectorsJagged)
+#endif
     {
         // Setup
         OC::OCRepresentation startRep;
@@ -652,7 +663,11 @@ namespace OCRepresentationEncodingTest
         OCPayloadDestroy(cparsed);
     }
 
+#if defined (_MSC_VER)
+    TEST(RepresentationEncoding, DISABLED_ThreeDVectors)
+#else
     TEST(RepresentationEncoding, ThreeDVectors)
+#endif
     {
         // Setup
         OC::OCRepresentation startRep;
@@ -795,7 +810,11 @@ namespace OCRepresentationEncodingTest
         OCPayloadDestroy(cparsed);
     }
 
+#if defined (_MSC_VER)
+    TEST(RepresentationEncoding, DISABLED_ThreeDVectorsJagged)
+#else
     TEST(RepresentationEncoding, ThreeDVectorsJagged)
+#endif
     {
         // Setup
         OC::OCRepresentation startRep;
index 9d2684f94b9ffbffb70144efb76085810b213b48..d138dd211689dbd39b6622016dd9e40951be27c8 100644 (file)
@@ -87,9 +87,9 @@ unittests_src = [
 
 # TODO: Fix errors in the following Windows tests.
 if target_os in ['windows']:
-       unittests_src.remove('OCPlatformTest.cpp')
-       unittests_src.remove('OCRepresentationEncodingTest.cpp')
        if '12.0' == unittests_env['MSVC_VERSION']:
+               unittests_src.remove('OCPlatformTest.cpp')
+               unittests_src.remove('OCRepresentationEncodingTest.cpp')
                unittests_src.remove('OCRepresentationTest.cpp')
                unittests_src.remove('OCResourceTest.cpp')