File: syscall_spec.cr

package info (click to toggle)
crystal 1.14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 24,384 kB
  • sloc: javascript: 6,400; sh: 695; makefile: 269; ansic: 121; python: 105; cpp: 77; xml: 32
file content (29 lines) | stat: -rw-r--r-- 965 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
{% skip_file unless flag?(:linux) && !flag?(:interpreted) %}

require "spec"
require "syscall"

private module TestSyscall
  Syscall.def_syscall pipe2, Int32, pipefd : Int32[2]*, flags : Int32
  Syscall.def_syscall write, Int32, fd : Int32, buf : UInt8*, count : LibC::SizeT
  Syscall.def_syscall read, Int32, fd : Int32, buf : UInt8*, count : LibC::SizeT
  Syscall.def_syscall close, Int32, fd : Int32
end

describe Syscall do
  it "can call into the system successfully" do
    pair = uninitialized Int32[2]
    TestSyscall.pipe2(pointerof(pair), 0).should eq(0)

    str = "Hello"
    TestSyscall.write(pair[1], str.to_unsafe, LibC::SizeT.new(str.bytesize)).should eq(str.bytesize)

    buf = Bytes.new(64)
    TestSyscall.read(pair[0], buf.to_unsafe, LibC::SizeT.new(buf.size)).should eq(str.bytesize)

    String.new(buf.to_unsafe, str.bytesize).should eq(str)

    TestSyscall.close(pair[0]).should eq(0)
    TestSyscall.close(pair[1]).should eq(0)
  end
end