CodeSampleX

サンプル

socket2 0.6.5

検証済みサンプル — cargo socket2 0.6.5. rust 1 · linux alpine/x64 · docker で contract を実行し、成功しました: Domain::for_address correctly identifies IPV4 and IPV6 socket…

sha256:dc044463bc09c2b5e444f486c1c05251f335563e0c3b8f41874ece1bd3f88d65

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。 MIT-0

実行証拠

宣言された環境と署名済みの実行を分けてあります。このサンプルが何をどこで実行したかをそのまま確認できます。

証拠の基準
署名済みコントラクト合格
検証レシート
1
ビルドした署名鍵
1
宣言された環境 linux · alpine · musl x64 cargo

検証実行環境

環境 コントラクト ステージ 実行日
rust 1 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · cargo@1rust:1-alpine@sha256:a10e64dd139b…
2026-09-19

ケース

HOW
ゴール
verify pkg:cargo/socket2@0.6.5
パッケージ
作成日
2026-09-18T16:12:01Z

コントラクト

  1. Domain::for_address correctly identifies IPV4 and IPV6 socket addresses
  2. Socket::new creates non-blocking IPv4 TCP stream sockets with configurable options
  3. Socket socket options for reuse address, tcp nodelay and IPv4 TTL are settable and retrievable
  4. SockAddr wraps IPv4 socket addresses and provides family and address accessors
  5. Connected IPv4 TCP sockets over loopback exchange stream bytes accurately
  6. Bound IPv4 UDP sockets over loopback exchange datagram packets accurately

ファイル

  • Cargo.lock
  • Cargo.toml
  • PROMPT.md
  • csx.json
  • spec.json
  • src/lib.rs
  • test/contract.rs

ソースアーティファクトをダウンロード (tar.gz)

ソース

Cargo.lock
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4

[[package]]
name = "libc"
version = "0.2.189"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3eaf3ede3fee6db1a4c2ee091bf8a8b4dccdc6d17f656fb07896ee72867612f2"

[[package]]
name = "sample-socket2"
version = "1.0.0"
dependencies = [
 "socket2",
]

[[package]]
name = "socket2"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3d1e2c7f27f8d4cb10542a02c49005dbd6e93095799d6f3be745fae9f8fedd4"
dependencies = [
 "libc",
 "windows-sys",
]

[[package]]
name = "windows-link"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"

[[package]]
name = "windows-sys"
version = "0.61.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"
dependencies = [
 "windows-link",
]
Cargo.toml
[package]
name = "sample-socket2"
version = "1.0.0"
edition = "2021"
publish = false

[dependencies]
socket2 = "=0.6.5"

[[bin]]
name = "contract"
path = "test/contract.rs"
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:cargo/socket2@0.6.5
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:cargo/socket2@0.6.5
Required runtime conditions:
  - ecosystem: golang
  - language: go
  - packageManager: go@1.26.6
  - runtime: go@1.26.6

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:026072011f966ed325b4309e1a28deb874edb2b191b0f484d19a95c95981653f","contract":["Domain::for_address correctly identifies IPV4 and IPV6 socket addresses","Socket::new creates non-blocking IPv4 TCP stream sockets with configurable options","Socket socket options for reuse address, tcp nodelay and IPv4 TTL are settable and retrievable","SockAddr wraps IPv4 socket addresses and provides family and address accessors","Connected IPv4 TCP sockets over loopback exchange stream bytes accurately","Bound IPv4 UDP sockets over loopback exchange datagram packets accurately"],"goal":"verify pkg:cargo/socket2@0.6.5","kind":"HOW","packages":["pkg:cargo/socket2@0.6.5"],"schemaVersion":1},"contractCommand":["cargo","run","--offline"],"environment":{"arch":"x64","distro":"alpine","ecosystem":"cargo","libc":"musl","os":"linux","packageManager":"cargo","schemaVersion":1},"license":"MIT-0","packages":["pkg:cargo/socket2@0.6.5"],"schemaVersion":1,"subject":"pkg:cargo/socket2@0.6.5","verifierAdapter":"cargo@1"}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:cargo/socket2@0.6.5",
  "kind": "HOW",
  "packages": [
    "pkg:cargo/socket2@0.6.5"
  ]
}
src/lib.rs
//! Clean-room verification sample for socket2.

pub use socket2::*;
test/contract.rs
use std::mem::MaybeUninit;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};
use socket2::{Domain, Protocol, SockAddr, Socket, Type};

fn main() {
    // 1. Domain::for_address correctly identifies IPV4 and IPV6 socket addresses
    let v4_addr = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::LOCALHOST, 8080));
    let v6_addr = SocketAddr::V6(SocketAddrV6::new(Ipv6Addr::LOCALHOST, 8080, 0, 0));
    assert_eq!(Domain::for_address(v4_addr), Domain::IPV4);
    assert_eq!(Domain::for_address(v6_addr), Domain::IPV6);

    // 2. Socket::new creates non-blocking IPv4 TCP stream sockets with configurable options
    let tcp_sock = Socket::new(Domain::IPV4, Type::STREAM, Some(Protocol::TCP))
        .expect("failed to create IPv4 TCP socket");
    tcp_sock.set_nonblocking(true).expect("failed to set nonblocking");

    // 3. Socket socket options for reuse address, tcp nodelay and IPv4 TTL are settable and retrievable
    tcp_sock.set_reuse_address(true).expect("failed to set reuse address");
    assert_eq!(tcp_sock.reuse_address().expect("failed to get reuse address"), true);

    tcp_sock.set_tcp_nodelay(true).expect("failed to set tcp nodelay");
    assert_eq!(tcp_sock.tcp_nodelay().expect("failed to get tcp nodelay"), true);

    tcp_sock.set_ttl_v4(64).expect("failed to set ttl_v4");
    assert_eq!(tcp_sock.ttl_v4().expect("failed to get ttl_v4"), 64);

    // 4. SockAddr wraps IPv4 socket addresses and provides family and address accessors
    let sa = SockAddr::from(v4_addr);
    assert!(sa.is_ipv4());
    assert_eq!(sa.domain(), Domain::IPV4);
    let resolved_v4 = sa.as_socket_ipv4().expect("expected IPv4 address");
    assert_eq!(resolved_v4.ip(), &Ipv4Addr::LOCALHOST);
    assert_eq!(resolved_v4.port(), 8080);

    // 5. Connected IPv4 TCP sockets over loopback exchange stream bytes accurately
    let listener = Socket::new(Domain::IPV4, Type::STREAM, Some(Protocol::TCP))
        .expect("failed to create listener socket");
    listener.set_reuse_address(true).expect("failed to set reuse address");
    let bind_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 0);
    listener.bind(&bind_addr.into()).expect("failed to bind listener");
    listener.listen(128).expect("failed to listen");
    let listen_addr = listener.local_addr().expect("failed to get local addr");

    let client = Socket::new(Domain::IPV4, Type::STREAM, Some(Protocol::TCP))
        .expect("failed to create client socket");
    client.connect(&listen_addr).expect("failed to connect");

    let (server, _client_addr) = listener.accept().expect("failed to accept");

    let msg = b"socket2-tcp-verification-token";
    let sent = client.send(msg).expect("failed to send");
    assert_eq!(sent, msg.len());

    let mut recv_buf = [MaybeUninit::uninit(); 64];
    let received = server.recv(&mut recv_buf).expect("failed to recv");
    assert_eq!(received, msg.len());

    let received_bytes: Vec<u8> = recv_buf[..received]
        .iter()
        .map(|b| unsafe { b.assume_init() })
        .collect();
    assert_eq!(&received_bytes[..], msg);

    // 6. Bound IPv4 UDP sockets over loopback exchange datagram packets accurately
    let udp_sender = Socket::new(Domain::IPV4, Type::DGRAM, Some(Protocol::UDP))
        .expect("failed to create udp sender");
    udp_sender.bind(&bind_addr.into()).expect("failed to bind udp sender");

    let udp_receiver = Socket::new(Domain::IPV4, Type::DGRAM, Some(Protocol::UDP))
        .expect("failed to create udp receiver");
    udp_receiver.bind(&bind_addr.into()).expect("failed to bind udp receiver");
    let receiver_addr = udp_receiver.local_addr().expect("failed to get udp receiver addr");

    let dgram = b"socket2-udp-datagram-test";
    let sent_dgram = udp_sender.send_to(dgram, &receiver_addr).expect("failed to send_to");
    assert_eq!(sent_dgram, dgram.len());

    let mut udp_buf = [MaybeUninit::uninit(); 64];
    let (received_dgram, from_addr) = udp_receiver.recv_from(&mut udp_buf).expect("failed to recv_from");
    assert_eq!(received_dgram, dgram.len());

    let sender_addr = udp_sender.local_addr().expect("failed to get udp sender addr");
    assert_eq!(
        from_addr.as_socket_ipv4().map(|a| a.port()),
        sender_addr.as_socket_ipv4().map(|a| a.port())
    );

    let received_dgram_bytes: Vec<u8> = udp_buf[..received_dgram]
        .iter()
        .map(|b| unsafe { b.assume_init() })
        .collect();
    assert_eq!(&received_dgram_bytes[..], dgram);

    println!("All socket2 contract assertions passed successfully.");
}

オリジンシーダー

匿名