Skip to content

Commit

Permalink
Fix test mocks for big endian systems
Browse files Browse the repository at this point in the history
Fix the mocked inotify data to respect system endianness.  Instead of
harcoding the raw data, reconstruct it using struct.pack(), respecting
host endianness.  This should also benefit readability a bit.

Closes #804
  • Loading branch information
mgorny committed Aug 19, 2021
1 parent 4ef348b commit ab65087
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions tests/test_inotify_c.py
Expand Up @@ -10,6 +10,7 @@
import errno
import logging
import os
import struct
from functools import partial
from queue import Queue

Expand Down Expand Up @@ -52,6 +53,19 @@ def teardown_function(function):
pass


def struct_inotify(wd, mask, cookie, length, name):
assert len(name) <= length
struct_format = (
"=" # (native endianness, standard sizes)
"i" # int wd
"i" # uint32_t mask
"i" # uint32_t cookie
"i" # uint32_t len
"%ds" % (length,) # char[] name
)
return struct.pack(struct_format, wd, mask, cookie, length, name)


def test_late_double_deletion(monkeypatch):
inotify_fd = type(str("FD"), (object,), {})() # Empty object
inotify_fd.last = 0
Expand All @@ -60,20 +74,18 @@ def test_late_double_deletion(monkeypatch):
# CREATE DELETE CREATE DELETE DELETE_SELF IGNORE DELETE_SELF IGNORE
inotify_fd.buf = (
# IN_CREATE|IS_DIR (wd = 1, path = subdir1)
b"\x01\x00\x00\x00\x00\x01\x00\x40\x00\x00\x00\x00\x10\x00\x00\x00"
b"\x73\x75\x62\x64\x69\x72\x31\x00\x00\x00\x00\x00\x00\x00\x00\x00"
struct_inotify(1, 0x40000100, 0, 16, b"subdir1") +
# IN_DELETE|IS_DIR (wd = 1, path = subdir1)
b"\x01\x00\x00\x00\x00\x02\x00\x40\x00\x00\x00\x00\x10\x00\x00\x00"
b"\x73\x75\x62\x64\x69\x72\x31\x00\x00\x00\x00\x00\x00\x00\x00\x00"
struct_inotify(1, 0x40000200, 0, 16, b"subdir1")
) * 2 + (
# IN_DELETE_SELF (wd = 2)
b"\x02\x00\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
struct_inotify(2, 0x00000400, 0, 0, b"") +
# IN_IGNORE (wd = 2)
b"\x02\x00\x00\x00\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
struct_inotify(2, 0x00008000, 0, 0, b"") +
# IN_DELETE_SELF (wd = 3)
b"\x03\x00\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
struct_inotify(3, 0x00000400, 0, 0, b"") +
# IN_IGNORE (wd = 3)
b"\x03\x00\x00\x00\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
struct_inotify(3, 0x00008000, 0, 0, b"")
)

os_read_bkp = os.read
Expand Down

0 comments on commit ab65087

Please sign in to comment.