Skip to content

Port test code to unit test project. #331

@daisukenishino2

Description

@daisukenishino2

Requirement

Port test code
https://github.com/OpenTouryoProject/OpenTouryo/blob/01-00/root/programs/C%23/Samples/WebApp_sample/ProjectX_sample/Aspx/testPublic/testScreen.aspx.cs

to unit test project.
https://github.com/OpenTouryoProject/OpenTouryo/tree/develop/root/programs/CS/Frameworks/Tests/TestCode

using System;
using System.IO;
using System.Text;
using System.Diagnostics;
using System.Security.Principal;

using System.Collections.Generic;
using System.Collections.ObjectModel;

using Touryo.Infrastructure.Business.Common;
using Touryo.Infrastructure.Business.Business;
using Touryo.Infrastructure.Business.Str;
using Touryo.Infrastructure.Business.Util;

using Touryo.Infrastructure.Framework.Common;
using Touryo.Infrastructure.Framework.Util;

using Touryo.Infrastructure.Public.Db;
using Touryo.Infrastructure.Public.IO;
using Touryo.Infrastructure.Public.Log;
using Touryo.Infrastructure.Public.Str;
using Touryo.Infrastructure.Public.Security;
using Touryo.Infrastructure.Public.Util;

namespace WebForms_Sample.Aspx.TestPublic
{
    /// <summary>共通部品テスト画面</summary>
    public partial class testScreen : System.Web.UI.Page
    {
        #region 初期化処理

        /// <summary>初期化処理</summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.txtFilePath1.Text = GetConfigParameter.GetConfigValue("TestFilePath")
                    + @"\testWrite.txt";

                this.txtFilePath2.Text = GetConfigParameter.GetConfigValue("TestFilePath")
                    + @"\testRead.txt";

                this.txtStrIn.Text = "az、az、AZ、AZ、09、09、@%&、@%&、アア、あ亜、  ";

                //---

                //コンピュータ上に存在するすべてのタイムゾーンをComboBoxに表示
                Dictionary<string, string> dic_tzs = new Dictionary<string, string>();
                ReadOnlyCollection<TimeZoneInfo> roc_tzs = TimeZoneInfo.GetSystemTimeZones();
                foreach (TimeZoneInfo tz in roc_tzs)
                {
                    dic_tzs.Add(tz.DisplayName, tz.Id);
                }

                this.ddlTimeZone.DataSource = dic_tzs;
                this.ddlTimeZone.DataTextField = "Key";
                this.ddlTimeZone.DataValueField = "Value";
                this.ddlTimeZone.DataBind();
            }

            this.MaintainScrollPositionOnPostBack = true;
        }

        #endregion

        #region ログ出力のテスト
        // 移行完了
        #endregion

        #region 性能測定のテスト
        // 移行しない
        #endregion

        #region ファイルIO部品のテスト
        // 内部的に利用されている
        #endregion

        #region 共有情報取得部品
        // 移行完了
        #endregion

        #region Message取得部品
        // 移行完了
        #endregion

        #region トランザクション制御機能
        // deprecated 
        #endregion

        #region JIS系

        #region JIS2004
        // 移行完了
        #endregion

        #region JISX0208-1983

        /// <summary>JISX0208-1983チェック</summary>
        protected void btnCheckJISX0208_1983_Click(object sender, EventArgs e)
        {
            int index;
            string ch;
            if (JISX0208_1983Checker.IsJISX0208_1983(this.txtCheckJISX0208_1983.Text, out index, out ch))
            {
                this.lblCheckJISX0208_1983.Text = "成功!";
            }
            else
            {
                this.lblCheckJISX0208_1983.Text = "失敗 index:" + index + ", ch:" + ch;
            }
        }

        #endregion

        #endregion

        #region ローカル⇔UTC対応

        /// <summary>ローカル→UTC対応</summary>
        protected void btnLocalToUtc_Click(object sender, EventArgs e)
        {
            //DateTime dt = new DateTime(this.Calendar1.SelectedDate.Ticks, DateTimeKind.Local); // 正
            DateTime dt = new DateTime(this.Calendar1.SelectedDate.Ticks, DateTimeKind.Unspecified); // 略
            //DateTime dt = new DateTime(this.Calendar1.SelectedDate.Ticks, DateTimeKind.Utc); // 不正

            if (this.cbxTimeZone.Checked)
            {
                dt = GMTMaster.ConvertLocalTimeToUtcTime35(dt, TimeZoneInfo.FindSystemTimeZoneById(this.ddlTimeZone.SelectedItem.Value));
            }
            else
            {
                dt = GMTMaster.ConvertLocalTimeToUtcTime35(dt, TimeZoneInfo.Local);
            }
            this.lblDateString.Text = dt.ToString("yyyy/MM/dd HH:mm:ss.fff");
        }

        /// <summary>UTC→ローカル対応</summary>
        protected void btnUtcToLocal_Click(object sender, EventArgs e)
        {
            //DateTime dt = new DateTime(this.Calendar1.SelectedDate.Ticks, DateTimeKind.Utc); // 正
            DateTime dt = new DateTime(this.Calendar1.SelectedDate.Ticks, DateTimeKind.Unspecified); // 略
            //DateTime dt = new DateTime(this.Calendar1.SelectedDate.Ticks, DateTimeKind.Local); // 不正

            if (this.cbxTimeZone.Checked)
            {
                dt = GMTMaster.ConvertUtcTimeToLocalTime35(dt, TimeZoneInfo.FindSystemTimeZoneById(this.ddlTimeZone.SelectedItem.Value));
            }
            else
            {
                dt = GMTMaster.ConvertUtcTimeToLocalTime35(dt, TimeZoneInfo.Local);
            }
            this.lblDateString.Text = dt.ToString("yyyy/MM/dd HH:mm:ss.fff");
        }

        #endregion

        #region 文字列処理
        #region CustomEncode
        // 移行完了
        #endregion

        #region StringConverter
        // 移行完了
        #endregion

        #region StringChecker
        // 移行完了
        #endregion

        #region FormatConverter

        #region 和暦・西暦
        // 移行完了
        #endregion

        #region 桁区切り
        // 移行完了
        #endregion

        #region サプレス
        // 移行完了
        #endregion

        #endregion

        #region FormatChecker

        #region 郵便
        // 移行完了
        #endregion

        #region 電話
        // 移行完了
        #endregion

        #endregion

        #endregion

        #region その他

        /// <summary>Sessionサイズ</summary>
        protected void btnSessionSize_Click(object sender, EventArgs e)
        {
            this.lblElse.Text = MyCmnFunction.CalculateSessionSize().ToString() + "バイト";
        }

        /// <summary>Sessionサイズ(KB)</summary>
        protected void btnSessionSizeKB_Click(object sender, EventArgs e)
        {
            this.lblElse.Text = MyCmnFunction.CalculateSessionSizeKB().ToString() + "Kバイト";
        }

        /// <summary>Sessionサイズ(MB)</summary>
        protected void btnSessionSizeMB_Click(object sender, EventArgs e)
        {
            this.lblElse.Text = MyCmnFunction.CalculateSessionSizeMB().ToString() + "Mバイト";
        }

        /// <summary>偽装のテスト</summary>
        protected void btnImpersonation_Click(object sender, EventArgs e)
        {
            // クリア
            this.lblElse.Text = "";

            try
            {
                // 偽装前の実行アカウント(ASP.NET偽装はある)

                //// 偽装起動のテスト → OK 正しく動作する。
                //this.testCreateProcessAsImpersonationUser();

                // 存在チェック
                Debug.WriteLine(System.Security.Principal.WindowsIdentity.GetCurrent().Name);
                this.lblElse.Text
                    += string.Format("偽装前(実行アカウント「{0}」):", WindowsIdentity.GetCurrent().Name)
                    + ResourceLoader.LoadAsString(@"c:\test.txt", Encoding.GetEncoding(CustomEncode.UTF_8));
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            //--

            bool ret;
            //string temp;

            IdentityImpersonation ii = null;

            try
            {
                // コードの特定部分を実行するときのみ、任意のユーザを偽装する。

                // 偽装して
                ii = new IdentityImpersonation();
                ret = ii.ImpersonateValidUser("x", "", "x");

                //// 偽装起動のテスト → OK 正しく動作する。
                //this.testCreateProcessAsImpersonationUser();

                // 存在チェック
                this.lblElse.Text
                    += string.Format("、偽装後(任意のユーザ「{0}」を偽装):", WindowsIdentity.GetCurrent().Name)
                    + ResourceLoader.LoadAsString(@"c:\test.txt", Encoding.GetEncoding(CustomEncode.UTF_8));
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            finally
            {
                // 偽装解除
                ret = ii.UndoImpersonation();
            }

            //--

            if (User.Identity is WindowsIdentity)
            {
                try
                {
                    // コードの特定部分を実行するときのみ、認証中のユーザ (User.Identity) を偽装する。
                    // このため、Windows認証で認証する必要がある。

                    // 偽装して
                    ii = new IdentityImpersonation();
                    ret = ii.ImpersonateWinIdUser((WindowsIdentity)User.Identity);

                    //// 偽装起動のテスト → OK 正しく動作する。
                    //this.testCreateProcessAsImpersonationUser();

                    // 存在チェック
                    this.lblElse.Text
                        += string.Format("、偽装後(認証中のユーザ「{0}」を偽装):", WindowsIdentity.GetCurrent().Name)
                        + ResourceLoader.LoadAsString(@"c:\test.txt", Encoding.GetEncoding(CustomEncode.UTF_8));
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
                finally
                {
                    // 偽装解除
                    ret = ii.UndoImpersonation();
                }
            }

            //--

            try
            {
                // 偽装解除後、存在チェック
                this.lblElse.Text
                    += string.Format("、偽装解除後「{0}」:", WindowsIdentity.GetCurrent().Name)
                    + ResourceLoader.LoadAsString(@"c:\test.txt", Encoding.GetEncoding(CustomEncode.UTF_8));
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }

        /// <summary>
        /// 偽装ユーザでノートパッドを起動する
        /// (タスクマネージャで実行アカウントをチェックする)。
        /// 注意:UIは表示されないので、タスクマネージャで確認
        /// </summary>
        private void testCreateProcessAsImpersonationUser()
        {
            bool ret;
            //string temp;

            try
            {
                string cmdNotepad = Environment.GetEnvironmentVariable(
                    "SystemRoot", EnvironmentVariableTarget.Process) + @"\system32\notepad.exe";

                // 通常起動
                Process.Start(cmdNotepad);

                // 偽装起動
                // ・ASP.NET偽装や、ImpersonateValidUserの偽装レベルはSecurityImpersonationなので、これに合わせる必要がある。
                // ・独自偽装の、偽装レベルは、SecurityImpersonation、SecurityDelegationどちらでも良いが、双方を合わせる必要がある。
                // ・実行アカウントには、「プロセス レベル トークンの置き換え」セキュリティ・ポリシー設定が必要になる。
                ret = IdentityImpersonation.CreateProcessAsImpersonationUser(cmdNotepad, "");
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }

        /// <summary>その他、なんでも(カバレージ上げる用)</summary>
        protected void btnElse_Click(object sender, EventArgs e)
        {
            // TextBox9.Text = (string)BinarySerialize.BytesToObject(BinarySerialize.ObjectToBytes(TextBox8.Text));
            // BinarySerialize.ObjectToBytes(null); // 引数例外
            // BinarySerialize.BytesToObject(null); // 引数例外
        }

        #endregion
    }
    
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions