|
| 1 | +import { Request, Response } from "express"; |
| 2 | +import { expect } from "chai"; |
| 3 | +import * as sinon from "sinon"; |
| 4 | +import { validateCompilerVersion } from "../../../src/server/apiv2/middlewares"; |
| 5 | +import { InvalidParametersError } from "../../../src/server/apiv2/errors"; |
| 6 | + |
| 7 | +describe("validateCompilerVersion middleware", () => { |
| 8 | + let req: Partial<Request>; |
| 9 | + let res: Partial<Response>; |
| 10 | + let next: sinon.SinonSpy; |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + req = { |
| 14 | + body: {}, |
| 15 | + }; |
| 16 | + res = {}; |
| 17 | + next = sinon.spy(); |
| 18 | + }); |
| 19 | + |
| 20 | + // Test helper functions |
| 21 | + const testValidVersions = ( |
| 22 | + versions: string[], |
| 23 | + language: string, |
| 24 | + description: string, |
| 25 | + ) => { |
| 26 | + it(description, () => { |
| 27 | + versions.forEach((version) => { |
| 28 | + next.resetHistory(); |
| 29 | + const originalVersion = version; |
| 30 | + |
| 31 | + req.body = { |
| 32 | + compilerVersion: version, |
| 33 | + stdJsonInput: { language }, |
| 34 | + }; |
| 35 | + |
| 36 | + validateCompilerVersion(req as Request, res as Response, next); |
| 37 | + |
| 38 | + // Check that v prefix is stripped |
| 39 | + const expectedVersion = originalVersion.startsWith("v") |
| 40 | + ? originalVersion.slice(1) |
| 41 | + : originalVersion; |
| 42 | + |
| 43 | + expect(req.body.compilerVersion).to.equal(expectedVersion); |
| 44 | + expect(next.calledOnce).to.be.true; |
| 45 | + }); |
| 46 | + }); |
| 47 | + }; |
| 48 | + |
| 49 | + const testInvalidVersions = ( |
| 50 | + versions: string[], |
| 51 | + language: string, |
| 52 | + description: string, |
| 53 | + ) => { |
| 54 | + it(description, () => { |
| 55 | + versions.forEach((version) => { |
| 56 | + req.body = { |
| 57 | + compilerVersion: version, |
| 58 | + stdJsonInput: { language }, |
| 59 | + }; |
| 60 | + |
| 61 | + try { |
| 62 | + validateCompilerVersion(req as Request, res as Response, next); |
| 63 | + expect.fail(`Expected ${version} to throw an error but it didn't`); |
| 64 | + } catch (error: any) { |
| 65 | + expect(error).to.be.instanceOf(InvalidParametersError); |
| 66 | + expect(error.statusCode).to.equal(400); |
| 67 | + expect(error.payload.customCode).to.equal("invalid_parameter"); |
| 68 | + } |
| 69 | + }); |
| 70 | + }); |
| 71 | + }; |
| 72 | + |
| 73 | + describe("Basic validation", () => { |
| 74 | + it("should throw error when compilerVersion is missing", () => { |
| 75 | + req.body = {}; |
| 76 | + |
| 77 | + try { |
| 78 | + validateCompilerVersion(req as Request, res as Response, next); |
| 79 | + expect.fail("Expected function to throw an error but it didn't"); |
| 80 | + } catch (error: any) { |
| 81 | + expect(error).to.be.instanceOf(InvalidParametersError); |
| 82 | + expect(error.statusCode).to.equal(400); |
| 83 | + expect(error.payload.customCode).to.equal("invalid_parameter"); |
| 84 | + } |
| 85 | + }); |
| 86 | + |
| 87 | + it("should throw error when compilerVersion is empty string", () => { |
| 88 | + req.body = { compilerVersion: "" }; |
| 89 | + |
| 90 | + try { |
| 91 | + validateCompilerVersion(req as Request, res as Response, next); |
| 92 | + expect.fail("Expected function to throw an error but it didn't"); |
| 93 | + } catch (error: any) { |
| 94 | + expect(error).to.be.instanceOf(InvalidParametersError); |
| 95 | + expect(error.statusCode).to.equal(400); |
| 96 | + expect(error.payload.customCode).to.equal("invalid_parameter"); |
| 97 | + } |
| 98 | + }); |
| 99 | + |
| 100 | + it("should strip 'v' prefix from version", () => { |
| 101 | + req.body = { |
| 102 | + compilerVersion: "v0.8.7+commit.e28d00a7", |
| 103 | + stdJsonInput: { language: "Solidity" }, |
| 104 | + }; |
| 105 | + |
| 106 | + validateCompilerVersion(req as Request, res as Response, next); |
| 107 | + |
| 108 | + expect(req.body.compilerVersion).to.equal("0.8.7+commit.e28d00a7"); |
| 109 | + expect(next.calledOnce).to.be.true; |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + describe("Solidity version validation", () => { |
| 114 | + const validSolidityVersions = [ |
| 115 | + // Regular versions |
| 116 | + "0.8.7+commit.e28d00a7", |
| 117 | + "0.8.30+commit.73712a01", |
| 118 | + "0.8.29+commit.ab55807c", |
| 119 | + "0.8.12+commit.f00d7308", |
| 120 | + "0.8.31+commit.73712a01", |
| 121 | + // Nightly versions |
| 122 | + "0.8.31-nightly.2025.8.11+commit.635fe8f8", |
| 123 | + "0.8.31-nightly.2025.7.31+commit.aef512f8", |
| 124 | + "0.8.31-nightly.2025.6.2+commit.5c3c9578", |
| 125 | + // With v prefix (should be stripped) |
| 126 | + "v0.8.20+commit.a1b79de6", |
| 127 | + "v0.8.7+commit.e28d00a7", |
| 128 | + ]; |
| 129 | + |
| 130 | + const invalidSolidityVersions = [ |
| 131 | + // Wrong commit hash length |
| 132 | + "0.8.7+commit.e28d00a", // too short |
| 133 | + "0.8.7+commit.e28d00a7123", // too long |
| 134 | + // Invalid commit hash characters |
| 135 | + "0.8.7+commit.g28d00a7", // contains 'g' |
| 136 | + "0.8.7+commit.E28D00A7", // uppercase |
| 137 | + "0.8.7+commit.e28D00A7", // mixed case |
| 138 | + // Missing commit hash |
| 139 | + "0.8.7", |
| 140 | + "0.8.31-nightly.2025.8.11", |
| 141 | + // Malformed nightly format |
| 142 | + "0.8.31-nightly.25.8.11+commit.635fe8f8", // 2-digit year |
| 143 | + "0.8.31-nightly.2025.8+commit.635fe8f8", // missing day |
| 144 | + // Invalid version format |
| 145 | + "invalid-version", |
| 146 | + "0.8+commit.e28d00a7", |
| 147 | + ]; |
| 148 | + |
| 149 | + testValidVersions( |
| 150 | + validSolidityVersions, |
| 151 | + "Solidity", |
| 152 | + "should accept all valid Solidity versions", |
| 153 | + ); |
| 154 | + |
| 155 | + testInvalidVersions( |
| 156 | + invalidSolidityVersions, |
| 157 | + "Solidity", |
| 158 | + "should reject all invalid Solidity versions", |
| 159 | + ); |
| 160 | + }); |
| 161 | + |
| 162 | + describe("Vyper version validation", () => { |
| 163 | + const validVyperVersions = [ |
| 164 | + // With v prefix |
| 165 | + "v0.3.10", |
| 166 | + "v0.4.1rc1", |
| 167 | + "v0.4.1b4", |
| 168 | + "v0.1.0-beta.17", |
| 169 | + // Without v prefix |
| 170 | + "0.3.8+commit.036f1536", |
| 171 | + "0.4.2+commit.c216787f", |
| 172 | + "0.3.10", |
| 173 | + // Various suffixes |
| 174 | + "0.4.1rc1", |
| 175 | + "0.4.1b4", |
| 176 | + "0.1.0-beta.17", |
| 177 | + ]; |
| 178 | + |
| 179 | + testValidVersions( |
| 180 | + validVyperVersions, |
| 181 | + "Vyper", |
| 182 | + "should accept all Vyper versions (permissive validation)", |
| 183 | + ); |
| 184 | + }); |
| 185 | +}); |
0 commit comments