Path: blob/main/components/gitpod-protocol/src/protocol.spec.ts
2498 views
/**1* Copyright (c) 2020 Gitpod GmbH. All rights reserved.2* Licensed under the GNU Affero General Public License (AGPL).3* See License.AGPL.txt in the project root for license information.4*/56import { suite, test } from "@testdeck/mocha";7import * as chai from "chai";8import { SSHPublicKeyValue, EnvVar, EnvVarWithValue } from ".";910const expect = chai.expect;1112@suite13class TestSSHPublicKeyValue {14private key =15"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDCnrN9UdK1bNGPmZfenTWXLuYYDjlYvZE8S+WOfP08WpR1GETzX5ZvgYOEZGwEE8KUPHC9cge4Hvo/ydIS9aqbZ5MiVGJ8cAIq1Ic89SjlDWU6fl8TwIqOPCi2imAASlEDP4q8vMLK1N6UOW1EVbxyL3uybGd10ysC1t1FxFPveIGNsYE/MOQeuEWS16AplpXYXIfVRSlgAskeBft2w8Ud3B4gNe8ECLA/FXu96UpvZkdtOarA3JZ9Z27GveNJg9Mtmmw0+US0KXiO9x9NyH7G8+mqVDwDY+nNvaFA5gtQxkkl/uY2oz9k/B4Rjlj3jOiUXe5uQs3XUm5m8g9a9fh62DabLpA2fEvtfg+a/VqNe52dNa5YjupwvBd6Inb5uMW/TYjNl6bNHPlXFKw/nwLOVzukpkjxMZUKS6+4BGkpoasj6y2rTU/wkpbdD8J7yjI1p6J9aKkC6KksIWgN7xGmHkv2PCGDqMHTNbnQyowtNKMgA/667vAYJ0qW7HAHBFXJRs6uRi/DI3+c1QV2s4wPCpEHDIYApovQ0fbON4WDPoGMyHd7kPh9xB/bX7Dj0uMXImu1pdTd62fQ/1XXX64+vjAAXS/P9RSCD0RCRt/K3LPKl2m7GPI3y1niaE52XhxZw+ms9ays6NasNVMw/ZC+f02Ti+L5FBEVf8230RVVRQ== [email protected]";1617@test public testValidate() {18const key = this.key;19const [t, k, e] = key.split(" ");20expect(21SSHPublicKeyValue.getData({22key,23name: "NiceName",24}),25).to.deep.equal({ type: t, key: k, email: e });26}2728@test public testValidateWithDiffType() {29const key = this.key;30const [_, k, e] = key.split(" ");31expect(32SSHPublicKeyValue.getData({33key: key.replace("ssh-rsa", "[email protected]"),34name: "NiceName",35}),36).to.deep.equal({ type: "[email protected]", key: k, email: e });37}3839@test public testValidateWithoutEmail() {40const key = this.key;41const [t, k, _] = key.split(" ");42expect(43SSHPublicKeyValue.getData({44key: key.replace(" [email protected]", ""),45name: "NiceName",46}),47).to.deep.equal({ type: t, key: k, email: undefined });48}4950@test public testValidateWithoutEmailButEndsWithSpaces() {51const key = this.key;52const [t, k, _] = key.split(" ");53expect(54SSHPublicKeyValue.getData({55key: key.replace("[email protected]", " "),56name: "NiceName",57}),58).to.deep.equal({ type: t, key: k, email: undefined });59}6061@test public testValidateWithError() {62expect(() =>63SSHPublicKeyValue.getData({64key: "Hello World",65name: "NiceName",66}),67).throw("Key is invalid");6869expect(() =>70SSHPublicKeyValue.getData({71key: "",72name: "NiceName",73}),74).throw("Key is invalid");75}7677@test public testGetFingerprint() {78const key = this.key;79expect(80SSHPublicKeyValue.getFingerprint({81key,82name: "NiceName",83}),84).to.equal("ykjP/b5aqoa3envmXzWpPMCGgEFMu3QvubfSTNrJCMA=");85}8687@test public testGetFingerprintWithIncorrectPublicKey() {88expect(() =>89SSHPublicKeyValue.getFingerprint({90key: "Hello World",91name: "NiceName",92}),93).to.throw("Key is invalid");94}95}9697@suite98class TestEnvVar {99@test100public testGetGitpodImageAuth_empty() {101const result = EnvVar.getGitpodImageAuth([]);102expect(result.size).to.equal(0);103}104105@test106public testGetGitpodImageAuth_noRelevantVar() {107const envVars: EnvVarWithValue[] = [{ name: "OTHER_VAR", value: "some_value" }];108const result = EnvVar.getGitpodImageAuth(envVars);109expect(result.size).to.equal(0);110}111112@test113public testGetGitpodImageAuth_singleEntryNoPort() {114const envVars: EnvVarWithValue[] = [115{116name: EnvVar.GITPOD_IMAGE_AUTH_ENV_VAR_NAME,117value: "my-registry.foo.net:Zm9vOmJhcg==",118},119];120const result = EnvVar.getGitpodImageAuth(envVars);121expect(result.size).to.equal(1);122expect(result.get("my-registry.foo.net")).to.equal("Zm9vOmJhcg==");123}124125@test126public testGetGitpodImageAuth_singleEntryWithPort() {127const envVars: EnvVarWithValue[] = [128{129name: EnvVar.GITPOD_IMAGE_AUTH_ENV_VAR_NAME,130value: "my-registry.foo.net:5000:Zm9vOmJhcg==",131},132];133const result = EnvVar.getGitpodImageAuth(envVars);134expect(result.size).to.equal(1);135expect(result.get("my-registry.foo.net:5000")).to.equal("Zm9vOmJhcg==");136}137138@test139public testGetGitpodImageAuth_multipleEntries() {140const envVars: EnvVarWithValue[] = [141{142name: EnvVar.GITPOD_IMAGE_AUTH_ENV_VAR_NAME,143value: "my-registry.foo.net:Zm9vOmJhcg==,my-registry2.bar.com:YWJjOmRlZg==",144},145];146const result = EnvVar.getGitpodImageAuth(envVars);147expect(result.size).to.equal(2);148expect(result.get("my-registry.foo.net")).to.equal("Zm9vOmJhcg==");149expect(result.get("my-registry2.bar.com")).to.equal("YWJjOmRlZg==");150}151152@test153public testGetGitpodImageAuth_multipleEntriesWithPortAndMalformed() {154const envVars: EnvVarWithValue[] = [155{156name: EnvVar.GITPOD_IMAGE_AUTH_ENV_VAR_NAME,157value: "my-registry.foo.net:5000:Zm9vOmJhcg==,my-registry2.bar.com:YWJjOmRlZg==,invalidEntry,another.host:anothercred",158},159];160const result = EnvVar.getGitpodImageAuth(envVars);161expect(result.size).to.equal(3);162expect(result.get("my-registry2.bar.com")).to.equal("YWJjOmRlZg==");163expect(result.get("another.host")).to.equal("anothercred");164expect(result.get("my-registry.foo.net:5000")).to.equal("Zm9vOmJhcg==");165}166167@test168public testGetGitpodImageAuth_emptyValue() {169const envVars: EnvVarWithValue[] = [170{171name: EnvVar.GITPOD_IMAGE_AUTH_ENV_VAR_NAME,172value: "",173},174];175const result = EnvVar.getGitpodImageAuth(envVars);176expect(result.size).to.equal(0);177}178179@test180public testGetGitpodImageAuth_malformedEntries() {181const envVars: EnvVarWithValue[] = [182{183name: EnvVar.GITPOD_IMAGE_AUTH_ENV_VAR_NAME,184value: "justhost,hostonly:,:credonly,:::,:,",185},186];187const result = EnvVar.getGitpodImageAuth(envVars);188expect(result.size).to.equal(0);189}190191@test192public testGetGitpodImageAuth_entriesWithSpaces() {193const envVars: EnvVarWithValue[] = [194{195name: EnvVar.GITPOD_IMAGE_AUTH_ENV_VAR_NAME,196value: " my-registry.foo.net : Zm9vOmJhcg== , my-registry2.bar.com:YWJjOmRlZg== ",197},198];199const result = EnvVar.getGitpodImageAuth(envVars);200expect(result.size).to.equal(2);201expect(result.get("my-registry.foo.net")).to.equal("Zm9vOmJhcg==");202expect(result.get("my-registry2.bar.com")).to.equal("YWJjOmRlZg==");203}204}205206// Exporting both test suites207const testSSHPublicKeyValue = new TestSSHPublicKeyValue();208const testEnvVar = new TestEnvVar();209module.exports = {210testSSHPublicKeyValue,211testEnvVar,212};213214215