| 1 | package host |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/carlosframework/platform/internal/hostbox" |
| 7 | "github.com/carlosframework/platform/internal/hostwrite" |
| 8 | ) |
| 9 | |
| 10 | // rootLaunchSpec is the preimage root hashes for a launch, built exactly as |
| 11 | // ClaimRoleAssembly.Launch builds it (claimroleassembly_linux.go): the |
| 12 | // verifier (claimlaunch_authority.go) refuses a launch whose spec's hash is |
| 13 | // not the attempt's reserved LaunchInputs. |
| 14 | func rootLaunchSpec(in ClaimLaunchInputs) hostbox.LaunchSpec { |
| 15 | return hostbox.LaunchSpec{Identity: in.Identity, Executable: in.Executable, Digest: in.ExecutableSHA256, Args: append([]string(nil), in.Args...), Env: append([]string(nil), in.Env...)} |
| 16 | } |
| 17 | |
| 18 | // TestClaimLaunchDigestIsMainsAndRoots (home part 2, Task 2b fix round 4): |
| 19 | // ClaimLaunchDigest is main's, byte for byte, for an empty launch and for |
| 20 | // one whose instance sets app variables, and it is root's LaunchSpec hash |
| 21 | // of the same launch, so a reserved attempt passes root's launch check. |
| 22 | func TestClaimLaunchDigestIsMainsAndRoots(t *testing.T) { |
| 23 | for name, c := range map[string]struct { |
| 24 | in ClaimLaunchInputs |
| 25 | main string |
| 26 | }{ |
| 27 | "empty": {ClaimLaunchInputs{}, "0ed115c96598a2877d539d837d785f574bd5c4c8630fe1803ca216a0cb76396b"}, |
| 28 | "app variables": {ClaimLaunchInputs{Identity: hostwrite.Identity{Config: "launch-env:sha256:c", Release: "v7"}, Executable: "/opt/carlos/live/a.example.com", ExecutableSHA256: "aaaa", |
| 29 | Args: []string{"--socket", "/run/a.sock"}, Env: []string{"A=1", "HOSTNAME=x"}}, "47246370546f9bec4ae2541bace116bd102be9655ab1b376de990d5374f26bbe"}, |
| 30 | } { |
| 31 | d, err := ClaimLaunchDigest(c.in) |
| 32 | if err != nil || d != c.main { |
| 33 | t.Fatalf("%s: %s %v, want main's %s", name, d, err, c.main) |
| 34 | } |
| 35 | root, err := rootLaunchSpec(c.in).Hash() |
| 36 | if err != nil || root != d { |
| 37 | t.Fatalf("%s: root's spec hash %s %v, the reserved digest %s", name, root, err, d) |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |