CodeSampleX

Sample

google.golang.org/protobuf v1.30.0

Verified sample for golang google.golang.org/protobuf v1.30.0. The contract ran on go 1.26 · linux debian/x64 · docker and passed.

sha256:9b50826fc4fdc65d60cb7b30bb07b22bc91718ef571133de183c656f6b4db2e7

This network offers one thing: a sample that builds. It ran the sample in a sandbox and kept the signed receipt. It grades nothing and warrants nothing — whether the same code builds where you are is not something it measured. How many distinct signing keys filed a passing contract receipt. One is the author alone; more than one means somebody else built it too. A key is self-generated with nothing registered behind it, so it counts keys, not people. MIT-0

Execution evidence

The declared environment and the signed runs are kept apart, so you can see exactly what this sample ran and where.

Evidence basis
Signed contract pass
Verification receipts
1
Signing keys that built it
1
Declared environment go linux 24 · ubuntu · glibc 2.39 x64 go go go

Verification-run environments

Environment Contract Stages Run
go 1.26 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26@sha256:e30143be198a…
2026-11-27

Case

HOW
Goal
verify pkg:golang/google.golang.org/protobuf@v1.30.0
Packages
Environment
go
Created
2026-11-27T01:57:57Z

Contract

  1. proto.Marshal, proto.Size, proto.Unmarshal, and proto.Equal round-trip a structpb.Struct accurately, and proto.Clone produces an independent deep copy unaffected by mutations.
  2. proto.MarshalOptions{Deterministic: true} produces byte-identical output across repeated marshals of a multi-key structpb.Struct.
  3. proto.Merge combines entries from a source structpb.Struct into a destination without mutating the source.
  4. anypb.New packs a timestamppb.Timestamp, MessageIs distinguishes matching and non-matching types, UnmarshalTo and UnmarshalNew recover the original message, and UnmarshalTo returns an error for a mismatched target type.
  5. protojson.Marshal and protojson.Unmarshal round-trip timestamppb.Timestamp as RFC 3339 JSON; default protojson.Unmarshal rejects unknown JSON fields whereas protojson.UnmarshalOptions{DiscardUnknown: true} ignores them.
  6. timestamppb.New and durationpb.New pass CheckValid() and round-trip time.Time and time.Duration, while invalid nanos configurations fail CheckValid().

Files

  • NOTES.md
  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • spec.json
  • test/contract_test.go

Download the source artifact (tar.gz)

Source

NOTES.md
# Verification of `google.golang.org/protobuf@v1.30.0`

Offline contract sample verifying core serialization, cloning, merging, JSON encoding, `Any` packing, and well-known type validation in `google.golang.org/protobuf@v1.30.0`.

## Verified Behaviors

1. **Binary Wire Round-Trip & Deep Copy (`proto`)**: `proto.Marshal`, `proto.Size`, `proto.Unmarshal`, `proto.Equal`, and `proto.Clone` preserve message equality and keep cloned messages independent from subsequent mutations.
2. **Deterministic Serialization (`proto.MarshalOptions`)**: Setting `Deterministic: true` produces identical byte sequences across repeated marshals of map-backed `structpb.Struct` values.
3. **Message Merging (`proto.Merge`)**: Merging overwrites colliding map entries and adds new ones on the destination without mutating the source.
4. **Typed Envelope Packing (`anypb`)**: `anypb.New`, `MessageIs`, `UnmarshalTo`, and `UnmarshalNew` round-trip `*timestamppb.Timestamp` and reject mismatched destination message types.
5. **JSON Encoding & Unknown Field Handling (`protojson`)**: `protojson.Marshal` and `protojson.Unmarshal` format and parse RFC 3339 timestamps; default unmarshaling rejects unknown JSON fields while `DiscardUnknown: true` ignores them.
6. **Well-Known Type Validation (`timestamppb`, `durationpb`)**: Valid `Timestamp` and `Duration` values pass `CheckValid()` and round-trip with Go `time.Time` / `time.Duration`, while invalid nanos values fail `CheckValid()`.
PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify pkg:golang/google.golang.org/protobuf@v1.30.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/google.golang.org/protobuf@v1.30.0
Constraints:
  - executionContext: node
Required runtime conditions:
  - ecosystem: npm
  - language: javascript
  - moduleSystem: cjs
  - packageManager: npm@10.9.8
  - runtime: node@22.23.2

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:7bedd7702d6e261355486507eaa18810c62cf1d30bfc03d2ee8b559e15550721","constraints":{"executionContext":"go"},"contract":["proto.Marshal, proto.Size, proto.Unmarshal, and proto.Equal round-trip a structpb.Struct accurately, and proto.Clone produces an independent deep copy unaffected by mutations.","proto.MarshalOptions{Deterministic: true} produces byte-identical output across repeated marshals of a multi-key structpb.Struct.","proto.Merge combines entries from a source structpb.Struct into a destination without mutating the source.","anypb.New packs a timestamppb.Timestamp, MessageIs distinguishes matching and non-matching types, UnmarshalTo and UnmarshalNew recover the original message, and UnmarshalTo returns an error for a mismatched target type.","protojson.Marshal and protojson.Unmarshal round-trip timestamppb.Timestamp as RFC 3339 JSON; default protojson.Unmarshal rejects unknown JSON fields whereas protojson.UnmarshalOptions{DiscardUnknown: true} ignores them.","timestamppb.New and durationpb.New pass CheckValid() and round-trip time.Time and time.Duration, while invalid nanos configurations fail CheckValid()."],"goal":"verify pkg:golang/google.golang.org/protobuf@v1.30.0","kind":"HOW","packages":["pkg:golang/google.golang.org/protobuf@v1.30.0"],"schemaVersion":1},"contractCommand":["go","test","./..."],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","executionContext":"go","language":"go","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","runtime":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/google.golang.org/protobuf@v1.30.0"],"schemaVersion":1,"subject":"pkg:golang/google.golang.org/protobuf@v1.30.0","verifierAdapter":"golang@1"}
go.mod
module example.com/protobufverify

go 1.22

require google.golang.org/protobuf v1.30.0
go.sum
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng=
google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:golang/google.golang.org/protobuf@v1.30.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/google.golang.org/protobuf@v1.30.0"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@10.9.8",
    "runtime": "node@22.23.2"
  }
}
test/contract_test.go
package contract_test

import (
	"bytes"
	"testing"
	"time"

	"google.golang.org/protobuf/encoding/protojson"
	"google.golang.org/protobuf/proto"
	"google.golang.org/protobuf/types/known/anypb"
	"google.golang.org/protobuf/types/known/durationpb"
	"google.golang.org/protobuf/types/known/structpb"
	"google.golang.org/protobuf/types/known/timestamppb"
)

func TestWireMarshalUnmarshalCloneEqual(t *testing.T) {
	original, err := structpb.NewStruct(map[string]any{
		"name":    "alpha",
		"count":   42,
		"enabled": true,
		"tags":    []any{"v1", "v2"},
		"nested":  map[string]any{"ratio": 0.5},
	})
	if err != nil {
		t.Fatalf("structpb.NewStruct failed: %v", err)
	}

	wire, err := proto.Marshal(original)
	if err != nil {
		t.Fatalf("proto.Marshal failed: %v", err)
	}
	if got, want := len(wire), proto.Size(original); got != want {
		t.Fatalf("marshaled length %d != proto.Size %d", got, want)
	}

	decoded := &structpb.Struct{}
	if err := proto.Unmarshal(wire, decoded); err != nil {
		t.Fatalf("proto.Unmarshal failed: %v", err)
	}
	if !proto.Equal(original, decoded) {
		t.Fatalf("decoded message does not equal original")
	}

	cloned, ok := proto.Clone(original).(*structpb.Struct)
	if !ok {
		t.Fatalf("proto.Clone did not return *structpb.Struct")
	}
	if !proto.Equal(original, cloned) {
		t.Fatalf("cloned message does not equal original")
	}

	cloned.Fields["name"] = structpb.NewStringValue("beta")
	if proto.Equal(original, cloned) {
		t.Fatalf("mutating clone should not mutate original")
	}
	if got := original.Fields["name"].GetStringValue(); got != "alpha" {
		t.Fatalf("original field mutated: got %q, want %q", got, "alpha")
	}
}

func TestDeterministicMarshal(t *testing.T) {
	msg, err := structpb.NewStruct(map[string]any{
		"a": 1,
		"b": 2,
		"c": 3,
		"d": 4,
		"e": 5,
		"f": 6,
		"g": 7,
		"h": 8,
	})
	if err != nil {
		t.Fatalf("structpb.NewStruct failed: %v", err)
	}

	opts := proto.MarshalOptions{Deterministic: true}
	baseline, err := opts.Marshal(msg)
	if err != nil {
		t.Fatalf("deterministic Marshal failed: %v", err)
	}

	for i := 0; i < 20; i++ {
		got, err := opts.Marshal(msg)
		if err != nil {
			t.Fatalf("deterministic Marshal iteration %d failed: %v", i, err)
		}
		if !bytes.Equal(baseline, got) {
			t.Fatalf("deterministic Marshal produced different bytes on iteration %d", i)
		}
	}
}

func TestProtoMerge(t *testing.T) {
	dst, err := structpb.NewStruct(map[string]any{
		"keep":     "original",
		"override": "old",
	})
	if err != nil {
		t.Fatalf("NewStruct dst failed: %v", err)
	}
	src, err := structpb.NewStruct(map[string]any{
		"override": "new",
		"added":    true,
	})
	if err != nil {
		t.Fatalf("NewStruct src failed: %v", err)
	}

	proto.Merge(dst, src)

	if got := dst.Fields["keep"].GetStringValue(); got != "original" {
		t.Fatalf("dst.keep = %q, want %q", got, "original")
	}
	if got := dst.Fields["override"].GetStringValue(); got != "new" {
		t.Fatalf("dst.override = %q, want %q", got, "new")
	}
	if got := dst.Fields["added"].GetBoolValue(); !got {
		t.Fatalf("dst.added = %v, want true", got)
	}
	if _, exists := src.Fields["keep"]; exists {
		t.Fatalf("proto.Merge unexpectedly modified source message")
	}
}

func TestAnyPackAndUnpack(t *testing.T) {
	ts := timestamppb.New(time.Date(2025, 3, 15, 10, 30, 0, 0, time.UTC))

	packed, err := anypb.New(ts)
	if err != nil {
		t.Fatalf("anypb.New failed: %v", err)
	}
	if !packed.MessageIs(&timestamppb.Timestamp{}) {
		t.Fatalf("MessageIs returned false for Timestamp")
	}
	if packed.MessageIs(&durationpb.Duration{}) {
		t.Fatalf("MessageIs returned true for unrelated Duration")
	}

	unpacked := &timestamppb.Timestamp{}
	if err := packed.UnmarshalTo(unpacked); err != nil {
		t.Fatalf("UnmarshalTo Timestamp failed: %v", err)
	}
	if !proto.Equal(ts, unpacked) {
		t.Fatalf("unpacked Timestamp does not equal original")
	}

	dynMsg, err := packed.UnmarshalNew()
	if err != nil {
		t.Fatalf("UnmarshalNew failed: %v", err)
	}
	if !proto.Equal(ts, dynMsg) {
		t.Fatalf("UnmarshalNew result does not equal original")
	}

	wrong := &durationpb.Duration{}
	if err := packed.UnmarshalTo(wrong); err == nil {
		t.Fatalf("expected UnmarshalTo wrong message type to fail, got nil")
	}
}

func TestProtoJSONRoundTripAndUnknownFields(t *testing.T) {
	ts := timestamppb.New(time.Date(2025, 6, 1, 12, 0, 0, 0, time.UTC))

	jsonBytes, err := protojson.Marshal(ts)
	if err != nil {
		t.Fatalf("protojson.Marshal failed: %v", err)
	}
	if got, want := string(jsonBytes), `"2025-06-01T12:00:00Z"`; got != want {
		t.Fatalf("protojson.Marshal(Timestamp) = %s, want %s", got, want)
	}

	roundTripped := &timestamppb.Timestamp{}
	if err := protojson.Unmarshal(jsonBytes, roundTripped); err != nil {
		t.Fatalf("protojson.Unmarshal failed: %v", err)
	}
	if !proto.Equal(ts, roundTripped) {
		t.Fatalf("round-tripped Timestamp does not equal original")
	}

	// Verify unknown-field handling with a message containing explicit proto fields (Any).
	payload := []byte(`{"@type":"type.googleapis.com/google.protobuf.Duration","value":"5s","extraUnknown":123}`)
	strictTarget := &anypb.Any{}
	if err := protojson.Unmarshal(payload, strictTarget); err == nil {
		t.Fatalf("expected default protojson.Unmarshal to reject unknown field, got nil")
	}

	lenientTarget := &anypb.Any{}
	if err := (protojson.UnmarshalOptions{DiscardUnknown: true}).Unmarshal(payload, lenientTarget); err != nil {
		t.Fatalf("expected DiscardUnknown protojson.Unmarshal to succeed, got error: %v", err)
	}
	dur := &durationpb.Duration{}
	if err := lenientTarget.UnmarshalTo(dur); err != nil {
		t.Fatalf("UnmarshalTo Duration after lenient JSON unmarshal failed: %v", err)
	}
	if got, want := dur.AsDuration(), 5*time.Second; got != want {
		t.Fatalf("unpacked duration = %v, want %v", got, want)
	}
}

func TestTimestampAndDurationValidation(t *testing.T) {
	wantTime := time.Date(2024, 11, 20, 8, 15, 30, 500000000, time.UTC)
	ts := timestamppb.New(wantTime)
	if err := ts.CheckValid(); err != nil {
		t.Fatalf("valid Timestamp failed CheckValid: %v", err)
	}
	if got := ts.AsTime(); !got.Equal(wantTime) {
		t.Fatalf("ts.AsTime() = %v, want %v", got, wantTime)
	}

	invalidTS := &timestamppb.Timestamp{Seconds: 0, Nanos: -1}
	if err := invalidTS.CheckValid(); err == nil {
		t.Fatalf("expected negative nanos Timestamp to fail CheckValid, got nil")
	}

	wantDur := 2*time.Hour + 30*time.Minute
	dur := durationpb.New(wantDur)
	if err := dur.CheckValid(); err != nil {
		t.Fatalf("valid Duration failed CheckValid: %v", err)
	}
	if got := dur.AsDuration(); got != wantDur {
		t.Fatalf("dur.AsDuration() = %v, want %v", got, wantDur)
	}

	invalidDur := &durationpb.Duration{Seconds: 10, Nanos: -500}
	if err := invalidDur.CheckValid(); err == nil {
		t.Fatalf("expected mismatched-sign Duration to fail CheckValid, got nil")
	}
}

Origin Seeder

anonymous