-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathitem.rb
More file actions
120 lines (102 loc) · 3.79 KB
/
item.rb
File metadata and controls
120 lines (102 loc) · 3.79 KB
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
# frozen_string_literal: true
require 'openscap/exceptions'
require 'openscap/text'
require 'openscap/xccdf/group'
require 'openscap/xccdf/rule'
require 'openscap/xccdf/reference'
module OpenSCAP
module Xccdf
class Item
def self.build(t)
raise OpenSCAP::OpenSCAPError, "Cannot initialize #{self.class.name} with #{t}" \
unless t.is_a?(FFI::Pointer)
# This is Abstract base class that enables you to build its child
case OpenSCAP.xccdf_item_get_type t
when :group
OpenSCAP::Xccdf::Group.new t
when :rule
OpenSCAP::Xccdf::Rule.new t
else
raise OpenSCAP::OpenSCAPError, "Unknown #{self.class.name} type: #{OpenSCAP.xccdf_item_get_type t}"
end
end
def initialize(t)
if instance_of?(OpenSCAP::Xccdf::Item)
raise OpenSCAP::OpenSCAPError, "Cannot initialize #{self.class.name} abstract base class."
end
@raw = t
end
def id
OpenSCAP.xccdf_item_get_id @raw
end
def title(prefered_lang = nil)
textlist = OpenSCAP::TextList.new(OpenSCAP.xccdf_item_get_title(@raw))
title = textlist.plaintext(prefered_lang)
textlist.destroy
title
end
def description(prefered_lang = nil)
textlist = OpenSCAP::TextList.new(OpenSCAP.xccdf_item_get_description(@raw))
description = textlist.plaintext(prefered_lang)
textlist.destroy
description
end
def rationale(prefered_lang = nil)
textlist = OpenSCAP::TextList.new(OpenSCAP.xccdf_item_get_rationale(@raw))
rationale = textlist.plaintext(prefered_lang)
textlist.destroy
rationale
end
def references
refs = []
refs_it = OpenSCAP.xccdf_item_get_references(@raw)
while OpenSCAP.oscap_reference_iterator_has_more refs_it
ref = OpenSCAP::Xccdf::Reference.new(OpenSCAP.oscap_reference_iterator_next(refs_it))
refs << ref
end
OpenSCAP.oscap_reference_iterator_free refs_it
refs
end
def sub_items
@sub_items ||= sub_items_init
end
def destroy
OpenSCAP.xccdf_item_free @raw
@raw = nil
end
private
def sub_items_init
collect = {}
items_it = OpenSCAP.xccdf_item_get_content @raw
while OpenSCAP.xccdf_item_iterator_has_more items_it
item_p = OpenSCAP.xccdf_item_iterator_next items_it
item = OpenSCAP::Xccdf::Item.build item_p
collect.merge! item.sub_items
collect[item.id] = item
end
OpenSCAP.xccdf_item_iterator_free items_it
collect
end
end
end
attach_function :xccdf_item_get_id, [:pointer], :string
attach_function :xccdf_item_get_content, [:pointer], :pointer
attach_function :xccdf_item_free, [:pointer], :void
attach_function :xccdf_item_get_title, [:pointer], :pointer
attach_function :xccdf_item_get_description, [:pointer], :pointer
attach_function :xccdf_item_get_rationale, [:pointer], :pointer
XccdfItemType = enum(:benchmark, 0x0100,
:profile, 0x0200,
:result, 0x0400,
:rule, 0x1000,
:group, 0x2000,
:value, 0x4000)
attach_function :xccdf_item_get_type, [:pointer], XccdfItemType
attach_function :xccdf_item_iterator_has_more, [:pointer], :bool
attach_function :xccdf_item_iterator_next, [:pointer], :pointer
attach_function :xccdf_item_iterator_free, [:pointer], :void
attach_function :xccdf_item_get_references, [:pointer], :pointer
attach_function :oscap_reference_iterator_has_more, [:pointer], :bool
attach_function :oscap_reference_iterator_next, [:pointer], :pointer
attach_function :oscap_reference_iterator_free, [:pointer], :void
end