testing_networklayers.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 <stdlib.h>
  5. #include <assert.h>
  6. #include "testing_networklayers.h"
  7. #include "testing_clock.h"
  8. #include "ua_config_default.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.localConf = UA_ConnectionConfig_default;
  49. c.remoteConf = UA_ConnectionConfig_default;
  50. c.channel = NULL;
  51. c.sockfd = 0;
  52. c.handle = NULL;
  53. c.incompleteMessage = UA_BYTESTRING_NULL;
  54. c.getSendBuffer = dummyGetSendBuffer;
  55. c.releaseSendBuffer = dummyReleaseSendBuffer;
  56. c.send = dummySend;
  57. c.recv = NULL;
  58. c.releaseRecvBuffer = dummyReleaseRecvBuffer;
  59. c.close = dummyClose;
  60. return c;
  61. }
  62. UA_UInt32 UA_Client_recvSleepDuration;
  63. UA_StatusCode (*UA_Client_recv)(UA_Connection *connection, UA_ByteString *response,
  64. UA_UInt32 timeout);
  65. UA_StatusCode
  66. UA_Client_recvTesting(UA_Connection *connection, UA_ByteString *response,
  67. UA_UInt32 timeout) {
  68. if(UA_Client_recvTesting_result != UA_STATUSCODE_GOOD) {
  69. UA_StatusCode temp = UA_Client_recvTesting_result;
  70. UA_Client_recvTesting_result = UA_STATUSCODE_GOOD;
  71. UA_fakeSleep(timeout);
  72. return temp;
  73. }
  74. UA_StatusCode res = UA_Client_recv(connection, response, timeout);
  75. if(res == UA_STATUSCODE_GOODNONCRITICALTIMEOUT)
  76. UA_fakeSleep(timeout);
  77. else
  78. UA_fakeSleep(UA_Client_recvSleepDuration);
  79. UA_Client_recvSleepDuration = 0;
  80. return res;
  81. }