CodeSampleX

Exemplo

google.golang.org/protobuf v1.26.0-rc.1: proto.Unmarshal

Amostra verificada para golang google.golang.org/protobuf v1.26.0-rc.1: proto.Unmarshal. O contrato rodou em go 1.26 · linux debian/x64 · docker e passou.

sha256:9c32899a973ca8dc9ee9d4d406ef80daab58a46c30123959738bbdd66cd842e3

Esta rede oferece uma coisa: uma amostra que compila. Ela a executou em um sandbox e guardou o recibo assinado. Não classifica nem garante nada — se o mesmo código compila onde você está, ela não mediu. Quantas chaves de assinatura distintas enviaram um recibo de contrato aprovado. Uma é só o autor; mais de uma significa que outra pessoa também o compilou. Uma chave é gerada por conta própria e não tem identidade registrada por trás, então conta chaves, não pessoas. MIT-0

Evidência de execução

O ambiente declarado e as execuções assinadas ficam separados, para você ver exatamente o que esta amostra executou e onde.

Base da evidência
Contrato assinado aprovado
Recibos de verificação
1
Chaves de assinatura que o compilaram
1
Ambiente declarado go 1.26 linux 24 · ubuntu · glibc 2.39 amd64 go 1.26 go 1.26 go 1

Ambientes das execuções de verificação

Ambiente Contrato Etapas Execução
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-04

Caso

HOW
Objetivo
verify google.golang.org/protobuf/proto.Unmarshal in pkg:golang/google.golang.org/protobuf@v1.26.0-rc.1
Pacotes
Símbolos
  • google.golang.org/protobuf/proto.Unmarshal
Ambiente
go 1.26.6
Criado
2026-09-04T23:37:42Z

Contrato

  1. proto.Unmarshal decodes valid wire-format bytes into a proto.Message
  2. proto.Unmarshal populates target proto.Message fields accurately
  3. proto.Unmarshal returns error for malformed wire-format bytes
  4. UnmarshalProto returns error when target proto.Message is nil
  5. UnmarshalStringWrapper decodes wire bytes into wrapperspb.StringValue

Arquivos

  • PROMPT.md
  • contract_test.go
  • csx.json
  • go.mod
  • go.sum
  • spec.json
  • unmarshal.go

Baixar o artefato de código-fonte (tar.gz)

Código-fonte

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 google.golang.org/protobuf/proto.Unmarshal in pkg:golang/google.golang.org/protobuf@v1.26.0-rc.1
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/google.golang.org/protobuf@v1.26.0-rc.1
Demonstrate these symbols/APIs:
  - google.golang.org/protobuf/proto.Unmarshal
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.
contract_test.go
package sample

import (
	"errors"
	"testing"

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

func TestContractUnmarshal(t *testing.T) {
	// 1. Verify proto.Unmarshal decodes valid wire-format bytes into a proto.Message
	origStr := wrapperspb.String("codesamplex verification")
	encodedStr, err := proto.Marshal(origStr)
	if err != nil {
		t.Fatalf("failed to marshal StringValue: %v", err)
	}

	decodedStr := &wrapperspb.StringValue{}
	if err := proto.Unmarshal(encodedStr, decodedStr); err != nil {
		t.Fatalf("proto.Unmarshal failed on valid bytes: %v", err)
	}
	if decodedStr.GetValue() != origStr.GetValue() {
		t.Fatalf("expected unmarshaled value %q, got %q", origStr.GetValue(), decodedStr.GetValue())
	}

	// 2. Verify proto.Unmarshal populates target message fields accurately
	origTS := &timestamppb.Timestamp{
		Seconds: 1700000000,
		Nanos:   500000,
	}
	encodedTS, err := proto.Marshal(origTS)
	if err != nil {
		t.Fatalf("failed to marshal Timestamp: %v", err)
	}

	decodedTS, err := UnmarshalTimestampWrapper(encodedTS)
	if err != nil {
		t.Fatalf("UnmarshalTimestampWrapper failed: %v", err)
	}
	if decodedTS.GetSeconds() != origTS.GetSeconds() || decodedTS.GetNanos() != origTS.GetNanos() {
		t.Fatalf("timestamp mismatch: got %v, expected %v", decodedTS, origTS)
	}

	// 3. Verify proto.Unmarshal returns error for malformed wire-format bytes
	malformedBytes := []byte{0xFF, 0xFF, 0xFF, 0xFF}
	targetMsg := &wrapperspb.StringValue{}
	if err := proto.Unmarshal(malformedBytes, targetMsg); err == nil {
		t.Fatalf("expected error unmarshaling malformed bytes, got nil")
	}

	// 4. Verify UnmarshalProto returns error when target is nil
	if err := UnmarshalProto(encodedStr, nil); !errors.Is(err, ErrNilTarget) {
		t.Fatalf("expected ErrNilTarget when unmarshaling into nil, got %v", err)
	}

	// 5. Verify UnmarshalStringWrapper helper decodes string wrapper
	wrapperRes, err := UnmarshalStringWrapper(encodedStr)
	if err != nil {
		t.Fatalf("UnmarshalStringWrapper failed: %v", err)
	}
	if wrapperRes.GetValue() != "codesamplex verification" {
		t.Fatalf("unexpected wrapper value: %v", wrapperRes.GetValue())
	}
}
csx.json
{"case":{"caseId":"case:sha256:989b0518437b5f367ffed369d68c349b17dd3e9be7f3554e45538dad0a2adbf5","contract":["proto.Unmarshal decodes valid wire-format bytes into a proto.Message","proto.Unmarshal populates target proto.Message fields accurately","proto.Unmarshal returns error for malformed wire-format bytes","UnmarshalProto returns error when target proto.Message is nil","UnmarshalStringWrapper decodes wire bytes into wrapperspb.StringValue"],"goal":"verify google.golang.org/protobuf/proto.Unmarshal in pkg:golang/google.golang.org/protobuf@v1.26.0-rc.1","kind":"HOW","packages":["pkg:golang/google.golang.org/protobuf@v1.26.0-rc.1"],"schemaVersion":1,"symbols":["google.golang.org/protobuf/proto.Unmarshal"]},"contractCommand":["go","test","./..."],"environment":{"arch":"amd64","compiler":"go","compilerVersion":"1.26.6","distro":"ubuntu","ecosystem":"golang","language":"go","languageVersion":"1.26.6","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","packageManagerVersion":"1.26.6","runtime":"go","runtimeVersion":"1.26.6","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/google.golang.org/protobuf@v1.26.0-rc.1"],"schemaVersion":1,"subject":"pkg:golang/google.golang.org/protobuf@v1.26.0-rc.1","symbols":["google.golang.org/protobuf/proto.Unmarshal"],"verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.26.6

require google.golang.org/protobuf v1.26.0-rc.1
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.26.0-rc.1 h1:7QnIQpGRHE5RnLKnESfDoxm2dTapTZua5a0kS0A+VXQ=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
spec.json
{
  "schemaVersion": 1,
  "goal": "verify google.golang.org/protobuf/proto.Unmarshal in pkg:golang/google.golang.org/protobuf@v1.26.0-rc.1",
  "kind": "HOW",
  "packages": [
    "pkg:golang/google.golang.org/protobuf@v1.26.0-rc.1"
  ],
  "symbols": [
    "google.golang.org/protobuf/proto.Unmarshal"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@10.9.8",
    "runtime": "node@22.23.2"
  }
}
unmarshal.go
package sample

import (
	"errors"
	"fmt"

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

// ErrNilTarget is returned when attempting to unmarshal into a nil message pointer.
var ErrNilTarget = errors.New("target message cannot be nil")

// UnmarshalProto decodes binary protobuf wire-format data into the provided message.
func UnmarshalProto(data []byte, msg proto.Message) error {
	if msg == nil {
		return ErrNilTarget
	}
	return proto.Unmarshal(data, msg)
}

// UnmarshalStringWrapper decodes wire bytes into a wrapperspb.StringValue message.
func UnmarshalStringWrapper(data []byte) (*wrapperspb.StringValue, error) {
	val := &wrapperspb.StringValue{}
	if err := proto.Unmarshal(data, val); err != nil {
		return nil, fmt.Errorf("failed to unmarshal string wrapper: %w", err)
	}
	return val, nil
}

// UnmarshalTimestampWrapper decodes wire bytes into a timestamppb.Timestamp message.
func UnmarshalTimestampWrapper(data []byte) (*timestamppb.Timestamp, error) {
	ts := &timestamppb.Timestamp{}
	if err := proto.Unmarshal(data, ts); err != nil {
		return nil, fmt.Errorf("failed to unmarshal timestamp: %w", err)
	}
	return ts, nil
}

Seeder de origem

anônimo