From 95242bf9c27683800135b335bc6c43b1db62e0d1 Mon Sep 17 00:00:00 2001 From: Dominik Richter Date: Mon, 26 Oct 2015 15:50:57 +0100 Subject: [PATCH] add content parser tests --- lib/utils/parser.rb | 32 ++++++++++++++------------ test/unit/utils/content_parser_test.rb | 30 ++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 15 deletions(-) create mode 100644 test/unit/utils/content_parser_test.rb diff --git a/lib/utils/parser.rb b/lib/utils/parser.rb index 8935e4d99..b6914b66f 100644 --- a/lib/utils/parser.rb +++ b/lib/utils/parser.rb @@ -5,25 +5,11 @@ module ContentParser # parse etc/passwd file def parse_passwd(content) - content.split("\n").map do |line| + content.to_s.split("\n").map do |line| parse_passwd_line(line) end end - # parse a etc/passwd line - def parse_passwd_line(line) - x = line.split(':') - { - 'name' => x.at(0), - 'password' => x.at(1), - 'uid' => x.at(2), - 'gid' => x.at(3), - 'desc' => x.at(4), - 'home' => x.at(5), - 'shell' => x.at(6), - } - end - def parse_comment_line(raw, opts) idx_nl = raw.index("\n") idx_comment = raw.index(opts[:comment_char]) @@ -46,4 +32,20 @@ module ContentParser end [line, idx_nl] end + + private + + # parse a etc/passwd line + def parse_passwd_line(line) + x = line.split(':') + { + 'name' => x.at(0), + 'password' => x.at(1), + 'uid' => x.at(2), + 'gid' => x.at(3), + 'desc' => x.at(4), + 'home' => x.at(5), + 'shell' => x.at(6), + } + end end diff --git a/test/unit/utils/content_parser_test.rb b/test/unit/utils/content_parser_test.rb new file mode 100644 index 000000000..61c6a021d --- /dev/null +++ b/test/unit/utils/content_parser_test.rb @@ -0,0 +1,30 @@ +# encoding: utf-8 +# author: Dominik Richter +# author: Christoph Hartmann + +describe ContentParser do + let (:parser) { Class.new() { include ContentParser }.new } + + describe '#parse_passwd' do + it 'parses nil content' do + parser.parse_passwd(nil).must_equal([]) + end + + it 'parses an empty passwd line' do + parser.parse_passwd('').must_equal([]) + end + + it 'parses a valid passwd line' do + info = [{ + "name"=>"root", + "password"=>"x", + "uid"=>"0", + "gid"=>"0", + "desc"=>"root", + "home"=>"/root", + "shell"=>"/bin/sh" + }] + parser.parse_passwd('root:x:0:0:root:/root:/bin/sh').must_equal(info) + end + end +end