1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <netinet/udp.h>
18 
19 #include <gtest/gtest.h>
20 
21 #if defined(__BIONIC__)
22   #define UDPHDR_USES_ANON_UNION
23 #elif defined(__GLIBC_PREREQ)
24   #if __GLIBC_PREREQ(2, 18)
25     #define UDPHDR_USES_ANON_UNION
26   #endif
27 #endif
28 
TEST(netinet_udp,compat)29 TEST(netinet_udp, compat) {
30 #if defined(UDPHDR_USES_ANON_UNION)
31     static_assert(offsetof(udphdr, uh_sport) == offsetof(udphdr, source), "udphdr::source");
32     static_assert(offsetof(udphdr, uh_dport) == offsetof(udphdr, dest), "udphdr::dest");
33     static_assert(offsetof(udphdr, uh_ulen) == offsetof(udphdr, len), "udphdr::len");
34     static_assert(offsetof(udphdr, uh_sum) == offsetof(udphdr, check), "udphdr::check");
35 
36     udphdr u;
37     u.uh_sport = 0x1111;
38     u.uh_dport = 0x2222;
39     u.uh_ulen = 0x3333;
40     u.uh_sum = 0x4444;
41     ASSERT_EQ(0x1111, u.source);
42     ASSERT_EQ(0x2222, u.dest);
43     ASSERT_EQ(0x3333, u.len);
44     ASSERT_EQ(0x4444, u.check);
45 #endif
46 }
47