object_tc.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (c) 2006-2025, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2025-07-18 kurisaW First commit
  9. */
  10. #include <utest.h>
  11. #include <rtthread.h>
  12. #include <string.h>
  13. /**
  14. * @brief Test case for verifying object name handling functionality
  15. *
  16. * @note This test suite validates:
  17. * 1. Proper truncation of long object names
  18. * 2. Correct NULL name handling
  19. * 3. Exact length name preservation
  20. * 4. Both static and dynamic object initialization
  21. * 5. Memory safety and boundary conditions
  22. */
  23. static void test_object_name_handling(void)
  24. {
  25. struct rt_object static_obj1;
  26. struct rt_object static_obj2;
  27. struct rt_object static_obj3;
  28. rt_object_t dyn_obj = RT_NULL;
  29. char test_name[RT_NAME_MAX + 5];
  30. for (int i = 0; i < sizeof(test_name) - 1; i++)
  31. {
  32. test_name[i] = 'A' + (i % 26);
  33. }
  34. test_name[sizeof(test_name) - 1] = '\0';
  35. /* Test 1: Static Object Initialization - Extra Long Name */
  36. rt_object_init(&static_obj1, RT_Object_Class_Thread, test_name);
  37. uassert_true(rt_strlen(static_obj1.name) <= RT_NAME_MAX - 1);
  38. uassert_true(static_obj1.name[RT_NAME_MAX - 1] == '\0');
  39. /* Test 2: Dynamic Object Allocation */
  40. dyn_obj = rt_object_allocate(RT_Object_Class_Thread, test_name);
  41. uassert_not_null(dyn_obj);
  42. if (dyn_obj)
  43. {
  44. uassert_true(rt_strlen(dyn_obj->name) <= RT_NAME_MAX - 1);
  45. uassert_true(dyn_obj->name[RT_NAME_MAX - 1] == '\0');
  46. rt_object_delete(dyn_obj);
  47. dyn_obj = RT_NULL;
  48. }
  49. /* Test 3: NULL Name Handling - Using New Static Object */
  50. rt_object_init(&static_obj2, RT_Object_Class_Thread, NULL);
  51. uassert_true(static_obj2.name[0] == '\0');
  52. /* Test 4: Dynamic Object with NULL Name */
  53. dyn_obj = rt_object_allocate(RT_Object_Class_Thread, NULL);
  54. uassert_not_null(dyn_obj);
  55. if (dyn_obj)
  56. {
  57. uassert_true(dyn_obj->name[0] == '\0');
  58. rt_object_delete(dyn_obj);
  59. dyn_obj = RT_NULL;
  60. }
  61. /* Test 5: Fixed-Length Name - Using Third Static Object */
  62. char exact_name[RT_NAME_MAX];
  63. rt_memset(exact_name, 'B', RT_NAME_MAX - 1);
  64. exact_name[RT_NAME_MAX - 1] = '\0';
  65. rt_object_init(&static_obj3, RT_Object_Class_Thread, exact_name);
  66. uassert_str_equal(static_obj3.name, exact_name);
  67. rt_object_detach(&static_obj1);
  68. rt_object_detach(&static_obj2);
  69. rt_object_detach(&static_obj3);
  70. }
  71. static rt_err_t testcase_init(void)
  72. {
  73. return RT_EOK;
  74. }
  75. static rt_err_t testcase_cleanup(void)
  76. {
  77. return RT_EOK;
  78. }
  79. static void test_object_suite(void)
  80. {
  81. UTEST_UNIT_RUN(test_object_name_handling);
  82. }
  83. UTEST_TC_EXPORT(test_object_suite, "testcases.kernel.object_test", testcase_init, testcase_cleanup, 10);