github.com/google/syzkaller@v0.0.0-20251211124644-a066d2bc4b02/syz-verifier/stats_test.go (about)

     1  // Copyright 2021 syzkaller project authors. All rights reserved.
     2  // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
     3  
     4  // TODO: switch syz-verifier to use syz-fuzzer.
     5  
     6  //go:build ignore
     7  
     8  package main
     9  
    10  import (
    11  	"testing"
    12  
    13  	"github.com/google/go-cmp/cmp"
    14  )
    15  
    16  func dummyStats() *Stats {
    17  	return (&Stats{
    18  		TotalProgs:          StatUint64{uint64: 24},
    19  		TotalCallMismatches: StatUint64{uint64: 10},
    20  		FlakyProgs:          StatUint64{uint64: 4},
    21  		MismatchingProgs:    StatUint64{uint64: 6},
    22  		Calls: StatMapStringToCallStats{
    23  			mapStringToCallStats: mapStringToCallStats{
    24  				"foo": {"foo", 2, 8, map[ReturnState]bool{
    25  					returnState(1, 7): true,
    26  					returnState(3, 7): true}},
    27  				"bar": {"bar", 5, 6, map[ReturnState]bool{
    28  					crashedReturnState(): true,
    29  					returnState(10, 7):   true,
    30  					returnState(22, 7):   true}},
    31  				"tar": {"tar", 3, 4, map[ReturnState]bool{
    32  					returnState(31, 7): true,
    33  					returnState(17, 7): true,
    34  					returnState(5, 7):  true}},
    35  				"biz": {"biz", 0, 2, map[ReturnState]bool{}},
    36  			},
    37  		},
    38  	}).Init()
    39  }
    40  
    41  func TestGetCallStatsTextDescription(t *testing.T) {
    42  	tests := []struct {
    43  		name, call, report string
    44  	}{
    45  		{
    46  			name:   "report for unsupported call",
    47  			call:   "read",
    48  			report: "",
    49  		},
    50  		{
    51  			name: "report for supported call",
    52  			call: "foo",
    53  			report: "statistics for foo:\n" +
    54  				"\t↳ mismatches of foo / occurrences of foo: 2 / 8 (25.00 %)\n" +
    55  				"\t↳ mismatches of foo / total number of mismatches: 2 / 10 (20.00 %)\n" +
    56  				"\t↳ 2 distinct states identified: " +
    57  				"[\"Flags: 7, Errno: 1 (operation not permitted)\" \"Flags: 7, Errno: 3 (no such process)\"]\n",
    58  		},
    59  	}
    60  
    61  	for _, test := range tests {
    62  		s := dummyStats()
    63  		t.Run(test.name, func(t *testing.T) {
    64  			got, want := s.getCallStatsTextDescription(test.call), test.report
    65  			if diff := cmp.Diff(want, got); diff != "" {
    66  				t.Errorf("s.getCallStatsTextDescription mismatch (-want +got):\n%s", diff)
    67  			}
    68  		})
    69  	}
    70  }
    71  
    72  func TestGetTextDescription(t *testing.T) {
    73  	stats := dummyStats()
    74  
    75  	got, want := stats.GetTextDescription(float64(10)),
    76  		"total number of mismatches / total number of calls "+
    77  			"executed: 10 / 20 (50.00 %)\n\n"+
    78  			"programs / minute: 2.40\n\n"+
    79  			"true mismatching programs: 6 / total number of programs: 24 (25.00 %)\n"+
    80  			"flaky programs: 4 / total number of programs: 24 (16.67 %)\n\n"+
    81  			"statistics for bar:\n"+
    82  			"\t↳ mismatches of bar / occurrences of bar: 5 / 6 (83.33 %)\n"+
    83  			"\t↳ mismatches of bar / total number of mismatches: 5 / 10 (50.00 %)\n"+
    84  			"\t↳ 3 distinct states identified: "+
    85  			"[\"Crashed\" \"Flags: 7, Errno: 10 (no child processes)\" "+
    86  			"\"Flags: 7, Errno: 22 (invalid argument)\"]\n\n"+
    87  			"statistics for tar:\n"+
    88  			"\t↳ mismatches of tar / occurrences of tar: 3 / 4 (75.00 %)\n"+
    89  			"\t↳ mismatches of tar / total number of mismatches: 3 / 10 (30.00 %)\n"+
    90  			"\t↳ 3 distinct states identified: "+
    91  			"[\"Flags: 7, Errno: 17 (file exists)\" "+
    92  			"\"Flags: 7, Errno: 31 (too many links)\" "+
    93  			"\"Flags: 7, Errno: 5 (input/output error)\"]\n\n"+
    94  			"statistics for foo:\n"+
    95  			"\t↳ mismatches of foo / occurrences of foo: 2 / 8 (25.00 %)\n"+
    96  			"\t↳ mismatches of foo / total number of mismatches: 2 / 10 (20.00 %)\n"+
    97  			"\t↳ 2 distinct states identified: "+
    98  			"[\"Flags: 7, Errno: 1 (operation not permitted)\" \"Flags: 7, Errno: 3 (no such process)\"]\n\n"
    99  
   100  	if diff := cmp.Diff(want, got); diff != "" {
   101  		t.Errorf("s.GetTextDescription mismatch (-want +got):\n%s", diff)
   102  	}
   103  }