test_fspm.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*********************************************************************
  2. * _ _ _
  3. * _ __ | |_ _ | | __ _ | |__ ___
  4. * | '__|| __|(_)| | / _` || '_ \ / __|
  5. * | | | |_ _ | || (_| || |_) |\__ \
  6. * |_| \__|(_)|_| \__,_||_.__/ |___/
  7. *
  8. * www.rt-labs.com
  9. * Copyright 2018 rt-labs AB, Sweden.
  10. *
  11. * This software is dual-licensed under GPLv3 and a commercial
  12. * license. See the file LICENSE.md distributed with this software for
  13. * full license information.
  14. ********************************************************************/
  15. #include "utils_for_testing.h"
  16. #include "mocks.h"
  17. #include "pf_includes.h"
  18. #include <gtest/gtest.h>
  19. class FspmTest : public PnetIntegrationTest
  20. {
  21. };
  22. class FspmUnitTest : public PnetUnitTest
  23. {
  24. };
  25. TEST_F (FspmUnitTest, FspmCheckValidateConfiguration)
  26. {
  27. pnet_cfg_t cfg;
  28. /* Known good values */
  29. memset (&cfg, 0, sizeof (pnet_cfg_t));
  30. cfg.tick_us = 1000;
  31. cfg.min_device_interval = 1;
  32. cfg.im_0_data.im_supported = 0;
  33. cfg.if_cfg.main_netif_name = "eth0";
  34. cfg.num_physical_ports = PNET_MAX_PHYSICAL_PORTS;
  35. EXPECT_EQ (pf_fspm_validate_configuration (&cfg), 0);
  36. /* Check pointer validity */
  37. EXPECT_EQ (pf_fspm_validate_configuration (NULL), -1);
  38. /* Check number of ports */
  39. cfg.num_physical_ports = PNET_MAX_PHYSICAL_PORTS + 1;
  40. EXPECT_EQ (pf_fspm_validate_configuration (&cfg), -1);
  41. cfg.num_physical_ports = 0;
  42. EXPECT_EQ (pf_fspm_validate_configuration (&cfg), -1);
  43. cfg.num_physical_ports = 1;
  44. EXPECT_EQ (pf_fspm_validate_configuration (&cfg), 0);
  45. cfg.num_physical_ports = PNET_MAX_PHYSICAL_PORTS;
  46. EXPECT_EQ (pf_fspm_validate_configuration (&cfg), 0);
  47. /* Check minimum stack update interval */
  48. cfg.min_device_interval = 0;
  49. EXPECT_EQ (pf_fspm_validate_configuration (&cfg), -1);
  50. cfg.min_device_interval = 0x1000;
  51. EXPECT_EQ (pf_fspm_validate_configuration (&cfg), 0);
  52. cfg.min_device_interval = 0x1001;
  53. EXPECT_EQ (pf_fspm_validate_configuration (&cfg), -1);
  54. cfg.min_device_interval = 0xFFFF;
  55. EXPECT_EQ (pf_fspm_validate_configuration (&cfg), -1);
  56. cfg.min_device_interval = 1;
  57. /* Check supported I&M fields */
  58. cfg.im_0_data.im_supported = PNET_SUPPORTED_IM1;
  59. EXPECT_EQ (pf_fspm_validate_configuration (&cfg), 0);
  60. cfg.im_0_data.im_supported = PNET_SUPPORTED_IM1 | PNET_SUPPORTED_IM2;
  61. EXPECT_EQ (pf_fspm_validate_configuration (&cfg), 0);
  62. cfg.im_0_data.im_supported = PNET_SUPPORTED_IM1 | PNET_SUPPORTED_IM2 |
  63. PNET_SUPPORTED_IM3 | PNET_SUPPORTED_IM4;
  64. EXPECT_EQ (pf_fspm_validate_configuration (&cfg), 0);
  65. cfg.im_0_data.im_supported = PNET_SUPPORTED_IM4;
  66. EXPECT_EQ (pf_fspm_validate_configuration (&cfg), 0);
  67. cfg.im_0_data.im_supported = 1;
  68. EXPECT_EQ (pf_fspm_validate_configuration (&cfg), -1);
  69. cfg.im_0_data.im_supported = 0x0020;
  70. EXPECT_EQ (pf_fspm_validate_configuration (&cfg), -1);
  71. cfg.im_0_data.im_supported = 0x8000;
  72. EXPECT_EQ (pf_fspm_validate_configuration (&cfg), -1);
  73. cfg.im_0_data.im_supported = 0xFFFF;
  74. EXPECT_EQ (pf_fspm_validate_configuration (&cfg), -1);
  75. cfg.im_0_data.im_supported = 0;
  76. }
  77. TEST_F (FspmTest, FspmGetImLocation)
  78. {
  79. const char expected[PNET_LOCATION_MAX_SIZE] = " ";
  80. char actual[PNET_LOCATION_MAX_SIZE];
  81. memset (actual, 0xff, sizeof (actual));
  82. /* Note that as loading from file failed in pf_fspm_init(), location was set
  83. * to its default value, which is all spaces.
  84. */
  85. pf_fspm_get_im_location (net, actual);
  86. EXPECT_STREQ (actual, expected);
  87. EXPECT_EQ (strlen (actual), 22u);
  88. }
  89. TEST_F (FspmTest, FspmSaveImLocation)
  90. {
  91. const char written[PNET_LOCATION_MAX_SIZE] = "location of device";
  92. const char expected[PNET_LOCATION_MAX_SIZE] = "location of device ";
  93. char actual[PNET_LOCATION_MAX_SIZE];
  94. memset (actual, 0xff, sizeof (actual));
  95. /* Note that extra spaces are added at the end */
  96. pf_fspm_save_im_location (net, written);
  97. /* The file save operation below is performed by the background worker
  98. * during normal execution of the stack.
  99. */
  100. pf_fspm_save_im (net);
  101. pf_fspm_get_im_location (net, actual);
  102. EXPECT_STREQ (actual, expected);
  103. EXPECT_EQ (strlen (actual), 22u);
  104. EXPECT_STREQ (mock_file_data.filename, PF_FILENAME_IM);
  105. }
  106. TEST_F (FspmTest, FspmSaveImLocationShouldAddTermination)
  107. {
  108. char not_terminated[PNET_LOCATION_MAX_SIZE];
  109. char actual[PNET_LOCATION_MAX_SIZE];
  110. memset (actual, 0xff, sizeof (actual));
  111. memset (not_terminated, 'n', sizeof (not_terminated));
  112. pf_fspm_save_im_location (net, not_terminated);
  113. /* The file save operation below is performed by the background worker
  114. * during normal execution of the stack.
  115. */
  116. pf_fspm_save_im (net);
  117. pf_fspm_get_im_location (net, actual);
  118. EXPECT_EQ (actual[sizeof (actual) - 1], '\0');
  119. EXPECT_EQ (strlen (actual), 22u);
  120. EXPECT_STREQ (mock_file_data.filename, PF_FILENAME_IM);
  121. }
  122. TEST_F (FspmTest, FspmSaveImLocationShouldTruncateLargeStrings)
  123. {
  124. const char large[] = "123456789012345678901234567890";
  125. const char expected[] = "1234567890123456789012";
  126. char actual[PNET_LOCATION_MAX_SIZE];
  127. memset (actual, 0xff, sizeof (actual));
  128. pf_fspm_save_im_location (net, large);
  129. /* The file save operation below is performed by the background worker
  130. * during normal execution of the stack.
  131. */
  132. pf_fspm_save_im (net);
  133. pf_fspm_get_im_location (net, actual);
  134. EXPECT_STREQ (actual, expected);
  135. EXPECT_EQ (strlen (actual), 22u);
  136. EXPECT_STREQ (mock_file_data.filename, PF_FILENAME_IM);
  137. }