aboutsummaryrefslogtreecommitdiff
path: root/test/sandbox.js
blob: 78a3435e78bab169af1f40234d96e9f0825ff06a (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
var semver = require("semver");
var vm = require("vm");

var setupContext = new vm.Script([
    "[ Array, Boolean, Error, Function, Number, Object, RegExp, String ].forEach(function(f) {",
    "    f.toString = Function.prototype.toString;",
    "});",
    "Function.prototype.toString = function() {",
    "    var id = 100000;",
    "    return function() {",
    "        var n = this.name;",
    "        if (!/^F[0-9]{6}N$/.test(n)) {",
    '            n = "F" + ++id + "N";',
].concat(Object.getOwnPropertyDescriptor(Function.prototype, "name").configurable ? [
    '            Object.defineProperty(this, "name", {',
    "                get: function() {",
    "                    return n;",
    "                }",
    "            });",
] : [], [
    "        }",
    '        return "function(){}";',
    "    };",
    "}();",
    "this;",
]).join("\n"));

function createContext() {
    var ctx = vm.createContext(Object.defineProperties({}, {
        console: { value: { log: log } },
        global: { get: self },
        self: { get: self },
        window: { get: self },
    }));
    var global = setupContext.runInContext(ctx);
    return ctx;

    function self() {
        return this;
    }

    function safe_log(arg, level) {
        if (arg) switch (typeof arg) {
          case "function":
            return arg.toString();
          case "object":
            if (arg === global) return "[object global]";
            if (/Error$/.test(arg.name)) return arg.toString();
            if (typeof arg.then == "function") return "[object Promise]";
            arg.constructor.toString();
            if (level--) for (var key in arg) {
                var desc = Object.getOwnPropertyDescriptor(arg, key);
                if (!desc || !desc.get && !desc.set) arg[key] = safe_log(arg[key], level);
            }
        }
        return arg;
    }

    function log(msg) {
        if (arguments.length == 1 && typeof msg == "string") return console.log("%s", msg);
        return console.log.apply(console, [].map.call(arguments, function(arg) {
            return safe_log(arg, 3);
        }));
    }
}

function run_code(code, toplevel, timeout) {
    timeout = timeout || 5000;
    var stdout = "";
    var original_write = process.stdout.write;
    process.stdout.write = function(chunk) {
        stdout += chunk;
    };
    try {
        vm.runInContext(toplevel ? "(function(){" + code + "})()" : code, createContext(), { timeout: timeout });
        return stdout;
    } catch (ex) {
        return ex;
    } finally {
        process.stdout.write = original_write;
    }
}

exports.run_code = semver.satisfies(process.version, "0.8") ? function(code, toplevel, timeout) {
    var stdout = run_code(code, toplevel, timeout);
    if (typeof stdout != "string" || !/arguments/.test(code)) return stdout;
    do {
        var prev = stdout;
        stdout = run_code(code, toplevel, timeout);
    } while (prev !== stdout);
    return stdout;
} : run_code;

function strip_func_ids(text) {
    return ("" + text).replace(/F[0-9]{6}N/g, "<F<>N>");
}

exports.same_stdout = semver.satisfies(process.version, "0.12") ? function(expected, actual) {
    if (typeof expected != typeof actual) return false;
    if (typeof expected == "object" && typeof expected.name == "string" && typeof expected.message == "string") {
        if (expected.name !== actual.name) return false;
        if (typeof actual.message != "string") return false;
        expected = expected.message.slice(expected.message.lastIndexOf("\n") + 1);
        actual = actual.message.slice(actual.message.lastIndexOf("\n") + 1);
    }
    return strip_func_ids(expected) == strip_func_ids(actual);
} : function(expected, actual) {
    return typeof expected == typeof actual && strip_func_ids(expected) == strip_func_ids(actual);
};
exports.has_toplevel = function(options) {
    return options.toplevel
        || options.mangle && options.mangle.toplevel
        || options.compress && options.compress.toplevel;
};
les. * tests/guix-daemon.sh: Add test with '--log-compression=gzip'. * doc/guix.texi (Invoking guix-daemon): Adjust accordingly. * config-daemon.ac: Check for libz and zlib.h. Ludovic Courtès 2017-06-22daemon: '--listen' can be passed several times, can specify TCP endpoints....* nix/nix-daemon/guix-daemon.cc (DEFAULT_GUIX_PORT): New macro. (listen_options): New variable. (parse_opt): Push back '--listen' options to LISTEN_OPTIONS. (open_unix_domain_socket, open_inet_socket) (listening_sockets): New functions. (main): Use it. Pass SOCKETS to 'run'. * nix/nix-daemon/nix-daemon.cc (matchUser): Remove. (SD_LISTEN_FDS_START): Remove. (acceptConnection): New function. (daemonLoop): Rewrite to take a vector of file descriptors, to select(2) on them, and to call 'acceptConnection'. (run): Change to take a vector of file descriptors. * tests/guix-daemon.sh: Add test. Ludovic Courtès 2017-06-04daemon: Add '--timeout' and '--max-silent-time'....* nix/nix-daemon/guix-daemon.cc (GUIX_OPT_TIMEOUT) (GUIX_OPT_MAX_SILENT_TIME): New macros. * nix/nix-daemon/guix-daemon.cc (options): Add '--timeout' and '--max-silent-time'. (parse_opt): Honor them. * tests/guix-daemon.sh: Add test. * doc/guix.texi (Invoking guix-daemon): Document the options. (Common Build Options): Properly describe default timeout/max-silent-time value. Add cross-ref to "Invoking guix-daemon". Ludovic Courtès 2017-01-15daemon: Client settings no longer override daemon settings....Fixes <http://bugs.gnu.org/20217>. * nix/libstore/worker-protocol.hh (PROTOCOL_VERSION): Bump to 0x161. * nix/nix-daemon/nix-daemon.cc (performOp): "build-max-jobs", "build-max-silent-time", and "build-cores" are no longer read upfront; instead, read them from the key/value list at the end. * nix/nix-daemon/guix-daemon.cc (main): Explicitly set 'settings.maxBuildJobs'. * guix/store.scm (%protocol-version): Bump to #x161. (set-build-options): #:max-build-jobs, #:max-silent-time, and #:build-cores now default to #f. Adjust handshake to new protocol. * tests/store.scm ("build-cores"): New test. * tests/guix-daemon.sh: Add test for default "build-cores" value. Ludovic Courtès 2016-03-16build: Default to "https://mirror.hydra.gnu.org/" for substitutes....* config-daemon.ac: Check for (gnutls) and define 'GUIX_SUBSTITUTE_URLS'. * nix/nix-daemon/guix-daemon.cc (main): Use GUIX_SUBSTITUTE_URLS. * guix/store.scm (%default-substitute-urls): Use 'https' when (gnutls) is available. * doc/guix.texi (Binary Installation): Mention mirrors (Invoking guix-daemon): Mention mirror.hydra.gnu.org. (Substitutes): Mention mirrors. (Invoking guix archive): Show https URLs. Ludovic Courtès