Fix scope ids for link-local IPv6 addresses from getifaddrs(3).
Bug: http://b/27219454
Change-Id: I7a166ff5553565f7afdab18dd2c703af4d475ab4
diff --git a/tests/ifaddrs_test.cpp b/tests/ifaddrs_test.cpp
index 2c34633..c3f0273 100644
--- a/tests/ifaddrs_test.cpp
+++ b/tests/ifaddrs_test.cpp
@@ -93,8 +93,7 @@
std::vector<std::string> sys_class_net;
{
- auto dir_deleter = [](DIR* handle) { if (handle) closedir(handle); };
- std::unique_ptr<DIR, decltype(dir_deleter)> d(opendir("/sys/class/net"), dir_deleter);
+ std::unique_ptr<DIR, decltype(&closedir)> d(opendir("/sys/class/net"), closedir);
ASSERT_TRUE(d != nullptr);
dirent* dir;
while ((dir = readdir(d.get())) != nullptr) {
@@ -150,6 +149,7 @@
for (auto ub = addrs.upper_bound(it->first); it != ub; ++it) {
if (it->second == addr) {
found = true;
+ break;
}
}
EXPECT_TRUE(found) << if_name;
@@ -220,3 +220,21 @@
freeifaddrs(addrs);
}
+
+TEST(ifaddrs, inet6_scope_ids) {
+ ifaddrs* addrs;
+ ASSERT_EQ(0, getifaddrs(&addrs));
+
+ for (ifaddrs* ifa = addrs; ifa != nullptr; ifa = ifa->ifa_next) {
+ if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
+ sockaddr_in6* sa6 = reinterpret_cast<sockaddr_in6*>(ifa->ifa_addr);
+ // Any link-local IPv6 address should have a scope id. (http://b/27219454.)
+ // 0 isn't a valid interface index, so that would mean the scope id wasn't set.
+ if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr) || IN6_IS_ADDR_MC_LINKLOCAL(&sa6->sin6_addr)) {
+ ASSERT_NE(sa6->sin6_scope_id, 0U);
+ }
+ }
+ }
+
+ freeifaddrs(addrs);
+}