chore(deps): update swashbuckle-aspnetcore monorepo to 7.1.0
[ArchiSteamFarm.git] / ArchiSteamFarm.Tests / MobileAuthenticator.cs
blob059531ba4379d905496f78a02b3f390a41430cfa
1 // ----------------------------------------------------------------------------------------------
2 // _ _ _ ____ _ _____
3 // / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
4 // / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
5 // / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
6 // /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
7 // ----------------------------------------------------------------------------------------------
8 // |
9 // Copyright 2015-2024 Ɓukasz "JustArchi" Domeradzki
10 // Contact: JustArchi@JustArchi.net
11 // |
12 // Licensed under the Apache License, Version 2.0 (the "License");
13 // you may not use this file except in compliance with the License.
14 // You may obtain a copy of the License at
15 // |
16 // http://www.apache.org/licenses/LICENSE-2.0
17 // |
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the License is distributed on an "AS IS" BASIS,
20 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 // See the License for the specific language governing permissions and
22 // limitations under the License.
24 using System;
25 using System.Reflection;
26 using System.Text.Json.Nodes;
27 using ArchiSteamFarm.Helpers.Json;
28 using Microsoft.VisualStudio.TestTools.UnitTesting;
30 namespace ArchiSteamFarm.Tests;
32 #pragma warning disable CA1812 // False positive, the class is used during MSTest
33 [TestClass]
34 internal sealed class MobileAuthenticator {
35 [DataRow("qrg+wW8/u/TDt2i/+FQuPhuVrmY=", (ulong) 1337, "QFo72j9TnG+uRXe9EIJs4zyBPo0=")]
36 [DataRow("qrg+wW8/u/TDt2i/+FQuPhuVrmY=", (ulong) 1337, "mYbCKs8ZvsVN2odCMxpvidrIu1c=", "conf")]
37 [DataRow("qrg+wW8/u/TDt2i/+FQuPhuVrmY=", (ulong) 1723332288, "hiEx+JBqJqFJnSSL+dEthPHOmsc=")]
38 [DataRow("qrg+wW8/u/TDt2i/+FQuPhuVrmY=", (ulong) 1723332288, "hpZUxyNgwBvtKPROvedjuvVPQiE=", "conf")]
39 [DataTestMethod]
40 internal void GenerateConfirmationHash(string identitySecret, ulong time, string expectedCode, string? tag = null) {
41 ArgumentException.ThrowIfNullOrEmpty(identitySecret);
42 ArgumentOutOfRangeException.ThrowIfZero(time);
43 ArgumentException.ThrowIfNullOrEmpty(expectedCode);
45 MethodInfo? method = typeof(Steam.Security.MobileAuthenticator).GetMethod(nameof(GenerateConfirmationHash), BindingFlags.Instance | BindingFlags.NonPublic, [typeof(ulong), typeof(string)]);
47 if (method == null) {
48 throw new InvalidOperationException(nameof(method));
51 using Steam.Security.MobileAuthenticator authenticator = GenerateMobileAuthenticator(identitySecret, identitySecret);
53 string? result = method.Invoke(authenticator, [time, tag]) as string;
55 Assert.IsNotNull(result);
56 Assert.AreEqual(expectedCode, result);
59 [DataRow("KDHC3rsY8+CmiswnXJcE5e5dRfd=", (ulong) 1337, "47J4D")]
60 [DataRow("KDHC3rsY8+CmiswnXJcE5e5dRfd=", (ulong) 1723332288, "JQ3HQ")]
61 [DataTestMethod]
62 internal void GenerateTokenForTime(string sharedSecret, ulong time, string expectedCode) {
63 ArgumentException.ThrowIfNullOrEmpty(sharedSecret);
64 ArgumentOutOfRangeException.ThrowIfZero(time);
65 ArgumentException.ThrowIfNullOrEmpty(expectedCode);
67 using Steam.Security.MobileAuthenticator authenticator = GenerateMobileAuthenticator(sharedSecret, sharedSecret);
69 string? result = authenticator.GenerateTokenForTime(time);
71 Assert.IsNotNull(result);
72 Assert.AreEqual(expectedCode, result);
75 private static Steam.Security.MobileAuthenticator GenerateMobileAuthenticator(string identitySecret, string sharedSecret) {
76 ArgumentException.ThrowIfNullOrEmpty(identitySecret);
77 ArgumentException.ThrowIfNullOrEmpty(sharedSecret);
79 JsonObject jsonObject = new() {
80 ["identity_secret"] = identitySecret,
81 ["shared_secret"] = sharedSecret
84 Steam.Security.MobileAuthenticator? result = jsonObject.ToJsonElement().ToJsonObject<Steam.Security.MobileAuthenticator>();
86 if (result == null) {
87 throw new InvalidOperationException(nameof(result));
90 Steam.Bot bot = Bot.GenerateBot();
92 result.Init(bot);
94 return result;
97 #pragma warning restore CA1812 // False positive, the class is used during MSTest