Path: blob/trunk/javascript/selenium-webdriver/test/lib/credentials_test.js
2885 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617'use strict'1819const assert = require('node:assert')20const virtualAuthenticatorCredential = require('selenium-webdriver/lib/virtual_authenticator').Credential2122describe('Credentials', function () {23const BASE64_ENCODED_PK = `MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDbBOu5Lhs4vpowbCnmCyLUpIE7JM9sm9QXzye2G+jr+Kr24MsinWohEce47BFPJlTaDzHSvOW2eeunBO89ZcvvVc8RLz4qyQ8rO98xS1jtgqi1NcBPETDrtzthODu/gd0sjB2Tk3TLuBGV25oPXt54a+Oo4JbBJ6h3s0+5eAfGplCbSNq6hN3Jh9YOTw5ZA6GCEy5l8zBaOgjXytd2v2OdSVoEDNiNQRkjJd2rmS2oi9AyQ26FR3B7BrPSiDlCcITZFOWgLF5C31Wp/PSHwQhlnh7/6YhnE2y9tzsUvzx0wJXrBADW13+oMxrneDK3WGbxTNYgIi1PvSqXlq27GjHtCK+R2QkXAgMBAAECggEAVc6bu7VAnP6v0gDOeX4razv4FX/adCao9ZsHZ+WPX8PQxtmWYqykH5CY4TSfsuizAgyPuQ028+j4Vjssr9VODLqFoanspT6YXsvaKanncUYbasNgUJnfnLnw3an2XpU2XdmXTNYckCPRX9nsAAURWT3/n9ljc/XYY22ecYxM298sDWnHu2uKZ1B7M3X60bQYL5T/lVXkKdD6xgSNLeP4AkRx0H4egaop68hoW8FIwmDPVWYVAvo8etzWCtibRXz5FcNld9MgD30/Ai7ycKy4Q1KhX5GBFI79MVVaHkSQfxPHpr7/XcmpQOEAr+BMPon4s4vnKqAGdGB3j/E3d/+4F2swykoQKBgQD8hCsp6FIQ315umJlk9/j/nGsMl85LgLaNVYpWlPRKPc54YNumtvj5vx1BG+zMbT7qIE3nmUPTCHP7qb5ERZG4CdMCS6S64/qzZEqijLCqe32pwj6j4fV5SyPWEcpxf6ehNdmcfgzVB3Wolfwh1ydhx/96L1jHJcTKchdJJzlfTvq8wwKBgQDeCnKws1t5GapfE1rmC/h4ol33L2qZTth9oQmbrXYohVnoqNFslDa43ePZwL9Jmd9kYb0axOTNMmyrP0NTj41uCfgDS0cJnNTc63ojKjegxHIyYDKRZNVUR/d34xAYB/vPfBYZUS7M89pO6LLsHhzS3qpu3/hppo/Uc/AM/r8PSflNHQKBgDnWgBh6OQncChPUlOLv9FMZPR1ZOfqLCYrjYEqi35uzGm6iKM13zXFO4AGAxu1P/IAd5BovFcTpg79Z8tWqZaUUwvscnl+cRlj+mMXAmdqCeO8VASOmqM1ml667axeZDIR867ZG836K5V029Wg+4qtX5uFypNAAi6GfHkxIKrD04yOHAoGACdh4wXESi0oiDdkz3KOHPwIjn6BhZC7z8mx+pnJODU3cYukxv3WTct37lUhAsyjJiQ/0bK1yX87ulqFVgO0Knmh+wNajrb9wiONAJTMICG7tiWJOm7fW5cfTJwWkBwYADmkfTRmHDvqzQSSvoC2S7aa389QulbC3C/qgGFNrcWgcT9kCgYAZTa1P9bFCDU7hJc2mHwJwAW7/FQKEJg8SL33KINpLwcR8fqaYOdAHWWz636osVEqosRrH39zJOGpf9x2RSWzQJ+dq8+6fACgfFZOVpN644+sAHfNPAI/gnNKU5OfUv+eav8fBnzlf1A3y3GIkyMyzFN3DE7e0n/lyqxE4H40BYGpI8g==`4142const data = {43_id: new Uint8Array([1, 2, 3, 4]),44rpId: 'localhost',45userHandle: new Uint8Array([1]),46privateKey: Buffer.from(BASE64_ENCODED_PK, 'base64').toString('binary'),47signCount: 0,48}4950it('can testRkEnabledCredential', function () {51const { _id, rpId, userHandle, privateKey, signCount } = data52const credential = virtualAuthenticatorCredential.createResidentCredential(53_id,54rpId,55userHandle,56privateKey,57signCount,58)5960let testCredentialId = new Uint8Array([1, 2, 3, 4])6162/**63* Checking if credential.id() matches with testCredentialId. Both values are64* arrays so we check if the lengths of both are equal and if one array has65* all its elements in the other array and vice-versa.66*/67assert.equal(68credential.id().length == testCredentialId.length &&69credential.id().every((item) => testCredentialId.includes(item)) &&70testCredentialId.every((item) => credential.id().includes(item)),71true,72)73if (credential.isResidentCredential() == true) {74assert(true)75} else {76assert(false)77}78assert.equal(credential.rpId(), 'localhost')7980let testUserHandle = new Uint8Array([1])8182/**83* Checking if credential.userHandle() matches with testUserHandle. Both values are84* arrays so we check if the lengths of both are equal and if one array has85* all its elements in the other array and vice-versa.86*/87assert.equal(88credential.userHandle().length == testUserHandle.length &&89credential.userHandle().every((item) => testUserHandle.includes(item)) &&90testUserHandle.every((item) => credential.userHandle().includes(item)),91true,92)93assert.equal(credential.privateKey(), Buffer.from(BASE64_ENCODED_PK, 'base64url').toString('binary'))94assert.equal(credential.signCount(), 0)95})9697it('can testRkDisabledCredential', function () {98const { _id, rpId, privateKey, signCount } = data99const credential = virtualAuthenticatorCredential.createNonResidentCredential(_id, rpId, privateKey, signCount)100101let testCredentialId = new Uint8Array([1, 2, 3, 4])102103/**104* Checking if credential.id() matches with testCredentialId. Both values are105* arrays so we check if the lengths of both are equal and if one array has106* all its elements in the other array and vice-versa.107*/108assert.equal(109credential.id().length == testCredentialId.length &&110credential.id().every((item) => testCredentialId.includes(item)) &&111testCredentialId.every((item) => credential.id().includes(item)),112true,113)114115if (credential.isResidentCredential() == false) {116assert(true)117} else {118assert(false)119}120121if (credential.userHandle() == null) {122assert(true)123} else {124assert(false)125}126})127128it('can testToDict', function () {129const { _id, rpId, userHandle, privateKey, signCount } = data130const credential = virtualAuthenticatorCredential.createResidentCredential(131_id,132rpId,133userHandle,134privateKey,135signCount,136)137138let credential_dict = credential.toDict()139assert.equal(credential_dict['credentialId'], Buffer.from(new Uint8Array([1, 2, 3, 4])).toString('base64url'))140141if (credential_dict['isResidentCredential'] == true) {142assert(true)143} else {144assert(false)145}146147assert.equal(credential_dict['rpId'], 'localhost')148assert.equal(credential_dict['userHandle'], Buffer.from(new Uint8Array([1])).toString('base64url'))149assert.equal(credential_dict['privateKey'], Buffer.from(privateKey, 'binary').toString('base64url'))150assert.equal(credential_dict['signCount'], 0)151})152153it('can testFromDict', function () {154let credential_data = {155credentialId: Buffer.from(new Uint8Array([1, 2, 3, 4])).toString('base64url'),156isResidentCredential: true,157rpId: 'localhost',158userHandle: Buffer.from(new Uint8Array([1])).toString('base64url'),159privateKey: BASE64_ENCODED_PK,160signCount: 0,161}162163let credential = new virtualAuthenticatorCredential().fromDict(credential_data)164let testCredentialId = new Uint8Array([1, 2, 3, 4])165assert.equal(166credential.id().length == testCredentialId.length &&167credential.id().every((item) => testCredentialId.includes(item)) &&168testCredentialId.every((item) => credential.id().includes(item)),169true,170)171172if (credential.isResidentCredential() == true) {173assert(true)174} else {175assert(false)176}177178assert.equal(credential.rpId(), 'localhost')179180let testUserHandle = new Uint8Array([1])181182/**183* Checking if credential.userHandle() matches with testUserHandle. Both values are184* arrays so we check if the lengths of both are equal and if one array has185* all its elements in the other array and vice-versa.186*/187assert.equal(188credential.userHandle().length == testUserHandle.length &&189credential.userHandle().every((item) => testUserHandle.includes(item)) &&190testUserHandle.every((item) => credential.userHandle().includes(item)),191true,192)193194assert.equal(credential.privateKey(), Buffer.from(BASE64_ENCODED_PK, 'base64url').toString('binary'))195assert.equal(credential.signCount(), 0)196})197})198199200