Skip to content

Commit

Permalink
mayhem-enhanced gtest example
Browse files Browse the repository at this point in the history
  • Loading branch information
xansec committed Sep 13, 2024
1 parent d6e8e18 commit 030cd0d
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ jobs:
uses: ForAllSecure/mcode-action@v1
with:
mayhem-token: ${{ secrets.MAYHEM_TOKEN }}
args: --image ${{ needs.build.outputs.image }} --file ${{ matrix.mayhemfile }} --duration 300
args: --image ${{ needs.build.outputs.image }} --file ${{ matrix.mayhemfile }} --regression --duration 60
sarif-output: sarif

- name: Upload SARIF file(s)
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ jobs:

- name: Run bazel for Mayhem package
run: |
bazel build --action_env=MAYHEM_URL=${{ env.MAYHEM_URL }} --action_env=MAYHEM_TOKEN=${{ secrets.MAYHEM_TOKEN }} //mayhem:run_calculator_package
bazel build --action_env=MAYHEM_URL=${{ env.MAYHEM_URL }} --action_env=MAYHEM_TOKEN=${{ secrets.MAYHEM_TOKEN }} //mayhem:run_test_calculator_package
env:
MAYHEM_URL: ${{ env.MAYHEM_URL }}
MAYHEM_TOKEN: ${{ secrets.MAYHEM_TOKEN }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:

- name: Run bazel for Mayhem package
run: |
bazel build --action_env=MAYHEM_URL=${{ env.MAYHEM_URL }} --action_env=MAYHEM_TOKEN=${{ secrets.MAYHEM_TOKEN }} //mayhem:run_calculator_package
bazel build --action_env=MAYHEM_URL=${{ env.MAYHEM_URL }} --action_env=MAYHEM_TOKEN=${{ secrets.MAYHEM_TOKEN }} //mayhem:run_test_calculator_package
env:
MAYHEM_URL: ${{ env.MAYHEM_URL }}
MAYHEM_TOKEN: ${{ secrets.MAYHEM_TOKEN }}
Expand Down
11 changes: 5 additions & 6 deletions mayhem/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ mayhem_run(
project = "mayhem-bazel-example",
target = "calculator_package",
image = "ubuntu:latest",
regression = True,
wait = True,
duration = "120"
target_path = ":calculator_package",

)
Expand All @@ -75,14 +74,14 @@ mayhem_run(
owner = "training",
project = "mayhem-bazel-example",
target = "fuzz_calculator_package",
image = "chainguard/glibc-dynamic",
image = "kubler/glibc",
target_path = ":fuzz_calculator_package",
testonly = True,
)

mayhem_package(
name = "test_calculator_package",
binary = "//test:test_calculator",
binary = "//test:combined_test_calculator",
testonly = True, # Needed to package test binaries
)

Expand All @@ -91,9 +90,9 @@ mayhem_run(
owner = "training",
project = "mayhem-bazel-example",
target = "test_calculator_package",
image = "ubuntu:latest",
image = "kubler/glibc",
target_path = ":test_calculator_package",
wait = True,
duration = "30",
regression = True,
testonly = True,
)
16 changes: 12 additions & 4 deletions test/BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
load("@rules_mayhem//mayhem:mayhem.bzl", "mayhem_run", "mayhem_package")

package(default_visibility = ["//visibility:public"])

cc_test(
Expand All @@ -19,6 +17,16 @@ cc_test(
deps = [
"//main:calculator_lib",
],
data = [":testsuite/1"],
args = ["$(location :testsuite/1)"],
data = [":testsuite"],
)

cc_test(
name = "combined_test_calculator",
size = "small",
srcs = ["combined_test_calculator.cc"],
deps = [
"@googletest//:gtest_main",
"//main:calculator_lib",
],
data = [":testsuite"],
)
80 changes: 80 additions & 0 deletions test/combined_test_calculator.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <fstream>
#include <gtest/gtest.h>

extern "C" {
#include "main/calculator.h"
}

void test_add(int x, int y) {
assert(add(x, y) == x + y);
}

void test_subtract(int x, int y) {
assert(subtract(x, y) == x - y);
}

void test_multiply(int x, int y) {
assert(multiply(x, y) == x * y);
}

void test_divide(int x, int y) {
assert(divide(x, y) == x / y);
}

void test_factor_game(int x, int y) {
factor_game(x, y); // bug is hidden in factor_game() itself, no need for extra assert() here
}

TEST(CalculatorTest, TestAdd) {
test_add(1, 2);
}

TEST(CalculatorTest, TestSubtract) {
test_subtract(2, 1);
}

TEST(CalculatorTest, TestMultiply) {
test_multiply(3, 2);
}

TEST(CalculatorTest, TestDivide) {
test_divide(6, 2);
}

TEST(CalculatorTest, TestFactorGame) {
test_factor_game(6, 2);
}


int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);

// Check for file input
if (argc > 1) {
std::ifstream inputFile(argv[1]);
if (!inputFile.is_open()) {
std::cerr << "Failed to open input file: " << argv[1] << "\n";
return 1;
}

int a, b;

// Assume the input file contains our two variables
inputFile >> a >> b;

test_add(a, b);
test_subtract(a, b);
test_multiply(a, b);
test_divide(a, b);
test_factor_game(a, b);

// Skip running the default tests since we're using file input
return 0;
}

// If no file input is provided, run standard test fixtures
return RUN_ALL_TESTS();
}
15 changes: 13 additions & 2 deletions test/gtest_calculator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
extern "C" {
#include "main/calculator.h"
}
// Simple unit test:
// "with this specific input, we expect this specific output"

TEST(CalculatorTest, TestAdd) {
EXPECT_EQ(add(1, 2), 3);
Expand All @@ -19,4 +17,17 @@ TEST(CalculatorTest, TestSubtract) {

TEST(CalculatorTest, TestMultiply) {
EXPECT_EQ(multiply(3, 2), 6);
}

TEST(CalculatorTest, TestDivide) {
EXPECT_EQ(divide(6, 2), 3);
}

TEST(CalculatorTest, TestFactorGame) {
EXPECT_EQ(factor_game(6, 2), 0);
}

int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
37 changes: 18 additions & 19 deletions test/test_calculator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ extern "C" {
#include "main/calculator.h"
}

// Dynamic testing with Mayhem:
// "with any given input, what happens? (Implicitly, we expect no crashes)"

void test_add(int x, int y) {
// assert(add(x, y) == x + y);
add(x, y);
Expand All @@ -24,6 +21,16 @@ void test_multiply(int x, int y) {
multiply(x, y);
}

void test_divide(int x, int y) {
// assert(divide(x, y) == x / y);
divide(x, y);
}

void test_factor_game(int x, int y) {
// assert(factor_game(x, y) == 0);
factor_game(x, y);
}

int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: %s <input_file>\n", argv[0]);
Expand All @@ -36,24 +43,16 @@ int main(int argc, char *argv[]) {
return 1;
}

int x, y, s;
int x, y;
char buf[64];
fgets(buf, sizeof(buf), file);
sscanf(buf, "%d %d %d", &x, &y ,&s);

switch (s % 3) {
case 0:
test_add(x, y);
break;
case 1:
test_subtract(x, y);
break;
case 2:
test_multiply(x, y);
break;
default:
return 0;
}
sscanf(buf, "%d %d", &x, &y);

test_add(x, y);
test_subtract(x, y);
test_multiply(x, y);
test_divide(x, y);
test_factor_game(x, y);

fclose(file);
return 0;
Expand Down
1 change: 0 additions & 1 deletion test/testsuite/1

This file was deleted.

1 change: 1 addition & 0 deletions test/testsuite/crash
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0 +0 5 �
1 change: 1 addition & 0 deletions test/testsuite/pass
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1 2

0 comments on commit 030cd0d

Please sign in to comment.