Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Frontend-go] Add unit test for channels and goroutine #1977

Merged
merged 2 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/fuzz_introspector/frontends/frontend_go.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,7 @@ def _detect_variable_type(
args = child.child_by_field_name('arguments')
target_name = self._process_call_expr_child(
call, all_funcs_meths)

if target_name in all_funcs_meths:
return all_funcs_meths[target_name].return_type

Expand All @@ -670,6 +671,16 @@ def _detect_variable_type(
if arg.type.endswith('identifier'):
return arg.text.decode()

elif target_name == 'make':
for arg in args.children:
type_node = arg.child_by_field_name('value')
if type_node:
return type_node.text.decode()

type_node = arg.child_by_field_name('element')
if type_node:
return type_node.text.decode()

# Selector expression
elif child.type == 'selector_expression':
target_name = self._process_call_expr_child(
Expand Down Expand Up @@ -705,6 +716,8 @@ def extract_local_variable_type(self,
for decl_node in exprs:
left = decl_node.child_by_field_name('left')
right = decl_node.child_by_field_name('right')
if not left or not right:
continue

for child in left.children:
if child.type == 'identifier':
Expand All @@ -722,13 +735,16 @@ def extract_local_variable_type(self,
if child.type == 'range_clause':
left = child.child_by_field_name('left')
right = child.child_by_field_name('right')
if not left or not right:
continue

for left_child in left.children:
if left_child.type == 'identifier':
decl_name = left_child.text.decode()

if right.type == 'identifier':
decl_type = self.var_map[right.text.decode()]
decl_type = self.var_map.get(
right.text.decode(), '')
if '[' in decl_type and ']' in decl_type:
decl_type = decl_type.split(']', 1)[-1]
elif decl_type == 'string':
Expand Down
74 changes: 74 additions & 0 deletions src/test/data/source-code/go/test-project-6/fuzzer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright 2025 Google LLC
//
// 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.
//

package channels

import (
"fmt"
"testing"
"time"
)

type Square struct {
Side float64
}

type Circle struct {
Radius float64
}

func (s Square) Describe() string {
return fmt.Sprintf("Square with side length %.2f", s.Side)
}

func (c Circle) Describe() string {
return fmt.Sprintf("Circle with radius %.2f", c.Radius)
}

func unreachableGoroutine() {
ch := make(chan Square)

go func() {
square := Square{Side: 4.0}
ch <- square
close(ch)
}()

for shape := range ch {
fmt.Println(shape.Describe())
}
}

func FuzzChannels(f *testing.F) {
f.Fuzz(func(t *testing.T, durationString string) {
duration, err := time.ParseDuration(durationString)
if err != nil || duration < 0 {
return
}

ch := make(chan Circle)

go func() {
time.Sleep(duration)
circle := Circle{Radius: 5.5}
ch <- circle
close(ch)
}()

for shape := range ch {
fmt.Println(shape.Describe())
}
})
}
10 changes: 9 additions & 1 deletion src/test/test_frontends_go.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,15 @@ def test_tree_sitter_go_sample6():

# Project check
harness = project.get_source_codes_with_harnesses()
assert len(harness) == 0
assert len(harness) == 1

functions_reached = project.get_reachable_functions(harness[0].source_file, harness[0])

# Callsite check
assert 'Circle.Describe' in functions_reached
assert 'time.Sleep' in functions_reached
assert 'Square.Describe' not in functions_reached
assert 'unreachableGoroutine' not in functions_reached


def test_tree_sitter_go_sample7():
Expand Down
Loading