Skip to content

Commit 8ea8a2d

Browse files
authored
fix panic with empty definitions in parent file (#31)
Updates #28
1 parent 6197dea commit 8ea8a2d

File tree

2 files changed

+146
-4
lines changed

2 files changed

+146
-4
lines changed

main.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,13 @@ func addParentPathComments(parentDoc *ParsedDocument) error {
224224
default:
225225
pathComment(parentSection, parentDoc.Path)
226226
case *jwcc.Array:
227-
pathComment(parentSection.Value.(*jwcc.Array).Values[0], parentDoc.Path)
227+
if len(parentSection.Value.(*jwcc.Array).Values) != 0 {
228+
pathComment(parentSection.Value.(*jwcc.Array).Values[0], parentDoc.Path)
229+
}
228230
case *jwcc.Object:
229-
pathComment(parentSection.Value.(*jwcc.Object).Members[0], parentDoc.Path)
231+
if len(parentSection.Value.(*jwcc.Object).Members) != 0 {
232+
pathComment(parentSection.Value.(*jwcc.Object).Members[0], parentDoc.Path)
233+
}
230234
}
231235
}
232236
return nil

main_test.go

Lines changed: 140 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,6 @@ func TestHandleArray(t *testing.T) {
438438
handlerFn("acls", parentDoc.Path, parentDoc.Object, "CHILD", childSection)
439439

440440
mergedValues := parentDoc.Object.Find("acls").Value.(*jwcc.Array).Values
441-
442441
if len(mergedValues) != 2 {
443442
t.Fatalf("section [%v] should be [1], not [%v]", "acls", len(mergedValues))
444443
}
@@ -471,7 +470,6 @@ func TestHandleObject(t *testing.T) {
471470
handlerFn("groups", parentDoc.Path, parentDoc.Object, "CHILD", childSection)
472471

473472
mergedValues := parentDoc.Object.Find("groups").Value.(*jwcc.Object).Members
474-
475473
if len(mergedValues) != 3 {
476474
t.Fatalf("section [%v] should be [1], not [%v]", "groups", len(mergedValues))
477475
}
@@ -520,6 +518,146 @@ func TestHandleAutoApprovers(t *testing.T) {
520518
}
521519
}
522520

521+
func TestEmptyParentObject(t *testing.T) {
522+
parent, err := jwcc.Parse(strings.NewReader(`{"hosts":{}}`))
523+
if err != nil {
524+
t.Fatalf("expected no error, got [%v]", err)
525+
}
526+
parentDoc := &ParsedDocument{
527+
Object: parent.Value.(*jwcc.Object),
528+
Path: "parent",
529+
}
530+
531+
child, err := jwcc.Parse(strings.NewReader(`{
532+
"hosts": {
533+
"host1": "100.99.98.97",
534+
}
535+
}`))
536+
if err != nil {
537+
t.Fatalf("expected no error, got [%v]", err)
538+
}
539+
540+
childDoc := &ParsedDocument{
541+
Object: child.Value.(*jwcc.Object),
542+
Path: "child",
543+
}
544+
545+
err = mergeDocs(preDefinedAclSections, parentDoc, []*ParsedDocument{childDoc})
546+
if err != nil {
547+
t.Fatalf("expected no error, got [%v]", err)
548+
}
549+
550+
mergedValues := parentDoc.Object.Find("hosts").Value.(*jwcc.Object).Members
551+
if len(mergedValues) != 1 {
552+
t.Fatalf("section [%v] should be [1], not [%v]", "hosts", len(mergedValues))
553+
}
554+
}
555+
556+
func TestEmptyParentArray(t *testing.T) {
557+
parent, err := jwcc.Parse(strings.NewReader(`{"acls":[]}`))
558+
if err != nil {
559+
t.Fatalf("expected no error, got [%v]", err)
560+
}
561+
parentDoc := &ParsedDocument{
562+
Object: parent.Value.(*jwcc.Object),
563+
Path: "parent",
564+
}
565+
566+
child, err := jwcc.Parse(strings.NewReader(`{
567+
"acls": [
568+
{"action": "accept", "src": ["finance1"], "dst": ["tag:demo-infra:22"]},
569+
]
570+
}`))
571+
if err != nil {
572+
t.Fatalf("expected no error, got [%v]", err)
573+
}
574+
575+
childDoc := &ParsedDocument{
576+
Object: child.Value.(*jwcc.Object),
577+
Path: "child",
578+
}
579+
580+
err = mergeDocs(preDefinedAclSections, parentDoc, []*ParsedDocument{childDoc})
581+
if err != nil {
582+
t.Fatalf("expected no error, got [%v]", err)
583+
}
584+
585+
mergedValues := parentDoc.Object.Find("acls").Value.(*jwcc.Array).Values
586+
if len(mergedValues) != 1 {
587+
t.Fatalf("section [%v] should be [1], not [%v]", "acls", len(mergedValues))
588+
}
589+
}
590+
591+
func TestEmptyChildObject(t *testing.T) {
592+
parent, err := jwcc.Parse(strings.NewReader(`{
593+
"hosts": {
594+
"host1": "100.99.98.97",
595+
}
596+
}`))
597+
if err != nil {
598+
t.Fatalf("expected no error, got [%v]", err)
599+
}
600+
parentDoc := &ParsedDocument{
601+
Object: parent.Value.(*jwcc.Object),
602+
Path: "parent",
603+
}
604+
605+
child, err := jwcc.Parse(strings.NewReader(`{"hosts":{}}`))
606+
if err != nil {
607+
t.Fatalf("expected no error, got [%v]", err)
608+
}
609+
610+
childDoc := &ParsedDocument{
611+
Object: child.Value.(*jwcc.Object),
612+
Path: "child",
613+
}
614+
615+
err = mergeDocs(preDefinedAclSections, parentDoc, []*ParsedDocument{childDoc})
616+
if err != nil {
617+
t.Fatalf("expected no error, got [%v]", err)
618+
}
619+
620+
mergedValues := parentDoc.Object.Find("hosts").Value.(*jwcc.Object).Members
621+
if len(mergedValues) != 1 {
622+
t.Fatalf("section [%v] should be [1], not [%v]", "hosts", len(mergedValues))
623+
}
624+
}
625+
626+
func TestEmptyChildArray(t *testing.T) {
627+
parent, err := jwcc.Parse(strings.NewReader(`{
628+
"acls": [
629+
{"action": "accept", "src": ["finance1"], "dst": ["tag:demo-infra:22"]},
630+
]
631+
}`))
632+
if err != nil {
633+
t.Fatalf("expected no error, got [%v]", err)
634+
}
635+
parentDoc := &ParsedDocument{
636+
Object: parent.Value.(*jwcc.Object),
637+
Path: "parent",
638+
}
639+
640+
child, err := jwcc.Parse(strings.NewReader(`{"acls":[]}`))
641+
if err != nil {
642+
t.Fatalf("expected no error, got [%v]", err)
643+
}
644+
645+
childDoc := &ParsedDocument{
646+
Object: child.Value.(*jwcc.Object),
647+
Path: "child",
648+
}
649+
650+
err = mergeDocs(preDefinedAclSections, parentDoc, []*ParsedDocument{childDoc})
651+
if err != nil {
652+
t.Fatalf("expected no error, got [%v]", err)
653+
}
654+
655+
mergedValues := parentDoc.Object.Find("acls").Value.(*jwcc.Array).Values
656+
if len(mergedValues) != 1 {
657+
t.Fatalf("section [%v] should be [1], not [%v]", "acls", len(mergedValues))
658+
}
659+
}
660+
523661
func TestSort(t *testing.T) {
524662
parent, err := jwcc.Parse(strings.NewReader(ACL_PARENT))
525663
if err != nil {

0 commit comments

Comments
 (0)