|
| 1 | +package dot |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "maps" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/cedar-policy/cedar-go/types" |
| 12 | +) |
| 13 | + |
| 14 | +func TestWrite(t *testing.T) { |
| 15 | + t.Run("WritesNodesAndEdges", func(t *testing.T) { |
| 16 | + var buf bytes.Buffer |
| 17 | + |
| 18 | + // Build a small graph: |
| 19 | + // Group::admins (no parents) |
| 20 | + // User::alice (parent = Group::admins) |
| 21 | + // User::bob (no parents) |
| 22 | + groupUID := types.NewEntityUID("Group", types.String("admins")) |
| 23 | + aliceUID := types.NewEntityUID("User", types.String("alice")) |
| 24 | + bobUID := types.NewEntityUID("User", types.String("bob")) |
| 25 | + |
| 26 | + entities := types.EntityMap{} |
| 27 | + entities[groupUID] = types.Entity{ |
| 28 | + UID: groupUID, |
| 29 | + Parents: types.NewEntityUIDSet(), // no parents |
| 30 | + } |
| 31 | + entities[aliceUID] = types.Entity{ |
| 32 | + UID: aliceUID, |
| 33 | + Parents: types.NewEntityUIDSet(groupUID), // parent is group |
| 34 | + } |
| 35 | + entities[bobUID] = types.Entity{ |
| 36 | + UID: bobUID, |
| 37 | + Parents: types.NewEntityUIDSet(), // no parents |
| 38 | + } |
| 39 | + |
| 40 | + if err := Write(&buf, maps.Values(entities)); err != nil { |
| 41 | + t.Fatalf("ToDotStr returned error: %v", err) |
| 42 | + } |
| 43 | + out := buf.String() |
| 44 | + |
| 45 | + // Basic prelude should be present |
| 46 | + if !strings.Contains(out, "strict digraph") { |
| 47 | + t.Fatalf("output missing digraph prelude: %q", out) |
| 48 | + } |
| 49 | + |
| 50 | + // Each entity should be present as a node with a quoted label matching the ID |
| 51 | + expectedGroupNode := fmt.Sprintf("\t\t%q [label=%q]\n", groupUID, groupUID.ID) |
| 52 | + if !strings.Contains(out, expectedGroupNode) { |
| 53 | + t.Errorf("expected group node line %q not found in output:\n%s", expectedGroupNode, out) |
| 54 | + } |
| 55 | + |
| 56 | + expectedAliceNode := fmt.Sprintf("\t\t%q [label=%q]\n", aliceUID, aliceUID.ID) |
| 57 | + if !strings.Contains(out, expectedAliceNode) { |
| 58 | + t.Errorf("expected alice node line %q not found in output:\n%s", expectedAliceNode, out) |
| 59 | + } |
| 60 | + |
| 61 | + expectedBobNode := fmt.Sprintf("\t\t%q [label=%q]\n", bobUID, bobUID.ID) |
| 62 | + if !strings.Contains(out, expectedBobNode) { |
| 63 | + t.Errorf("expected bob node line %q not found in output:\n%s", expectedBobNode, out) |
| 64 | + } |
| 65 | + |
| 66 | + // Edge from alice to group should be present |
| 67 | + expectedEdge := fmt.Sprintf("\t%q -> %q\n", aliceUID, groupUID) |
| 68 | + if !strings.Contains(out, expectedEdge) { |
| 69 | + t.Errorf("expected edge %q not found in output:\n%s", expectedEdge, out) |
| 70 | + } |
| 71 | + }) |
| 72 | + |
| 73 | + t.Run("NoEdgesWhenNoParents", func(t *testing.T) { |
| 74 | + var buf bytes.Buffer |
| 75 | + |
| 76 | + // Two entities of different types with no parents; output must contain nodes but no edges |
| 77 | + uidA := types.NewEntityUID("TypeA", types.String("a1")) |
| 78 | + uidB := types.NewEntityUID("TypeB", types.String("b1")) |
| 79 | + |
| 80 | + entities := types.EntityMap{ |
| 81 | + uidA: {UID: uidA, Parents: types.NewEntityUIDSet()}, |
| 82 | + uidB: {UID: uidB, Parents: types.NewEntityUIDSet()}, |
| 83 | + } |
| 84 | + |
| 85 | + if err := Write(&buf, maps.Values(entities)); err != nil { |
| 86 | + t.Fatalf("ToDotStr returned error: %v", err) |
| 87 | + } |
| 88 | + out := buf.String() |
| 89 | + |
| 90 | + // Ensure nodes exist |
| 91 | + if !strings.Contains(out, strconv.Quote(uidA.String())) { |
| 92 | + t.Errorf("expected node for uidA %q not present", uidA.String()) |
| 93 | + } |
| 94 | + if !strings.Contains(out, strconv.Quote(uidB.String())) { |
| 95 | + t.Errorf("expected node for uidB %q not present", uidB.String()) |
| 96 | + } |
| 97 | + |
| 98 | + // Ensure there are no edges in the graph |
| 99 | + if strings.Contains(out, "->") { |
| 100 | + t.Errorf("did not expect any edges, but found some in output:\n%s", out) |
| 101 | + } |
| 102 | + }) |
| 103 | + |
| 104 | + t.Run("WriterFailure", func(t *testing.T) { |
| 105 | + // Build entities with multiple types and parents to trigger all write paths: |
| 106 | + // - prelude write |
| 107 | + // - subgraph header write (first type) |
| 108 | + // - node write (first type) |
| 109 | + // - subgraph close write (first type) |
| 110 | + // - subgraph header write (second type) |
| 111 | + // - node write (second type) |
| 112 | + // - subgraph close write (second type) |
| 113 | + // - edge write |
| 114 | + // - final close write |
| 115 | + groupUID := types.NewEntityUID("Group", types.String("admins")) |
| 116 | + aliceUID := types.NewEntityUID("User", types.String("alice")) |
| 117 | + bobUID := types.NewEntityUID("User", types.String("bob")) |
| 118 | + |
| 119 | + entities := types.EntityMap{ |
| 120 | + groupUID: {UID: groupUID, Parents: types.NewEntityUIDSet()}, |
| 121 | + aliceUID: {UID: aliceUID, Parents: types.NewEntityUIDSet(groupUID)}, |
| 122 | + bobUID: {UID: bobUID, Parents: types.NewEntityUIDSet()}, |
| 123 | + } |
| 124 | + |
| 125 | + // Test each failure point by allowing N successful writes before failing |
| 126 | + testCases := []struct { |
| 127 | + name string |
| 128 | + allowedWrites int |
| 129 | + expectedErrorMsg string |
| 130 | + }{ |
| 131 | + {"FailOnPrelude", 0, "write failed"}, |
| 132 | + {"FailOnFirstSubgraphHeader", 1, "write failed"}, |
| 133 | + {"FailOnFirstNodeWrite", 2, "write failed"}, |
| 134 | + {"FailOnSecondNodeWrite", 3, "write failed"}, |
| 135 | + {"FailOnFirstSubgraphClose", 4, "write failed"}, |
| 136 | + {"FailOnSecondSubgraphHeader", 5, "write failed"}, |
| 137 | + {"FailOnSecondTypeNodeWrite", 6, "write failed"}, |
| 138 | + {"FailOnSecondSubgraphClose", 7, "write failed"}, |
| 139 | + {"FailOnEdgeWrite", 8, "write failed"}, |
| 140 | + {"FailOnFinalClose", 9, "write failed"}, |
| 141 | + } |
| 142 | + |
| 143 | + for _, tc := range testCases { |
| 144 | + t.Run(tc.name, func(t *testing.T) { |
| 145 | + failingWriter := &failAfterNWriter{allowedWrites: tc.allowedWrites} |
| 146 | + err := Write(failingWriter, maps.Values(entities)) |
| 147 | + if err == nil { |
| 148 | + t.Fatal("expected Write to return error when writer fails, got nil") |
| 149 | + } |
| 150 | + if !strings.Contains(err.Error(), tc.expectedErrorMsg) { |
| 151 | + t.Errorf("expected error message to contain %q, got: %v", tc.expectedErrorMsg, err) |
| 152 | + } |
| 153 | + }) |
| 154 | + } |
| 155 | + }) |
| 156 | +} |
| 157 | + |
| 158 | +// failAfterNWriter is a writer that fails after N successful writes |
| 159 | +type failAfterNWriter struct { |
| 160 | + allowedWrites int |
| 161 | + writeCount int |
| 162 | +} |
| 163 | + |
| 164 | +func (f *failAfterNWriter) Write(p []byte) (n int, err error) { |
| 165 | + if f.writeCount >= f.allowedWrites { |
| 166 | + return 0, fmt.Errorf("write failed") |
| 167 | + } |
| 168 | + f.writeCount++ |
| 169 | + return len(p), nil |
| 170 | +} |
0 commit comments