| 1 | package guestos |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "os/exec" |
| 6 | "path/filepath" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | |
| 12 | |
| 13 | func TestMacSetupWaitsForCommittedGroupMembership(t *testing.T) { |
| 14 | data, err := os.ReadFile(filepath.Join("..", "..", "scripts", "guest-os", "macos-setup-account.m")) |
| 15 | if err != nil { |
| 16 | t.Fatal(err) |
| 17 | } |
| 18 | source := string(data) |
| 19 | start := strings.Index(source, "static BOOL member(") |
| 20 | end := strings.Index(source, "static BOOL disabled(") |
| 21 | if start < 0 || end <= start { |
| 22 | t.Fatal("membership helper not found") |
| 23 | } |
| 24 | harness := `#import <Foundation/Foundation.h> |
| 25 | #import <OpenDirectory/OpenDirectory.h> |
| 26 | #include <unistd.h> |
| 27 | static void require(BOOL ok, const char *reason) { if (!ok) { fprintf(stderr,"%s\n",reason); exit(1); } } |
| 28 | @interface FixtureGroup : NSObject |
| 29 | @property BOOL visible; |
| 30 | @property BOOL target; |
| 31 | @property BOOL pending; |
| 32 | @property BOOL stuck; |
| 33 | @property int reads; |
| 34 | @property int writes; |
| 35 | @end |
| 36 | @implementation FixtureGroup |
| 37 | - (BOOL)isMemberRecord:(ODRecord *)user error:(NSError **)error { |
| 38 | if (self.pending && !self.stuck && ++self.reads >= 3) self.visible = self.target; |
| 39 | return self.visible; |
| 40 | } |
| 41 | - (BOOL)addMemberRecord:(ODRecord *)user error:(NSError **)error { self.writes++; self.target=YES; self.pending=YES; return YES; } |
| 42 | - (BOOL)removeMemberRecord:(ODRecord *)user error:(NSError **)error { self.writes++; self.target=NO; self.pending=YES; return YES; } |
| 43 | - (BOOL)synchronizeAndReturnError:(NSError **)error { return YES; } |
| 44 | @end |
| 45 | ` + source[start:end] + ` |
| 46 | int main(int argc, const char **argv) { @autoreleasepool { |
| 47 | FixtureGroup *group = [FixtureGroup new]; |
| 48 | BOOL target = strcmp(argv[1],"revoke") != 0; |
| 49 | group.visible = !target; |
| 50 | group.stuck = strcmp(argv[1],"stuck") == 0; |
| 51 | setMember((ODRecord *)group, (ODRecord *)[NSObject new], target); |
| 52 | require(group.visible == target && group.writes == 1 && group.reads >= 3,"membership was not verified"); |
| 53 | setMember((ODRecord *)group, (ODRecord *)[NSObject new], target); |
| 54 | require(group.writes == 1,"idempotent setup wrote membership again"); |
| 55 | return 0; |
| 56 | } } |
| 57 | ` |
| 58 | program := filepath.Join(t.TempDir(), "membership-check") |
| 59 | compile := exec.CommandContext(t.Context(), "clang", "-x", "objective-c", "-fobjc-arc", "-Werror", "-", "-framework", "Foundation", "-framework", "OpenDirectory", "-o", program) |
| 60 | compile.Stdin = strings.NewReader(harness) |
| 61 | if output, err := compile.CombinedOutput(); err != nil { |
| 62 | t.Fatalf("compile membership harness: %v\n%s", err, output) |
| 63 | } |
| 64 | for _, mode := range []string{"grant", "revoke", "stuck"} { |
| 65 | t.Run(mode, func(t *testing.T) { |
| 66 | output, err := exec.CommandContext(t.Context(), program, mode).CombinedOutput() |
| 67 | if mode == "stuck" { |
| 68 | if err == nil || !strings.Contains(string(output), "Guest setup privileges were not applied") { |
| 69 | t.Fatalf("uncommitted membership accepted: %v %s", err, output) |
| 70 | } |
| 71 | } else if err != nil { |
| 72 | t.Fatalf("delayed membership rejected: %v %s", err, output) |
| 73 | } |
| 74 | }) |
| 75 | } |
| 76 | } |