Skip to content
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
Binary file modified editor/gottp.wasm
Binary file not shown.
1 change: 1 addition & 0 deletions editor/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ <h3 style="margin-bottom: 10px;">Add YANG Module</h3>
<input type="text" id="yang-module-name" placeholder="Module name (optional, will be extracted from content)" style="width: 100%; padding: 8px; margin-bottom: 10px;">
<button class="button-primary" id="yang-paste-load-btn">Load</button>
</div>
<div id="yang-load-status" role="status" aria-live="polite" style="display: none; margin-top: 10px; padding: 8px 10px; border-radius: 4px; border: 1px solid var(--border-color); font-family: var(--font-mono); font-size: 12px; white-space: pre-wrap; word-break: break-word;"></div>
</div>
<div>
<h3 style="margin-bottom: 10px;">Loaded Modules</h3>
Expand Down
97 changes: 97 additions & 0 deletions editor/internal/yangcheck/yangcheck.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Package yangcheck validates a YANG module for the browser editor's YANG
// Modules dialog, so a bad module is rejected when it is loaded instead of
// surfacing later as a failure of the whole parse.
package yangcheck

import (
"fmt"
"sort"
"strings"

"github.com/openconfig/goyang/pkg/yang"
)

const noSuchModule = "no such module:"

// Check parses content as the YANG module name and resolves it together
// with the already-loaded modules (module name -> content), the same way
// internal/yang's ModuleSet does at parse time: every module is parsed into
// one goyang Modules set and then processed together, so imports resolve
// against the other loaded modules regardless of load order.
//
// It returns nil when the module is usable. Otherwise the error says whether
// the module has a syntax error, imports modules that are not loaded, or has
// another problem (an unknown type, for example). Errors caused only by the
// other loaded modules are not blamed on this one. A loaded entry stored
// under name is treated as the module being replaced.
func Check(name, content string, loaded map[string]string) error {
if strings.TrimSpace(content) == "" {
return fmt.Errorf("YANG module '%s' is empty", name)
}

probe := yang.NewModules()
if err := probe.Parse(content, name); err != nil {
return fmt.Errorf("syntax error in YANG module '%s': %v", name, err)
}
if len(probe.Modules)+len(probe.SubModules) == 0 {
return fmt.Errorf("YANG module '%s' has no module or submodule statement", name)
}

baseline := make(map[string]bool)
for _, err := range parseOthers(loaded, name).Process() {
baseline[err.Error()] = true
}

ms := parseOthers(loaded, name)
if err := ms.Parse(content, name); err != nil {
return fmt.Errorf("YANG module '%s' conflicts with a loaded module: %v", name, err)
}

var missing, other []string
seen := make(map[string]bool)
for _, err := range ms.Process() {
msg := err.Error()
if baseline[msg] || seen[msg] {
continue
}
seen[msg] = true
if i := strings.Index(msg, noSuchModule); i >= 0 {
missing = append(missing, strings.TrimSpace(msg[i+len(noSuchModule):]))
continue
}
other = append(other, msg)
}
sort.Strings(missing)
sort.Strings(other)

switch {
case len(missing) > 0 && len(other) > 0:
return fmt.Errorf("YANG module '%s' imports module(s) not loaded: %s (load them first); other errors: %s",
name, strings.Join(missing, ", "), strings.Join(other, "; "))
case len(missing) > 0:
return fmt.Errorf("YANG module '%s' imports module(s) not loaded: %s (load them first)",
name, strings.Join(missing, ", "))
case len(other) > 0:
return fmt.Errorf("YANG module '%s' is invalid: %s", name, strings.Join(other, "; "))
}
return nil
}

// parseOthers parses every loaded module except skip into a fresh set.
// Modules that no longer parse are left out: they cannot satisfy an import,
// and their own errors belong to them, not to the module being checked.
func parseOthers(loaded map[string]string, skip string) *yang.Modules {
names := make([]string, 0, len(loaded))
for n := range loaded {
if n != skip {
names = append(names, n)
}
}
sort.Strings(names)

ms := yang.NewModules()
for _, n := range names {
_ = ms.Parse(loaded[n], n)
}
return ms
}
106 changes: 106 additions & 0 deletions editor/internal/yangcheck/yangcheck_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package yangcheck

import (
"strings"
"testing"
)

const baseModule = `module base {
namespace "urn:base";
prefix b;
typedef port { type uint16; }
}`

const userModule = `module user {
namespace "urn:user";
prefix u;
import base { prefix b; }
container interfaces {
leaf port { type b:port; }
}
}`

func TestCheckAcceptsValidModule(t *testing.T) {
if err := Check("base", baseModule, nil); err != nil {
t.Fatalf("valid module rejected: %v", err)
}
}

func TestCheckRejectsSyntaxError(t *testing.T) {
err := Check("broken", "module broken { namespace \"urn:x\"; prefix x; leaf a { type string; ", nil)
if err == nil {
t.Fatal("module with unbalanced braces was accepted")
}
msg := err.Error()
for _, want := range []string{"broken", "syntax error", "closing brace"} {
if !strings.Contains(msg, want) {
t.Errorf("error %q should mention %q", msg, want)
}
}
}

func TestCheckRejectsGarbage(t *testing.T) {
err := Check("garbage", "this is not yang at all", nil)
if err == nil {
t.Fatal("garbage was accepted")
}
if !strings.Contains(err.Error(), "syntax error") {
t.Errorf("error %q should say syntax error", err)
}
}

func TestCheckRejectsEmptyContent(t *testing.T) {
if err := Check("empty", " \n", nil); err == nil {
t.Fatal("empty content was accepted")
}
}

func TestCheckReportsMissingImport(t *testing.T) {
err := Check("user", userModule, nil)
if err == nil {
t.Fatal("module with unresolved import was accepted")
}
msg := err.Error()
for _, want := range []string{"user", "base", "not loaded"} {
if !strings.Contains(msg, want) {
t.Errorf("error %q should mention %q", msg, want)
}
}
}

func TestCheckResolvesImportsAgainstLoadedModules(t *testing.T) {
loaded := map[string]string{"base": baseModule}
if err := Check("user", userModule, loaded); err != nil {
t.Fatalf("import of an already-loaded module not resolved: %v", err)
}
}

func TestCheckRejectsSemanticError(t *testing.T) {
bad := `module bad { namespace "urn:bad"; prefix x; leaf a { type nosuchtype; } }`
err := Check("bad", bad, nil)
if err == nil {
t.Fatal("module with unknown type was accepted")
}
if !strings.Contains(err.Error(), "nosuchtype") {
t.Errorf("error %q should mention the unknown type", err)
}
}

func TestCheckReplacingLoadedModuleIsNotADuplicate(t *testing.T) {
loaded := map[string]string{"base": baseModule}
if err := Check("base", baseModule, loaded); err != nil {
t.Fatalf("re-loading a module under its own name rejected: %v", err)
}
}

func TestCheckIgnoresErrorsOwnedByOtherLoadedModules(t *testing.T) {
// A previously stored module that no longer parses or resolves must not
// block loading an unrelated, valid module.
loaded := map[string]string{
"junk": "not yang",
"orphan": `module orphan { namespace "urn:o"; prefix o; import gone { prefix g; } }`,
}
if err := Check("base", baseModule, loaded); err != nil {
t.Fatalf("unrelated loaded-module errors blamed on new module: %v", err)
}
}
Loading
Loading