| 1 | package trustgraph |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/containers/libtrust" |
| 8 | ) |
| 9 | |
| 10 | func createTestKeysAndGrants(count int) ([]*Grant, []libtrust.PrivateKey) { |
| 11 | grants := make([]*Grant, count) |
| 12 | keys := make([]libtrust.PrivateKey, count) |
| 13 | for i := 0; i < count; i++ { |
| 14 | pk, err := libtrust.GenerateECP256PrivateKey() |
| 15 | if err != nil { |
| 16 | panic(err) |
| 17 | } |
| 18 | grant := &Grant{ |
| 19 | Subject: fmt.Sprintf("/user-%d", i+1), |
| 20 | Permission: 0x0f, |
| 21 | Grantee: pk.KeyID(), |
| 22 | } |
| 23 | keys[i] = pk |
| 24 | grants[i] = grant |
| 25 | } |
| 26 | return grants, keys |
| 27 | } |
| 28 | |
| 29 | func testVerified(t *testing.T, g TrustGraph, k libtrust.PublicKey, keyName, target string, permission uint16) { |
| 30 | if ok, err := g.Verify(k, target, permission); err != nil { |
| 31 | t.Fatalf("Unexpected error during verification: %s", err) |
| 32 | } else if !ok { |
| 33 | t.Errorf("key failed verification\n\tKey: %s(%s)\n\tNamespace: %s", keyName, k.KeyID(), target) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | func testNotVerified(t *testing.T, g TrustGraph, k libtrust.PublicKey, keyName, target string, permission uint16) { |
| 38 | if ok, err := g.Verify(k, target, permission); err != nil { |
| 39 | t.Fatalf("Unexpected error during verification: %s", err) |
| 40 | } else if ok { |
| 41 | t.Errorf("key should have failed verification\n\tKey: %s(%s)\n\tNamespace: %s", keyName, k.KeyID(), target) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | func TestVerify(t *testing.T) { |
| 46 | grants, keys := createTestKeysAndGrants(4) |
| 47 | extraGrants := make([]*Grant, 3) |
| 48 | extraGrants[0] = &Grant{ |
| 49 | Subject: "/user-3", |
| 50 | Permission: 0x0f, |
| 51 | Grantee: "/user-2", |
| 52 | } |
| 53 | extraGrants[1] = &Grant{ |
| 54 | Subject: "/user-3/sub-project", |
| 55 | Permission: 0x0f, |
| 56 | Grantee: "/user-4", |
| 57 | } |
| 58 | extraGrants[2] = &Grant{ |
| 59 | Subject: "/user-4", |
| 60 | Permission: 0x07, |
| 61 | Grantee: "/user-1", |
| 62 | } |
| 63 | grants = append(grants, extraGrants...) |
| 64 | |
| 65 | g := NewMemoryGraph(grants) |
| 66 | |
| 67 | testVerified(t, g, keys[0].PublicKey(), "user-key-1", "/user-1", 0x0f) |
| 68 | testVerified(t, g, keys[0].PublicKey(), "user-key-1", "/user-1/some-project/sub-value", 0x0f) |
| 69 | testVerified(t, g, keys[0].PublicKey(), "user-key-1", "/user-4", 0x07) |
| 70 | testVerified(t, g, keys[1].PublicKey(), "user-key-2", "/user-2/", 0x0f) |
| 71 | testVerified(t, g, keys[2].PublicKey(), "user-key-3", "/user-3/sub-value", 0x0f) |
| 72 | testVerified(t, g, keys[1].PublicKey(), "user-key-2", "/user-3/sub-value", 0x0f) |
| 73 | testVerified(t, g, keys[1].PublicKey(), "user-key-2", "/user-3", 0x0f) |
| 74 | testVerified(t, g, keys[1].PublicKey(), "user-key-2", "/user-3/", 0x0f) |
| 75 | testVerified(t, g, keys[3].PublicKey(), "user-key-4", "/user-3/sub-project", 0x0f) |
| 76 | testVerified(t, g, keys[3].PublicKey(), "user-key-4", "/user-3/sub-project/app", 0x0f) |
| 77 | testVerified(t, g, keys[3].PublicKey(), "user-key-4", "/user-4", 0x0f) |
| 78 | |
| 79 | testNotVerified(t, g, keys[0].PublicKey(), "user-key-1", "/user-2", 0x0f) |
| 80 | testNotVerified(t, g, keys[0].PublicKey(), "user-key-1", "/user-3/sub-value", 0x0f) |
| 81 | testNotVerified(t, g, keys[0].PublicKey(), "user-key-1", "/user-4", 0x0f) |
| 82 | testNotVerified(t, g, keys[1].PublicKey(), "user-key-2", "/user-1/", 0x0f) |
| 83 | testNotVerified(t, g, keys[2].PublicKey(), "user-key-3", "/user-2", 0x0f) |
| 84 | testNotVerified(t, g, keys[1].PublicKey(), "user-key-2", "/user-4", 0x0f) |
| 85 | testNotVerified(t, g, keys[3].PublicKey(), "user-key-4", "/user-3", 0x0f) |
| 86 | } |
| 87 | |
| 88 | func TestCircularWalk(t *testing.T) { |
| 89 | grants, keys := createTestKeysAndGrants(3) |
| 90 | user1Grant := &Grant{ |
| 91 | Subject: "/user-2", |
| 92 | Permission: 0x0f, |
| 93 | Grantee: "/user-1", |
| 94 | } |
| 95 | user2Grant := &Grant{ |
| 96 | Subject: "/user-1", |
| 97 | Permission: 0x0f, |
| 98 | Grantee: "/user-2", |
| 99 | } |
| 100 | grants = append(grants, user1Grant, user2Grant) |
| 101 | |
| 102 | g := NewMemoryGraph(grants) |
| 103 | |
| 104 | testVerified(t, g, keys[0].PublicKey(), "user-key-1", "/user-1", 0x0f) |
| 105 | testVerified(t, g, keys[0].PublicKey(), "user-key-1", "/user-2", 0x0f) |
| 106 | testVerified(t, g, keys[1].PublicKey(), "user-key-2", "/user-2", 0x0f) |
| 107 | testVerified(t, g, keys[1].PublicKey(), "user-key-2", "/user-1", 0x0f) |
| 108 | testVerified(t, g, keys[2].PublicKey(), "user-key-3", "/user-3", 0x0f) |
| 109 | |
| 110 | testNotVerified(t, g, keys[0].PublicKey(), "user-key-1", "/user-3", 0x0f) |
| 111 | testNotVerified(t, g, keys[1].PublicKey(), "user-key-2", "/user-3", 0x0f) |
| 112 | } |
| 113 | |
| 114 | func assertGrantSame(t *testing.T, actual, expected *Grant) { |
| 115 | if actual != expected { |
| 116 | t.Fatalf("Unexpected grant retrieved\n\tExpected: %v\n\tActual: %v", expected, actual) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | func TestGetGrants(t *testing.T) { |
| 121 | grants, keys := createTestKeysAndGrants(5) |
| 122 | extraGrants := make([]*Grant, 4) |
| 123 | extraGrants[0] = &Grant{ |
| 124 | Subject: "/user-3/friend-project", |
| 125 | Permission: 0x0f, |
| 126 | Grantee: "/user-2/friends", |
| 127 | } |
| 128 | extraGrants[1] = &Grant{ |
| 129 | Subject: "/user-3/sub-project", |
| 130 | Permission: 0x0f, |
| 131 | Grantee: "/user-4", |
| 132 | } |
| 133 | extraGrants[2] = &Grant{ |
| 134 | Subject: "/user-2/friends", |
| 135 | Permission: 0x0f, |
| 136 | Grantee: "/user-5/fun-project", |
| 137 | } |
| 138 | extraGrants[3] = &Grant{ |
| 139 | Subject: "/user-5/fun-project", |
| 140 | Permission: 0x0f, |
| 141 | Grantee: "/user-1", |
| 142 | } |
| 143 | grants = append(grants, extraGrants...) |
| 144 | |
| 145 | g := NewMemoryGraph(grants) |
| 146 | |
| 147 | grantChains, err := g.GetGrants(keys[3], "/user-3/sub-project/specific-app", 0x0f) |
| 148 | if err != nil { |
| 149 | t.Fatalf("Error getting grants: %s", err) |
| 150 | } |
| 151 | if len(grantChains) != 1 { |
| 152 | t.Fatalf("Expected number of grant chains returned, expected %d, received %d", 1, len(grantChains)) |
| 153 | } |
| 154 | if len(grantChains[0]) != 2 { |
| 155 | t.Fatalf("Unexpected number of grants retrieved\n\tExpected: %d\n\tActual: %d", 2, len(grantChains[0])) |
| 156 | } |
| 157 | assertGrantSame(t, grantChains[0][0], grants[3]) |
| 158 | assertGrantSame(t, grantChains[0][1], extraGrants[1]) |
| 159 | |
| 160 | grantChains, err = g.GetGrants(keys[0], "/user-3/friend-project/fun-app", 0x0f) |
| 161 | if err != nil { |
| 162 | t.Fatalf("Error getting grants: %s", err) |
| 163 | } |
| 164 | if len(grantChains) != 1 { |
| 165 | t.Fatalf("Expected number of grant chains returned, expected %d, received %d", 1, len(grantChains)) |
| 166 | } |
| 167 | if len(grantChains[0]) != 4 { |
| 168 | t.Fatalf("Unexpected number of grants retrieved\n\tExpected: %d\n\tActual: %d", 2, len(grantChains[0])) |
| 169 | } |
| 170 | assertGrantSame(t, grantChains[0][0], grants[0]) |
| 171 | assertGrantSame(t, grantChains[0][1], extraGrants[3]) |
| 172 | assertGrantSame(t, grantChains[0][2], extraGrants[2]) |
| 173 | assertGrantSame(t, grantChains[0][3], extraGrants[0]) |
| 174 | } |