Path: blob/main/python/python-wasm/src/test/posix/unistd.test.ts
1067 views
import { syncPython } from "../../node";1import { hostname, userInfo } from "os";23test("test getting the hostname via python, which calls the gethostname system call", async () => {4const { exec, repr } = await syncPython();5exec("import socket");6expect(repr("socket.gethostname()")).toEqual(`'${hostname()}'`);7});89test("test python's os.getlogin() which calls the getlogin systemcall", async () => {10const { exec, repr } = await syncPython();11exec("import os");12expect(repr("os.getlogin()")).toEqual(`'${userInfo?.()?.username}'`);13});1415test("test python's os.getpgrp returns a positive integer", async () => {16const { exec, repr } = await syncPython();17exec("import os");18expect(eval(repr("os.getpgrp()"))).toBeGreaterThan(0);19});2021test("test python's os.getgroups returns a list of positive integer", async () => {22const { exec, repr } = await syncPython();23exec("import os");24const v = eval(repr("os.getgroups()"));25for (const a of v) {26expect(a).toBeGreaterThanOrEqual(0);27}28});2930test("consistency check involving statvfs", async () => {31const { exec, repr } = await syncPython();32exec("import os");33const f_namemax = eval(repr("os.statvfs('/').f_namemax"));34// it's 255 on some linux and macos, so we'll just do a consistency check,35// and not have to add another dependency on posix-node.36// import posix from "posix-node";37//expect(f_namemax).toBe(posix.statvfs?.("/").f_namemax);38expect(f_namemax >= 128 && f_namemax <= 1024).toBe(true);39});4041// can't do this during jest testing, though it works on the command line:42// test("consistency check involving fstatvfs", async () => {43// await init({ debug: true });44// await exec("import os");45// const f_namemax = eval(await repr("os.fstatvfs(1).f_namemax"));46// expect(f_namemax).toBe(posix.fstatvfs?.(1).f_namemax);47// });4849test("using getresuid on Linux only", async () => {50const { exec, repr } = await syncPython();51exec("import os");52if (process.platform == "linux") {53const resuid = eval("[" + repr("os.getresuid()").slice(1, -1) + "]");54// should be a triple of numbers55expect(resuid.length).toBe(3);56for (const n of resuid) {57expect(typeof n).toBe("number");58}59}60});6162test("using getresgid on Linux only", async () => {63const { exec, repr } = await syncPython();64exec("import os");65if (process.platform == "linux") {66const resgid = eval("[" + repr("os.getresgid()").slice(1, -1) + "]");67// should be a triple of numbers68expect(resgid.length).toBe(3);69for (const n of resgid) {70expect(typeof n).toBe("number");71}72}73});7475// setresuid/setresgid can only be done as root, so we only test that they throw here7677// test("setresuid throws", async () => {78// await exec("import os");79// expect(repr("os.setresuid(0,0,0)")).rejects.toThrow();80// });8182// >>> import os, posix; os.getgrouplist(os.getlogin(),0)83// [0, 12, 20, 61, 79, 80, 81, 98, 701, 33, 100, 204, 250, 395, 398, 399, 400]84test("getgrouplist returns a list of numbers", async () => {85const { exec, repr } = await syncPython();86exec("import os, posix");87const v = eval(repr("os.getgrouplist(os.getlogin(),0)"));88expect(v.length).toBeGreaterThan(0);89expect(typeof v[0]).toBe("number");90});9192// wasi doesn't have fchdir, so I implemented it and I'm testing it here via python.93test("fchdir works", async () => {94// This is complicated, since e.g., on macos if you do "cd /tmp" you95// end up in /private/tmp", etc. It's weird.96const { exec, repr } = await syncPython();97exec("import tempfile; td = tempfile.TemporaryDirectory()");98exec("import os; fd = os.open(td.name, os.O_RDONLY)");99exec("os.fchdir(fd)");100const actual = eval(repr("os.getcwd()"));101exec("os.mkdir('abc')");102exec("fd2 = os.open('abc', os.O_RDONLY)");103exec("os.fchdir(fd2)");104expect(eval(repr("os.getcwd()"))).toBe(actual + "/abc");105// it doesn't seem to always get removed (which is weird)106exec("import shutil; shutil.rmtree(td.name)");107});108109110