utils.cpp
Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 #include <opc/ua/protocol/datetime.h>
00012 #include <opc/ua/protocol/guid.h>
00013 #include <opc/ua/protocol/string_utils.h>
00014 #include <gtest/gtest.h>
00015 #include <stdexcept>
00016 
00017 using namespace testing;
00018 using namespace OpcUa;
00019 
00020 TEST(DateTime, FixedTimeT_to_DateTime)
00021 {
00022   time_t timet03_10_1980 = 321494400;
00023   unsigned usec = 1;
00024   const DateTime dateTime_03_10_1980 = DateTime::FromTimeT(timet03_10_1980, usec);
00025   ASSERT_EQ(dateTime_03_10_1980, 138495*24*3600LL*10000000LL + 10);
00026 }
00027 
00028 TEST(DateTime, FixedDateTime_to_TimeT)
00029 {
00030   const DateTime dateTime_03_10_1980(138495*24*3600LL*10000000LL);
00031   time_t timet_03_10_1980 = DateTime::ToTimeT(dateTime_03_10_1980);
00032   ASSERT_EQ(timet_03_10_1980, 321494400);
00033 }
00034 
00035 TEST(DateTime, ToTimeT_And_Back)
00036 {
00037   time_t timet = time(0);
00038   const time_t t = DateTime::ToTimeT(DateTime::FromTimeT(timet));
00039   ASSERT_EQ(t, timet);
00040 }
00041 
00042 TEST(DateTime, FromTimeT)
00043 {
00044   time_t t = 1;
00045   const time_t converted = OpcUa::DateTime::ToTimeT(OpcUa::DateTime::FromTimeT(t));
00046   ASSERT_EQ(t, converted);
00047 }
00048 
00049 TEST(DateTime, FromDateTime)
00050 {
00051   OpcUa::DateTime t = OpcUa::DateTime::Current();
00052   const OpcUa::DateTime converted = OpcUa::DateTime::FromTimeT(OpcUa::DateTime::ToTimeT(t));
00053   ASSERT_EQ(t/10000000LL*10000000LL, converted);
00054 }
00055 
00056 TEST(DateTime, ZeroDateTime_ThrowInvalidArgument)
00057 {
00058   DateTime t(0);
00059   ASSERT_THROW(OpcUa::DateTime::ToTimeT(t), std::invalid_argument);
00060 }
00061 
00062 TEST(DateTime, ZeroTimeT)
00063 {
00064   time_t t = 0;
00065   const DateTime converted = OpcUa::DateTime::FromTimeT(t);
00066   const DateTime expected(134774LL*24*3600*10000000LL);
00067   ASSERT_EQ(converted, expected);
00068 }
00069 
00070 
00071 TEST(Guid, ToString)
00072 {
00073   OpcUa::Guid guid;
00074   guid.Data1 = 0x01020304;
00075   guid.Data2 = 0x0506;
00076   guid.Data3 = 0x0708;
00077   guid.Data4[0] = 0x09;
00078   guid.Data4[1] = 0x0A;
00079   guid.Data4[2] = 0x0B;
00080   guid.Data4[3] = 0x0C;
00081   guid.Data4[4] = 0x0D;
00082   guid.Data4[5] = 0x0E;
00083   guid.Data4[6] = 0x0F;
00084   guid.Data4[7] = 0x10;
00085 
00086   std::string converted = OpcUa::ToString(guid);
00087   EXPECT_EQ(converted, "01020304-0506-0708-090A0B0C0D0E0F10");
00088 }
00089 
00090 TEST(Guid, FromString)
00091 {
00092   OpcUa::Guid guid;
00093   guid.Data1 = 0x01020304;
00094   guid.Data2 = 0x0506;
00095   guid.Data3 = 0x0708;
00096   guid.Data4[0] = 0x09;
00097   guid.Data4[1] = 0x0A;
00098   guid.Data4[2] = 0x0B;
00099   guid.Data4[3] = 0x0C;
00100   guid.Data4[4] = 0x0D;
00101   guid.Data4[5] = 0x0E;
00102   guid.Data4[6] = 0x0F;
00103   guid.Data4[7] = 0x10;
00104 
00105   const OpcUa::Guid converted = OpcUa::ToGuid("01020304-0506-0708-090A0B0C0D0E0F10");
00106   EXPECT_EQ(converted, guid);
00107 }
00108 
00109 TEST(Guid, InvalidString)
00110 {
00111   EXPECT_EQ(OpcUa::ToGuid("01020304-0506-0708-090A0B0C0D0E0F10S"), OpcUa::Guid()); // 36 symbols
00112   EXPECT_EQ(OpcUa::ToGuid("0102030400506007080090A0B0C0D0E0F10"), OpcUa::Guid());
00113 }
00114 
00115 TEST(NodeId, NumericToString)
00116 {
00117   OpcUa::NodeId id = OpcUa::NumericNodeId(1,2);
00118   std::string strId = OpcUa::ToString(id);
00119   ASSERT_EQ(strId, "ns=2;i=1;");
00120 }
00121 
00122 TEST(NodeId, NumericFromString)
00123 {
00124   OpcUa::NodeId expected = OpcUa::NumericNodeId(1,2);
00125   OpcUa::NodeId converted = OpcUa::ToNodeId("ns=2;i=1;");
00126   ASSERT_EQ(expected, converted);
00127 }
00128 
00129 TEST(NodeId, StringToString)
00130 {
00131   OpcUa::NodeId id = OpcUa::StringNodeId("string", 1);
00132   std::string strId = OpcUa::ToString(id);
00133   ASSERT_EQ(strId, "ns=1;s=string;");
00134 }
00135 
00136 TEST(NodeId, StringFromString)
00137 {
00138   OpcUa::NodeId expected = OpcUa::StringNodeId("str",2);
00139   OpcUa::NodeId converted = OpcUa::ToNodeId("ns=2;s=str;");
00140   ASSERT_EQ(expected, converted);
00141 }
00142 
00143 TEST(NodeId, GuidToString)
00144 {
00145   OpcUa::Guid guid;
00146   guid.Data1 = 0x01020304;
00147   guid.Data2 = 0x0506;
00148   guid.Data3 = 0x0708;
00149   guid.Data4[0] = 0x09;
00150   guid.Data4[1] = 0x0A;
00151   guid.Data4[2] = 0x0B;
00152   guid.Data4[3] = 0x0C;
00153   guid.Data4[4] = 0x0D;
00154   guid.Data4[5] = 0x0E;
00155   guid.Data4[6] = 0x0F;
00156   guid.Data4[7] = 0x10;
00157 
00158   OpcUa::NodeId id = OpcUa::GuidNodeId(guid, 1);
00159   std::string strId = OpcUa::ToString(id);
00160   ASSERT_EQ(strId, "ns=1;g=01020304-0506-0708-090A0B0C0D0E0F10;");
00161 }
00162 
00163 TEST(NodeId, GuidFromString)
00164 {
00165   OpcUa::Guid guid;
00166   guid.Data1 = 0x01020304;
00167   guid.Data2 = 0x0506;
00168   guid.Data3 = 0x0708;
00169   guid.Data4[0] = 0x09;
00170   guid.Data4[1] = 0x0A;
00171   guid.Data4[2] = 0x0B;
00172   guid.Data4[3] = 0x0C;
00173   guid.Data4[4] = 0x0D;
00174   guid.Data4[5] = 0x0E;
00175   guid.Data4[6] = 0x0F;
00176   guid.Data4[7] = 0x10;
00177 
00178   OpcUa::NodeId expected = OpcUa::GuidNodeId(guid,2);
00179   OpcUa::NodeId converted = OpcUa::ToNodeId("ns=1;g=01020304-0506-0708-090A0B0C0D0E0F10;");
00180   ASSERT_EQ(expected.Encoding, converted.Encoding);
00181 
00182   OpcUa::Guid expectedGuid = converted.GetGuidIdentifier();
00183   ASSERT_EQ(guid.Data1, expectedGuid.Data1);
00184 }
00185 
00186 TEST(NodeId, NamespaceUriToString)
00187 {
00188   OpcUa::NodeId id = OpcUa::NumericNodeId(1,2);
00189   id.SetNamespaceURI("uri");
00190 
00191   std::string strId = OpcUa::ToString(id);
00192   ASSERT_EQ(strId, "nsu=uri;ns=2;i=1;");
00193 }
00194 
00195 TEST(NodeId, NamespaceUriFromString)
00196 {
00197   OpcUa::NodeId expected = OpcUa::NumericNodeId(1,2);
00198   expected.SetNamespaceURI("uri");
00199 
00200   OpcUa::NodeId converted = OpcUa::ToNodeId("nsu=uri;ns=2;i=1;");
00201   ASSERT_EQ(converted, expected);
00202 }
00203 
00204 TEST(NodeId, ServerIndexToString)
00205 {
00206   OpcUa::NodeId id = OpcUa::NumericNodeId(1,2);
00207   id.SetServerIndex(3);
00208 
00209   std::string strId = OpcUa::ToString(id);
00210   ASSERT_EQ(strId, "srv=3;ns=2;i=1;");
00211 }
00212 
00213 TEST(NodeId, ServerIndexFromString)
00214 {
00215   OpcUa::NodeId expected = OpcUa::NumericNodeId(1,2);
00216   expected.SetServerIndex(3);
00217 
00218   OpcUa::NodeId converted = OpcUa::ToNodeId("srv=3;ns=2;i=1;");
00219   ASSERT_EQ(converted, expected);
00220 }
00221 
00222 TEST(NodeId, ServerIndexAndNamespaceUriToString)
00223 {
00224   OpcUa::NodeId id = OpcUa::NumericNodeId(1,2);
00225   id.SetServerIndex(3);
00226   id.SetNamespaceURI("uri");
00227 
00228   std::string strId = OpcUa::ToString(id);
00229   ASSERT_EQ(strId, "srv=3;nsu=uri;ns=2;i=1;");
00230 }
00231 
00232 TEST(NodeId, ServerIndexAndNamespaceUriString)
00233 {
00234   OpcUa::NodeId expected = OpcUa::NumericNodeId(1,2);
00235   expected.SetServerIndex(3);
00236   expected.SetNamespaceURI("uri");
00237 
00238   OpcUa::NodeId converted = OpcUa::ToNodeId("srv=3;nsu=uri;ns=2;i=1;");
00239   ASSERT_EQ(converted, expected);
00240 }
00241 
00242 TEST(NodeId, WithDefaultNamespace)
00243 {
00244   OpcUa::NodeId expected = OpcUa::NumericNodeId(1, 2);
00245   OpcUa::NodeId converted = OpcUa::ToNodeId("i=1;", 2);
00246   ASSERT_EQ(expected, converted);
00247 }


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Sat Jun 8 2019 18:24:57