testing_networklayers.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. UA_ByteString *vBuffer;
  10. UA_StatusCode UA_Client_recvTesting_result = UA_STATUSCODE_GOOD;
  11. static UA_StatusCode
  12. dummyGetSendBuffer(UA_Connection *connection, size_t length, UA_ByteString *buf) {
  13. buf->data = (length == 0) ? NULL : (UA_Byte*)UA_malloc(length);
  14. buf->length = length;
  15. return UA_STATUSCODE_GOOD;
  16. }
  17. static void
  18. dummyReleaseSendBuffer(UA_Connection *connection, UA_ByteString *buf) {
  19. UA_ByteString_deleteMembers(buf);
  20. }
  21. static UA_StatusCode
  22. dummySend(UA_Connection *connection, UA_ByteString *buf) {
  23. assert(connection != NULL);
  24. assert(buf != NULL);
  25. UA_ByteString_deleteMembers(vBuffer);
  26. UA_ByteString_copy(buf, vBuffer);
  27. UA_ByteString_deleteMembers(buf);
  28. return UA_STATUSCODE_GOOD;
  29. }
  30. static void
  31. dummyReleaseRecvBuffer(UA_Connection *connection, UA_ByteString *buf) {
  32. }
  33. static void
  34. dummyClose(UA_Connection *connection) {
  35. UA_ByteString_deleteMembers(vBuffer);
  36. }
  37. UA_Connection createDummyConnection(UA_ByteString *verificationBuffer) {
  38. assert(verificationBuffer != NULL);
  39. vBuffer = verificationBuffer;
  40. UA_Connection c;
  41. c.state = UA_CONNECTION_ESTABLISHED;
  42. c.localConf = UA_ConnectionConfig_default;
  43. c.remoteConf = UA_ConnectionConfig_default;
  44. c.channel = NULL;
  45. c.sockfd = 0;
  46. c.handle = NULL;
  47. c.incompleteMessage = UA_BYTESTRING_NULL;
  48. c.getSendBuffer = dummyGetSendBuffer;
  49. c.releaseSendBuffer = dummyReleaseSendBuffer;
  50. c.send = dummySend;
  51. c.recv = NULL;
  52. c.releaseRecvBuffer = dummyReleaseRecvBuffer;
  53. c.close = dummyClose;
  54. return c;
  55. }
  56. UA_UInt32 UA_Client_recvSleepDuration;
  57. UA_StatusCode (*UA_Client_recv)(UA_Connection *connection, UA_ByteString *response,
  58. UA_UInt32 timeout);
  59. UA_StatusCode
  60. UA_Client_recvTesting(UA_Connection *connection, UA_ByteString *response,
  61. UA_UInt32 timeout) {
  62. if(UA_Client_recvTesting_result != UA_STATUSCODE_GOOD) {
  63. UA_StatusCode temp = UA_Client_recvTesting_result;
  64. UA_Client_recvTesting_result = UA_STATUSCODE_GOOD;
  65. UA_fakeSleep(timeout);
  66. return temp;
  67. }
  68. UA_StatusCode res = UA_Client_recv(connection, response, timeout);
  69. if(res == UA_STATUSCODE_GOODNONCRITICALTIMEOUT)
  70. UA_fakeSleep(timeout);
  71. else
  72. UA_fakeSleep(UA_Client_recvSleepDuration);
  73. UA_Client_recvSleepDuration = 0;
  74. return res;
  75. }