CodeSampleX

Exemple

github.com/google/gopacket v1.1.19: NewPacket

Échantillon vérifié pour golang github.com/google/gopacket v1.1.19: NewPacket. Le contrat s'est exécuté sur go 1.26 · linux debian/x64 · docker et a réussi.

sha256:b3761c794b0795713153cf8aca08aa3f4614604d5f3c72dc77264d30d7ed96a1

Ce réseau offre une seule chose : un échantillon qui compile. Il l'a exécuté dans un bac à sable et conservé le reçu signé. Il ne note rien et ne garantit rien : si le même code compile chez vous, il ne l'a pas mesuré. Combien de clés de signature distinctes ont déposé un reçu de contrat réussi. Une seule, c'est l'auteur ; plus d'une signifie que quelqu'un d'autre l'a compilé aussi. Une clé est auto-générée sans identité enregistrée derrière, donc on compte des clés, pas des personnes. MIT-0

Preuves d'exécution

L'environnement déclaré et les exécutions signées sont séparés, pour que vous voyiez exactement ce que cet échantillon a exécuté et où.

Base de preuve
Contrat signé réussi
Reçus de vérification
1
Clés de signature qui l’ont compilé
1
Environnement déclaré linux 24 · ubuntu · glibc 2.39 x64 go

Environnements des exécutions de vérification

Environnement Contrat Étapes Exécution
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

Cas

HOW
Objectif
verify github.com/google/gopacket.NewPacket in pkg:golang/github.com/google/gopacket@v1.1.19
Paquets
Symboles
  • github.com/google/gopacket.NewPacket
Créé
2026-09-04T03:56:28Z

Contrat

  1. NewPacket parses raw packet bytes into structured protocol layers starting from LayerTypeEthernet.
  2. NewPacket accurately extracts Ethernet, IPv4, and UDP layer headers and addresses.
  3. NewPacket provides access to NetworkLayer, TransportLayer, and ApplicationLayer convenience accessors.
  4. NewPacket supports Lazy decoding option deferring layer parsing until accessed.
  5. NewPacket supports NoCopy decoding option using the provided byte slice directly.
  6. NewPacket handles truncated or malformed packet data by recording an ErrorLayer without panicking.

Fichiers

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

Télécharger l’artefact source (tar.gz)

Code source

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 github.com/google/gopacket.NewPacket in pkg:golang/github.com/google/gopacket@v1.1.19
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/google/gopacket@v1.1.19
Demonstrate these symbols/APIs:
  - github.com/google/gopacket.NewPacket

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:2d79345f56f66c1273171788fab977d60c9f7ff76d337e12bf57ab38e0d10ca9","contract":["NewPacket parses raw packet bytes into structured protocol layers starting from LayerTypeEthernet.","NewPacket accurately extracts Ethernet, IPv4, and UDP layer headers and addresses.","NewPacket provides access to NetworkLayer, TransportLayer, and ApplicationLayer convenience accessors.","NewPacket supports Lazy decoding option deferring layer parsing until accessed.","NewPacket supports NoCopy decoding option using the provided byte slice directly.","NewPacket handles truncated or malformed packet data by recording an ErrorLayer without panicking."],"goal":"verify github.com/google/gopacket.NewPacket in pkg:golang/github.com/google/gopacket@v1.1.19","kind":"HOW","packages":["pkg:golang/github.com/google/gopacket@v1.1.19"],"schemaVersion":1,"symbols":["github.com/google/gopacket.NewPacket"]},"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/github.com/google/gopacket@v1.1.19"],"schemaVersion":1,"subject":"pkg:golang/github.com/google/gopacket@v1.1.19","symbols":["github.com/google/gopacket.NewPacket"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.26.6

require github.com/google/gopacket v1.1.19
go.sum
github.com/google/gopacket v1.1.19 h1:ves8RnFZPGiFnTS0uPQStjwru6uO6h+nlr9j6fL7kF8=
github.com/google/gopacket v1.1.19/go.mod h1:iJ8V8n6KS+z2U1A8pUwu8bW5SyEMkXJB8Yo/Vo+TKTo=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
main.go
package main

import (
	"fmt"
	"net"

	"github.com/google/gopacket"
	"github.com/google/gopacket/layers"
)

func main() {
	// Construct an Ethernet + IPv4 + UDP packet with payload
	eth := &layers.Ethernet{
		SrcMAC:       net.HardwareAddr{0x00, 0x11, 0x22, 0x33, 0x44, 0x55},
		DstMAC:       net.HardwareAddr{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff},
		EthernetType: layers.EthernetTypeIPv4,
	}
	ip := &layers.IPv4{
		Version:  4,
		IHL:      5,
		TTL:      64,
		Protocol: layers.IPProtocolUDP,
		SrcIP:    net.IP{192, 168, 1, 10},
		DstIP:    net.IP{192, 168, 1, 20},
	}
	udp := &layers.UDP{
		SrcPort: layers.UDPPort(1234),
		DstPort: layers.UDPPort(5678),
	}
	if err := udp.SetNetworkLayerForChecksum(ip); err != nil {
		panic(err)
	}
	payload := gopacket.Payload([]byte("CodeSampleX verification payload"))

	buf := gopacket.NewSerializeBuffer()
	opts := gopacket.SerializeOptions{
		FixLengths:       true,
		ComputeChecksums: true,
	}
	if err := gopacket.SerializeLayers(buf, opts, eth, ip, udp, payload); err != nil {
		panic(err)
	}

	// Demonstrate gopacket.NewPacket decoding
	rawBytes := buf.Bytes()
	packet := gopacket.NewPacket(rawBytes, layers.LayerTypeEthernet, gopacket.Default)

	fmt.Printf("Packet length: %d bytes\n", len(packet.Data()))

	// Access decoded layers
	if ethLayer := packet.Layer(layers.LayerTypeEthernet); ethLayer != nil {
		ethParsed, _ := ethLayer.(*layers.Ethernet)
		fmt.Printf("Ethernet: %s -> %s\n", ethParsed.SrcMAC, ethParsed.DstMAC)
	}
	if netLayer := packet.NetworkLayer(); netLayer != nil {
		ipParsed, _ := netLayer.(*layers.IPv4)
		fmt.Printf("IPv4: %s -> %s\n", ipParsed.SrcIP, ipParsed.DstIP)
	}
	if transLayer := packet.TransportLayer(); transLayer != nil {
		udpParsed, _ := transLayer.(*layers.UDP)
		fmt.Printf("UDP: %d -> %d\n", udpParsed.SrcPort, udpParsed.DstPort)
	}
	if appLayer := packet.ApplicationLayer(); appLayer != nil {
		fmt.Printf("Payload: %s\n", string(appLayer.Payload()))
	}
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify github.com/google/gopacket.NewPacket in pkg:golang/github.com/google/gopacket@v1.1.19",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/google/gopacket@v1.1.19"
  ],
  "symbols": [
    "github.com/google/gopacket.NewPacket"
  ]
}
test/contract.go
package main

import (
	"bytes"
	"fmt"
	"net"
	"os"

	"github.com/google/gopacket"
	"github.com/google/gopacket/layers"
)

func main() {
	srcMAC := net.HardwareAddr{0x00, 0x11, 0x22, 0x33, 0x44, 0x55}
	dstMAC := net.HardwareAddr{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}
	srcIP := net.IP{192, 168, 1, 10}
	dstIP := net.IP{192, 168, 1, 20}
	srcPort := layers.UDPPort(1234)
	dstPort := layers.UDPPort(5678)
	payloadBytes := []byte("verification-test-payload-123")

	eth := &layers.Ethernet{
		SrcMAC:       srcMAC,
		DstMAC:       dstMAC,
		EthernetType: layers.EthernetTypeIPv4,
	}
	ip := &layers.IPv4{
		Version:  4,
		IHL:      5,
		TTL:      64,
		Protocol: layers.IPProtocolUDP,
		SrcIP:    srcIP,
		DstIP:    dstIP,
	}
	udp := &layers.UDP{
		SrcPort: srcPort,
		DstPort: dstPort,
	}
	if err := udp.SetNetworkLayerForChecksum(ip); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: SetNetworkLayerForChecksum: %v\n", err)
		os.Exit(1)
	}
	payload := gopacket.Payload(payloadBytes)

	buf := gopacket.NewSerializeBuffer()
	opts := gopacket.SerializeOptions{
		FixLengths:       true,
		ComputeChecksums: true,
	}
	if err := gopacket.SerializeLayers(buf, opts, eth, ip, udp, payload); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: SerializeLayers: %v\n", err)
		os.Exit(1)
	}
	rawBytes := buf.Bytes()

	// Assertion 1: NewPacket parses raw packet bytes into structured protocol layers starting from LayerTypeEthernet.
	packet := gopacket.NewPacket(rawBytes, layers.LayerTypeEthernet, gopacket.Default)
	if packet == nil {
		fmt.Fprintf(os.Stderr, "FAIL: NewPacket returned nil\n")
		os.Exit(1)
	}
	if !bytes.Equal(packet.Data(), rawBytes) {
		fmt.Fprintf(os.Stderr, "FAIL: packet.Data() does not match original bytes\n")
		os.Exit(1)
	}
	layerList := packet.Layers()
	if len(layerList) != 4 {
		fmt.Fprintf(os.Stderr, "FAIL: expected 4 layers, got %d\n", len(layerList))
		os.Exit(1)
	}

	// Assertion 2: NewPacket accurately extracts Ethernet, IPv4, and UDP layer headers and addresses.
	ethLayer := packet.Layer(layers.LayerTypeEthernet)
	if ethLayer == nil {
		fmt.Fprintf(os.Stderr, "FAIL: missing Ethernet layer\n")
		os.Exit(1)
	}
	ethParsed, ok := ethLayer.(*layers.Ethernet)
	if !ok || !bytes.Equal(ethParsed.SrcMAC, srcMAC) || !bytes.Equal(ethParsed.DstMAC, dstMAC) {
		fmt.Fprintf(os.Stderr, "FAIL: Ethernet layer properties mismatch\n")
		os.Exit(1)
	}

	ipLayer := packet.Layer(layers.LayerTypeIPv4)
	if ipLayer == nil {
		fmt.Fprintf(os.Stderr, "FAIL: missing IPv4 layer\n")
		os.Exit(1)
	}
	ipParsed, ok := ipLayer.(*layers.IPv4)
	if !ok || !ipParsed.SrcIP.Equal(srcIP) || !ipParsed.DstIP.Equal(dstIP) {
		fmt.Fprintf(os.Stderr, "FAIL: IPv4 layer properties mismatch\n")
		os.Exit(1)
	}

	udpLayer := packet.Layer(layers.LayerTypeUDP)
	if udpLayer == nil {
		fmt.Fprintf(os.Stderr, "FAIL: missing UDP layer\n")
		os.Exit(1)
	}
	udpParsed, ok := udpLayer.(*layers.UDP)
	if !ok || udpParsed.SrcPort != srcPort || udpParsed.DstPort != dstPort {
		fmt.Fprintf(os.Stderr, "FAIL: UDP layer properties mismatch\n")
		os.Exit(1)
	}

	// Assertion 3: NewPacket provides access to NetworkLayer, TransportLayer, and ApplicationLayer convenience accessors.
	netLayer := packet.NetworkLayer()
	if netLayer == nil || netLayer.LayerType() != layers.LayerTypeIPv4 {
		fmt.Fprintf(os.Stderr, "FAIL: NetworkLayer accessor mismatch\n")
		os.Exit(1)
	}
	transLayer := packet.TransportLayer()
	if transLayer == nil || transLayer.LayerType() != layers.LayerTypeUDP {
		fmt.Fprintf(os.Stderr, "FAIL: TransportLayer accessor mismatch\n")
		os.Exit(1)
	}
	appLayer := packet.ApplicationLayer()
	if appLayer == nil || !bytes.Equal(appLayer.Payload(), payloadBytes) {
		fmt.Fprintf(os.Stderr, "FAIL: ApplicationLayer payload mismatch\n")
		os.Exit(1)
	}

	// Assertion 4: NewPacket supports Lazy decoding option deferring layer parsing until accessed.
	lazyPacket := gopacket.NewPacket(rawBytes, layers.LayerTypeEthernet, gopacket.Lazy)
	if lazyPacket == nil {
		fmt.Fprintf(os.Stderr, "FAIL: NewPacket with Lazy returned nil\n")
		os.Exit(1)
	}
	if lazyPacket.NetworkLayer() == nil || !bytes.Equal(lazyPacket.NetworkLayer().LayerContents(), ipLayer.LayerContents()) {
		fmt.Fprintf(os.Stderr, "FAIL: Lazy packet NetworkLayer mismatch\n")
		os.Exit(1)
	}

	// Assertion 5: NewPacket supports NoCopy decoding option using the provided byte slice directly.
	noCopyPacket := gopacket.NewPacket(rawBytes, layers.LayerTypeEthernet, gopacket.NoCopy)
	if noCopyPacket == nil || !bytes.Equal(noCopyPacket.Data(), rawBytes) {
		fmt.Fprintf(os.Stderr, "FAIL: NewPacket with NoCopy failed\n")
		os.Exit(1)
	}

	// Assertion 6: NewPacket handles truncated or malformed packet data by recording an ErrorLayer without panicking.
	truncatedBytes := rawBytes[:8]
	errPacket := gopacket.NewPacket(truncatedBytes, layers.LayerTypeEthernet, gopacket.Default)
	if errPacket == nil {
		fmt.Fprintf(os.Stderr, "FAIL: NewPacket on truncated data returned nil\n")
		os.Exit(1)
	}
	if errPacket.ErrorLayer() == nil {
		fmt.Fprintf(os.Stderr, "FAIL: expected ErrorLayer on truncated packet, got nil\n")
		os.Exit(1)
	}

	fmt.Println("PASS: all gopacket.NewPacket contract assertions passed.")
}

Seeder d'origine

anonyme