Created
April 21, 2022 08:27
-
-
Save cmaglie/ca117f4f6fe9940ab5789b85ce58a8ec to your computer and use it in GitHub Desktop.
golang imports ACL test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This file is part of arduino-cli. | |
// | |
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) | |
// | |
// This software is released under the GNU General Public License version 3, | |
// which covers the main part of arduino-cli. | |
// The terms of this license can be found at: | |
// https://www.gnu.org/licenses/gpl-3.0.en.html | |
// | |
// You can be released from the requirements of the above licenses by purchasing | |
// a commercial license. Buying such a license is mandatory if you want to | |
// modify or otherwise use the software for commercial activities involving the | |
// Arduino software without disclosing the source code of your own applications. | |
// To purchase a commercial license, send an email to [email protected]. | |
package main_test | |
import ( | |
"fmt" | |
"go/build" | |
"strings" | |
"testing" | |
"github.com/stretchr/testify/require" | |
) | |
func TestImportPathIsCorrect(t *testing.T) { | |
imports := resolveImports(t, "github.com/arduino/arduino-cli") | |
fails := 0 | |
for source, dests := range imports { | |
for dest := range dests { | |
if checkAllowedImport(t, source, dest) { | |
fmt.Printf(" OK: %s -> %s\n", source, dest) | |
} else { | |
fails++ | |
fmt.Printf("FAIL: %s -> %s\n", source, dest) | |
//t.Errorf("FAIL: %s -> %s\n", source, dest) | |
} | |
} | |
} | |
fmt.Println("ZZ FAILURES:", fails) | |
} | |
func checkAllowedImport(t *testing.T, source, dest string) bool { | |
alwaysImportable := map[string]bool{ | |
"github.com/arduino/arduino-cli/i18n": true, | |
"github.com/arduino/arduino-cli/configuration": true, | |
"github.com/arduino/arduino-cli/executils": true, | |
"github.com/arduino/arduino-cli/metrics": true, | |
"github.com/arduino/arduino-cli/inventory": true, | |
"github.com/arduino/arduino-cli/table": true, | |
"github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1": true, | |
"github.com/arduino/arduino-cli/rpc/cc/arduino/cli/debug/v1": true, | |
"github.com/arduino/arduino-cli/rpc/cc/arduino/cli/monitor/v1": true, // DEPRECATED | |
"github.com/arduino/arduino-cli/rpc/cc/arduino/cli/settings/v1": true, // REPLACE with a better solution | |
} | |
allowedPaths := []struct{ Src, Dest string }{ | |
// big package dependencies | |
{"github.com/arduino/arduino-cli/commands*", "github.com/arduino/arduino-cli/arduino*"}, | |
{"github.com/arduino/arduino-cli/cli/*", "github.com/arduino/arduino-cli/commands*"}, | |
{"github.com/arduino/arduino-cli", "github.com/arduino/arduino-cli/cli"}, | |
// "commands" package internals | |
{"github.com/arduino/arduino-cli/commands/*", "github.com/arduino/arduino-cli/commands"}, | |
{"github.com/arduino/arduino-cli/commands/daemon", "github.com/arduino/arduino-cli/commands/*"}, | |
// "arduino" package internals | |
{"github.com/arduino/arduino-cli/arduino/*", "github.com/arduino/arduino-cli/arduino"}, | |
{"github.com/arduino/arduino-cli/arduino/*", "github.com/arduino/arduino-cli/arduino/globals"}, | |
{"github.com/arduino/arduino-cli/arduino/builder", "github.com/arduino/arduino-cli/arduino/sketch"}, | |
{"github.com/arduino/arduino-cli/arduino/cores", "github.com/arduino/arduino-cli/arduino/resources"}, | |
{"github.com/arduino/arduino-cli/arduino/libraries", "github.com/arduino/arduino-cli/arduino/cores"}, | |
{"github.com/arduino/arduino-cli/arduino/libraries", "github.com/arduino/arduino-cli/arduino/sketch"}, | |
{"github.com/arduino/arduino-cli/arduino/libraries", "github.com/arduino/arduino-cli/arduino/utils"}, | |
{"github.com/arduino/arduino-cli/arduino/libraries/librariesresolver", "github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"}, | |
{"github.com/arduino/arduino-cli/arduino/resources", "github.com/arduino/arduino-cli/arduino/httpclient"}, | |
{"github.com/arduino/arduino-cli/arduino", "github.com/arduino/arduino-cli/arduino/discovery"}, // WHY? | |
{"github.com/arduino/arduino-cli/arduino/cores/packageindex", "github.com/arduino/arduino-cli/arduino/resources"}, | |
{"github.com/arduino/arduino-cli/arduino/cores/packageindex", "github.com/arduino/arduino-cli/arduino/security"}, | |
{"github.com/arduino/arduino-cli/arduino/cores/packagemanager", "github.com/arduino/arduino-cli/arduino/cores"}, | |
{"github.com/arduino/arduino-cli/arduino/cores/packagemanager", "github.com/arduino/arduino-cli/arduino/cores/packageindex"}, | |
{"github.com/arduino/arduino-cli/arduino/cores/packagemanager", "github.com/arduino/arduino-cli/arduino/discovery"}, | |
{"github.com/arduino/arduino-cli/arduino/cores/packagemanager", "github.com/arduino/arduino-cli/arduino/discovery/discoverymanager"}, | |
{"github.com/arduino/arduino-cli/arduino/cores/packagemanager", "github.com/arduino/arduino-cli/arduino/resources"}, | |
{"github.com/arduino/arduino-cli/arduino/discovery/discoverymanager", "github.com/arduino/arduino-cli/arduino/discovery"}, | |
{"github.com/arduino/arduino-cli/arduino/libraries/librariesindex", "github.com/arduino/arduino-cli/arduino/libraries"}, | |
{"github.com/arduino/arduino-cli/arduino/libraries/librariesindex", "github.com/arduino/arduino-cli/arduino/resources"}, | |
{"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager", "github.com/arduino/arduino-cli/arduino/cores"}, | |
{"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager", "github.com/arduino/arduino-cli/arduino/libraries"}, | |
{"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager", "github.com/arduino/arduino-cli/arduino/libraries/librariesindex"}, | |
{"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager", "github.com/arduino/arduino-cli/arduino/utils"}, | |
{"github.com/arduino/arduino-cli/arduino/libraries/librariesresolver", "github.com/arduino/arduino-cli/arduino/libraries"}, | |
{"github.com/arduino/arduino-cli/arduino/libraries/librariesresolver", "github.com/arduino/arduino-cli/arduino/utils"}, | |
// "cli" package | |
{"github.com/arduino/arduino-cli/cli", "github.com/arduino/arduino-cli/cli/*"}, | |
{"github.com/arduino/arduino-cli/cli/*", "github.com/arduino/arduino-cli/cli/errorcodes"}, | |
{"github.com/arduino/arduino-cli/cli/*", "github.com/arduino/arduino-cli/cli/feedback"}, | |
{"github.com/arduino/arduino-cli/cli/*", "github.com/arduino/arduino-cli/cli/instance"}, | |
{"github.com/arduino/arduino-cli/cli/*", "github.com/arduino/arduino-cli/cli/arguments"}, | |
{"github.com/arduino/arduino-cli/cli/*", "github.com/arduino/arduino-cli/cli/output"}, | |
{"github.com/arduino/arduino-cli/cli/*", "github.com/arduino/arduino-cli/cli/globals"}, | |
// legacy | |
{"github.com/arduino/arduino-cli/legacy/*", "github.com/arduino/arduino-cli/legacy/*"}, // LEGACY | |
{"github.com/arduino/arduino-cli/legacy/*", "github.com/arduino/arduino-cli/arduino/*"}, // LEGACY | |
{"github.com/arduino/arduino-cli/legacy/*", "github.com/arduino/arduino-cli/arduino"}, // LEGACY | |
{"github.com/arduino/arduino-cli/legacy/*", "github.com/arduino/arduino-cli/commands"}, // LEGACY | |
{"github.com/arduino/arduino-cli/commands/compile", "github.com/arduino/arduino-cli/legacy/builder"}, // LEGACY | |
{"github.com/arduino/arduino-cli/commands/compile", "github.com/arduino/arduino-cli/legacy/builder/types"}, // LEGACY | |
// inter-package weird dependencies (TO BE RESOLVED) | |
{"github.com/arduino/arduino-cli/cli/compile", "github.com/arduino/arduino-cli/arduino/cores/packagemanager"}, // WHY? | |
{"github.com/arduino/arduino-cli/cli/core", "github.com/arduino/arduino-cli/arduino"}, // WHY? | |
{"github.com/arduino/arduino-cli/cli/core", "github.com/arduino/arduino-cli/arduino/utils"}, // WHY? | |
{"github.com/arduino/arduino-cli/cli/globals", "github.com/arduino/arduino-cli/version"}, // MERGE two packages | |
{"github.com/arduino/arduino-cli/cli/upload", "github.com/arduino/arduino-cli/arduino"}, // WHY? | |
{"github.com/arduino/arduino-cli/cli/upload", "github.com/arduino/arduino-cli/arduino/sketch"}, // WHY? | |
{"github.com/arduino/arduino-cli/arduino/discovery", "github.com/arduino/arduino-cli/cli/globals"}, // WHY? | |
{"github.com/arduino/arduino-cli/commands", "github.com/arduino/arduino-cli/cli/globals"}, // WHY? | |
{"github.com/arduino/arduino-cli/configuration", "github.com/arduino/arduino-cli/cli/globals"}, // WHY? | |
{"github.com/arduino/arduino-cli/configuration", "github.com/arduino/arduino-cli/cli/feedback"}, // WHY? | |
{"github.com/arduino/arduino-cli", "github.com/arduino/arduino-cli/cli/errorcodes"}, // WHY? | |
} | |
if alwaysImportable[dest] { | |
return true | |
} | |
for _, allowed := range allowedPaths { | |
if match(source, allowed.Src) && match(dest, allowed.Dest) { | |
return true | |
} | |
} | |
return false | |
} | |
func match(in, expr string) bool { | |
split := strings.Split(expr, "*") | |
if len(split) > 1 { | |
return strings.HasPrefix(in, split[0]) && strings.HasSuffix(in, split[1]) | |
} | |
return in == expr | |
} | |
func resolveImports(t *testing.T, root string) map[string]map[string]bool { | |
resolvedImports := map[string]map[string]bool{} | |
queuedImports := map[string]bool{root: true} | |
for len(queuedImports) > 0 { | |
var i string | |
for i = range queuedImports { | |
break | |
} | |
delete(queuedImports, i) | |
pkg, err := build.Default.Import(i, ".", build.IgnoreVendor) | |
require.NoError(t, err) | |
resolvedImports[i] = map[string]bool{} | |
for _, n := range pkg.Imports { | |
if !strings.HasPrefix(n, "github.com/arduino/arduino-cli") { | |
continue | |
} | |
resolvedImports[i][n] = true | |
if _, has := resolvedImports[n]; has { | |
continue | |
} | |
queuedImports[n] = true | |
} | |
} | |
return resolvedImports | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment