Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8550b25d authored by Ibrahim Kanouche's avatar Ibrahim Kanouche Committed by Gerrit Code Review
Browse files

Merge "Added a Breadth-first top down walk function to policy_walk."

parents 0cf4107c e7c33de1
Loading
Loading
Loading
Loading
+49 −1
Original line number Diff line number Diff line
@@ -45,7 +45,7 @@ func (ctx ApplicableConditionsContext) Context(lg *LicenseGraph, path TargetEdge
}

// VisitNode is called for each root and for each walked dependency node by
// WalkTopDown. When VisitNode returns true, WalkTopDown will proceed to walk
// WalkTopDown and WalkTopDownBreadthFirst. When VisitNode returns true, WalkTopDown will proceed to walk
// down the dependences of the node
type VisitNode func(lg *LicenseGraph, target *TargetNode, path TargetEdgePath) bool

@@ -79,6 +79,54 @@ func WalkTopDown(ctx EdgeContextProvider, lg *LicenseGraph, visit VisitNode) {
	}
}

// WalkTopDownBreadthFirst performs a Breadth-first top down walk of `lg` calling `visit` and descending
// into depenencies when `visit` returns true.
func WalkTopDownBreadthFirst(ctx EdgeContextProvider, lg *LicenseGraph, visit VisitNode) {
	path := NewTargetEdgePath(32)

	var walk func(fnode *TargetNode)
	walk = func(fnode *TargetNode) {
		edgesToWalk := make(TargetEdgeList, 0, len(fnode.edges))
		for _, edge := range fnode.edges {
			var edgeContext interface{}
			if ctx == nil {
				edgeContext = nil
			} else {
				edgeContext = ctx.Context(lg, *path, edge)
			}
			path.Push(edge, edgeContext)
			if visit(lg, edge.dependency, *path){
				edgesToWalk = append(edgesToWalk, edge)
			}
			path.Pop()
		}

		for _, edge := range(edgesToWalk) {
			var edgeContext interface{}
			if ctx == nil {
				edgeContext = nil
			} else {
				edgeContext = ctx.Context(lg, *path, edge)
			}
			path.Push(edge, edgeContext)
			walk(edge.dependency)
			path.Pop()
		}
	}

	path.Clear()
	rootsToWalk := make([]*TargetNode, 0, len(lg.rootFiles))
	for _, r := range lg.rootFiles {
		if visit(lg, lg.targets[r], *path){
			rootsToWalk = append(rootsToWalk, lg.targets[r])
		}
	}

	for _, rnode := range(rootsToWalk) {
		walk(rnode)
	}
}

// resolutionKey identifies results from walking a specific target for a
// specific set of conditions.
type resolutionKey struct {
+427 −0
Original line number Diff line number Diff line
@@ -16,9 +16,22 @@ package compliance

import (
	"bytes"
	"fmt"
	"os"
	"strings"
	"testing"
)

func TestMain(m *testing.M) {
	// Change into the cmd directory before running the tests
	// so they can find the testdata directory.
	if err := os.Chdir("cmd"); err != nil {
		fmt.Printf("failed to change to testdata directory: %s\n", err)
		os.Exit(1)
	}
	os.Exit(m.Run())
}

func TestWalkResolutionsForCondition(t *testing.T) {
	tests := []struct {
		name                string
@@ -1197,3 +1210,417 @@ func TestWalkActionsForCondition(t *testing.T) {
		})
	}
}

func TestWalkTopDownBreadthFirst(t *testing.T) {
	tests := []struct {
		name           string
		roots          []string
		edges          []annotated
		expectedResult []string
	}{
		{
			name:  "bin/bin1",
			roots: []string{"bin/bin1.meta_lic"},
			expectedResult: []string{
				"testdata/notice/bin/bin1.meta_lic",
				"testdata/notice/lib/liba.so.meta_lic",
				"testdata/notice/lib/libc.a.meta_lic",
			},
		},
		{
			name:  "bin/bin2",
			roots: []string{"bin/bin2.meta_lic"},
			expectedResult: []string{
				"testdata/notice/bin/bin2.meta_lic",
				"testdata/notice/lib/libb.so.meta_lic",
				"testdata/notice/lib/libd.so.meta_lic",
			},
		},
		{
			name:  "bin/bin3",
			roots: []string{"bin/bin3.meta_lic"},
			expectedResult: []string{
				"testdata/notice/bin/bin3.meta_lic",
			},
		},
		{
			name:  "lib/liba.so",
			roots: []string{"lib/liba.so.meta_lic"},
			expectedResult: []string{
				"testdata/notice/lib/liba.so.meta_lic",
			},
		},
		{
			name:  "lib/libb.so",
			roots: []string{"lib/libb.so.meta_lic"},
			expectedResult: []string{
				"testdata/notice/lib/libb.so.meta_lic",
			},
		},
		{
			name:  "lib/libc.so",
			roots: []string{"lib/libc.a.meta_lic"},
			expectedResult: []string{
				"testdata/notice/lib/libc.a.meta_lic",
			},
		},
		{
			name:  "lib/libd.so",
			roots: []string{"lib/libd.so.meta_lic"},
			expectedResult: []string{
				"testdata/notice/lib/libd.so.meta_lic",
			},
		},
		{
			name:  "highest.apex",
			roots: []string{"highest.apex.meta_lic"},
			expectedResult: []string{
				"testdata/notice/highest.apex.meta_lic",
				"testdata/notice/bin/bin1.meta_lic",
				"testdata/notice/bin/bin2.meta_lic",
				"testdata/notice/lib/liba.so.meta_lic",
				"testdata/notice/lib/libb.so.meta_lic",
				"testdata/notice/lib/liba.so.meta_lic",
				"testdata/notice/lib/libc.a.meta_lic",
				"testdata/notice/lib/libb.so.meta_lic",
				"testdata/notice/lib/libd.so.meta_lic",
			},
		},
		{
			name:  "container.zip",
			roots: []string{"container.zip.meta_lic"},
			expectedResult: []string{
				"testdata/notice/container.zip.meta_lic",
				"testdata/notice/bin/bin1.meta_lic",
				"testdata/notice/bin/bin2.meta_lic",
				"testdata/notice/lib/liba.so.meta_lic",
				"testdata/notice/lib/libb.so.meta_lic",
				"testdata/notice/lib/liba.so.meta_lic",
				"testdata/notice/lib/libc.a.meta_lic",
				"testdata/notice/lib/libb.so.meta_lic",
				"testdata/notice/lib/libd.so.meta_lic",
			},
		},
		{
			name:  "application",
			roots: []string{"application.meta_lic"},
			expectedResult: []string{
				"testdata/notice/application.meta_lic",
				"testdata/notice/bin/bin3.meta_lic",
				"testdata/notice/lib/liba.so.meta_lic",
				"testdata/notice/lib/libb.so.meta_lic",
			},
		},
		{
			name:  "bin/bin1&lib/liba",
			roots: []string{"bin/bin1.meta_lic","lib/liba.so.meta_lic"},
			expectedResult: []string{
				"testdata/notice/bin/bin1.meta_lic",
				"testdata/notice/lib/liba.so.meta_lic",
				"testdata/notice/lib/liba.so.meta_lic",
				"testdata/notice/lib/libc.a.meta_lic",
			},
		},
		{
			name:  "bin/bin2&lib/libd",
			roots: []string{"bin/bin2.meta_lic", "lib/libd.so.meta_lic"},
			expectedResult: []string{
				"testdata/notice/bin/bin2.meta_lic",
				"testdata/notice/lib/libd.so.meta_lic",
				"testdata/notice/lib/libb.so.meta_lic",
				"testdata/notice/lib/libd.so.meta_lic",
			},
		},
		{
			name:  "application&bin/bin3",
			roots: []string{"application.meta_lic", "bin/bin3.meta_lic"},
			expectedResult: []string{
				"testdata/notice/application.meta_lic",
				"testdata/notice/bin/bin3.meta_lic",
				"testdata/notice/bin/bin3.meta_lic",
				"testdata/notice/lib/liba.so.meta_lic",
				"testdata/notice/lib/libb.so.meta_lic",
			},
		},
		{
			name:  "highest.apex&container.zip",
			roots: []string{"highest.apex.meta_lic", "container.zip.meta_lic"},
			expectedResult: []string{
				"testdata/notice/highest.apex.meta_lic",
				"testdata/notice/container.zip.meta_lic",
				"testdata/notice/bin/bin1.meta_lic",
				"testdata/notice/bin/bin2.meta_lic",
				"testdata/notice/lib/liba.so.meta_lic",
				"testdata/notice/lib/libb.so.meta_lic",
				"testdata/notice/lib/liba.so.meta_lic",
				"testdata/notice/lib/libc.a.meta_lic",
				"testdata/notice/lib/libb.so.meta_lic",
				"testdata/notice/lib/libd.so.meta_lic",
				"testdata/notice/bin/bin1.meta_lic",
				"testdata/notice/bin/bin2.meta_lic",
				"testdata/notice/lib/liba.so.meta_lic",
				"testdata/notice/lib/libb.so.meta_lic",
				"testdata/notice/lib/liba.so.meta_lic",
				"testdata/notice/lib/libc.a.meta_lic",
				"testdata/notice/lib/libb.so.meta_lic",
				"testdata/notice/lib/libd.so.meta_lic",
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			stderr := &bytes.Buffer{}
			actualOut := &bytes.Buffer{}

			rootFiles := make([]string, 0, len(tt.roots))
			for _, r := range tt.roots {
				rootFiles = append(rootFiles, "testdata/notice/"+r)
			}

			lg, err := ReadLicenseGraph(GetFS(""), stderr, rootFiles)

			if err != nil {
				t.Errorf("unexpected test data error: got %s, want no error", err)
				return
			}

			expectedRst := tt.expectedResult

			WalkTopDownBreadthFirst(nil, lg, func(lg *LicenseGraph, tn *TargetNode, path TargetEdgePath) bool {
				fmt.Fprintln(actualOut, tn.Name())
				return true
			})

			actualRst := strings.Split(actualOut.String(), "\n")

			if len(actualRst) > 0 {
				actualRst = actualRst[:len(actualRst)-1]
			}

			t.Logf("actual nodes visited: %s", actualOut.String())
			t.Logf("expected nodes visited: %s", strings.Join(expectedRst, "\n"))

			if len(actualRst) != len(expectedRst) {
				t.Errorf("WalkTopDownBreadthFirst: number of visited nodes is different: got %d, want %d", len(actualRst), len(expectedRst))
			}

			for i := 0; i < len(actualRst) && i < len(expectedRst); i++ {
				if actualRst[i] != expectedRst[i] {
					t.Errorf("WalkTopDownBreadthFirst: lines differ at index %d: got %q, want %q", i, actualRst[i], expectedRst[i])
					break
				}
			}

			if len(actualRst) < len(expectedRst) {
				t.Errorf("WalkTopDownBreadthFirst: extra lines at %d: got %q, want nothing", len(actualRst), expectedRst[len(actualRst)])
			}

			if len(expectedRst) < len(actualRst) {
				t.Errorf("WalkTopDownBreadthFirst: missing lines at %d: got nothing, want %q", len(expectedRst), actualRst[len(expectedRst)])
			}
		})
	}
}

func TestWalkTopDownBreadthFirstWithoutDuplicates(t *testing.T) {
	tests := []struct {
		name           string
		roots          []string
		edges          []annotated
		expectedResult []string
	}{
		{
			name:  "bin/bin1",
			roots: []string{"bin/bin1.meta_lic"},
			expectedResult: []string{
				"testdata/notice/bin/bin1.meta_lic",
				"testdata/notice/lib/liba.so.meta_lic",
				"testdata/notice/lib/libc.a.meta_lic",
			},
		},
		{
			name:  "bin/bin2",
			roots: []string{"bin/bin2.meta_lic"},
			expectedResult: []string{
				"testdata/notice/bin/bin2.meta_lic",
				"testdata/notice/lib/libb.so.meta_lic",
				"testdata/notice/lib/libd.so.meta_lic",
			},
		},
		{
			name:  "bin/bin3",
			roots: []string{"bin/bin3.meta_lic"},
			expectedResult: []string{
				"testdata/notice/bin/bin3.meta_lic",
			},
		},
		{
			name:  "lib/liba.so",
			roots: []string{"lib/liba.so.meta_lic"},
			expectedResult: []string{
				"testdata/notice/lib/liba.so.meta_lic",
			},
		},
		{
			name:  "lib/libb.so",
			roots: []string{"lib/libb.so.meta_lic"},
			expectedResult: []string{
				"testdata/notice/lib/libb.so.meta_lic",
			},
		},
		{
			name:  "lib/libc.so",
			roots: []string{"lib/libc.a.meta_lic"},
			expectedResult: []string{
				"testdata/notice/lib/libc.a.meta_lic",
			},
		},
		{
			name:  "lib/libd.so",
			roots: []string{"lib/libd.so.meta_lic"},
			expectedResult: []string{
				"testdata/notice/lib/libd.so.meta_lic",
			},
		},
		{
			name:  "highest.apex",
			roots: []string{"highest.apex.meta_lic"},
			expectedResult: []string{
				"testdata/notice/highest.apex.meta_lic",
				"testdata/notice/bin/bin1.meta_lic",
				"testdata/notice/bin/bin2.meta_lic",
				"testdata/notice/lib/liba.so.meta_lic",
				"testdata/notice/lib/libb.so.meta_lic",
				"testdata/notice/lib/libc.a.meta_lic",
				"testdata/notice/lib/libd.so.meta_lic",
			},
		},
		{
			name:  "container.zip",
			roots: []string{"container.zip.meta_lic"},
			expectedResult: []string{
				"testdata/notice/container.zip.meta_lic",
				"testdata/notice/bin/bin1.meta_lic",
				"testdata/notice/bin/bin2.meta_lic",
				"testdata/notice/lib/liba.so.meta_lic",
				"testdata/notice/lib/libb.so.meta_lic",
				"testdata/notice/lib/libc.a.meta_lic",
				"testdata/notice/lib/libd.so.meta_lic",
			},
		},
		{
			name:  "application",
			roots: []string{"application.meta_lic"},
			expectedResult: []string{
				"testdata/notice/application.meta_lic",
				"testdata/notice/bin/bin3.meta_lic",
				"testdata/notice/lib/liba.so.meta_lic",
				"testdata/notice/lib/libb.so.meta_lic",
			},
		},
		{
			name:  "bin/bin1&lib/liba",
			roots: []string{"bin/bin1.meta_lic", "lib/liba.so.meta_lic"},
			expectedResult: []string{
				"testdata/notice/bin/bin1.meta_lic",
				"testdata/notice/lib/liba.so.meta_lic",
				"testdata/notice/lib/libc.a.meta_lic",
			},
		},
		{
			name:  "bin/bin2&lib/libd",
			roots: []string{"bin/bin2.meta_lic", "lib/libd.so.meta_lic"},
			expectedResult: []string{
				"testdata/notice/bin/bin2.meta_lic",
				"testdata/notice/lib/libd.so.meta_lic",
				"testdata/notice/lib/libb.so.meta_lic",
			},
		},
		{
			name:  "application&bin/bin3",
			roots: []string{"application.meta_lic", "bin/bin3.meta_lic"},
			expectedResult: []string{
				"testdata/notice/application.meta_lic",
				"testdata/notice/bin/bin3.meta_lic",
				"testdata/notice/lib/liba.so.meta_lic",
				"testdata/notice/lib/libb.so.meta_lic",
			},
		},
		{
			name:  "highest.apex&container.zip",
			roots: []string{"highest.apex.meta_lic", "container.zip.meta_lic"},
			expectedResult: []string{
				"testdata/notice/highest.apex.meta_lic",
				"testdata/notice/container.zip.meta_lic",
				"testdata/notice/bin/bin1.meta_lic",
				"testdata/notice/bin/bin2.meta_lic",
				"testdata/notice/lib/liba.so.meta_lic",
				"testdata/notice/lib/libb.so.meta_lic",
				"testdata/notice/lib/libc.a.meta_lic",
				"testdata/notice/lib/libd.so.meta_lic",
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			stderr := &bytes.Buffer{}
			actualOut := &bytes.Buffer{}

			rootFiles := make([]string, 0, len(tt.roots))
			for _, r := range tt.roots {
				rootFiles = append(rootFiles, "testdata/notice/"+r)
			}

			lg, err := ReadLicenseGraph(GetFS(""), stderr, rootFiles)

			if err != nil {
				t.Errorf("unexpected test data error: got %s, want no error", err)
				return
			}

			expectedRst := tt.expectedResult

			//Keeping track of the visited nodes
			//Only add to actualOut if not visited
			visitedNodes := make(map[string]struct{})
			WalkTopDownBreadthFirst(nil, lg, func(lg *LicenseGraph, tn *TargetNode, path TargetEdgePath) bool {
				if _, alreadyVisited := visitedNodes[tn.Name()]; alreadyVisited {
					return false
				}
				fmt.Fprintln(actualOut, tn.Name())
				visitedNodes[tn.Name()] = struct{}{}
				return true
			})

			actualRst := strings.Split(actualOut.String(), "\n")

			if len(actualRst) > 0 {
				actualRst = actualRst[:len(actualRst)-1]
			}

			t.Logf("actual nodes visited: %s", actualOut.String())
			t.Logf("expected nodes visited: %s", strings.Join(expectedRst, "\n"))

			if len(actualRst) != len(expectedRst) {
				t.Errorf("WalkTopDownBreadthFirst: number of visited nodes is different: got %d, want %d", len(actualRst), len(expectedRst))
			}

			for i := 0; i < len(actualRst) && i < len(expectedRst); i++ {
				if actualRst[i] != expectedRst[i] {
					t.Errorf("WalkTopDownBreadthFirst: lines differ at index %d: got %q, want %q", i, actualRst[i], expectedRst[i])
					break
				}
			}

			if len(actualRst) < len(expectedRst) {
				t.Errorf("WalkTopDownBreadthFirst: extra lines at %d: got %q, want nothing", len(actualRst), expectedRst[len(actualRst)])
			}

			if len(expectedRst) < len(actualRst) {
				t.Errorf("WalkTopDownBreadthFirst: missing lines at %d: got nothing, want %q", len(expectedRst), actualRst[len(expectedRst)])
			}
		})
	}
}