Skip to content

Commit

Permalink
Merge pull request #433 from mchf/bnc992821-internal-error-utf8
Browse files Browse the repository at this point in the history
Handle errors related to non utf-8 characters in /etc/sysctl.conf more carefully
  • Loading branch information
mchf committed Sep 14, 2016
2 parents 7239593 + e28e809 commit 5ecde12
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 45 deletions.
58 changes: 17 additions & 41 deletions src/modules/Lan.rb
Expand Up @@ -214,51 +214,27 @@ def isAnyInterfaceDown
down
end

# Checks local configuration if IPv6 is allowed
#
# return [Boolean] true when IPv6 is enabled in the system
def readIPv6
@ipv6 = true
ipv6 = true

methods =
# "module" : $[
# "filelist" : ["ipv6", "50-ipv6.conf"],
# "filepath" : "/etc/modprobe.d/",
# "regexp" : "^[[:space:]]*(install ipv6 /bin/true)"
# ]
{
"builtin" => {
"filelist" => ["sysctl.conf"],
"filepath" => "/etc/",
"regexp" => "^[[:space:]]*(net.ipv6.conf.all.disable_ipv6)[[:space:]]*=[[:space:]]*1"
}
}
sysctl_path = "/etc/sysctl.conf"
ipv6_regexp = /^[[:space:]]*(net.ipv6.conf.all.disable_ipv6)[[:space:]]*=[[:space:]]*1/

Builtins.foreach(methods) do |which, method|
filelist = Ops.get_list(method, "filelist", [])
filepath = Ops.get_string(method, "filepath", "")
regexp = Ops.get_string(method, "regexp", "")
Builtins.foreach(filelist) do |file|
filename = Builtins.sformat("%1/%2", filepath, file)
if FileUtils.Exists(filename)
Builtins.foreach(
Builtins.splitstring(
Convert.to_string(SCR.Read(path(".target.string"), filename)),
"\n"
)
) do |row|
if Ops.greater_than(
Builtins.size(
Builtins.regexptokenize(String.CutBlanks(row), regexp)
),
0
)
Builtins.y2milestone("IPv6 is disabled by '%1' method.", which)
@ipv6 = false
end
end
end
end
# sysctl.conf is kind of "mandatory" config file, use default
if !FileUtils.Exists(sysctl_path)
log.error("readIPv6: #{sysctl_path} is missing")
return true
end

nil
lines = (SCR.Read(path(".target.string"), sysctl_path) || []).split("\n")
ipv6 = false if lines.any? { |row| row =~ ipv6_regexp }

log.info("readIPv6: IPv6 is #{ipv6 ? "enabled" : "disabled"}")

ipv6
end

# Read all network settings from the SCR
Expand Down Expand Up @@ -374,7 +350,7 @@ def Read(cache)
ProgressNextStage(_("Reading network configuration...")) if @gui
NetworkConfig.Read

readIPv6
@ipv6 = readIPv6

Builtins.sleep(sl)

Expand Down
31 changes: 28 additions & 3 deletions test/SCRStub.rb
Expand Up @@ -50,12 +50,37 @@ def written_value_for(key)
# Yaml files are stored in the scr_read subdirectory of the data directory
# and named after the yast path (without the leading '.').
#
# @param path_name [String] an SCR path, eg. ".etc.xinetd_conf.services"
#
# @return Object
def stub_scr_read(path_name)
def yaml_stub_scr_read(path_name)
file = File.join(DATA_PATH, "scr_read", path_name[1..-1] + ".yml")
info = YAML.load_file(file)
path = Yast::Path.new(path_name)
allow(Yast::SCR).to receive(:Read).with(path).and_return info
mock_path(path_name, info)
end

# Stubs calls to SCR.Read. Returns file content as a list of lines.
#
# Should be equivalent to ycp's SCR::Read(".target.string", path)
#
# @param path_name [String] an absolute filesystem path, eg. "/etc/sysctl.conf"
#
# @return [Array] list of lines
def string_stub_scr_read(path_name)
file = File.join(DATA_PATH, "scr_read", path_name[1..-1])
info = File.read(file)
mock_path(".target.string", info, path_name)
end

# Stubs SCR.Read for given target file using given content
def mock_path(target, content, params = nil)
path = Yast::Path.new(target)

if params
allow(Yast::SCR).to receive(:Read).with(path, params).and_return content
else
allow(Yast::SCR).to receive(:Read).with(path).and_return content
end
end
end

Expand Down
5 changes: 5 additions & 0 deletions test/data/scr_read/etc/sysctl.conf
@@ -0,0 +1,5 @@
kernel.sysrq = 0
net.ipv4.ip_forward = 0
net.ipv4.tcp_syncookies = 1
net.ipv6.conf.all.forwarding = 0
net.ipv6.conf.all.disable_ipv6 = 1
15 changes: 15 additions & 0 deletions test/lan_test.rb
Expand Up @@ -210,3 +210,18 @@ def expect_modification_succeedes(modname, method)
expect(Yast::Lan.Modified).to be false
end
end

describe "LanClass#readIPv6" do
it "reads IPv6 setup from /etc/sysctl.conf" do
allow(Yast::FileUtils).to receive(:Exists).and_return(true)
string_stub_scr_read("/etc/sysctl.conf")

expect(Yast::Lan.readIPv6).to be false
end

it "returns true when /etc/sysctl.conf is missing" do
allow(Yast::FileUtils).to receive(:Exists).and_return(false)

expect(Yast::Lan.readIPv6).to be true
end
end
2 changes: 1 addition & 1 deletion test/remote_test.rb
Expand Up @@ -150,7 +150,7 @@ module Yast
describe ".configure_display_manager" do
before do
stub_scr_write
stub_scr_read(".etc.xinetd_conf.services")
yaml_stub_scr_read(".etc.xinetd_conf.services")
allow(Package).to receive(:Installed).and_return true
end

Expand Down

0 comments on commit 5ecde12

Please sign in to comment.