00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <opc/ua/protocol/input_from_buffer.h>
00021
00022 #include <gtest/gtest.h>
00023
00024
00025 class InputFromBuffer : public testing::Test
00026 {
00027 protected:
00028 InputFromBuffer()
00029 {
00030 const char data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
00031 SourceBuffer = std::vector<char>(data, data + sizeof(data));
00032 }
00033 std::vector<char> SourceBuffer;
00034 };
00035
00036 TEST_F(InputFromBuffer, CanBeCreated)
00037 {
00038 OpcUa::InputFromBuffer input(&SourceBuffer[0], SourceBuffer.size());
00039 }
00040
00041 TEST_F(InputFromBuffer, ThrowsIfNullBuffer)
00042 {
00043 ASSERT_THROW(OpcUa::InputFromBuffer(nullptr, 100500), std::invalid_argument);
00044 }
00045
00046 TEST_F(InputFromBuffer, ThrowsIfZeroSize)
00047 {
00048 ASSERT_THROW(OpcUa::InputFromBuffer((char*)1, 0), std::invalid_argument);
00049 }
00050
00051 TEST_F(InputFromBuffer, ReadReturnsZeroWhenReadZero)
00052 {
00053 OpcUa::InputFromBuffer input(&SourceBuffer[0], SourceBuffer.size());
00054
00055 std::vector<char> targetBuf{9,8,7};
00056 std::vector<char> expectedBuf{9,8,7};
00057 const size_t sizeToRead = 0;
00058
00059 ASSERT_EQ(input.Receive(&targetBuf[0], sizeToRead), 0);
00060 ASSERT_EQ(targetBuf, expectedBuf);
00061 }
00062
00063 TEST_F(InputFromBuffer, ReadSmallBuffer)
00064 {
00065 OpcUa::InputFromBuffer input(&SourceBuffer[0], SourceBuffer.size());
00066
00067 std::vector<char> targetBuf{9,8,7};
00068 const std::vector<char> expectedBuf{0, 1, 2};
00069 size_t revceivedSize = 0;
00070 ASSERT_NO_THROW(revceivedSize = input.Receive(&targetBuf[0], 3));
00071 ASSERT_EQ(revceivedSize, 3);
00072 ASSERT_EQ(targetBuf, expectedBuf);
00073 }
00074
00075 TEST_F(InputFromBuffer, ReadLargeBuffer)
00076 {
00077 OpcUa::InputFromBuffer input(&SourceBuffer[0], SourceBuffer.size());
00078
00079 const std::vector<char> expectedBuf{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11};
00080 std::vector<char> targetBuf(11, 11);
00081 size_t revceivedSize = 0;
00082 ASSERT_NO_THROW(revceivedSize = input.Receive(&targetBuf[0], targetBuf.size()));
00083 ASSERT_EQ(revceivedSize, SourceBuffer.size());
00084 ASSERT_EQ(targetBuf, expectedBuf);
00085 }
00086
00087 TEST_F(InputFromBuffer, ReadWithLittlePortions)
00088 {
00089 OpcUa::InputFromBuffer input(&SourceBuffer[0], SourceBuffer.size());
00090 size_t revceivedSize = 0;
00091
00092
00093 std::vector<char> targetBuf1(3, 11);
00094 ASSERT_NO_THROW(revceivedSize = input.Receive(&targetBuf1[0], targetBuf1.size()));
00095 ASSERT_EQ(revceivedSize, 3);
00096 const std::vector<char> expectedBuf1{0,1,2};
00097 ASSERT_EQ(targetBuf1, expectedBuf1);
00098
00099
00100
00101 std::vector<char> targetBuf2(3, 11);
00102 ASSERT_NO_THROW(revceivedSize = input.Receive(&targetBuf2[0], targetBuf2.size()));
00103 ASSERT_EQ(revceivedSize, 3);
00104 const std::vector<char> expectedBuf2{3,4,5};
00105 ASSERT_EQ(targetBuf2, expectedBuf2);
00106
00107
00108 std::vector<char> targetBuf3(3, 11);
00109 ASSERT_NO_THROW(revceivedSize = input.Receive(&targetBuf3[0], targetBuf3.size()));
00110 ASSERT_EQ(revceivedSize, 3);
00111 const std::vector<char> expectedBuf3{6,7,8};
00112 ASSERT_EQ(targetBuf3, expectedBuf3);
00113
00114
00115 std::vector<char> targetBuf4(3, 11);
00116 ASSERT_NO_THROW(revceivedSize = input.Receive(&targetBuf4[0], targetBuf4.size()));
00117 ASSERT_EQ(revceivedSize, 1);
00118 const std::vector<char> expectedBuf4{9, 11, 11};
00119 ASSERT_EQ(targetBuf4, expectedBuf4);
00120
00121
00122 std::vector<char> targetBuf5(3, 11);
00123 ASSERT_NO_THROW(revceivedSize = input.Receive(&targetBuf5[0], targetBuf5.size()));
00124 ASSERT_EQ(revceivedSize, 0);
00125 const std::vector<char> expectedBuf5{11,11,11};
00126 ASSERT_EQ(targetBuf5, expectedBuf5);
00127 }