Skip to content

A games launcher for GOG, Amazon and Epic Games for Linux, Windows and macOS.

2.6 KB
Raw Blame
import {
  buildPowerShellCommand,
  callRunner,
  getRunnerCallWithoutCredentials
} from '../launcher'
import { LogPrefix } from '../logger'

jest.mock('../logger', () => ({
  ...jest.requireActual<typeof import('../logger')>('../logger'),
  logInfo: jest.fn(),
  logError: jest.fn(),
  getRunnerLogWriter: () => ({ logInfo: jest.fn(), writeString: jest.fn() })
}))

const unixIt = process.platform === 'win32' ? it.skip : it

describe('callRunner', () => {
  unixIt('passes the exit code of the runner through', async () => {
    const runner = { name: 'gog' as const, logPrefix: LogPrefix.Gog, bin: 'sh' }

    expect((await callRunner(['-c', 'exit 3'], runner, {})).exitCode).toBe(3)
    expect((await callRunner(['-c', 'exit 0'], runner, {})).exitCode).toBe(0)
  })
})

describe('buildPowerShellCommand', () => {
  const backtick = '`'
  const quote = (value: string) => `"${backtick}"${value}${backtick}""`

  it('builds the exact Start-Process invocation with quoting and ExitCode handling', () => {
    const runnerPath = 'C:\\Program Files & Games\\gogdl.exe'
    const args = [
      quote('download'),
      quote('1207664643'),
      quote('C:\\\\Games & Stuff')
    ].join(',')
    const expectedScript =
      `$process = Start-Process ${quote(runnerPath)} -Wait -NoNewWindow -PassThru ` +
      `-ArgumentList ${args}; if ($null -eq $process) { exit 1 }; exit $process.ExitCode`

    expect(
      buildPowerShellCommand(
        ['download', '1207664643', 'C:\\Games & Stuff'],
        runnerPath
      )
    ).toEqual(['-NoProfile', '-Command', expectedScript])
  })

  it('keeps credentials in the command that is executed', () => {
    const command = buildPowerShellCommand(
      ['auth', '--code', 'secret-code', '--password', 'secret-branch-password'],
      'gogdl.exe'
    )

    expect(command[2]).toContain('--code')
    expect(command[2]).toContain('secret-code')
    expect(command[2]).toContain('--password')
    expect(command[2]).toContain('secret-branch-password')
  })
})

describe('getRunnerCallWithoutCredentials', () => {
  it('redacts GOG and Legendary auth credentials', () => {
    const commands = [
      ['--auth-config-path', 'auth.json', 'auth', '--code', 'secret-code'],
      ['auth', '--token', 'secret-token'],
      ['update', '1207664643', '--password', 'secret-branch-password'],
      ['-p', 'secret-password'],
      ['--guard', 'secret-guard']
    ]

    for (const command of commands) {
      const safeCommand = getRunnerCallWithoutCredentials(command, {}, 'gogdl')

      expect(safeCommand).not.toContain(command.at(-1))
      expect(safeCommand).toContain('<redacted>')
    }
  })
})