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