Skip to content
tmatis edited this page Mar 26, 2023 · 1 revision

E2E

Overview

The end-to-end tests are written in nodejs with jest and typescript. They are located in the e2e directory. Their purpose is to test the whole project from the host to the library.

How to add tests

Put two new files in the e2e/mains directory. One file will contain the code to test and the other one will contain the test code.

For example, if you want to test the basic.c file, you will create two files: basic.c and basic.test.ts.

basic.c:

/* Funcheck - A tool for checking functions calls return protections
 * Copyright (C) 2023  Theo Matis
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
    char *str = strdup("Hello world\n");
    if (!str)
        return EXIT_FAILURE;
    printf("%s", str);
    free(str);
    return EXIT_SUCCESS;
}

basic.test.ts:

/* Funcheck - A tool for checking functions calls return protections
 * Copyright (C) 2023  Theo Matis
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

import buildAndRun, { OUTPUT_NAME } from '../src/build-and-run';
import { FuncheckType } from '../src/types/funcheck.type';

const FILE_NAME = 'mains/basic.c';

describe(FILE_NAME, () => {
  const output = buildAndRun([FILE_NAME], {});

  it('should be a valid json', () => {
    expect(() => JSON.parse(output)).not.toThrow();
  });

  const outputObject: FuncheckType = JSON.parse(output);

  describe('outputObject', () => {
    it('should have a valid version', () => {
      expect(outputObject['funcheck-version']).toBeDefined();
    });

    it('should have a valid program name', () => {
      expect(outputObject.program).toBe(OUTPUT_NAME);
    });

    it('should have a valid arguments', () => {
      expect(outputObject.arguments).toEqual([]);
    });
  });

  describe('function fetch', () => {
    const functionFetch = outputObject['function-fetch'];

    it('should be defined', () => {
      expect(functionFetch).toBeDefined();
    });

    it('should have a valid output', () => {
      expect(functionFetch.output).toBe('Hello world\n');
    });

    it('should have a valid return code', () => {
      expect(functionFetch['return-code']).toBe(0);
    });

    it('should have a valid function detected count', () => {
      expect(functionFetch['function-detected-count']).toBe(1);
    });
  });

  describe('function tests', () => {
    const functionTests = outputObject['function-tests'];

    it('should be defined', () => {
      expect(functionTests).toBeDefined();
    });

    it('should have a length of 1', () => {
      expect(functionTests.length).toBe(1);
    });

    describe('function test', () => {
      const functionTest = functionTests[0];

      it('should have a valid function name', () => {
        expect(functionTest['function-name']).toBe('strdup');
      });

      it('should have a valid return code', () => {
        expect(functionTest['return-code']).toBe(1);
      });

      it('should have a valid function backtrace', () => {
        expect(functionTest['function-backtrace']).toBeDefined();

        const functionBacktrace = functionTest['function-backtrace'];
        expect(functionBacktrace.length).toBe(1);

        const backtrace = functionBacktrace[0];
        expect(backtrace.address).toBeDefined();
        expect(backtrace.function).toBe('main');
        expect(backtrace.file).toBe(FILE_NAME);
        expect(backtrace.line).toBe(24);
        expect(backtrace.column).toBe(17);
      });
    });
  });
});
Clone this wiki locally