--[[----------------------------------------------------------------------------

KSKeychainServiceTest.lua

--------------------------------------------------------------------------------

ADOBE CONFIDENTIAL
------------------
Copyright 2011 Adobe Systems Incorporated
All Rights Reserved.

NOTICE: All information contained herein is, and remain the property of
Adobe Systems Incorporated and its suppliers, if any. The intellectual and
technical concepts contained herein are proprietary to Adobe Systems
Incorporated and its suppliers and may be covered by U.S. and Foreign
Patents, patents in process, and are protected by trade secret or copyright
law.  Dissemination of this information or reproduction of this material is
strictly forbidden unless prior written permission is obtained from
Adobe Systems Incorporated.

--------------------------------------------------------------------------------

This namespace provides secure storage for passwords, access tokens, and similar
information that is especially important to the application. It may be accessed
from any thread.

This API is a minimalist wrapper over Mac and iOS Keychain Services.

Note: Due to the keychain becoming "locked" on MacOS, in the Mac unit test runner,
the underlying implementation is currently configured to read and write to an
in-memory cache, instead of talking to the native service. We have been unable
to find a way to consistently keep the keychain unlocked on automation systems,
which causes an endless birage of meaningless failures.

------------------------------------------------------------------------------]]

	-- WF.core.os
local KSKeychainService = require 'KSKeychainService'

	-- WF.core.self_test
local WFAsyncTestCase = require 'WFAsyncTestCase'
local WFRxTestCase = require 'WFRxTestCase'


--============================================================================--

local KSKeychainServiceTest = {}

--------------------------------------------------------------------------------

function KSKeychainServiceTest.tearDownClass( coveragePath )

	if coveragePath then

		local WFWorkerThread = require 'WFWorkerThread'
		local KSKeyChainServiceEP = WFWorkerThread( 'KSKeychainServiceImp.lua' )

		KSKeyChainServiceEP:requestP( 'DEBUG_dumpCoverageP', coveragePath )
			:runUntilDone()

		KSKeyChainServiceEP:close()

	end

end

--------------------------------------------------------------------------------

function KSKeychainServiceTest.testDeleteReadWrite( test )

	WFAsyncTestCase( test, 10 )

		:waitFor( function()

			return KSKeychainService.deleteP( "test_key" )

		end )

		:waitFor( function()

			return KSKeychainService.readP( "test_key" )

		end )
		:continueWhenValueMatches( 'data', nil )

		:waitFor( function()

			return KSKeychainService.writeP( "test_key", "foo" )

		end )

		:waitFor( function()

			return KSKeychainService.readP( "test_key" )

		end )
		:continueWhenValueMatches( 'data', "foo" )

		:waitFor( function()

			return KSKeychainService.deleteP( "test_key" )

		end )

		:waitFor( function()

			return KSKeychainService.readP( "test_key" )

		end )
		:continueWhenValueMatches( 'data', nil )

end

--------------------------------------------------------------------------------

function KSKeychainServiceTest.rxTestRxDeleteReadWrite( rxTest )

	rxTest:setTimeLimit( 10 )

	rxTest:expectOnlyCompleted( KSKeychainService.delete( 'test_key' ) )

	rxTest:expectSingleValue( KSKeychainService.read( 'test_key' ), nil )

	rxTest:expectOnlyCompleted( KSKeychainService.write( 'test_key', "foo" ) )

	rxTest:expectSingleValue( KSKeychainService.read( 'test_key' ), "foo" )

	rxTest:expectOnlyCompleted( KSKeychainService.delete( 'test_key' ) )

	rxTest:expectSingleValue( KSKeychainService.read( 'test_key' ), nil )

end

--------------------------------------------------------------------------------

-- FIXME [scouten 2011-07-26]: Should also test various error conditions.

--------------------------------------------------------------------------------

-- FIXME [jandrew 2018-03-28]: This test suite fails on one specific Windows
-- build machine. Disabling for now.

local WFPlatformInfo = require 'WFPlatformInfo'

if WFPlatformInfo.isWindows then
	return {}
else
	return WFRxTestCase.wrapSuite( KSKeychainServiceTest )
end
