CodeSampleX

Ejemplo

google.golang.org/protobuf v1.33.0: proto.Marshal

Muestra verificada para golang google.golang.org/protobuf v1.33.0: proto.Marshal. El contrato se ejecutó en go 1.26 · linux debian/x64 · docker y pasó.

sha256:872d862eaf547e6a0b13796fde4c542edcb1450e2d6a825b77076c0b8ec3a13d

Esta red ofrece una sola cosa: una muestra que compila. La ejecutó en un sandbox y guardó el recibo firmado. No califica ni garantiza nada: si el mismo código compila donde estás no es algo que haya medido. Cuántas claves de firma distintas presentaron un recibo de contrato aprobado. Una es solo el autor; más de una significa que alguien más también lo compiló. Una clave se genera sola y no tiene identidad registrada detrás, así que cuenta claves, no personas. MIT-0

Evidencia de ejecución

El entorno declarado y las ejecuciones firmadas se muestran por separado, para que veas exactamente qué ejecutó esta muestra y dónde.

Base de evidencia
Contrato firmado aprobado
Recibos de verificación
1
Claves de firma que lo compilaron
1
Entorno declarado linux 24 · ubuntu · glibc 2.39 x64 go

Entornos de las ejecuciones de verificación

Entorno Contrato Etapas Ejecución
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-09-01

Caso

HOW
Objetivo
verify proto.Marshal in pkg:golang/google.golang.org/protobuf@v1.33.0
Paquetes
Símbolos
  • proto.Marshal
Creado
2026-09-01T22:50:48Z

Contrato

  1. proto.Marshal serializes a populated Timestamp message to wire-format bytes that unmarshal to an equal message
  2. proto.Marshal serializes a wrapperspb StringValue to binary wire format
  3. proto.Marshal serializes a dynamic structpb Struct containing string and numeric fields
  4. proto.Marshal serializes a nil message into nil bytes with no error
  5. proto.MarshalOptions with Deterministic enabled produces byte-identical output across repeated marshals

Archivos

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • main.go
  • spec.json
  • test/contract.go

Descargar el artefacto de código fuente (tar.gz)

Código fuente

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 proto.Marshal in pkg:golang/google.golang.org/protobuf@v1.33.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/google.golang.org/protobuf@v1.33.0
Demonstrate these symbols/APIs:
  - proto.Marshal

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:674f2fc12256ce109ff271631d17050ab12821854928be85088649d8b8e1fe4c","contract":["proto.Marshal serializes a populated Timestamp message to wire-format bytes that unmarshal to an equal message","proto.Marshal serializes a wrapperspb StringValue to binary wire format","proto.Marshal serializes a dynamic structpb Struct containing string and numeric fields","proto.Marshal serializes a nil message into nil bytes with no error","proto.MarshalOptions with Deterministic enabled produces byte-identical output across repeated marshals"],"goal":"verify proto.Marshal in pkg:golang/google.golang.org/protobuf@v1.33.0","kind":"HOW","packages":["pkg:golang/google.golang.org/protobuf@v1.33.0"],"schemaVersion":1,"symbols":["proto.Marshal"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/google.golang.org/protobuf@v1.33.0"],"schemaVersion":1,"subject":"pkg:golang/google.golang.org/protobuf@v1.33.0","symbols":["proto.Marshal"],"verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.23

require google.golang.org/protobuf v1.33.0
go.sum
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.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
main.go
package main

import (
	"fmt"
	"os"

	"google.golang.org/protobuf/proto"
	"google.golang.org/protobuf/types/known/structpb"
	"google.golang.org/protobuf/types/known/timestamppb"
	"google.golang.org/protobuf/types/known/wrapperspb"
)

// SerializeMessage marshals a proto.Message into wire-format bytes.
func SerializeMessage(m proto.Message) ([]byte, error) {
	return proto.Marshal(m)
}

// DeserializeMessage unmarshals wire-format bytes into a target proto.Message.
func DeserializeMessage(data []byte, target proto.Message) error {
	return proto.Unmarshal(data, target)
}

func main() {
	// Example 1: Marshal a Timestamp message
	ts := &timestamppb.Timestamp{Seconds: 1700000000, Nanos: 123456}
	data, err := SerializeMessage(ts)
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to marshal timestamp: %v\n", err)
		os.Exit(1)
	}
	fmt.Printf("Marshaled Timestamp (%d bytes)\n", len(data))

	var decodedTs timestamppb.Timestamp
	if err := DeserializeMessage(data, &decodedTs); err != nil {
		fmt.Fprintf(os.Stderr, "failed to unmarshal timestamp: %v\n", err)
		os.Exit(1)
	}
	fmt.Printf("Unmarshaled Timestamp: seconds=%d, nanos=%d\n", decodedTs.GetSeconds(), decodedTs.GetNanos())

	// Example 2: Marshal a StringValue wrapper
	strVal := wrapperspb.String("hello protobuf")
	strData, err := SerializeMessage(strVal)
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to marshal string value: %v\n", err)
		os.Exit(1)
	}
	fmt.Printf("Marshaled StringValue (%d bytes)\n", len(strData))

	// Example 3: Marshal a dynamic Struct message
	st, err := structpb.NewStruct(map[string]interface{}{
		"status": "active",
		"count":  float64(42),
	})
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to build struct: %v\n", err)
		os.Exit(1)
	}
	stData, err := SerializeMessage(st)
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to marshal struct: %v\n", err)
		os.Exit(1)
	}
	fmt.Printf("Marshaled Struct (%d bytes)\n", len(stData))
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify proto.Marshal in pkg:golang/google.golang.org/protobuf@v1.33.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/google.golang.org/protobuf@v1.33.0"
  ],
  "symbols": [
    "proto.Marshal"
  ]
}
test/contract.go
package main

import (
	"bytes"
	"fmt"
	"os"

	"google.golang.org/protobuf/proto"
	"google.golang.org/protobuf/types/known/structpb"
	"google.golang.org/protobuf/types/known/timestamppb"
	"google.golang.org/protobuf/types/known/wrapperspb"
)

func main() {
	// Assertion 1: proto.Marshal serializes a populated Timestamp message to wire-format bytes that unmarshal to an equal message
	ts := &timestamppb.Timestamp{Seconds: 1700000000, Nanos: 654321}
	tsBytes, err := proto.Marshal(ts)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: proto.Marshal failed on Timestamp: %v\n", err)
		os.Exit(1)
	}
	if len(tsBytes) == 0 {
		fmt.Fprintf(os.Stderr, "FAIL: expected non-empty bytes for marshaled Timestamp\n")
		os.Exit(1)
	}
	var unmarshaledTs timestamppb.Timestamp
	if err := proto.Unmarshal(tsBytes, &unmarshaledTs); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: proto.Unmarshal failed on Timestamp: %v\n", err)
		os.Exit(1)
	}
	if !proto.Equal(ts, &unmarshaledTs) {
		fmt.Fprintf(os.Stderr, "FAIL: unmarshaled Timestamp does not match original: got %+v, want %+v\n", &unmarshaledTs, ts)
		os.Exit(1)
	}

	// Assertion 2: proto.Marshal serializes a wrapperspb StringValue to binary wire format
	strVal := wrapperspb.String("test-string-payload")
	strBytes, err := proto.Marshal(strVal)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: proto.Marshal failed on StringValue: %v\n", err)
		os.Exit(1)
	}
	var unmarshaledStr wrapperspb.StringValue
	if err := proto.Unmarshal(strBytes, &unmarshaledStr); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: proto.Unmarshal failed on StringValue: %v\n", err)
		os.Exit(1)
	}
	if unmarshaledStr.GetValue() != "test-string-payload" {
		fmt.Fprintf(os.Stderr, "FAIL: unmarshaled StringValue mismatch: got %q, want %q\n", unmarshaledStr.GetValue(), "test-string-payload")
		os.Exit(1)
	}

	// Assertion 3: proto.Marshal serializes a dynamic structpb Struct containing string and numeric fields
	st, err := structpb.NewStruct(map[string]interface{}{
		"key":   "value",
		"count": float64(100),
	})
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: structpb.NewStruct failed: %v\n", err)
		os.Exit(1)
	}
	stBytes, err := proto.Marshal(st)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: proto.Marshal failed on Struct: %v\n", err)
		os.Exit(1)
	}
	var unmarshaledSt structpb.Struct
	if err := proto.Unmarshal(stBytes, &unmarshaledSt); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: proto.Unmarshal failed on Struct: %v\n", err)
		os.Exit(1)
	}
	if !proto.Equal(st, &unmarshaledSt) {
		fmt.Fprintf(os.Stderr, "FAIL: unmarshaled Struct does not match original\n")
		os.Exit(1)
	}

	// Assertion 4: proto.Marshal serializes a nil message into nil bytes with no error
	nilBytes, err := proto.Marshal(nil)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: proto.Marshal(nil) returned error: %v\n", err)
		os.Exit(1)
	}
	if nilBytes != nil {
		fmt.Fprintf(os.Stderr, "FAIL: proto.Marshal(nil) returned non-nil slice: %v\n", nilBytes)
		os.Exit(1)
	}

	// Assertion 5: proto.MarshalOptions with Deterministic enabled produces byte-identical output across repeated marshals
	opts := proto.MarshalOptions{Deterministic: true}
	b1, err1 := opts.Marshal(st)
	b2, err2 := opts.Marshal(st)
	if err1 != nil || err2 != nil {
		fmt.Fprintf(os.Stderr, "FAIL: Deterministic Marshal failed: %v, %v\n", err1, err2)
		os.Exit(1)
	}
	if !bytes.Equal(b1, b2) {
		fmt.Fprintf(os.Stderr, "FAIL: Deterministic Marshal output was not identical\n")
		os.Exit(1)
	}

	fmt.Println("PASS: proto.Marshal contract verified")
}

Seeder de origen

anónimo