From fbdef6f5c98120bb23fff1f6d7dfd50c101b05b2 Mon Sep 17 00:00:00 2001 From: Milos Zivkovic Date: Sat, 6 Jul 2024 18:06:21 +0200 Subject: [PATCH 1/2] Revert "feat: add p/nestedpkg (#2342)" This reverts commit 77ceda41e6252d4d30196d8a530df8bc5bbc99c7. --- examples/gno.land/p/demo/nestedpkg/gno.mod | 3 - .../gno.land/p/demo/nestedpkg/nestedpkg.gno | 84 ------------------- .../p/demo/nestedpkg/nestedpkg_test.gno | 75 ----------------- examples/gno.land/r/demo/tests/gno.mod | 5 +- examples/gno.land/r/demo/tests/tests.gno | 13 --- 5 files changed, 1 insertion(+), 179 deletions(-) delete mode 100644 examples/gno.land/p/demo/nestedpkg/nestedpkg.gno delete mode 100644 examples/gno.land/p/demo/nestedpkg/nestedpkg_test.gno diff --git a/examples/gno.land/p/demo/nestedpkg/gno.mod b/examples/gno.land/p/demo/nestedpkg/gno.mod index b6db60926ca..e69de29bb2d 100644 --- a/examples/gno.land/p/demo/nestedpkg/gno.mod +++ b/examples/gno.land/p/demo/nestedpkg/gno.mod @@ -1,3 +0,0 @@ -module gno.land/p/demo/nestedpkg - -require gno.land/r/demo/tests v0.0.0-latest diff --git a/examples/gno.land/p/demo/nestedpkg/nestedpkg.gno b/examples/gno.land/p/demo/nestedpkg/nestedpkg.gno deleted file mode 100644 index cf44dae007b..00000000000 --- a/examples/gno.land/p/demo/nestedpkg/nestedpkg.gno +++ /dev/null @@ -1,84 +0,0 @@ -// Package nestedpkg provides helpers for package-path based access control. -// It is useful for upgrade patterns relying on namespaces. -package nestedpkg - -import ( - "std" - "strings" -) - -// IsCallerSubPath checks if the caller realm is located in a subfolder of the current realm. -func IsCallerSubPath() bool { - var ( - cur = std.CurrentRealm().PkgPath() + "/" - prev = std.PrevRealm().PkgPath() + "/" - ) - return strings.HasPrefix(prev, cur) -} - -// AssertCallerIsSubPath panics if IsCallerSubPath returns false. -func AssertCallerIsSubPath() { - var ( - cur = std.CurrentRealm().PkgPath() + "/" - prev = std.PrevRealm().PkgPath() + "/" - ) - if !strings.HasPrefix(prev, cur) { - panic("call restricted to nested packages. current realm is " + cur + ", previous realm is " + prev) - } -} - -// IsCallerParentPath checks if the caller realm is located in a parent location of the current realm. -func IsCallerParentPath() bool { - var ( - cur = std.CurrentRealm().PkgPath() + "/" - prev = std.PrevRealm().PkgPath() + "/" - ) - return strings.HasPrefix(cur, prev) -} - -// AssertCallerIsParentPath panics if IsCallerParentPath returns false. -func AssertCallerIsParentPath() { - var ( - cur = std.CurrentRealm().PkgPath() + "/" - prev = std.PrevRealm().PkgPath() + "/" - ) - if !strings.HasPrefix(cur, prev) { - panic("call restricted to parent packages. current realm is " + cur + ", previous realm is " + prev) - } -} - -// IsSameNamespace checks if the caller realm and the current realm are in the same namespace. -func IsSameNamespace() bool { - var ( - cur = nsFromPath(std.CurrentRealm().PkgPath()) + "/" - prev = nsFromPath(std.PrevRealm().PkgPath()) + "/" - ) - return cur == prev -} - -// AssertIsSameNamespace panics if IsSameNamespace returns false. -func AssertIsSameNamespace() { - var ( - cur = nsFromPath(std.CurrentRealm().PkgPath()) + "/" - prev = nsFromPath(std.PrevRealm().PkgPath()) + "/" - ) - if cur != prev { - panic("call restricted to packages from the same namespace. current realm is " + cur + ", previous realm is " + prev) - } -} - -// nsFromPath extracts the namespace from a package path. -func nsFromPath(pkgpath string) string { - parts := strings.Split(pkgpath, "/") - - // Specifically for gno.land, potential paths are in the form of DOMAIN/r/NAMESPACE/... - // XXX: Consider extra checks. - // XXX: Support non gno.land domains, where p/ and r/ won't be enforced. - if len(parts) >= 3 { - return parts[2] - } - return "" -} - -// XXX: Consider adding IsCallerDirectlySubPath -// XXX: Consider adding IsCallerDirectlyParentPath diff --git a/examples/gno.land/p/demo/nestedpkg/nestedpkg_test.gno b/examples/gno.land/p/demo/nestedpkg/nestedpkg_test.gno deleted file mode 100644 index bc0a98f47fb..00000000000 --- a/examples/gno.land/p/demo/nestedpkg/nestedpkg_test.gno +++ /dev/null @@ -1,75 +0,0 @@ -package nestedpkg - -import ( - "std" - "testing" - - "gno.land/r/demo/tests" -) - -func TestPackage(t *testing.T) { - // direct child - cur := "gno.land/r/demo/tests/foo" - std.TestSetRealm(std.NewCodeRealm(cur)) - if !tests.IsCallerSubPath() { - t.Errorf(cur + " should be a sub path") - } - if tests.IsCallerParentPath() { - t.Errorf(cur + " should not be a parent path") - } - if !tests.HasCallerSameNamespace() { - t.Errorf(cur + " should be from the same namespace") - } - - // grand-grand-child - cur = "gno.land/r/demo/tests/foo/bar/baz" - std.TestSetRealm(std.NewCodeRealm(cur)) - if !tests.IsCallerSubPath() { - t.Errorf(cur + " should be a sub path") - } - if tests.IsCallerParentPath() { - t.Errorf(cur + " should not be a parent path") - } - if !tests.HasCallerSameNamespace() { - t.Errorf(cur + " should be from the same namespace") - } - - // direct parent - cur = "gno.land/r/demo" - std.TestSetRealm(std.NewCodeRealm(cur)) - if tests.IsCallerSubPath() { - t.Errorf(cur + " should not be a sub path") - } - if !tests.IsCallerParentPath() { - t.Errorf(cur + " should be a parent path") - } - if !tests.HasCallerSameNamespace() { - t.Errorf(cur + " should be from the same namespace") - } - - // fake parent (prefix) - cur = "gno.land/r/dem" - std.TestSetRealm(std.NewCodeRealm(cur)) - if tests.IsCallerSubPath() { - t.Errorf(cur + " should not be a sub path") - } - if tests.IsCallerParentPath() { - t.Errorf(cur + " should not be a parent path") - } - if tests.HasCallerSameNamespace() { - t.Errorf(cur + " should not be from the same namespace") - } - - // different namespace - cur = "gno.land/r/foo" - std.TestSetRealm(std.NewCodeRealm(cur)) - if tests.IsCallerSubPath() { - t.Errorf(cur + " should not be a sub path") - } - if tests.IsCallerParentPath() { - t.Errorf(cur + " should not be a parent path") - } - if tests.HasCallerSameNamespace() { - t.Errorf(cur + " should not be from the same namespace") - } -} diff --git a/examples/gno.land/r/demo/tests/gno.mod b/examples/gno.land/r/demo/tests/gno.mod index c51571e7d04..f34d41d327a 100644 --- a/examples/gno.land/r/demo/tests/gno.mod +++ b/examples/gno.land/r/demo/tests/gno.mod @@ -1,6 +1,3 @@ module gno.land/r/demo/tests -require ( - gno.land/p/demo/nestedpkg v0.0.0-latest - gno.land/r/demo/tests/subtests v0.0.0-latest -) +require gno.land/r/demo/tests/subtests v0.0.0-latest diff --git a/examples/gno.land/r/demo/tests/tests.gno b/examples/gno.land/r/demo/tests/tests.gno index 421ac6528c9..773412c3db9 100644 --- a/examples/gno.land/r/demo/tests/tests.gno +++ b/examples/gno.land/r/demo/tests/tests.gno @@ -3,7 +3,6 @@ package tests import ( "std" - "gno.land/p/demo/nestedpkg" rsubtests "gno.land/r/demo/tests/subtests" ) @@ -100,15 +99,3 @@ func GetRSubtestsPrevRealm() std.Realm { func Exec(fn func()) { fn() } - -func IsCallerSubPath() bool { - return nestedpkg.IsCallerSubPath() -} - -func IsCallerParentPath() bool { - return nestedpkg.IsCallerParentPath() -} - -func HasCallerSameNamespace() bool { - return nestedpkg.IsSameNamespace() -} From 71d33e83f4f1e433c20b05f4cd7e75b6ed613a4a Mon Sep 17 00:00:00 2001 From: Milos Zivkovic Date: Sat, 6 Jul 2024 18:07:53 +0200 Subject: [PATCH 2/2] Remove leftover gno.mod --- examples/gno.land/p/demo/nestedpkg/gno.mod | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 examples/gno.land/p/demo/nestedpkg/gno.mod diff --git a/examples/gno.land/p/demo/nestedpkg/gno.mod b/examples/gno.land/p/demo/nestedpkg/gno.mod deleted file mode 100644 index e69de29bb2d..00000000000