|
| 1 | +'use strict'; |
| 2 | +// fs.readFileSync(path, 'utf8') takes a dedicated native path. Its result must |
| 3 | +// equal fs.readFileSync(path).toString('utf8') for every file size (in |
| 4 | +// particular around its internal 8 KiB stack buffer and for multi-megabyte |
| 5 | +// files), for file descriptors positioned mid-file, and for files whose |
| 6 | +// reported size is wrong (procfs reports 0, sysfs reports a page). |
| 7 | +const common = require('../common'); |
| 8 | +const tmpdir = require('../common/tmpdir'); |
| 9 | +const assert = require('assert'); |
| 10 | +const fs = require('fs'); |
| 11 | + |
| 12 | +tmpdir.refresh(); |
| 13 | + |
| 14 | +function content(size) { |
| 15 | + // Multi-byte characters straddling every possible chunk boundary. |
| 16 | + const unit = 'abcdé€\u{1F600}\n'; |
| 17 | + let s = unit.repeat(Math.ceil(size / unit.length)); |
| 18 | + s = s.slice(0, size); |
| 19 | + // Avoid ending on a lone surrogate produced by slice(). |
| 20 | + if (/[\ud800-\udbff]$/.test(s)) s = s.slice(0, -1) + 'x'; |
| 21 | + return s; |
| 22 | +} |
| 23 | + |
| 24 | +const sizes = [0, 1, 8190, 8191, 8192, 8193, 8194, 16383, 16384, 16385, |
| 25 | + 65535, 65536, 65537, 100000, (1 << 20) - 1, 1 << 20, (1 << 20) + 1, |
| 26 | + (8 << 20) + 5]; |
| 27 | +for (const size of sizes) { |
| 28 | + const file = tmpdir.resolve(`f-${size}.txt`); |
| 29 | + const str = content(size); |
| 30 | + fs.writeFileSync(file, str); |
| 31 | + const expected = fs.readFileSync(file).toString('utf8'); |
| 32 | + assert.strictEqual(fs.readFileSync(file, 'utf8'), expected, `size ${size} by path`); |
| 33 | + assert.strictEqual(fs.readFileSync(file, { encoding: 'utf-8' }), expected, `size ${size} utf-8 alias`); |
| 34 | + // By fd: from the start (leaves the fd at EOF), then at EOF, then from a |
| 35 | + // mid-file position on a fresh fd. |
| 36 | + let fd = fs.openSync(file, 'r'); |
| 37 | + try { |
| 38 | + assert.strictEqual(fs.readFileSync(fd, 'utf8'), expected, `size ${size} by fd`); |
| 39 | + assert.strictEqual(fs.readFileSync(fd, 'utf8'), '', `size ${size} by fd at EOF`); |
| 40 | + } finally { |
| 41 | + fs.closeSync(fd); |
| 42 | + } |
| 43 | + if (size > 10) { |
| 44 | + fd = fs.openSync(file, 'r'); |
| 45 | + try { |
| 46 | + // Advance the fd 3 bytes (inside the ASCII prefix, so still valid UTF-8). |
| 47 | + assert.strictEqual(fs.readSync(fd, Buffer.alloc(3), 0, 3, null), 3); |
| 48 | + assert.strictEqual(fs.readFileSync(fd, 'utf8'), Buffer.from(expected).subarray(3).toString('utf8'), |
| 49 | + `size ${size} by fd at offset 3`); |
| 50 | + } finally { |
| 51 | + fs.closeSync(fd); |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +// Binary garbage is decoded with replacement characters identically. |
| 57 | +{ |
| 58 | + const file = tmpdir.resolve('binary.bin'); |
| 59 | + const buf = Buffer.alloc(20000); |
| 60 | + for (let i = 0; i < buf.length; i++) buf[i] = (i * 7919) & 0xff; |
| 61 | + fs.writeFileSync(file, buf); |
| 62 | + assert.strictEqual(fs.readFileSync(file, 'utf8'), buf.toString('utf8')); |
| 63 | +} |
| 64 | + |
| 65 | +// Files whose st_size does not describe their content. |
| 66 | +if (common.isLinux) { |
| 67 | + for (const file of ['/proc/self/status', '/proc/self/smaps', '/proc/cpuinfo', |
| 68 | + '/proc/version', '/sys/kernel/mm/transparent_hugepage/enabled']) { |
| 69 | + let viaBuffer; |
| 70 | + try { |
| 71 | + viaBuffer = fs.readFileSync(file); |
| 72 | + } catch { |
| 73 | + continue; // Not available in this environment. |
| 74 | + } |
| 75 | + const viaUtf8 = fs.readFileSync(file, 'utf8'); |
| 76 | + if (file !== '/proc/version' && file.startsWith('/proc/')) { |
| 77 | + // Content legitimately differs between two reads; compare shape instead. |
| 78 | + assert.ok(viaUtf8.length > 0); |
| 79 | + assert.strictEqual(viaUtf8.split('\n').length > 5, true, file); |
| 80 | + // Of these, smaps reliably exceeds the 8 KiB stack buffer. |
| 81 | + if (file === '/proc/self/smaps') assert.ok(viaUtf8.length > 8192, `smaps is only ${viaUtf8.length} chars`); |
| 82 | + } else { |
| 83 | + assert.strictEqual(viaUtf8, viaBuffer.toString('utf8'), file); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +// Directory: same outcome either way (EISDIR, except on platforms where |
| 89 | +// read() accepts directories, e.g. AIX). |
| 90 | +function outcome(read) { |
| 91 | + try { |
| 92 | + return read(); |
| 93 | + } catch (err) { |
| 94 | + return err.code; |
| 95 | + } |
| 96 | +} |
| 97 | +assert.strictEqual(outcome(() => fs.readFileSync(tmpdir.path, 'utf8')), |
| 98 | + outcome(() => fs.readFileSync(tmpdir.path).toString('utf8'))); |