summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLorenzo Colitti <lorenzo@google.com>2022-12-22 14:48:45 +0900
committerLorenzo Colitti <lorenzo@google.com>2022-12-22 14:49:40 +0900
commitbd91fbaaad4fccb7763021355196c1b87f5862fa (patch)
treea3733d0cba0b400bb00a6ec63482be29e539e891
parent4296aa0ea5f09fde169201f1ae7265cb58a8b8b1 (diff)
Add a utility function for checking kernel versions.
Test: test-only change, covered in upcoming CL Change-Id: Ia356f43c6ec3d0ee84e87489e6c7968a0705f72a
-rw-r--r--net/test/net_test.py26
1 files changed, 26 insertions, 0 deletions
diff --git a/net/test/net_test.py b/net/test/net_test.py
index 0dae1c2..bbff4e7 100644
--- a/net/test/net_test.py
+++ b/net/test/net_test.py
@@ -96,6 +96,32 @@ KERN_INFO = 6
LINUX_VERSION = csocket.LinuxVersion()
LINUX_ANY_VERSION = (0, 0)
+def KernelAtLeast(versions):
+ """Checks the kernel version matches the specified versions.
+
+ Args:
+ versions: a list of versions expressed as tuples,
+ e.g., [(5, 10, 108), (5, 15, 31)]. The kernel version matches if it's
+ between each specified version and the next minor version with last digit
+ set to 0. In this example, the kernel version must match either:
+ >= 5.10.108 and < 5.15.0
+ >= 5.15.31
+ While this is less flexible than matching exact tuples, it allows the caller
+ to pass in fewer arguments, because Android only supports certain minor
+ versions (4.19, 5.4, 5.10, ...)
+
+ Returns:
+ True if the kernel version matches, False otherwise
+ """
+ maxversion = (1000, 255, 65535)
+ for version in sorted(versions, reverse=True):
+ if version[:2] == maxversion[:2]:
+ raise ValueError("Duplicate minor version: %s %s", (version, maxversion))
+ if LINUX_VERSION >= version and LINUX_VERSION < maxversion:
+ return True
+ maxversion = (version[0], version[1], 0)
+ return False
+
def ByteToHex(b):
return "%02x" % (ord(b) if isinstance(b, str) else b)