File: data_test.rb

package info (click to toggle)
ruby-gpgme 2.0.23-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,920 kB
  • sloc: ruby: 3,129; ansic: 2,559; sh: 7; makefile: 5
file content (142 lines) | stat: -rw-r--r-- 3,844 bytes parent folder | download | duplicates (4)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# -*- encoding: utf-8 -*-
require 'test_helper'

describe GPGME::Data do
  describe :new do
    it "smartly creates an empty buffer if nothing passed" do
      data = GPGME::Data.new
      assert_instance_of GPGME::Data, data
      assert_respond_to data, :read
      assert_respond_to data, :write
    end

    it "doesn't create a new object if the object passed is a Data already" do
      data = GPGME::Data.new
      new_data = GPGME::Data.new(data)

      assert_equal data, new_data
    end

    it "creates a data from strings" do
      data = GPGME::Data.new("wadus")
      assert_equal "wadus", data.read
    end

    it "creates a data from a file" do
      # magic fromfile
      data = GPGME::Data.new(File.open(__FILE__))
      assert_match /magic fromfile/, data.read
    end

    it "creates a data from file descriptor" do
      # magic filedescriptor
      File.open(__FILE__) do |f|
        data = GPGME::Data.new(f.fileno)
        assert_match /magic filedescriptor/, data.read
      end
    end
  end

  describe :read do
    it "allows to read only a length of the object" do
      data = GPGME::Data.new("wadus")
      assert_equal "wad", data.read(3)
    end

    it "returns nil if reading 0 length" do
      data = GPGME::Data.new("wadus")
      assert_nil data.read(0)
    end

    it "returns the full thing if reading without parameter" do
      data = GPGME::Data.new("wadus")
      assert_equal "wadus", data.read
    end
  end

  ##
  # We consider seek tested by these ones, since we have to seek(0) before
  # reading.
  describe :write do
    it "writes data to it" do
      data = GPGME::Data.new
      data.write("wadus")
      data.seek(0)
      assert_equal "wadus", data.read
    end

    it "writes data to it, specifying the length of the things to write" do
      data = GPGME::Data.new
      data.write("wadus", 5)
      data.seek(0)
      assert_equal "wadus", data.read
    end

    it "writes only a limited part if specified a small number" do
      data = GPGME::Data.new
      data.write("wadus", 3)
      data.seek(0)
      assert_equal "wad", data.read
    end

    # TODO: test doesn't pass, I believe there might be a security issue here,
    # random crap is written to the buffer if a longer size is passed.
    #
    # it "writes only the full data passed even if the length is bigger" do
    #   data = GPGME::Data.new
    #   data.write("wadus", 100)
    #   data.seek(0)
    #   assert_equal "wadus", data.read
    # end
  end

  describe :encoding do
    it "has encoding 0 by default (DATA_ENCODING_NONE)" do
      data = GPGME::Data.new("wadus")
      assert_equal GPGME::DATA_ENCODING_NONE, data.encoding
    end

    it "can set encodings" do
      data = GPGME::Data.new("wadus")
      [ GPGME::DATA_ENCODING_ARMOR, GPGME::DATA_ENCODING_BASE64,
        GPGME::DATA_ENCODING_BINARY,GPGME::DATA_ENCODING_NONE ].each do |encoding|
        data.encoding = encoding
        assert_equal encoding, data.encoding
      end
    end

    it "breaks if not set a proper encoding value" do
      data = GPGME::Data.new("wadus")
      assert_raises GPGME::Error::InvalidValue do
        data.encoding = 64
      end
    end
  end

  describe :file_name do
    it "has no name by default" do
      data = GPGME::Data.new("wadus")
      assert_nil data.file_name
    end

    it "can set file_name" do
      data = GPGME::Data.new("wadus")
      [ "foo.bar", nil ].each do |file_name|
        data.file_name = file_name
        assert_equal file_name, data.file_name
      end
    end
  end

  describe :to_s do
    it "returns the entire content of data" do
      data = GPGME::Data.new("wadus")
      data.read
      old_pos = data.seek(0, IO::SEEK_CUR)
      assert_equal "wadus", data.to_s
      new_pos = data.seek(0, IO::SEEK_CUR)
      assert_equal old_pos, new_pos
    end
  end
end