CodeSampleX

Exemple

github.com/miekg/dns v1.1.63: NewZoneParser, ZoneParser.Err, ZoneParser.Next

Échantillon vérifié pour golang github.com/miekg/dns v1.1.63: NewZoneParser, ZoneParser.Err, ZoneParser.Next. Le contrat s'est exécuté sur go 1.26 · linux…

sha256:9fcd9066fe9284d5fa1fd54a67db5dbde1ad9e27c10ace11c48fd6a0e242dbbb

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 amd64 go 1.26 go 1

Environnements des exécutions de vérification

Environnement Contrat Étapes Exécution
go 1.26 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26-alpine@sha256:28d89ee9cc0f…
2026-08-27

Cas

HOW
Objectif
verify dns.ZoneParser in pkg:golang/github.com/miekg/dns@v1.1.63
Paquets
Symboles
  • dns.NewZoneParser
  • dns.ZoneParser.Err
  • dns.ZoneParser.Next
Créé
2026-08-27T21:48:10Z

Contrat

  1. dns.NewZoneParser initializes a zone file parser with specified reader, origin, and filename
  2. dns.ZoneParser.Next iterates and returns parsed resource records including SOA, NS, A, AAAA, MX, CNAME, and TXT
  3. dns.ZoneParser.Next applies master file directives such as $ORIGIN and $TTL during parsing
  4. dns.ZoneParser.Err reports syntax errors when invalid or malformed zone data is encountered

Fichiers

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • spec.json
  • test/contract_test.go
  • zone_parser.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 dns.ZoneParser in pkg:golang/github.com/miekg/dns@v1.1.63
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/miekg/dns@v1.1.63
Demonstrate these symbols/APIs:
  - dns.NewZoneParser
  - dns.ZoneParser.Err
  - dns.ZoneParser.Next

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:598694f87560a15bf3dffcd8cd3025823084d0ebb79a72385dd09de683143ab9","contract":["dns.NewZoneParser initializes a zone file parser with specified reader, origin, and filename","dns.ZoneParser.Next iterates and returns parsed resource records including SOA, NS, A, AAAA, MX, CNAME, and TXT","dns.ZoneParser.Next applies master file directives such as $ORIGIN and $TTL during parsing","dns.ZoneParser.Err reports syntax errors when invalid or malformed zone data is encountered"],"goal":"verify dns.ZoneParser in pkg:golang/github.com/miekg/dns@v1.1.63","kind":"HOW","packages":["pkg:golang/github.com/miekg/dns@v1.1.63"],"schemaVersion":1,"symbols":["dns.NewZoneParser","dns.ZoneParser.Err","dns.ZoneParser.Next"]},"contractCommand":["go","test","-v","./..."],"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","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/github.com/miekg/dns@v1.1.63"],"schemaVersion":1,"subject":"pkg:golang/github.com/miekg/dns@v1.1.63","symbols":["dns.NewZoneParser","dns.ZoneParser.Err","dns.ZoneParser.Next"],"verifierAdapter":"golang@1"}
go.mod
module example.com/zoneparser

go 1.26.6

require github.com/miekg/dns v1.1.63

require (
	golang.org/x/mod v0.18.0 // indirect
	golang.org/x/net v0.31.0 // indirect
	golang.org/x/sync v0.7.0 // indirect
	golang.org/x/sys v0.27.0 // indirect
	golang.org/x/tools v0.22.0 // indirect
)
go.sum
github.com/miekg/dns v1.1.63 h1:8M5aAw6OMZfFXTT7K5V0Eu5YiiL8l7nUAkyN6C9YwaY=
github.com/miekg/dns v1.1.63/go.mod h1:6NGHfjhpmr5lt3XPLuyfDJi5AXbNIPM9PY6H6sF1Nfs=
golang.org/x/mod v0.18.0 h1:5+9lSbEzPSdWkH32vYPBwEpX8KwDbM52Ud9xBUvNlb0=
golang.org/x/mod v0.18.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.31.0 h1:68CPQngjLL0r2AlUKiSxtQFKvzRVbnzLwMUn5SzcLHo=
golang.org/x/net v0.31.0/go.mod h1:P4fl1q7dY2hnZFxEk4pPSkDHF+QqjitcnDjUQyMM+pM=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/tools v0.22.0 h1:gqSGLZqv+AI9lIQzniJ0nZDRG5GBPsSi+DRNHWNz6yA=
golang.org/x/tools v0.22.0/go.mod h1:aCwcsjqvq7Yqt6TNyX7QMU2enbQ/Gt0bo6krSeEri+c=
spec.json
{
  "schemaVersion": 1,
  "goal": "verify dns.ZoneParser in pkg:golang/github.com/miekg/dns@v1.1.63",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/miekg/dns@v1.1.63"
  ],
  "symbols": [
    "dns.NewZoneParser",
    "dns.ZoneParser.Err",
    "dns.ZoneParser.Next"
  ]
}
test/contract_test.go
package test

import (
	"strings"
	"testing"

	"example.com/zoneparser"
	"github.com/miekg/dns"
)

func TestZoneParserBasicAndRecordTypes(t *testing.T) {
	zoneData := `$ORIGIN example.com.
$TTL 3600
@   IN  SOA ns1.example.com. admin.example.com. (
            2026082701 ; serial
            7200       ; refresh
            3600       ; retry
            1209600    ; expire
            3600       ; minimum
        )
@   IN  NS  ns1.example.com.
@   IN  NS  ns2.example.com.
@   IN  A   93.184.216.34
@   IN  AAAA 2606:2800:220:1:248:1893:25c8:1946
@   IN  MX  10 mail.example.com.
www IN  CNAME example.com.
txt IN  TXT "v=spf1 include:_spf.example.com ~all"
`

	records, err := zoneparser.ParseZoneString(zoneData, "example.com.", 3600)
	if err != nil {
		t.Fatalf("ParseZoneString failed: %v", err)
	}

	if len(records) != 8 {
		t.Fatalf("expected 8 resource records, got %d", len(records))
	}

	// Verify SOA record
	soa, ok := records[0].(*dns.SOA)
	if !ok {
		t.Fatalf("expected record 0 to be *dns.SOA, got %T", records[0])
	}
	if soa.Hdr.Name != "example.com." {
		t.Errorf("expected SOA name 'example.com.', got '%s'", soa.Hdr.Name)
	}
	if soa.Ns != "ns1.example.com." {
		t.Errorf("expected SOA Ns 'ns1.example.com.', got '%s'", soa.Ns)
	}
	if soa.Mbox != "admin.example.com." {
		t.Errorf("expected SOA Mbox 'admin.example.com.', got '%s'", soa.Mbox)
	}
	if soa.Serial != 2026082701 {
		t.Errorf("expected SOA Serial 2026082701, got %d", soa.Serial)
	}

	// Verify NS records
	ns1, ok := records[1].(*dns.NS)
	if !ok || ns1.Ns != "ns1.example.com." {
		t.Errorf("unexpected NS record 1: %v", records[1])
	}

	// Verify A record
	aRec, ok := records[3].(*dns.A)
	if !ok {
		t.Fatalf("expected record 3 to be *dns.A, got %T", records[3])
	}
	if aRec.A.String() != "93.184.216.34" {
		t.Errorf("expected A IP '93.184.216.34', got '%s'", aRec.A.String())
	}

	// Verify AAAA record
	aaaaRec, ok := records[4].(*dns.AAAA)
	if !ok {
		t.Fatalf("expected record 4 to be *dns.AAAA, got %T", records[4])
	}
	if aaaaRec.AAAA.String() != "2606:2800:220:1:248:1893:25c8:1946" {
		t.Errorf("expected AAAA IP '2606:2800:220:1:248:1893:25c8:1946', got '%s'", aaaaRec.AAAA.String())
	}

	// Verify MX record
	mxRec, ok := records[5].(*dns.MX)
	if !ok {
		t.Fatalf("expected record 5 to be *dns.MX, got %T", records[5])
	}
	if mxRec.Preference != 10 || mxRec.Mx != "mail.example.com." {
		t.Errorf("expected MX 10 mail.example.com., got %d %s", mxRec.Preference, mxRec.Mx)
	}

	// Verify CNAME record
	cnameRec, ok := records[6].(*dns.CNAME)
	if !ok {
		t.Fatalf("expected record 6 to be *dns.CNAME, got %T", records[6])
	}
	if cnameRec.Hdr.Name != "www.example.com." || cnameRec.Target != "example.com." {
		t.Errorf("expected CNAME www.example.com. -> example.com., got %s -> %s", cnameRec.Hdr.Name, cnameRec.Target)
	}

	// Verify TXT record
	txtRec, ok := records[7].(*dns.TXT)
	if !ok {
		t.Fatalf("expected record 7 to be *dns.TXT, got %T", records[7])
	}
	if len(txtRec.Txt) != 1 || txtRec.Txt[0] != "v=spf1 include:_spf.example.com ~all" {
		t.Errorf("unexpected TXT payload: %v", txtRec.Txt)
	}
}

func TestZoneParserDirectivesAndDefaultTTL(t *testing.T) {
	zoneData := `
$TTL 1800
host1   IN  A   192.0.2.1
$ORIGIN sub.example.com.
host2   IN  A   192.0.2.2
`
	reader := strings.NewReader(zoneData)
	records, err := zoneparser.ParseZoneReader(reader, "example.com.", "test.zone")
	if err != nil {
		t.Fatalf("ParseZoneReader failed: %v", err)
	}

	if len(records) != 2 {
		t.Fatalf("expected 2 records, got %d", len(records))
	}

	if records[0].Header().Name != "host1.example.com." {
		t.Errorf("expected host1.example.com., got '%s'", records[0].Header().Name)
	}
	if records[0].Header().Ttl != 1800 {
		t.Errorf("expected TTL 1800, got %d", records[0].Header().Ttl)
	}

	if records[1].Header().Name != "host2.sub.example.com." {
		t.Errorf("expected host2.sub.example.com., got '%s'", records[1].Header().Name)
	}
}

func TestZoneParserErrorHandling(t *testing.T) {
	// Malformed zone data: invalid IP in A record
	invalidZoneData := `example.com. 3600 IN A not-a-valid-ip`
	_, err := zoneparser.ParseZoneString(invalidZoneData, "example.com.", 3600)
	if err == nil {
		t.Fatalf("expected error parsing invalid A record IP, got nil")
	}

	// Verify zp.Err directly
	reader := strings.NewReader("example.com. 3600 IN INVALIDTYPE foo")
	zp := dns.NewZoneParser(reader, "example.com.", "")
	for _, ok := zp.Next(); ok; _, ok = zp.Next() {
	}
	if zp.Err() == nil {
		t.Fatalf("expected zp.Err() to return non-nil error on invalid RR type")
	}
}
zone_parser.go
package zoneparser

import (
	"fmt"
	"io"
	"strings"

	"github.com/miekg/dns"
)

// ParseZoneString parses a DNS zone from a string with the given origin and default TTL.
func ParseZoneString(zoneData string, origin string, defaultTTL uint32) ([]dns.RR, error) {
	reader := strings.NewReader(zoneData)
	zp := dns.NewZoneParser(reader, origin, "")
	if defaultTTL > 0 {
		zp.SetDefaultTTL(defaultTTL)
	}

	var records []dns.RR
	for rr, ok := zp.Next(); ok; rr, ok = zp.Next() {
		records = append(records, rr)
	}

	if err := zp.Err(); err != nil {
		return nil, fmt.Errorf("zone parsing failed: %w", err)
	}

	return records, nil
}

// ParseZoneReader parses DNS resource records from an io.Reader stream.
func ParseZoneReader(r io.Reader, origin string, file string) ([]dns.RR, error) {
	zp := dns.NewZoneParser(r, origin, file)

	var records []dns.RR
	for rr, ok := zp.Next(); ok; rr, ok = zp.Next() {
		records = append(records, rr)
	}

	if err := zp.Err(); err != nil {
		return nil, fmt.Errorf("zone parsing failed: %w", err)
	}

	return records, nil
}

Seeder d'origine

anonyme