16881f68fSopenharmony_ci#!/usr/bin/env python3
26881f68fSopenharmony_ci
36881f68fSopenharmony_ciif __name__ == '__main__':
46881f68fSopenharmony_ci    import sys
56881f68fSopenharmony_ci
66881f68fSopenharmony_ci    import pytest
76881f68fSopenharmony_ci    sys.exit(pytest.main([__file__] + sys.argv[1:]))
86881f68fSopenharmony_ci
96881f68fSopenharmony_ciimport os
106881f68fSopenharmony_ciimport socket
116881f68fSopenharmony_ciimport struct
126881f68fSopenharmony_ciimport subprocess
136881f68fSopenharmony_ciimport sys
146881f68fSopenharmony_ciimport time
156881f68fSopenharmony_cifrom os.path import join as pjoin
166881f68fSopenharmony_ci
176881f68fSopenharmony_ciimport pytest
186881f68fSopenharmony_ci
196881f68fSopenharmony_cifrom util import base_cmdline, basename
206881f68fSopenharmony_ci
216881f68fSopenharmony_ciFUSE_OP_INIT = 26
226881f68fSopenharmony_ci
236881f68fSopenharmony_ciFUSE_MAJOR_VERSION = 7
246881f68fSopenharmony_ciFUSE_MINOR_VERSION = 38
256881f68fSopenharmony_ci
266881f68fSopenharmony_cifuse_in_header_fmt = '<IIQQIIII'
276881f68fSopenharmony_cifuse_out_header_fmt = '<IiQ'
286881f68fSopenharmony_ci
296881f68fSopenharmony_cifuse_init_in_fmt = '<IIIII44x'
306881f68fSopenharmony_cifuse_init_out_fmt = '<IIIIHHIIHHI28x'
316881f68fSopenharmony_ci
326881f68fSopenharmony_ci
336881f68fSopenharmony_cidef sock_recvall(sock: socket.socket, bufsize: int) -> bytes:
346881f68fSopenharmony_ci    buf = bytes()
356881f68fSopenharmony_ci    while len(buf) < bufsize:
366881f68fSopenharmony_ci        buf += sock.recv(bufsize - len(buf))
376881f68fSopenharmony_ci    return buf
386881f68fSopenharmony_ci
396881f68fSopenharmony_ci
406881f68fSopenharmony_cidef tst_init(sock: socket.socket):
416881f68fSopenharmony_ci    unique_req = 10
426881f68fSopenharmony_ci    dummy_init_req_header = struct.pack(
436881f68fSopenharmony_ci        fuse_in_header_fmt, struct.calcsize(fuse_in_header_fmt) +
446881f68fSopenharmony_ci        struct.calcsize(fuse_init_in_fmt), FUSE_OP_INIT, unique_req, 0, 0, 0,
456881f68fSopenharmony_ci        0, 0)
466881f68fSopenharmony_ci    dummy_init_req_payload = struct.pack(
476881f68fSopenharmony_ci        fuse_init_in_fmt, FUSE_MAJOR_VERSION, FUSE_MINOR_VERSION, 0, 0, 0)
486881f68fSopenharmony_ci    dummy_init_req = dummy_init_req_header + dummy_init_req_payload
496881f68fSopenharmony_ci
506881f68fSopenharmony_ci    sock.sendall(dummy_init_req)
516881f68fSopenharmony_ci
526881f68fSopenharmony_ci    response_header = sock_recvall(sock, struct.calcsize(fuse_out_header_fmt))
536881f68fSopenharmony_ci    packet_len, _, unique_res = struct.unpack(
546881f68fSopenharmony_ci        fuse_out_header_fmt, response_header)
556881f68fSopenharmony_ci    assert unique_res == unique_req
566881f68fSopenharmony_ci
576881f68fSopenharmony_ci    response_payload = sock_recvall(sock, packet_len - len(response_header))
586881f68fSopenharmony_ci    response_payload = struct.unpack(fuse_init_out_fmt, response_payload)
596881f68fSopenharmony_ci    assert response_payload[0] == FUSE_MAJOR_VERSION
606881f68fSopenharmony_ci
616881f68fSopenharmony_ci
626881f68fSopenharmony_cidef test_hello_uds(output_checker):
636881f68fSopenharmony_ci    cmdline = base_cmdline + [pjoin(basename, 'example', 'hello_ll_uds')]
646881f68fSopenharmony_ci    print(cmdline)
656881f68fSopenharmony_ci    uds_process = subprocess.Popen(cmdline, stdout=output_checker.fd,
666881f68fSopenharmony_ci                                   stderr=output_checker.fd)
676881f68fSopenharmony_ci    time.sleep(1)
686881f68fSopenharmony_ci
696881f68fSopenharmony_ci    sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
706881f68fSopenharmony_ci    sock.settimeout(1)
716881f68fSopenharmony_ci    sock.connect("/tmp/libfuse-hello-ll.sock")
726881f68fSopenharmony_ci
736881f68fSopenharmony_ci    tst_init(sock)
746881f68fSopenharmony_ci
756881f68fSopenharmony_ci    sock.close()
766881f68fSopenharmony_ci    uds_process.terminate()
776881f68fSopenharmony_ci    try:
786881f68fSopenharmony_ci        uds_process.wait(1)
796881f68fSopenharmony_ci    except subprocess.TimeoutExpired:
806881f68fSopenharmony_ci        uds_process.kill()
816881f68fSopenharmony_ci    os.remove("/tmp/libfuse-hello-ll.sock")
82