testing_networklayers.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. #include "testing_networklayers.h"
  5. #include <open62541/server_config_default.h>
  6. #include <assert.h>
  7. #include <stdlib.h>
  8. #include "testing_clock.h"
  9. static UA_ByteString *vBuffer;
  10. static UA_ByteString sendBuffer;
  11. UA_StatusCode UA_Client_recvTesting_result = UA_STATUSCODE_GOOD;
  12. static UA_StatusCode
  13. dummyGetSendBuffer(UA_Connection *connection, size_t length, UA_ByteString *buf) {
  14. if(length > sendBuffer.length)
  15. return UA_STATUSCODE_BADCOMMUNICATIONERROR;
  16. *buf = sendBuffer;
  17. buf->length = length;
  18. return UA_STATUSCODE_GOOD;
  19. }
  20. static void
  21. dummyReleaseSendBuffer(UA_Connection *connection, UA_ByteString *buf) {
  22. }
  23. static UA_StatusCode
  24. dummySend(UA_Connection *connection, UA_ByteString *buf) {
  25. assert(connection != NULL);
  26. assert(buf != NULL);
  27. if(vBuffer) {
  28. UA_ByteString_deleteMembers(vBuffer);
  29. UA_ByteString_copy(buf, vBuffer);
  30. }
  31. return UA_STATUSCODE_GOOD;
  32. }
  33. static void
  34. dummyReleaseRecvBuffer(UA_Connection *connection, UA_ByteString *buf) {
  35. }
  36. static void
  37. dummyClose(UA_Connection *connection) {
  38. if(vBuffer)
  39. UA_ByteString_deleteMembers(vBuffer);
  40. UA_ByteString_deleteMembers(&sendBuffer);
  41. }
  42. UA_Connection createDummyConnection(size_t sendBufferSize,
  43. UA_ByteString *verificationBuffer) {
  44. vBuffer = verificationBuffer;
  45. UA_ByteString_allocBuffer(&sendBuffer, sendBufferSize);
  46. UA_Connection c;
  47. c.state = UA_CONNECTION_ESTABLISHED;
  48. c.config = UA_ConnectionConfig_default;
  49. c.channel = NULL;
  50. c.sockfd = 0;
  51. c.handle = NULL;
  52. c.incompleteChunk = UA_BYTESTRING_NULL;
  53. c.getSendBuffer = dummyGetSendBuffer;
  54. c.releaseSendBuffer = dummyReleaseSendBuffer;
  55. c.send = dummySend;
  56. c.recv = NULL;
  57. c.releaseRecvBuffer = dummyReleaseRecvBuffer;
  58. c.close = dummyClose;
  59. return c;
  60. }
  61. UA_UInt32 UA_Client_recvSleepDuration;
  62. UA_StatusCode (*UA_Client_recv)(UA_Connection *connection, UA_ByteString *response,
  63. UA_UInt32 timeout);
  64. UA_StatusCode
  65. UA_Client_recvTesting(UA_Connection *connection, UA_ByteString *response,
  66. UA_UInt32 timeout) {
  67. if(UA_Client_recvTesting_result != UA_STATUSCODE_GOOD) {
  68. UA_StatusCode temp = UA_Client_recvTesting_result;
  69. UA_Client_recvTesting_result = UA_STATUSCODE_GOOD;
  70. UA_fakeSleep(timeout);
  71. return temp;
  72. }
  73. UA_StatusCode res = UA_Client_recv(connection, response, timeout);
  74. if(res == UA_STATUSCODE_GOODNONCRITICALTIMEOUT)
  75. UA_fakeSleep(timeout);
  76. else
  77. UA_fakeSleep(UA_Client_recvSleepDuration);
  78. UA_Client_recvSleepDuration = 0;
  79. return res;
  80. }