Chenbo Feng | 837ddfc | 2018-05-08 13:45:08 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2018 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 <string> |
| 18 | |
| 19 | #include <fcntl.h> |
| 20 | #include <inttypes.h> |
| 21 | #include <limits.h> |
| 22 | #include <linux/inet_diag.h> |
| 23 | #include <linux/sock_diag.h> |
| 24 | #include <net/if.h> |
| 25 | #include <sys/socket.h> |
| 26 | #include <sys/types.h> |
| 27 | #include <unistd.h> |
| 28 | |
| 29 | #include <gtest/gtest.h> |
| 30 | |
| 31 | #include <cutils/qtaguid.h> |
| 32 | |
| 33 | #include <android-base/stringprintf.h> |
| 34 | #include <android-base/strings.h> |
| 35 | |
| 36 | #include "bpf/BpfUtils.h" |
| 37 | |
| 38 | using namespace android::bpf; |
| 39 | |
| 40 | using android::base::unique_fd; |
| 41 | using android::netdutils::status::ok; |
| 42 | |
| 43 | namespace android { |
| 44 | namespace bpf { |
| 45 | |
| 46 | // Use the upper limit of uid to avoid conflict with real app uids. We can't use UID_MAX because |
| 47 | // it's -1, which is INVALID_UID. |
| 48 | constexpr uid_t TEST_UID = UID_MAX - 1; |
| 49 | constexpr uint32_t TEST_TAG = 42; |
| 50 | constexpr int TEST_COUNTERSET = 1; |
| 51 | constexpr int DEFAULT_COUNTERSET = 0; |
| 52 | |
| 53 | class BpfBasicTest : public testing::Test { |
| 54 | protected: |
| 55 | BpfBasicTest() {} |
| 56 | }; |
| 57 | |
| 58 | TEST_F(BpfBasicTest, TestCgroupMounted) { |
| 59 | SKIP_IF_BPF_NOT_SUPPORTED; |
| 60 | |
| 61 | ASSERT_EQ(0, access(CGROUP_ROOT_PATH, R_OK)); |
| 62 | ASSERT_EQ(0, access("/dev/cg2_bpf/cgroup.controllers", R_OK)); |
| 63 | } |
| 64 | |
| 65 | TEST_F(BpfBasicTest, TestTrafficControllerSetUp) { |
| 66 | SKIP_IF_BPF_NOT_SUPPORTED; |
| 67 | |
| 68 | ASSERT_EQ(0, access(BPF_EGRESS_PROG_PATH, R_OK)); |
| 69 | ASSERT_EQ(0, access(BPF_INGRESS_PROG_PATH, R_OK)); |
| 70 | ASSERT_EQ(0, access(XT_BPF_INGRESS_PROG_PATH, R_OK)); |
| 71 | ASSERT_EQ(0, access(XT_BPF_EGRESS_PROG_PATH, R_OK)); |
| 72 | ASSERT_EQ(0, access(COOKIE_TAG_MAP_PATH, R_OK)); |
| 73 | ASSERT_EQ(0, access(UID_COUNTERSET_MAP_PATH, R_OK)); |
| 74 | ASSERT_EQ(0, access(UID_STATS_MAP_PATH, R_OK)); |
| 75 | ASSERT_EQ(0, access(TAG_STATS_MAP_PATH, R_OK)); |
| 76 | ASSERT_EQ(0, access(IFACE_INDEX_NAME_MAP_PATH, R_OK)); |
| 77 | ASSERT_EQ(0, access(IFACE_STATS_MAP_PATH, R_OK)); |
| 78 | ASSERT_EQ(0, access(DOZABLE_UID_MAP_PATH, R_OK)); |
| 79 | ASSERT_EQ(0, access(STANDBY_UID_MAP_PATH, R_OK)); |
| 80 | ASSERT_EQ(0, access(POWERSAVE_UID_MAP_PATH, R_OK)); |
| 81 | } |
| 82 | |
| 83 | TEST_F(BpfBasicTest, TestTagSocket) { |
| 84 | SKIP_IF_BPF_NOT_SUPPORTED; |
| 85 | |
| 86 | unique_fd cookieTagMap = unique_fd(mapRetrieve(COOKIE_TAG_MAP_PATH, 0)); |
| 87 | ASSERT_LE(0, cookieTagMap); |
| 88 | int sock = socket(AF_INET6, SOCK_STREAM, 0); |
| 89 | ASSERT_LE(0, sock); |
| 90 | uint64_t cookie = getSocketCookie(sock); |
| 91 | ASSERT_NE(NONEXISTENT_COOKIE, cookie); |
| 92 | ASSERT_EQ(0, qtaguid_tagSocket(sock, TEST_TAG, TEST_UID)); |
| 93 | struct UidTag tagResult; |
| 94 | ASSERT_EQ(0, findMapEntry(cookieTagMap, &cookie, &tagResult)); |
| 95 | ASSERT_EQ(TEST_UID, tagResult.uid); |
| 96 | ASSERT_EQ(TEST_TAG, tagResult.tag); |
| 97 | ASSERT_EQ(0, qtaguid_untagSocket(sock)); |
| 98 | ASSERT_EQ(-1, findMapEntry(cookieTagMap, &cookie, &tagResult)); |
| 99 | ASSERT_EQ(ENOENT, errno); |
| 100 | } |
| 101 | |
| 102 | TEST_F(BpfBasicTest, TestChangeCounterSet) { |
| 103 | SKIP_IF_BPF_NOT_SUPPORTED; |
| 104 | |
| 105 | unique_fd uidCounterSetMap = unique_fd(mapRetrieve(UID_COUNTERSET_MAP_PATH, 0)); |
| 106 | ASSERT_LE(0, uidCounterSetMap); |
| 107 | ASSERT_EQ(0, qtaguid_setCounterSet(TEST_COUNTERSET, TEST_UID)); |
| 108 | uid_t uid = TEST_UID; |
| 109 | int counterSetResult; |
| 110 | ASSERT_EQ(0, findMapEntry(uidCounterSetMap, &uid, &counterSetResult)); |
| 111 | ASSERT_EQ(TEST_COUNTERSET, counterSetResult); |
| 112 | ASSERT_EQ(0, qtaguid_setCounterSet(DEFAULT_COUNTERSET, TEST_UID)); |
| 113 | ASSERT_EQ(-1, findMapEntry(uidCounterSetMap, &uid, &counterSetResult)); |
| 114 | ASSERT_EQ(ENOENT, errno); |
| 115 | } |
| 116 | |
| 117 | TEST_F(BpfBasicTest, TestDeleteTagData) { |
| 118 | SKIP_IF_BPF_NOT_SUPPORTED; |
| 119 | |
| 120 | unique_fd uidStatsMap = unique_fd(mapRetrieve(UID_STATS_MAP_PATH, 0)); |
| 121 | ASSERT_LE(0, uidStatsMap); |
| 122 | unique_fd tagStatsMap = unique_fd(mapRetrieve(TAG_STATS_MAP_PATH, 0)); |
| 123 | ASSERT_LE(0, tagStatsMap); |
| 124 | |
| 125 | StatsKey key = {.uid = TEST_UID, .tag = TEST_TAG, .counterSet = TEST_COUNTERSET, |
| 126 | .ifaceIndex = 1}; |
| 127 | StatsValue statsMapValue = {.rxPackets = 1, .rxBytes = 100}; |
| 128 | EXPECT_EQ(0, writeToMapEntry(tagStatsMap, &key, &statsMapValue, BPF_ANY)); |
| 129 | key.tag = 0; |
| 130 | EXPECT_EQ(0, writeToMapEntry(uidStatsMap, &key, &statsMapValue, BPF_ANY)); |
| 131 | ASSERT_EQ(0, qtaguid_deleteTagData(0, TEST_UID)); |
| 132 | ASSERT_EQ(-1, findMapEntry(uidStatsMap, &key, &statsMapValue)); |
| 133 | ASSERT_EQ(ENOENT, errno); |
| 134 | key.tag = TEST_TAG; |
| 135 | ASSERT_EQ(-1, findMapEntry(tagStatsMap, &key, &statsMapValue)); |
| 136 | ASSERT_EQ(ENOENT, errno); |
| 137 | } |
| 138 | |
| 139 | } |
| 140 | } |