<commit_before>local utils = require "lime.utils"
local test_utils = require "tests.utils"
local config = require 'lime.config'

local test_file_name = "packages/ubus-lime-utils/files/usr/libexec/rpcd/lime-utils-admin"
local ubus_lime_utils = test_utils.load_lua_file_as_function(test_file_name)

local rpcd_call = test_utils.rpcd_call
local uci

local openwrt_release = [[DISTRIB_ID='LiMe'
DISTRIB_RELEASE='96dcfa439d2757067bc73812b218aa689be9ea57'
DISTRIB_REVISION='96dcfa4'
DISTRIB_TARGET='ar71xx/generic'
DISTRIB_ARCH='mips_24kc'
DISTRIB_DESCRIPTION='LiMe 96dcfa439d27570...'
DISTRIB_TAINTS='no-all busybox'
]]

describe('ubus-lime-utils-admin tests #ubuslimeutilsadmin', function()
    it('test list methods', function()
        local response  = rpcd_call(ubus_lime_utils, {'list'})
        assert.is.equal(nil, response.set_root_password.no_params)
    end)

    it('test set_root_password', function()
        uci:set('lime-community', 'system', 'lime')
        stub(utils, "set_password", function (user, pass) return pass end)

        local response  = rpcd_call(ubus_lime_utils, {'call', 'set_root_password'},
                                    '{"password": "<PASSWORD>"}')

        assert.is.equal("ok", response.status)
        assert.stub(utils.set_password).was.called_with('root', 'foo')
        assert.is.equal("SET_SECRET", uci:get("lime-community", 'system', 'root_password_policy'))
    end)

    it('test is_upgrade_confirm_supported in unsupported board', function()
        stub(os, "execute", function() return 1 end)
        local response  = rpcd_call(ubus_lime_utils, {'call', 'is_upgrade_confirm_supported'}, '')
        assert.is.equal("ok", response.status)
        assert.is_false(response.supported)
        os.execute:revert()
    end)

    it('test is_upgrade_confirm_supported in supported board', function()
        stub(os, "execute", function() return 0 end)
        local response  = rpcd_call(ubus_lime_utils, {'call', 'is_upgrade_confirm_supported'}, '')
        assert.is.equal("ok", response.status)
        assert.is_true(response.supported)
        os.execute:revert()
    end)

    it('test firmware_verify inexistent file', function()
        local response  = rpcd_call(ubus_lime_utils, {'call', 'firmware_verify'},
                                    '{"fw_path": "/foo"}')
        assert.is.equal("error", response.status)
        assert.is.equal("Firmware file not found", response.message)
    end)

    it('test firmware_verify with confirm method', function()
        stub(os, "execute", function() return 0 end)
        stub(utils, "file_exists", function() return true end)
        local response  = rpcd_call(ubus_lime_utils, {'call', 'firmware_verify'},
                                    '{"fw_path": "/foo"}')
        assert.is.equal("ok", response.status)
        os.execute:revert()
        utils.file_exists:revert()
    end)

    it('test firmware_upgrade without new metadata', function()

        stub(os, "execute", function() return 0 end)
        stub(utils, "file_exists", function() return true end)
        stub(utils, "read_file", function() return openwrt_release end)
        local response  = rpcd_call(ubus_lime_utils, {'call', 'firmware_upgrade'},
                                    '{"fw_path": "/foo"}')
        assert.is.equal("ok", response.status)
        assert.is.equal("LiMe 96dcfa439d27570...", response.metadata.old_release_description)
        assert.is_false(response.metadata.config_preserved)
    end)

    it('test firmware_upgrade with metadata', function()

        stub(os, "execute", function() return 0 end)
        stub(utils, "file_exists", function() return true end)
        stub(utils, "read_file", function() return openwrt_release end)
        local response  = rpcd_call(ubus_lime_utils, {'call', 'firmware_upgrade'},
                                   '{"fw_path": "/foo", "preserve_config": true, "metadata": {"foo": 1}}')
        assert.is.equal("ok", response.status)
        assert.is.equal("LiMe 96dcfa439d27570...", response.metadata.old_release_description)
        assert.is_true(response.metadata.config_preserved)
        assert.is.equal(1, response.metadata.foo)
    end)

    it('test last_upgrade_metadata', function()
        stub(utils, "file_exists", function() return true end)
        stub(utils, "read_file", function() return '{"foo": "bar"}' end)
        local response  = rpcd_call(ubus_lime_utils, {'call', 'last_upgrade_metadata'}, '')
        assert.is.equal("ok", response.status)
        assert.is.equal("bar", response.metadata.foo)
    end)

    it('test last_upgrade_metadata but no metadata is available', function()
        local response  = rpcd_call(ubus_lime_utils, {'call', 'last_upgrade_metadata'}, '')
        assert.is.equal("error", response.status)
        assert.is.equal("No metadata available", response.message)
    end)

    it('test safe_upgrade_confirm_remaining_s', function()
        stub(utils, "unsafe_shell", function() return "253" end)
        local response  = rpcd_call(ubus_lime_utils, {'call', 'safe_upgrade_confirm_remaining_s'}, '')
        assert.is.equal("ok", response.status)
        assert.is.equal(253, response.remaining_s)
    end)

    it('test safe_upgrade_confirm_remaining_s when not in a confirmable state', function()
        stub(utils, "unsafe_shell", function() return "-1" end)
        local response  = rpcd_call(ubus_lime_utils, {'call', 'safe_upgrade_confirm_remaining_s'}, '')
        assert.is.equal("ok", response.status)
        assert.is.equal(-1, response.remaining_s)
    end)

    it('test firmware_confirm', function()
        stub(os, "execute", function() return 0 end)
        local response  = rpcd_call(ubus_lime_utils, {'call', 'firmware_confirm'}, '')
        assert.is.equal("ok", response.status)
        os.execute:revert()
    end)

    before_each('', function()
        uci = test_utils.setup_test_uci()
    end)

    after_each('', function()
        test_utils.teardown_test_uci(uci)
    end)
end)<commit_msg>ubus-lime-admin: simplify testing stub reverting<commit_after>local utils = require "lime.utils"
local test_utils = require "tests.utils"
local config = require 'lime.config'

local test_file_name = "packages/ubus-lime-utils/files/usr/libexec/rpcd/lime-utils-admin"
local ubus_lime_utils = test_utils.load_lua_file_as_function(test_file_name)

local rpcd_call = test_utils.rpcd_call
local uci

local openwrt_release = [[DISTRIB_ID='LiMe'
DISTRIB_RELEASE='96dcfa439d2757067bc73812b218aa689be9ea57'
DISTRIB_REVISION='96dcfa4'
DISTRIB_TARGET='ar71xx/generic'
DISTRIB_ARCH='mips_24kc'
DISTRIB_DESCRIPTION='LiMe 96dcfa439d27570...'
DISTRIB_TAINTS='no-all busybox'
]]

describe('ubus-lime-utils-admin tests #ubuslimeutilsadmin', function()
    local snapshot -- to revert luassert stubs and spies
    it('test list methods', function()
        local response  = rpcd_call(ubus_lime_utils, {'list'})
        assert.is.equal(nil, response.set_root_password.no_params)
    end)

    it('test set_root_password', function()
        uci:set('lime-community', 'system', 'lime')
        stub(utils, "set_password", function (user, pass) return pass end)

        local response  = rpcd_call(ubus_lime_utils, {'call', 'set_root_password'},
                                    '{"password": "<PASSWORD>"}')

        assert.is.equal("ok", response.status)
        assert.stub(utils.set_password).was.called_with('root', 'foo')
        assert.is.equal("SET_SECRET", uci:get("lime-community", 'system', 'root_password_policy'))
    end)

    it('test is_upgrade_confirm_supported in unsupported board', function()
        stub(os, "execute", function() return 1 end)
        local response  = rpcd_call(ubus_lime_utils, {'call', 'is_upgrade_confirm_supported'}, '')
        assert.is.equal("ok", response.status)
        assert.is_false(response.supported)
    end)

    it('test is_upgrade_confirm_supported in supported board', function()
        stub(os, "execute", function() return 0 end)
        local response  = rpcd_call(ubus_lime_utils, {'call', 'is_upgrade_confirm_supported'}, '')
        assert.is.equal("ok", response.status)
        assert.is_true(response.supported)
    end)

    it('test firmware_verify inexistent file', function()
        local response  = rpcd_call(ubus_lime_utils, {'call', 'firmware_verify'},
                                    '{"fw_path": "/foo"}')
        assert.is.equal("error", response.status)
        assert.is.equal("Firmware file not found", response.message)
    end)

    it('test firmware_verify with confirm method', function()
        stub(os, "execute", function() return 0 end)
        stub(utils, "file_exists", function() return true end)
        local response  = rpcd_call(ubus_lime_utils, {'call', 'firmware_verify'},
                                    '{"fw_path": "/foo"}')
        assert.is.equal("ok", response.status)
    end)

    it('test firmware_upgrade without new metadata', function()

        stub(os, "execute", function() return 0 end)
        stub(utils, "file_exists", function() return true end)
        stub(utils, "read_file", function() return openwrt_release end)
        local response  = rpcd_call(ubus_lime_utils, {'call', 'firmware_upgrade'},
                                    '{"fw_path": "/foo"}')
        assert.is.equal("ok", response.status)
        assert.is.equal("LiMe 96dcfa439d27570...", response.metadata.old_release_description)
        assert.is_false(response.metadata.config_preserved)
    end)

    it('test firmware_upgrade with metadata', function()

        stub(os, "execute", function() return 0 end)
        stub(utils, "file_exists", function() return true end)
        stub(utils, "read_file", function() return openwrt_release end)
        local response  = rpcd_call(ubus_lime_utils, {'call', 'firmware_upgrade'},
                                   '{"fw_path": "/foo", "preserve_config": true, "metadata": {"foo": 1}}')
        assert.is.equal("ok", response.status)
        assert.is.equal("LiMe 96dcfa439d27570...", response.metadata.old_release_description)
        assert.is_true(response.metadata.config_preserved)
        assert.is.equal(1, response.metadata.foo)
    end)

    it('test last_upgrade_metadata', function()
        stub(utils, "file_exists", function() return true end)
        stub(utils, "read_file", function() return '{"foo": "bar"}' end)
        local response  = rpcd_call(ubus_lime_utils, {'call', 'last_upgrade_metadata'}, '')
        assert.is.equal("ok", response.status)
        assert.is.equal("bar", response.metadata.foo)
    end)

    it('test last_upgrade_metadata but no metadata is available', function()
        local response  = rpcd_call(ubus_lime_utils, {'call', 'last_upgrade_metadata'}, '')
        assert.is.equal("error", response.status)
        assert.is.equal("No metadata available", response.message)
    end)

    it('test safe_upgrade_confirm_remaining_s', function()
        stub(utils, "unsafe_shell", function() return "253" end)
        local response  = rpcd_call(ubus_lime_utils, {'call', 'safe_upgrade_confirm_remaining_s'}, '')
        assert.is.equal("ok", response.status)
        assert.is.equal(253, response.remaining_s)
    end)

    it('test safe_upgrade_confirm_remaining_s when not in a confirmable state', function()
        stub(utils, "unsafe_shell", function() return "-1" end)
        local response  = rpcd_call(ubus_lime_utils, {'call', 'safe_upgrade_confirm_remaining_s'}, '')
        assert.is.equal("ok", response.status)
        assert.is.equal(-1, response.remaining_s)
    end)

    it('test firmware_confirm', function()
        stub(os, "execute", function() return 0 end)
        local response  = rpcd_call(ubus_lime_utils, {'call', 'firmware_confirm'}, '')
        assert.is.equal("ok", response.status)
    end)

    before_each('', function()
        snapshot = assert:snapshot()
        uci = test_utils.setup_test_uci()
    end)

    after_each('', function()
        snapshot:revert()
        test_utils.teardown_test_uci(uci)
    end)
end)
{'fraction_non_alphanumeric': 0.13447189520809152, 'fraction_numerical': 0.013098988559111258, 'mean_word_length': 3.3993435448577682, 'pattern_counts': {'":': 18, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6740272', 'n_tokens_mistral': 3975, 'n_tokens_neox': 3789, 'n_words': 755}