test-download.js 7.3 KB

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