bionic/tests/netinet_udp_test.cpp
Elliott Hughes f8a2243ebe <netinet/udp.h> should include <linux/udp.h>.
The comment about "other stuff" referred to pre-uapi headers. Everything
in the current <linux/udp.h> should be exposed to userspace. The only
problem is that BSD and Linux use different names for the members of
struct udphdr. We can move the Linux udphdr out of the way and use an
anonymous union to get the best of both worlds. (Though unfortunately
this means that code that includes <linux/udp.h> directly instead of
using <netinet/udp.h> now won't have any definition of struct udphdr.
We've taken the stance in the past that you shouldn't include a linux/
header if there's a standard equivalent --- you should rely on us
transitively including it for you.)

Change-Id: Ie625892441b0edd8df3b76d3fcf2cbe299077bc4
2015-09-22 12:34:13 -07:00

47 lines
1.5 KiB
C++

/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <netinet/udp.h>
#include <gtest/gtest.h>
#if defined(__BIONIC__)
#define UDPHDR_USES_ANON_UNION
#elif defined(__GLIBC_PREREQ)
#if __GLIBC_PREREQ(2, 18)
#define UDPHDR_USES_ANON_UNION
#endif
#endif
TEST(netinet_udp, compat) {
#if defined(UDPHDR_USES_ANON_UNION)
static_assert(offsetof(udphdr, uh_sport) == offsetof(udphdr, source), "udphdr::source");
static_assert(offsetof(udphdr, uh_dport) == offsetof(udphdr, dest), "udphdr::dest");
static_assert(offsetof(udphdr, uh_ulen) == offsetof(udphdr, len), "udphdr::len");
static_assert(offsetof(udphdr, uh_sum) == offsetof(udphdr, check), "udphdr::check");
udphdr u;
u.uh_sport = 0x1111;
u.uh_dport = 0x2222;
u.uh_ulen = 0x3333;
u.uh_sum = 0x4444;
ASSERT_EQ(0x1111, u.source);
ASSERT_EQ(0x2222, u.dest);
ASSERT_EQ(0x3333, u.len);
ASSERT_EQ(0x4444, u.check);
#endif
}