diff --git a/libraries/analysis-solidity/lib/instrumentation/Instrumenter.ts b/libraries/analysis-solidity/lib/instrumentation/Instrumenter.ts index 90e1a0f..331fc96 100644 --- a/libraries/analysis-solidity/lib/instrumentation/Instrumenter.ts +++ b/libraries/analysis-solidity/lib/instrumentation/Instrumenter.ts @@ -23,10 +23,12 @@ import { InstrumentationData } from "./datastructures/InstrumentationData"; import * as SolidityParser from "@solidity-parser/parser"; import { ASTNode } from "@solidity-parser/parser/dist/src/ast-types"; -import Injector from "./injector"; // Local copy -import preprocess from "solidity-coverage/lib/preprocessor"; - -import parse from "./parse"; // Local copy +// eslint-disable-next-line @typescript-eslint/no-var-requires, unicorn/prefer-module +const Injector = require("./injector"); // Local copy +// eslint-disable-next-line @typescript-eslint/no-var-requires, unicorn/prefer-module +const preprocess = require("solidity-coverage/lib/preprocessor"); +// eslint-disable-next-line @typescript-eslint/no-var-requires, unicorn/prefer-module +const parse = require("./parse"); // Local copy export class Instrumenter { protected instrumentationData; diff --git a/libraries/analysis-solidity/lib/types/Type.ts b/libraries/analysis-solidity/lib/types/Type.ts index 7601193..fee2f07 100644 --- a/libraries/analysis-solidity/lib/types/Type.ts +++ b/libraries/analysis-solidity/lib/types/Type.ts @@ -48,26 +48,26 @@ export type BaseType = { export type Address = { type: TypeEnum.ADDRESS; - stateMutability: StateMutability | null; + stateMutability?: StateMutability | undefined; }; export type Bool = { type: TypeEnum.BOOL; - stateMutability: StateMutability | null; + stateMutability?: StateMutability | undefined; }; export type Int = { type: TypeEnum.INT; bits: number; signed: true; - stateMutability: StateMutability | null; + stateMutability?: StateMutability | undefined; }; export type Uint = { type: TypeEnum.INT; bits: number; signed: false; - stateMutability: StateMutability | null; + stateMutability?: StateMutability | undefined; }; export type Fixed = { @@ -75,7 +75,7 @@ export type Fixed = { bits: number; signed: true; decimals: number; - stateMutability: StateMutability | null; + stateMutability?: StateMutability | undefined; }; export type Ufixed = { @@ -83,7 +83,7 @@ export type Ufixed = { bits: number; signed: false; decimals: number; - stateMutability: StateMutability | null; + stateMutability?: StateMutability | undefined; }; export type FixedSizeByteArray = { @@ -113,7 +113,7 @@ export type FunctionType = { parameters: Parameter[]; returns: Parameter[]; visibility: Visibility; - stateMutability: StateMutability | null; + stateMutability?: StateMutability | undefined; }; export type Mapping = { diff --git a/libraries/analysis-solidity/tsconfig.json b/libraries/analysis-solidity/tsconfig.json index 97ea4a6..a5dd55f 100644 --- a/libraries/analysis-solidity/tsconfig.json +++ b/libraries/analysis-solidity/tsconfig.json @@ -5,5 +5,5 @@ "rootDir": ".", "composite": true }, - "include": ["**/*.ts"] + "include": ["**/*.ts", "**/*.js"] } diff --git a/libraries/search-solidity/lib/search/SolidityExecutionResult.ts b/libraries/search-solidity/lib/search/SolidityExecutionResult.ts index 1fcf7fa..36942e1 100644 --- a/libraries/search-solidity/lib/search/SolidityExecutionResult.ts +++ b/libraries/search-solidity/lib/search/SolidityExecutionResult.ts @@ -135,7 +135,7 @@ export class SolidityExecutionResult implements ExecutionResult { /** * @inheritDoc */ - public getTraces(): Datapoint[] { + public getTraces(): Trace[] { return this._traces; } diff --git a/libraries/search-solidity/lib/testbuilding/SolidityDecoder.ts b/libraries/search-solidity/lib/testbuilding/SolidityDecoder.ts index 98ba327..1e2cb75 100644 --- a/libraries/search-solidity/lib/testbuilding/SolidityDecoder.ts +++ b/libraries/search-solidity/lib/testbuilding/SolidityDecoder.ts @@ -54,7 +54,7 @@ export class SolidityDecoder implements Decoder { } } const formattedArguments = arguments_ - .map((a: PrimitiveStatement) => a.varName) + .map((a: PrimitiveStatement) => a.varName) .join(", "); const sender = (statement as ConstructorCall).getSender().getValue(); @@ -101,8 +101,8 @@ export class SolidityDecoder implements Decoder { throw new TypeError(`${statement} is not a primitive statement`); } - const primitive: PrimitiveStatement = - statement as PrimitiveStatement; + const primitive: PrimitiveStatement = + statement as PrimitiveStatement; // TODO what happened to float support? if ( statement.type.type.startsWith("int") || @@ -369,7 +369,7 @@ export class SolidityDecoder implements Decoder { for (const variableName of gene.varNames) { functionCalls.push( `\t\tawait fs.writeFileSync('${path.join( - CONFIG.tempLogDirectory, + this.tempLogDirectory, ind.id, variableName )}', '' + ${variableName})` @@ -379,7 +379,7 @@ export class SolidityDecoder implements Decoder { for (const variableName of gene.varNames) { testString.push( `\t\tawait fs.writeFileSync('${path.join( - CONFIG.tempLogDirectory, + this.tempLogDirectory, ind.id, variableName )}', '' + ${variableName})` @@ -403,7 +403,7 @@ export class SolidityDecoder implements Decoder { if (addLogs) { testString.push(`} catch (e) {`, `await fs.writeFileSync('${path.join( - CONFIG.tempLogDirectory, + this.tempLogDirectory, ind.id, "error" )}', '' + e.stack)`, diff --git a/libraries/search-solidity/lib/testcase/sampling/SolidityRandomSampler.ts b/libraries/search-solidity/lib/testcase/sampling/SolidityRandomSampler.ts index 72f8bea..eea5835 100644 --- a/libraries/search-solidity/lib/testcase/sampling/SolidityRandomSampler.ts +++ b/libraries/search-solidity/lib/testcase/sampling/SolidityRandomSampler.ts @@ -44,13 +44,12 @@ import { TypeEnum, Ufixed, Uint, - Visibility, } from "@syntest/analysis-solidity"; import { TargetType } from "@syntest/analysis"; import { FunctionTarget } from "@syntest/analysis-solidity"; import { IntegerStatement } from "../statements/primitive/IntegerStatement"; import { DynamicSizeByteArrayStatement } from "../statements/primitive/DynamicSizeByteArrayStatement"; -import { FixedSizeByteArrayStatement } from "../statements/complex/FixedSizeByteArrayStatement"; +import { FixedSizeByteArrayStatement } from "../statements/primitive/FixedSizeByteArrayStatement"; /** * SolidityRandomSampler class @@ -129,7 +128,13 @@ export class SolidityRandomSampler extends SoliditySampler { root, action.name, arguments_, - AddressStatement.getRandom() + this.sampleAddressStatement(depth + 1, { + name: 'address', + type: { + type: TypeEnum.ADDRESS, + stateMutability: undefined + } + }) ); } @@ -152,7 +157,13 @@ export class SolidityRandomSampler extends SoliditySampler { `${action.name}`, arguments_, [], - AddressStatement.getRandom() + this.sampleAddressStatement(depth + 1, { + name: 'address', + type: { + type: TypeEnum.ADDRESS, + stateMutability: undefined + } + }) ); const nCalls = prng.nextInt(1, 5); @@ -171,7 +182,13 @@ export class SolidityRandomSampler extends SoliditySampler { `${this._subject.name}`, [], [], - AddressStatement.getRandom() + this.sampleAddressStatement(depth + 1, { + name: 'address', + type: { + type: TypeEnum.ADDRESS, + stateMutability: undefined + } + }) ); const nCalls = prng.nextInt(1, 5); diff --git a/libraries/search-solidity/lib/testcase/sampling/SoliditySampler.ts b/libraries/search-solidity/lib/testcase/sampling/SoliditySampler.ts index e609e81..7665d4a 100644 --- a/libraries/search-solidity/lib/testcase/sampling/SoliditySampler.ts +++ b/libraries/search-solidity/lib/testcase/sampling/SoliditySampler.ts @@ -26,12 +26,11 @@ import { Statement } from "../statements/Statement"; import { SoliditySubject } from "../../search/SoliditySubject"; import { ConstantPool, RootContext } from "@syntest/analysis-solidity"; import { NumericStatement } from "../statements/primitive/NumericStatement"; -import { ArrayStatement } from "../statements/complex/ArrayStatement"; import { AddressStatement } from "../statements/primitive/AddressStatement"; import { BoolStatement } from "../statements/primitive/BoolStatement"; import { IntegerStatement } from "../statements/primitive/IntegerStatement"; import { StringStatement } from "../statements/primitive/StringStatement"; -import { FixedSizeByteArrayStatement } from "../statements/complex/FixedSizeByteArrayStatement"; +import { FixedSizeByteArrayStatement } from "../statements/primitive/FixedSizeByteArrayStatement"; import { DynamicSizeByteArrayStatement } from "../statements/primitive/DynamicSizeByteArrayStatement"; /** diff --git a/libraries/search-solidity/lib/testcase/statements/action/ActionStatement.ts b/libraries/search-solidity/lib/testcase/statements/action/ActionStatement.ts index 9a31f70..2c857ea 100644 --- a/libraries/search-solidity/lib/testcase/statements/action/ActionStatement.ts +++ b/libraries/search-solidity/lib/testcase/statements/action/ActionStatement.ts @@ -17,8 +17,6 @@ */ import { Statement } from "../Statement"; -import { EncodingSampler } from "@syntest/search"; -import { Encoding } from "@syntest/search"; import { Parameter } from "@syntest/analysis-solidity"; /** @@ -36,13 +34,6 @@ export abstract class ActionStatement extends Statement { this._arguments_ = arguments_; } - abstract mutate( - sampler: EncodingSampler, - depth: number - ): ActionStatement; - - abstract copy(): ActionStatement; - hasChildren(): boolean { return this._arguments_.length > 0; } diff --git a/libraries/search-solidity/lib/testcase/statements/action/ObjectFunctionCall.ts b/libraries/search-solidity/lib/testcase/statements/action/ObjectFunctionCall.ts index fc346b8..4bba533 100644 --- a/libraries/search-solidity/lib/testcase/statements/action/ObjectFunctionCall.ts +++ b/libraries/search-solidity/lib/testcase/statements/action/ObjectFunctionCall.ts @@ -93,12 +93,12 @@ export class ObjectFunctionCall extends ActionStatement { ); } - hasChildren(): boolean { + override hasChildren(): boolean { // since every object function call has an instance there must be atleast one child return true; } - getChildren(): Statement[] { + override getChildren(): Statement[] { return [...this.arguments_]; } diff --git a/libraries/search-solidity/lib/testcase/statements/primitive/FixedSizeByteArrayStatement.ts b/libraries/search-solidity/lib/testcase/statements/primitive/FixedSizeByteArrayStatement.ts index 87bdb25..4717ebc 100644 --- a/libraries/search-solidity/lib/testcase/statements/primitive/FixedSizeByteArrayStatement.ts +++ b/libraries/search-solidity/lib/testcase/statements/primitive/FixedSizeByteArrayStatement.ts @@ -20,6 +20,7 @@ import { prng } from "@syntest/prng"; import { PrimitiveStatement } from "../primitive/PrimitiveStatement"; import { FixedSizeByteArray, Parameter } from "@syntest/analysis-solidity"; import { SoliditySampler } from "../../sampling/SoliditySampler"; +import { Statement } from "../Statement"; /** * Special statement specific to solidity contracts diff --git a/libraries/search-solidity/lib/testcase/statements/primitive/NumericStatement.ts b/libraries/search-solidity/lib/testcase/statements/primitive/NumericStatement.ts index 64cf4d6..29c7f4b 100644 --- a/libraries/search-solidity/lib/testcase/statements/primitive/NumericStatement.ts +++ b/libraries/search-solidity/lib/testcase/statements/primitive/NumericStatement.ts @@ -19,10 +19,8 @@ import { prng } from "@syntest/prng"; import BigNumber from "bignumber.js"; -import { ConstantPool } from "../../../../../analysis-solidity/lib/constant/ConstantPool"; import { PrimitiveStatement } from "./PrimitiveStatement"; import { Fixed, Parameter, Ufixed } from "@syntest/analysis-solidity"; -import { SolidityArguments } from "../../../SolidityLauncher"; import { SoliditySampler } from "../../sampling/SoliditySampler"; import { Statement } from "../Statement"; diff --git a/libraries/search-solidity/lib/util/fileSystem.ts b/libraries/search-solidity/lib/util/fileSystem.ts index 5c4b7fd..532210b 100644 --- a/libraries/search-solidity/lib/util/fileSystem.ts +++ b/libraries/search-solidity/lib/util/fileSystem.ts @@ -16,13 +16,12 @@ * limitations under the License. */ -import { existsSync, mkdirSync, rmdirSync, writeFileSync } from "node:fs"; +import { existsSync, writeFileSync } from "node:fs"; -import { CONFIG, getUserInterface } from "@syntest/search"; -import globby = require("globby"); -import recursive = require("recursive-readdir"); +// import globby = require("globby"); +// import recursive = require("recursive-readdir"); -export async function createTruffleConfig() { +export async function createTruffleConfig(temporaryTestDirectory: string) { const filepath = "./truffle-config.js"; if (existsSync(filepath)) { @@ -32,32 +31,32 @@ export async function createTruffleConfig() { await writeFileSync( filepath, `module.exports = { - test_directory: "${CONFIG.tempTestDirectory}", + test_directory: "${temporaryTestDirectory}", plugins: ["@syntest/solidity"] };` ); } -/** - * Returns a list of test files to pass to mocha. - * @param {Object} config truffleConfig - * @return {String[]} list of files to pass to mocha - */ -export async function getTestFilePaths(config) { - let target; - - // Handle --file cli option (subset of tests) - typeof config.file === "string" - ? (target = globby.sync([config.file])) - : (target = await recursive(config.testDir)); - - // Filter native solidity tests and warn that they're skipped - const solregex = /.*\.(sol)$/; - const hasSols = target.filter((f) => f.match(solregex) != undefined); - - if (hasSols.length > 0) LOGGER.info("sol-tests " + [hasSols.length]); - - // Return list of test files - const testregex = /.*\.(js|ts|es|es6|jsx)$/; - return target.filter((f) => f.match(testregex) != undefined); -} +// /** +// * Returns a list of test files to pass to mocha. +// * @param {Object} config truffleConfig +// * @return {String[]} list of files to pass to mocha +// */ +// export async function getTestFilePaths(config) { +// let target; + +// // Handle --file cli option (subset of tests) +// typeof config.file === "string" +// ? (target = globby.sync([config.file])) +// : (target = await recursive(config.testDir)); + +// // Filter native solidity tests and warn that they're skipped +// const solregex = /.*\.(sol)$/; +// const hasSols = target.filter((f) => f.match(solregex) != undefined); + +// if (hasSols.length > 0) console.info("sol-tests " + [hasSols.length]); + +// // Return list of test files +// const testregex = /.*\.(js|ts|es|es6|jsx)$/; +// return target.filter((f) => f.match(testregex) != undefined); +// } diff --git a/libraries/search-solidity/test/testcase/AddressStatement.test.ts b/libraries/search-solidity/test/testcase/AddressStatement.test.ts deleted file mode 100644 index 03c21af..0000000 --- a/libraries/search-solidity/test/testcase/AddressStatement.test.ts +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright 2020-2023 Delft University of Technology and SynTest contributors - * - * This file is part of SynTest Framework - SynTest Solidity. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -import * as chai from "chai"; - -import { AddressStatement } from "../../lib/testcase/statements/primitive/AddressStatement"; - -const expect = chai.expect; - -describe("AddressStatement", () => { - it("should not have a null address", () => { - const statement = AddressStatement.getRandom(); - - expect(statement != undefined); - }); -}); diff --git a/libraries/search-solidity/test/testcase/primitive/String.test.ts b/libraries/search-solidity/test/testcase/primitive/String.test.ts index 0e64723..63b7bcb 100644 --- a/libraries/search-solidity/test/testcase/primitive/String.test.ts +++ b/libraries/search-solidity/test/testcase/primitive/String.test.ts @@ -33,49 +33,48 @@ const mockSampler = new SolidityRandomSampler( "abcdef", 10, 0.5, - true + true, + 10 ); +function getRandom() { + return new StringStatement(undefined, "0000", "abcde") +} + describe("StringStatement", () => { it("Add mutation increases statement's length by one", () => { - const statement = StringStatement.getRandom(); + const statement = getRandom(); const mutated = statement.addMutation(mockSampler); expect(statement.value.length + 1 === mutated.value.length); }); it("Remove mutation decreases statement's length by one", () => { - const statement = StringStatement.getRandom(); - const mutated = statement.removeMutation(mockSampler); + const statement = getRandom(); + const mutated = statement.removeMutation(); expect(statement.value.length - 1 === mutated.value.length); }); it("Replace mutation doesnt affect statement's length", () => { - const statement = StringStatement.getRandom(); + const statement = getRandom(); const mutated = statement.replaceMutation(mockSampler); expect(statement.value.length === mutated.value.length); }); it("Delta mutation doesnt affect statement's length", () => { - const statement = StringStatement.getRandom(); + const statement = getRandom(); const mutated = statement.deltaMutation(mockSampler); expect(statement.value.length - 1 === mutated.value.length); }); it("Copy gives exact same value", () => { - const statement = StringStatement.getRandom(); + const statement = getRandom(); const copy = statement.copy(); expect(statement.value).to.equal(copy.value); }); - it("Mutate gives exact other value", () => { - const statement = StringStatement.getRandom(); - const mutation = statement.mutate(mockSampler); - - expect(statement.value != mutation.value); - }); });