package hostedwire import ( "bytes" "encoding/json" "errors" "math" "strings" "testing" "time" "amadan.net/oficina/equipo/internal/isolatedjob" ) func TestDecodeStrict(t *testing.T) { pad := func(n int) string { // {"reason":"aaa..."} padded to exactly n bytes. const frame = `{"reason":""}` return `{"reason":"` + strings.Repeat("a", n-len(frame)) + `"}` } cases := []struct { name string in string want error }{ {"valid", `{"reason":"done"}`, nil}, {"valid with space", " \n{\"reason\":\"done\"}\n ", nil}, {"exactly MaxBody", pad(MaxBody), nil}, {"over MaxBody", pad(MaxBody + 1), ErrTooLarge}, {"unknown field", `{"reason":"done","extra":1}`, ErrBadJSON}, {"field in another case", `{"Reason":"done"}`, ErrBadJSON}, {"duplicate field", `{"reason":"a","reason":"b"}`, ErrBadJSON}, {"trailing value", `{"reason":"a"} {"reason":"b"}`, ErrBadJSON}, {"trailing garbage", `{"reason":"a"}x`, ErrBadJSON}, {"empty", ``, ErrBadJSON}, {"null", `null`, ErrBadJSON}, {"array", `[]`, ErrBadJSON}, {"truncated", `{"reason":"a"`, ErrBadJSON}, {"wrong type", `{"reason":5}`, ErrBadJSON}, } for _, c := range cases { var v StopRequest err := DecodeStrict(strings.NewReader(c.in), &v) if !errors.Is(err, c.want) || (c.want == nil) != (err == nil) { name := c.name t.Fatalf("DecodeStrict(%s) = %v, want %v", name, err, c.want) } if c.want == nil && v.Reason == "" { t.Fatalf("DecodeStrict(%s) left Reason empty", c.name) } } // Nested objects are held to the same rules. nested := []struct { name string in string want error }{ {"valid", `{"vms":[{"vm_ref":"vm-1","state":"running","attempt_id":"a1","age_known":true,"age_ms":5}]}`, nil}, {"nested duplicate", `{"vms":[{"vm_ref":"vm-1","vm_ref":"vm-2"}]}`, ErrBadJSON}, {"nested unknown", `{"vms":[{"vm_ref":"vm-1","owner":"x"}]}`, ErrBadJSON}, {"nested other case", `{"vms":[{"VM_REF":"vm-1"}]}`, ErrBadJSON}, {"same key in sibling objects", `{"vms":[{"vm_ref":"vm-1"},{"vm_ref":"vm-2"}]}`, nil}, } for _, c := range nested { var v VMListResponse err := DecodeStrict(strings.NewReader(c.in), &v) if !errors.Is(err, c.want) || (c.want == nil) != (err == nil) { t.Fatalf("DecodeStrict(%s) = %v, want %v", c.name, err, c.want) } } } // TestWireNames pins the JSON names, so the controller and the instance cannot // drift apart silently. func TestWireNames(t *testing.T) { // Controller time, a duration since the controller's epoch, kept to the // nanosecond and above 2^53 so a float on the way would show. at := time.Duration(1<<62 + 1) cases := []struct { v any into func() any want string }{ {LaunchRequest{AttemptID: "a1", TaskID: "t1", Account: "acct", Replaces: "a0", Image: "sha256:ab", Size: isolatedjob.SizeSmall, MaxLifetimeMS: 1000, InstanceCode: "code"}, func() any { return &LaunchRequest{} }, `{"attempt_id":"a1","task_id":"t1","account":"acct","replaces":"a0","image":"sha256:ab","size":"small","max_lifetime_ms":1000,"instance_code":"code"}`}, {JobView{AttemptID: "a1", LaunchID: "l1", State: "running", VMRef: "vm-1", Epoch: 2, DeadlineInMS: 3, DrainInMS: 4, StopInMS: 5}, func() any { return &JobView{} }, `{"attempt_id":"a1","launch_id":"l1","state":"running","vm_ref":"vm-1","epoch":2,"deadline_in_ms":3,"drain_in_ms":4,"stop_in_ms":5}`}, {StopRequest{Reason: "done"}, func() any { return &StopRequest{} }, `{"reason":"done"}`}, {StopProof{AttemptID: "a1", VMRef: "vm-1", State: isolatedjob.StateStopped, ObservedAt: at}, func() any { return &StopProof{} }, `{"attempt_id":"a1","vm_ref":"vm-1","state":"stopped","observed_at_ns":4611686018427387905}`}, {DrainRequest{}, func() any { return &DrainRequest{} }, `{}`}, {DrainResponse{Generation: 4}, func() any { return &DrainResponse{} }, `{"generation":4}`}, {QuiescentRequest{Generation: 4}, func() any { return &QuiescentRequest{} }, `{"generation":4}`}, {QuiescentResponse{Generation: 4}, func() any { return &QuiescentResponse{} }, `{"generation":4}`}, {SuspendRequest{Generation: 4}, func() any { return &SuspendRequest{} }, `{"generation":4}`}, {SuspendResponse{Generation: 4}, func() any { return &SuspendResponse{} }, `{"generation":4}`}, {ReopenRequest{Generation: 4}, func() any { return &ReopenRequest{} }, `{"generation":4}`}, {ReopenResponse{Generation: 4}, func() any { return &ReopenResponse{} }, `{"generation":4}`}, {ResumeRequest{OperationID: "op1"}, func() any { return &ResumeRequest{} }, `{"operation_id":"op1"}`}, {ResumeResponse{Epoch: 3}, func() any { return &ResumeResponse{} }, `{"epoch":3}`}, {VMListResponse{VMs: []VMView{{VMRef: "vm-1", State: isolatedjob.StateRunning, AttemptID: "a1", AgeKnown: true, AgeMS: 9}}}, func() any { return &VMListResponse{} }, `{"vms":[{"vm_ref":"vm-1","state":"running","attempt_id":"a1","age_known":true,"age_ms":9}]}`}, {ErrorResponse{Code: "forbidden", Message: "not yours"}, func() any { return &ErrorResponse{} }, `{"code":"forbidden","message":"not yours"}`}, {Bootstrap{Version: BootstrapVersion, LaunchID: "l1", InstanceCode: "code"}, func() any { return &Bootstrap{} }, `{"version":1,"launch_id":"l1","instance_code":"code"}`}, } for _, c := range cases { b, err := json.Marshal(c.v) if err != nil { t.Fatal(err) } if string(b) != c.want { t.Fatalf("json of %T = %s, want %s", c.v, b, c.want) } back := c.into() if err := DecodeStrict(bytes.NewReader(b), back); err != nil { t.Fatalf("DecodeStrict(%s) into %T = %v", b, back, err) } again, _ := json.Marshal(back) if string(again) != c.want { t.Fatalf("round trip of %T = %s, want %s", c.v, again, c.want) } } } func TestLaunchLifetimeBounds(t *testing.T) { const most = math.MaxInt64 / int64(time.Millisecond) cases := []struct { ms int64 want time.Duration ok bool }{ {0, 0, true}, {1000, time.Second, true}, {28_800_000, 8 * time.Hour, true}, {most, time.Duration(most) * time.Millisecond, true}, {most + 1, 0, false}, // Each of these wraps to exactly one hour if multiplied unchecked. {288230376155311744, 0, false}, {-288230376148111744, 0, false}, {-1, 0, false}, {math.MaxInt64, 0, false}, {math.MinInt64, 0, false}, } for _, c := range cases { got, err := LaunchRequest{MaxLifetimeMS: c.ms}.MaxLifetime() if c.ok && (err != nil || got != c.want) { t.Fatalf("MaxLifetime(%d ms) = (%v, %v), want (%v, nil)", c.ms, got, err, c.want) } if !c.ok && (!errors.Is(err, ErrDuration) || got != 0) { t.Fatalf("MaxLifetime(%d ms) = (%v, %v), want (0, ErrDuration)", c.ms, got, err) } } }