55 */
66
77#include <unistd.h>
8+ #include <errno.h>
9+ #include <string.h>
810
911#include <zephyr/ztest.h>
12+ #include <zephyr/net/hostname.h>
1013
1114ZTEST (net , test_gethostname )
1215{
@@ -19,3 +22,87 @@ ZTEST(net, test_gethostname)
1922 zassert_equal (strcmp (hostname , CONFIG_NET_HOSTNAME ), 0 ,
2023 "gethostname() returned unexpected hostname: %s" , hostname );
2124}
25+
26+ ZTEST (net , test_gethostname_buffer_too_small )
27+ {
28+ char small_hostname [2 ];
29+ int ret ;
30+
31+ ret = gethostname (small_hostname , sizeof (small_hostname ));
32+ /* Should fail with buffer too small */
33+ zassert_equal (ret , -1 , "gethostname() should fail with small buffer" );
34+ zassert_equal (errno , ENAMETOOLONG , "Expected ENAMETOOLONG, got %d" , errno );
35+ }
36+
37+ ZTEST (net , test_gethostname_zero_length )
38+ {
39+ char hostname [CONFIG_NET_HOSTNAME_MAX_LEN + 1 ];
40+ int ret ;
41+
42+ ret = gethostname (hostname , 0 );
43+ /* Should fail with zero length */
44+ zassert_equal (ret , -1 , "gethostname() should fail with zero length" );
45+ zassert_equal (errno , EINVAL , "Expected EINVAL, got %d" , errno );
46+ }
47+
48+ ZTEST (posix_net , test_hostname_max_len_consistency )
49+ {
50+ /* Verify that CONFIG_NET_HOSTNAME_MAX_LEN is properly defined and > 0 */
51+ zassert_true (CONFIG_NET_HOSTNAME_MAX_LEN > 0 ,
52+ "CONFIG_NET_HOSTNAME_MAX_LEN must be positive" );
53+
54+ /* Verify it can hold at least the configured hostname */
55+ zassert_true (CONFIG_NET_HOSTNAME_MAX_LEN >= strlen (CONFIG_NET_HOSTNAME ),
56+ "CONFIG_NET_HOSTNAME_MAX_LEN too small for CONFIG_NET_HOSTNAME" );
57+
58+ #ifdef CONFIG_POSIX_HOST_NAME_MAX
59+ /* If POSIX is enabled, verify consistency */
60+ zassert_true (CONFIG_POSIX_HOST_NAME_MAX >= CONFIG_NET_HOSTNAME_MAX_LEN ,
61+ "POSIX_HOST_NAME_MAX should be >= NET_HOSTNAME_MAX_LEN" );
62+ #endif
63+ }
64+
65+ #ifdef CONFIG_NET_HOSTNAME_DYNAMIC
66+ ZTEST (posix_net , test_gethostname_dynamic_update )
67+ {
68+ char hostname [CONFIG_NET_HOSTNAME_MAX_LEN + 1 ];
69+ char original_hostname [CONFIG_NET_HOSTNAME_MAX_LEN + 1 ];
70+ char test_hostname [] = "test-dynamic" ;
71+ int ret ;
72+
73+ /* Get original hostname */
74+ ret = gethostname (original_hostname , sizeof (original_hostname ));
75+ zassert_equal (ret , 0 , "Failed to get original hostname" );
76+
77+ /* Set new hostname */
78+ ret = net_hostname_set (test_hostname , strlen (test_hostname ));
79+ zassert_equal (ret , 0 , "Failed to set hostname" );
80+
81+ /* Verify hostname changed */
82+ ret = gethostname (hostname , sizeof (hostname ));
83+ zassert_equal (ret , 0 , "Failed to get hostname after update" );
84+ zassert_equal (strcmp (hostname , test_hostname ), 0 ,
85+ "Hostname not updated correctly" );
86+
87+ /* Restore original hostname */
88+ ret = net_hostname_set (original_hostname , strlen (original_hostname ));
89+ zassert_equal (ret , 0 , "Failed to restore original hostname" );
90+ }
91+ #endif /* CONFIG_NET_HOSTNAME_DYNAMIC */
92+
93+ #ifdef CONFIG_NET_HOSTNAME_UNIQUE_UPDATE
94+ ZTEST (net , test_gethostname_with_unique_update )
95+ {
96+ char hostname [CONFIG_NET_HOSTNAME_MAX_LEN + 1 ];
97+ int ret ;
98+
99+ /* This test validates that gethostname works when NET_HOSTNAME_UNIQUE_UPDATE is enabled */
100+ ret = gethostname (hostname , sizeof (hostname ));
101+ zassert_equal (ret , 0 , "gethostname() failed with NET_HOSTNAME_UNIQUE_UPDATE enabled" );
102+
103+ /* Hostname should contain the configured name possibly with unique suffix */
104+ zassert_true (strlen (hostname ) > 0 , "Hostname should not be empty" );
105+ zassert_true (strlen (hostname ) <= CONFIG_NET_HOSTNAME_MAX_LEN ,
106+ "Hostname length exceeds maximum" );
107+ }
108+ #endif /* CONFIG_NET_HOSTNAME_UNIQUE_UPDATE */
0 commit comments