|
1 | | -import fs = require('fs'); |
2 | | -import assert = require('assert'); |
3 | | -import path = require('path'); |
| 1 | +import * as path from "path"; |
| 2 | +import * as assert from "assert"; |
| 3 | +import { MockTestRunner } from "azure-pipelines-task-lib/mock-test"; |
| 4 | +import tl = require('azure-pipelines-task-lib'); |
4 | 5 |
|
5 | | -describe('GoToolV0 Suite', function () { |
6 | | - before(() => { |
| 6 | +describe('GoToolV0 Suite', function() { |
| 7 | + this.timeout(60000); |
| 8 | + |
| 9 | + before((done) => { |
| 10 | + done(); |
7 | 11 | }); |
8 | 12 |
|
9 | | - after(() => { |
| 13 | + after(function () { |
| 14 | + // Cleanup if needed |
10 | 15 | }); |
11 | 16 |
|
12 | | - it('Does a basic hello world test', function(done: MochaDone) { |
13 | | - // TODO - add real tests |
14 | | - done(); |
| 17 | + // Official Go (go.dev) tests |
| 18 | + it('Should install official Go with full patch version', async () => { |
| 19 | + let tp = path.join(__dirname, 'L0OfficialGoPatch.js'); |
| 20 | + let tr: MockTestRunner = new MockTestRunner(tp); |
| 21 | + await tr.runAsync(); |
| 22 | + assert(tr.succeeded, 'Should have succeeded'); |
| 23 | + assert(tr.stdOutContained('https://go.dev/dl/go1.22.3'), 'Should use official Go storage URL'); |
| 24 | + assert(tr.stdOutContained('Caching tool: go version: 1.22.3'), 'Should cache with toolName "go"'); |
| 25 | + }); |
| 26 | + |
| 27 | + it('Should resolve official Go major.minor to latest patch', async () => { |
| 28 | + let tp = path.join(__dirname, 'L0OfficialGoMinor.js'); |
| 29 | + let tr: MockTestRunner = new MockTestRunner(tp); |
| 30 | + await tr.runAsync(); |
| 31 | + assert(tr.succeeded, 'Should have succeeded'); |
| 32 | + assert(tr.stdOutContained('Resolved 1.21 to 1.21.5'), 'Should resolve to latest patch via go.dev API'); |
| 33 | + assert(tr.stdOutContained('https://go.dev/dl/go1.21.5'), 'Should download resolved version'); |
| 34 | + }); |
| 35 | + |
| 36 | + // Microsoft Go (aka.ms) tests |
| 37 | + it('Should install Microsoft Go with major.minor version', async () => { |
| 38 | + let tp = path.join(__dirname, 'L0MicrosoftGoMinor.js'); |
| 39 | + let tr: MockTestRunner = new MockTestRunner(tp); |
| 40 | + await tr.runAsync(); |
| 41 | + assert(tr.succeeded, 'Should have succeeded'); |
| 42 | + assert(tr.stdOutContained('https://aka.ms/golang/release/latest/go1.25.0'), 'Should use Microsoft aka.ms URL'); |
| 43 | + assert(tr.stdOutContained('Caching tool: go-aka version: 1.25.0'), 'Should cache with toolName "go-aka"'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('Should install Microsoft Go with patch version', async () => { |
| 47 | + let tp = path.join(__dirname, 'L0MicrosoftGoPatch.js'); |
| 48 | + let tr: MockTestRunner = new MockTestRunner(tp); |
| 49 | + await tr.runAsync(); |
| 50 | + assert(tr.succeeded, 'Should have succeeded'); |
| 51 | + assert(tr.stdOutContained('https://aka.ms/golang/release/latest/go1.24.7'), 'Should use Microsoft URL'); |
| 52 | + assert(tr.stdOutContained('Caching tool: go-aka version: 1.24.7-2'), 'Should resolve to latest revision from manifest'); |
| 53 | + }); |
| 54 | + |
| 55 | + it('Should install Microsoft Go with revision format', async () => { |
| 56 | + let tp = path.join(__dirname, 'L0MicrosoftGoRevision.js'); |
| 57 | + let tr: MockTestRunner = new MockTestRunner(tp); |
| 58 | + await tr.runAsync(); |
| 59 | + assert(tr.succeeded, 'Should have succeeded'); |
| 60 | + assert(tr.stdOutContained('https://aka.ms/golang/release/latest/go1.24.7-1'), 'Should use exact revision'); |
| 61 | + assert(tr.stdOutContained('Caching tool: go-aka version: 1.24.7-1'), 'Should cache with specified revision'); |
| 62 | + }); |
| 63 | + |
| 64 | + // Caching tests |
| 65 | + it('Should use cached official Go version', async () => { |
| 66 | + let tp = path.join(__dirname, 'L0CachedOfficial.js'); |
| 67 | + let tr: MockTestRunner = new MockTestRunner(tp); |
| 68 | + await tr.runAsync(); |
| 69 | + assert(tr.succeeded, 'Should have succeeded'); |
| 70 | + assert(tr.stdOutContained('Found cached tool: go version 1.22.3'), 'Should find cached version'); |
| 71 | + assert(!tr.stdOutContained('Downloading Go from'), 'Should not download when cached'); |
| 72 | + }); |
| 73 | + |
| 74 | + it('Should use cached Microsoft Go version', async () => { |
| 75 | + let tp = path.join(__dirname, 'L0CachedMicrosoft.js'); |
| 76 | + let tr: MockTestRunner = new MockTestRunner(tp); |
| 77 | + await tr.runAsync(); |
| 78 | + assert(tr.succeeded, 'Should have succeeded'); |
| 79 | + assert(tr.stdOutContained('Found cached tool: go-aka version 1.25.0-1'), 'Should find cached Microsoft build with resolved version'); |
| 80 | + assert(!tr.stdOutContained('Downloading Go from'), 'Should not download when cached'); |
| 81 | + }); |
| 82 | + |
| 83 | + // Environment variable tests |
| 84 | + it('Should set environment variables correctly', async () => { |
| 85 | + let tp = path.join(__dirname, 'L0EnvironmentVariables.js'); |
| 86 | + let tr: MockTestRunner = new MockTestRunner(tp); |
| 87 | + await tr.runAsync(); |
| 88 | + assert(tr.succeeded, 'Should have succeeded'); |
| 89 | + assert(tr.stdOutContained('##vso[task.setvariable variable=GOROOT'), 'Should set GOROOT'); |
| 90 | + assert(tr.stdOutContained('##vso[task.setvariable variable=GOPATH'), 'Should set GOPATH'); |
| 91 | + assert(tr.stdOutContained('##vso[task.setvariable variable=GOBIN'), 'Should set GOBIN'); |
| 92 | + }); |
| 93 | + |
| 94 | + // Cross-platform tests |
| 95 | + it('Should generate correct filename for Windows', async () => { |
| 96 | + let tp = path.join(__dirname, 'L0FilenameWindows.js'); |
| 97 | + let tr: MockTestRunner = new MockTestRunner(tp); |
| 98 | + await tr.runAsync(); |
| 99 | + assert(tr.succeeded, 'Should have succeeded'); |
| 100 | + assert(tr.stdOutContained('go1.22.3.windows-amd64.zip'), 'Should generate Windows zip filename'); |
| 101 | + }); |
| 102 | + |
| 103 | + it('Should generate correct filename for Linux', async () => { |
| 104 | + let tp = path.join(__dirname, 'L0FilenameLinux.js'); |
| 105 | + let tr: MockTestRunner = new MockTestRunner(tp); |
| 106 | + await tr.runAsync(); |
| 107 | + assert(tr.succeeded, 'Should have succeeded'); |
| 108 | + assert(tr.stdOutContained('go1.22.3.linux-amd64.tar.gz'), 'Should generate Linux tar.gz filename'); |
| 109 | + }); |
| 110 | + |
| 111 | + it('Should generate correct filename for Darwin/macOS', async () => { |
| 112 | + let tp = path.join(__dirname, 'L0FilenameDarwin.js'); |
| 113 | + let tr: MockTestRunner = new MockTestRunner(tp); |
| 114 | + await tr.runAsync(); |
| 115 | + assert(tr.succeeded, 'Should have succeeded'); |
| 116 | + assert(tr.stdOutContained('go1.22.3.darwin-amd64.tar.gz'), 'Should generate Darwin tar.gz filename'); |
| 117 | + }); |
| 118 | + |
| 119 | + it('Should generate correct filename for ARM64 architecture', async () => { |
| 120 | + let tp = path.join(__dirname, 'L0FilenameArm64.js'); |
| 121 | + let tr: MockTestRunner = new MockTestRunner(tp); |
| 122 | + await tr.runAsync(); |
| 123 | + assert(tr.succeeded, 'Should have succeeded'); |
| 124 | + assert(tr.stdOutContained('go1.22.3.linux-arm64.tar.gz'), 'Should generate ARM64 filename'); |
| 125 | + }); |
| 126 | + |
| 127 | + // Error handling tests |
| 128 | + it('Should fail with empty version input', async () => { |
| 129 | + let tp = path.join(__dirname, 'L0InvalidVersionEmpty.js'); |
| 130 | + let tr: MockTestRunner = new MockTestRunner(tp); |
| 131 | + await tr.runAsync(); |
| 132 | + assert(tr.failed, 'Should have failed'); |
| 133 | + assert(tr.stdOutContained('Input required: version'), 'Should show validation error'); |
| 134 | + }); |
| 135 | + |
| 136 | + it('Should fail with null version input', async () => { |
| 137 | + let tp = path.join(__dirname, 'L0InvalidVersionNull.js'); |
| 138 | + let tr: MockTestRunner = new MockTestRunner(tp); |
| 139 | + await tr.runAsync(); |
| 140 | + assert(tr.failed, 'Should have failed'); |
| 141 | + assert(tr.stdOutContained('Input required: version') || tr.stdOutContained('Input \'version\' is required'), 'Should reject null version'); |
| 142 | + }); |
| 143 | + |
| 144 | + it('Should fail with undefined version input', async () => { |
| 145 | + let tp = path.join(__dirname, 'L0InvalidVersionUndefined.js'); |
| 146 | + let tr: MockTestRunner = new MockTestRunner(tp); |
| 147 | + await tr.runAsync(); |
| 148 | + assert(tr.failed, 'Should have failed'); |
| 149 | + assert(tr.stdOutContained('Input required: version'), 'Should reject undefined version'); |
| 150 | + }); |
| 151 | + |
| 152 | + it('Should fail with unsupported base URL', async () => { |
| 153 | + let tp = path.join(__dirname, 'L0InvalidBaseUrl.js'); |
| 154 | + let tr: MockTestRunner = new MockTestRunner(tp); |
| 155 | + await tr.runAsync(); |
| 156 | + assert(tr.failed, 'Should have failed'); |
| 157 | + assert(tr.stdOutContained('Invalid download URL'), 'Should reject unsupported URLs'); |
| 158 | + }); |
| 159 | + |
| 160 | + it('Should fail with unparseable version format', async () => { |
| 161 | + let tp = path.join(__dirname, 'L0InvalidVersionFormatParseError.js'); |
| 162 | + let tr: MockTestRunner = new MockTestRunner(tp); |
| 163 | + await tr.runAsync(); |
| 164 | + assert(tr.failed, 'Should have failed'); |
| 165 | + assert(tr.stdOutContained('Invalid version format'), 'Should reject version that cannot be parsed'); |
| 166 | + }); |
| 167 | + |
| 168 | + it('Should fail when official Go version includes revision', async () => { |
| 169 | + let tp = path.join(__dirname, 'L0InvalidVersionFormatOfficialWithRevision.js'); |
| 170 | + let tr: MockTestRunner = new MockTestRunner(tp); |
| 171 | + await tr.runAsync(); |
| 172 | + assert(tr.failed, 'Should have failed'); |
| 173 | + assert(tr.stdOutContained('Official Go version must be'), 'Should reject revision syntax for official Go'); |
| 174 | + }); |
| 175 | + |
| 176 | + it('Should fail on download errors', async () => { |
| 177 | + let tp = path.join(__dirname, 'L0DownloadFailure.js'); |
| 178 | + let tr: MockTestRunner = new MockTestRunner(tp); |
| 179 | + await tr.runAsync(); |
| 180 | + assert(tr.failed, 'Should have failed'); |
| 181 | + assert(tr.stdOutContained('Failed to download version'), 'Should show download failure'); |
| 182 | + }); |
| 183 | + |
| 184 | + it('Should fail when go.dev API returns no matching version', async () => { |
| 185 | + let tp = path.join(__dirname, 'L0NoMatchingVersion.js'); |
| 186 | + let tr: MockTestRunner = new MockTestRunner(tp); |
| 187 | + await tr.runAsync(); |
| 188 | + assert(tr.failed, 'Should have failed'); |
| 189 | + assert(tr.stdOutContained('has no stable patch release yet'), 'Should indicate no stable release'); |
| 190 | + }); |
| 191 | + |
| 192 | + // Security tests for URL validation against SSRF attacks |
| 193 | + it('Should block URL parser confusion attack (@-based)', async () => { |
| 194 | + let tp = path.join(__dirname, 'L0SecurityURLValidation.js'); |
| 195 | + let tr: MockTestRunner = new MockTestRunner(tp); |
| 196 | + await tr.runAsync(); |
| 197 | + assert(tr.failed, 'Should have failed with malicious URL'); |
| 198 | + assert(tr.stdOutContained('Invalid download URL'), 'Should reject malicious URL with validation error'); |
| 199 | + }); |
| 200 | + |
| 201 | + // Environment variable tests for goDownloadUrl |
| 202 | + it('Should use GoTool.GoDownloadUrl environment variable when parameter is not set', async () => { |
| 203 | + let tp = path.join(__dirname, 'L0EnvVarOnly.js'); |
| 204 | + let tr: MockTestRunner = new MockTestRunner(tp); |
| 205 | + await tr.runAsync(); |
| 206 | + assert(tr.succeeded, 'Should have succeeded'); |
| 207 | + assert(tr.stdOutContained('Using GoTool.GoDownloadUrl environment variable'), 'Should log environment variable usage'); |
| 208 | + assert(tr.stdOutContained('go.dev/dl'), 'Should use URL from environment variable'); |
| 209 | + }); |
| 210 | + |
| 211 | + it('Should use parameter value when both parameter and environment variable are set', async () => { |
| 212 | + let tp = path.join(__dirname, 'L0BothParamAndEnvVar.js'); |
| 213 | + let tr: MockTestRunner = new MockTestRunner(tp); |
| 214 | + await tr.runAsync(); |
| 215 | + assert(tr.succeeded, 'Should have succeeded'); |
| 216 | + assert(tr.stdOutContained('Both goDownloadUrl parameter and GoTool.GoDownloadUrl environment variable are set'), 'Should log precedence decision'); |
| 217 | + assert(tr.stdOutContained('Correctly using parameter URL over environment variable'), 'Should use parameter URL'); |
| 218 | + }); |
| 219 | + |
| 220 | + it('Should fail with invalid URL in GoTool.GoDownloadUrl environment variable', async () => { |
| 221 | + let tp = path.join(__dirname, 'L0InvalidEnvVarUrl.js'); |
| 222 | + let tr: MockTestRunner = new MockTestRunner(tp); |
| 223 | + await tr.runAsync(); |
| 224 | + assert(tr.failed, 'Should have failed'); |
| 225 | + assert(tr.stdOutContained('Invalid download URL'), 'Should reject unsupported URL from environment variable'); |
15 | 226 | }); |
16 | 227 | }); |
0 commit comments