@@ -36,8 +36,10 @@ START_TEST(test_route_nexthop_api_set_get_all)
3636 _nl_auto_nl_addr struct nl_addr * via6 = NULL ;
3737 _nl_auto_nl_addr struct nl_addr * mpls = NULL ;
3838 _nl_auto_rtnl_nexthop struct rtnl_nexthop * clone = NULL ;
39+ _nl_auto_rtnl_nexthop struct rtnl_nexthop * clone_with_encap = NULL ;
3940 struct rtnl_nh_encap * encap = NULL ;
4041 struct rtnl_nh_encap * got = NULL ;
42+ struct rtnl_nh_encap * encap_clone = NULL ;
4143 uint32_t realms = 0xAABBCCDDu ;
4244 char flags_buf [64 ];
4345 int flags_parsed ;
@@ -123,8 +125,7 @@ START_TEST(test_route_nexthop_api_set_get_all)
123125 ck_assert_ptr_nonnull (encap );
124126
125127 /* Invalid: missing destination labels */
126- ck_assert_int_eq (rtnl_nh_encap_mpls (encap , NULL , 0 ),
127- - NLE_INVAL );
128+ ck_assert_int_eq (rtnl_nh_encap_mpls (encap , NULL , 0 ), - NLE_INVAL );
128129
129130 /* Valid MPLS encap: push label 100 with TTL 64 */
130131 ck_assert_int_eq (nl_addr_parse ("100" , AF_MPLS , & mpls ), 0 );
@@ -139,6 +140,28 @@ START_TEST(test_route_nexthop_api_set_get_all)
139140 ck_assert_ptr_nonnull (rtnl_nh_get_encap_mpls_dst (got ));
140141 ck_assert_uint_eq (rtnl_nh_get_encap_mpls_ttl (got ), 64 );
141142
143+ /* Exercise rtnl_nh_encap_clone() directly */
144+ encap_clone = rtnl_nh_encap_clone (encap );
145+ ck_assert_ptr_nonnull (encap_clone );
146+ ck_assert_ptr_nonnull (rtnl_nh_get_encap_mpls_dst (encap_clone ));
147+ ck_assert_int_eq (
148+ nl_addr_cmp (rtnl_nh_get_encap_mpls_dst (encap_clone ), mpls ), 0 );
149+ ck_assert_uint_eq (rtnl_nh_get_encap_mpls_ttl (encap_clone ), 64 );
150+ /* Free the cloned encap explicitly */
151+ rtnl_nh_encap_free (encap_clone );
152+ encap_clone = NULL ;
153+
154+ /* Exercise nexthop clone with encap: encap should be deep-cloned */
155+ clone_with_encap = rtnl_route_nh_clone (nh );
156+ ck_assert_ptr_nonnull (clone_with_encap );
157+ encap_clone = rtnl_route_nh_get_encap (clone_with_encap );
158+ ck_assert_ptr_nonnull (encap_clone );
159+ /* The encap on the clone must not be the same pointer */
160+ ck_assert_ptr_ne (encap_clone , encap );
161+ ck_assert_int_eq (nl_addr_cmp (rtnl_nh_get_encap_mpls_dst (encap_clone ), mpls ),
162+ 0 );
163+ ck_assert_uint_eq (rtnl_nh_get_encap_mpls_ttl (encap_clone ), 64 );
164+
142165 /* Clear encap and verify it is removed */
143166 ck_assert_int_eq (rtnl_route_nh_set_encap (nh , NULL ), 0 );
144167 ck_assert_ptr_eq (rtnl_route_nh_get_encap (nh ), NULL );
0 commit comments