Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ccd9d51b authored by Harry Cutts's avatar Harry Cutts
Browse files

TestEventMatchers: bounds check pointer indexes

b/365166534 was caused by a WithPointer… matcher not checking that its
pointer index was in bounds, and this check is missing from a couple of
other WithPointer… matchers. Add it, so that more bugs like this can be
found instantly in future rather than causing flaky assertions against
uninitialized memory.

Test: atest inputflinger_tests
Bug: 365166534
Flag: TEST_ONLY
Change-Id: I57d23909888749e15d4d034ddb0c091336b016d1
parent 75155343
Loading
Loading
Loading
Loading
+13 −4
Original line number Diff line number Diff line
@@ -615,7 +615,12 @@ public:
    explicit WithPointerIdMatcher(size_t index, int32_t pointerId)
          : mIndex(index), mPointerId(pointerId) {}

    bool MatchAndExplain(const NotifyMotionArgs& args, std::ostream*) const {
    bool MatchAndExplain(const NotifyMotionArgs& args, std::ostream* os) const {
        if (mIndex >= args.pointerCoords.size()) {
            *os << "Pointer index " << mIndex << " is out of bounds";
            return false;
        }

        return args.pointerProperties[mIndex].id == mPointerId;
    }

@@ -797,10 +802,14 @@ MATCHER_P(WithToolType, toolType, "InputEvent with specified tool type") {
    return argToolType == toolType;
}

MATCHER_P2(WithPointerToolType, pointer, toolType,
MATCHER_P2(WithPointerToolType, pointerIndex, toolType,
           "InputEvent with specified tool type for pointer") {
    const auto argToolType = arg.pointerProperties[pointer].toolType;
    *result_listener << "expected pointer " << pointer << " to have tool type "
    if (std::cmp_greater_equal(pointerIndex, arg.getPointerCount())) {
        *result_listener << "Pointer index " << pointerIndex << " is out of bounds";
        return false;
    }
    const auto argToolType = arg.pointerProperties[pointerIndex].toolType;
    *result_listener << "expected pointer " << pointerIndex << " to have tool type "
                     << ftl::enum_string(toolType) << ", but got " << ftl::enum_string(argToolType);
    return argToolType == toolType;
}