OSDN Git Service

GitHub Actionsで使用するactionをアップデート
[opentween/open-tween.git] / OpenTween.Tests / ApiKeyTest.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2022 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
4 //
5 // This file is part of OpenTween.
6 //
7 // This program is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General public License as published by the Free
9 // Software Foundation; either version 3 of the License, or (at your option)
10 // any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General public License
15 // for more details.
16 //
17 // You should have received a copy of the GNU General public License along
18 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
19 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 // Boston, MA 02110-1301, USA.
21
22 using System;
23 using System.Collections.Generic;
24 using System.Linq;
25 using System.Text;
26 using System.Threading.Tasks;
27 using Xunit;
28
29 namespace OpenTween
30 {
31     public class ApiKeyTest
32     {
33         [Fact]
34         public void Encrypt_FormatTest()
35         {
36             var password = "hoge";
37             var plainText = "aaaaaaa";
38             var encrypted = ApiKey.Encrypt(password, plainText);
39             Assert.StartsWith("%e%", encrypted);
40             Assert.Equal(5, encrypted.Split('%').Length);
41         }
42
43         [Fact]
44         public void Encrypt_NonceTest()
45         {
46             // 同じ平文に対する暗号文を繰り返し生成しても出力は毎回変化する
47             var password = "hoge";
48             var plainText = "aaaaaaa";
49             var encrypted1 = ApiKey.Encrypt(password, plainText);
50             var encrypted2 = ApiKey.Encrypt(password, plainText);
51             Assert.NotEqual(encrypted1, encrypted2);
52         }
53
54         [Fact]
55         public void Decrypt_Test()
56         {
57             var password = "password";
58             var encrypted = "%e%m6EH2dECH7HWT9SFE0SK4Q==%mAAWPhPALf48s32s/yQarg==%zoCs8crMqZN6Nfj8ALkl2R3kbD/FORecuepU1LJ3CK0=";
59             var decrypted = ApiKey.Decrypt(password, encrypted);
60             Assert.Equal("hogehoge", decrypted);
61         }
62
63         [Fact]
64         public void Decrypt_PlainTextTest()
65         {
66             // %e% から始まっていない文字列は平文として何もせずに返す
67             var password = "password";
68             var plainText = "plaintext";
69             var decrypted = ApiKey.Decrypt(password, plainText);
70             Assert.Equal("plaintext", decrypted);
71         }
72
73         [Fact]
74         public void Decrypt_InvalidFormatTest()
75         {
76             var password = "password";
77             var encrypted = "%e%INVALID_FORMAT";
78             Assert.Throws<ApiKeyDecryptException>(() => ApiKey.Decrypt(password, encrypted));
79         }
80
81         [Fact]
82         public void Decrypt_InvalidBase64Test()
83         {
84             var password = "password";
85             var encrypted = "%e%!!!!!!!!!!%!!!!!!!!!!%!!!!!!!!!!";
86             Assert.Throws<ApiKeyDecryptException>(() => ApiKey.Decrypt(password, encrypted));
87         }
88
89         [Fact]
90         public void Decrypt_InvalidHMACTest()
91         {
92             var password = "password";
93             var encrypted = "%e%m6EH2dECH7HWT9SFE0SK4Q==%mAAWPhPALf48s32s/yQarg==%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
94             Assert.Throws<ApiKeyDecryptException>(() => ApiKey.Decrypt(password, encrypted));
95         }
96
97         [Fact]
98         public void Decrypt_InvalidHMACLengthTest()
99         {
100             // HMAC が途中まで一致しているが長さが足りない場合
101             var password = "password";
102             var encrypted = "%e%m6EH2dECH7HWT9SFE0SK4Q==%mAAWPhPALf48s32s/yQarg==%zoCs8";
103             Assert.Throws<ApiKeyDecryptException>(() => ApiKey.Decrypt(password, encrypted));
104         }
105
106         [Fact]
107         public void EncryptAndDecrypt_Test()
108         {
109             var password = "hoge";
110             var plainText = "aaaaaaa";
111             var encrypted = ApiKey.Encrypt(password, plainText);
112             Assert.Equal(plainText, ApiKey.Decrypt(password, encrypted));
113         }
114     }
115 }