You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test-download.js 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. 'use strict'
  2. const { describe, it, after } = require('mocha')
  3. const assert = require('assert')
  4. const fs = require('fs')
  5. const path = require('path')
  6. const util = require('util')
  7. const http = require('http')
  8. const https = require('https')
  9. const install = require('../lib/install')
  10. const semver = require('semver')
  11. const devDir = require('./common').devDir()
  12. const rimraf = require('rimraf')
  13. const gyp = require('../lib/node-gyp')
  14. const log = require('npmlog')
  15. const certs = require('./fixtures/certs')
  16. log.level = 'warn'
  17. describe('download', function () {
  18. it('download over http', async function () {
  19. const server = http.createServer((req, res) => {
  20. assert.strictEqual(req.headers['user-agent'], `node-gyp v42 (node ${process.version})`)
  21. res.end('ok')
  22. })
  23. after(() => new Promise((resolve) => server.close(resolve)))
  24. const host = 'localhost'
  25. await new Promise((resolve) => server.listen(0, host, resolve))
  26. const { port } = server.address()
  27. const gyp = {
  28. opts: {},
  29. version: '42'
  30. }
  31. const url = `http://${host}:${port}`
  32. const res = await install.test.download(gyp, url)
  33. assert.strictEqual(await res.text(), 'ok')
  34. })
  35. it('download over https with custom ca', async function () {
  36. const cafile = path.join(__dirname, 'fixtures/ca.crt')
  37. const cacontents = certs['ca.crt']
  38. const cert = certs['server.crt']
  39. const key = certs['server.key']
  40. await fs.promises.writeFile(cafile, cacontents, 'utf8')
  41. const ca = await install.test.readCAFile(cafile)
  42. assert.strictEqual(ca.length, 1)
  43. const options = { ca: ca, cert: cert, key: key }
  44. const server = https.createServer(options, (req, res) => {
  45. assert.strictEqual(req.headers['user-agent'], `node-gyp v42 (node ${process.version})`)
  46. res.end('ok')
  47. })
  48. after(async () => {
  49. await new Promise((resolve) => server.close(resolve))
  50. await fs.promises.unlink(cafile)
  51. })
  52. server.on('clientError', (err) => { throw err })
  53. const host = 'localhost'
  54. await new Promise((resolve) => server.listen(0, host, resolve))
  55. const { port } = server.address()
  56. const gyp = {
  57. opts: { cafile },
  58. version: '42'
  59. }
  60. const url = `https://${host}:${port}`
  61. const res = await install.test.download(gyp, url)
  62. assert.strictEqual(await res.text(), 'ok')
  63. })
  64. it('download over http with proxy', async function () {
  65. const server = http.createServer((_, res) => {
  66. res.end('ok')
  67. })
  68. const pserver = http.createServer((req, res) => {
  69. assert.strictEqual(req.headers['user-agent'], `node-gyp v42 (node ${process.version})`)
  70. res.end('proxy ok')
  71. })
  72. after(() => Promise.all([
  73. new Promise((resolve) => server.close(resolve)),
  74. new Promise((resolve) => pserver.close(resolve))
  75. ]))
  76. const host = 'localhost'
  77. await new Promise((resolve) => server.listen(0, host, resolve))
  78. const { port } = server.address()
  79. await new Promise((resolve) => pserver.listen(port + 1, host, resolve))
  80. const gyp = {
  81. opts: {
  82. proxy: `http://${host}:${port + 1}`,
  83. noproxy: 'bad'
  84. },
  85. version: '42'
  86. }
  87. const url = `http://${host}:${port}`
  88. const res = await install.test.download(gyp, url)
  89. assert.strictEqual(await res.text(), 'proxy ok')
  90. })
  91. it('download over http with noproxy', async function () {
  92. const server = http.createServer((req, res) => {
  93. assert.strictEqual(req.headers['user-agent'], `node-gyp v42 (node ${process.version})`)
  94. res.end('ok')
  95. })
  96. const pserver = http.createServer((_, res) => {
  97. res.end('proxy ok')
  98. })
  99. after(() => Promise.all([
  100. new Promise((resolve) => server.close(resolve)),
  101. new Promise((resolve) => pserver.close(resolve))
  102. ]))
  103. const host = 'localhost'
  104. await new Promise((resolve) => server.listen(0, host, resolve))
  105. const { port } = server.address()
  106. await new Promise((resolve) => pserver.listen(port + 1, host, resolve))
  107. const gyp = {
  108. opts: {
  109. proxy: `http://${host}:${port + 1}`,
  110. noproxy: host
  111. },
  112. version: '42'
  113. }
  114. const url = `http://${host}:${port}`
  115. const res = await install.test.download(gyp, url)
  116. assert.strictEqual(await res.text(), 'ok')
  117. })
  118. it('download with missing cafile', async function () {
  119. const gyp = {
  120. opts: { cafile: 'no.such.file' }
  121. }
  122. try {
  123. await install.test.download(gyp, {}, 'http://bad/')
  124. } catch (e) {
  125. assert.ok(/no.such.file/.test(e.message))
  126. }
  127. })
  128. it('check certificate splitting', async function () {
  129. const cafile = path.join(__dirname, 'fixtures/ca-bundle.crt')
  130. const cacontents = certs['ca-bundle.crt']
  131. await fs.promises.writeFile(cafile, cacontents, 'utf8')
  132. after(async () => {
  133. await fs.promises.unlink(cafile)
  134. })
  135. const cas = await install.test.readCAFile(path.join(__dirname, 'fixtures/ca-bundle.crt'))
  136. assert.strictEqual(cas.length, 2)
  137. assert.notStrictEqual(cas[0], cas[1])
  138. })
  139. // only run this test if we are running a version of Node with predictable version path behavior
  140. it('download headers (actual)', async function () {
  141. if (process.env.FAST_TEST ||
  142. process.release.name !== 'node' ||
  143. semver.prerelease(process.version) !== null ||
  144. semver.satisfies(process.version, '<10')) {
  145. return this.skip('Skipping actual download of headers due to test environment configuration')
  146. }
  147. this.timeout(300000)
  148. const expectedDir = path.join(devDir, process.version.replace(/^v/, ''))
  149. await util.promisify(rimraf)(expectedDir)
  150. const prog = gyp()
  151. prog.parseArgv([])
  152. prog.devDir = devDir
  153. log.level = 'warn'
  154. await util.promisify(install)(prog, [])
  155. const data = await fs.promises.readFile(path.join(expectedDir, 'installVersion'), 'utf8')
  156. assert.strictEqual(data, '11\n', 'correct installVersion')
  157. const list = await fs.promises.readdir(path.join(expectedDir, 'include/node'))
  158. assert.ok(list.includes('common.gypi'))
  159. assert.ok(list.includes('config.gypi'))
  160. assert.ok(list.includes('node.h'))
  161. assert.ok(list.includes('node_version.h'))
  162. assert.ok(list.includes('openssl'))
  163. assert.ok(list.includes('uv'))
  164. assert.ok(list.includes('uv.h'))
  165. assert.ok(list.includes('v8-platform.h'))
  166. assert.ok(list.includes('v8.h'))
  167. assert.ok(list.includes('zlib.h'))
  168. const lines = (await fs.promises.readFile(path.join(expectedDir, 'include/node/node_version.h'), 'utf8')).split('\n')
  169. // extract the 3 version parts from the defines to build a valid version string and
  170. // and check them against our current env version
  171. const version = ['major', 'minor', 'patch'].reduce((version, type) => {
  172. const re = new RegExp(`^#define\\sNODE_${type.toUpperCase()}_VERSION`)
  173. const line = lines.find((l) => re.test(l))
  174. const i = line ? parseInt(line.replace(/^[^0-9]+([0-9]+).*$/, '$1'), 10) : 'ERROR'
  175. return `${version}${type !== 'major' ? '.' : 'v'}${i}`
  176. }, '')
  177. assert.strictEqual(version, process.version)
  178. })
  179. })