Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-protocol/src/protocol.spec.ts
2498 views
1
/**
2
* Copyright (c) 2020 Gitpod GmbH. All rights reserved.
3
* Licensed under the GNU Affero General Public License (AGPL).
4
* See License.AGPL.txt in the project root for license information.
5
*/
6
7
import { suite, test } from "@testdeck/mocha";
8
import * as chai from "chai";
9
import { SSHPublicKeyValue, EnvVar, EnvVarWithValue } from ".";
10
11
const expect = chai.expect;
12
13
@suite
14
class TestSSHPublicKeyValue {
15
private key =
16
"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]";
17
18
@test public testValidate() {
19
const key = this.key;
20
const [t, k, e] = key.split(" ");
21
expect(
22
SSHPublicKeyValue.getData({
23
key,
24
name: "NiceName",
25
}),
26
).to.deep.equal({ type: t, key: k, email: e });
27
}
28
29
@test public testValidateWithDiffType() {
30
const key = this.key;
31
const [_, k, e] = key.split(" ");
32
expect(
33
SSHPublicKeyValue.getData({
34
key: key.replace("ssh-rsa", "[email protected]"),
35
name: "NiceName",
36
}),
37
).to.deep.equal({ type: "[email protected]", key: k, email: e });
38
}
39
40
@test public testValidateWithoutEmail() {
41
const key = this.key;
42
const [t, k, _] = key.split(" ");
43
expect(
44
SSHPublicKeyValue.getData({
45
key: key.replace(" [email protected]", ""),
46
name: "NiceName",
47
}),
48
).to.deep.equal({ type: t, key: k, email: undefined });
49
}
50
51
@test public testValidateWithoutEmailButEndsWithSpaces() {
52
const key = this.key;
53
const [t, k, _] = key.split(" ");
54
expect(
55
SSHPublicKeyValue.getData({
56
key: key.replace("[email protected]", " "),
57
name: "NiceName",
58
}),
59
).to.deep.equal({ type: t, key: k, email: undefined });
60
}
61
62
@test public testValidateWithError() {
63
expect(() =>
64
SSHPublicKeyValue.getData({
65
key: "Hello World",
66
name: "NiceName",
67
}),
68
).throw("Key is invalid");
69
70
expect(() =>
71
SSHPublicKeyValue.getData({
72
key: "",
73
name: "NiceName",
74
}),
75
).throw("Key is invalid");
76
}
77
78
@test public testGetFingerprint() {
79
const key = this.key;
80
expect(
81
SSHPublicKeyValue.getFingerprint({
82
key,
83
name: "NiceName",
84
}),
85
).to.equal("ykjP/b5aqoa3envmXzWpPMCGgEFMu3QvubfSTNrJCMA=");
86
}
87
88
@test public testGetFingerprintWithIncorrectPublicKey() {
89
expect(() =>
90
SSHPublicKeyValue.getFingerprint({
91
key: "Hello World",
92
name: "NiceName",
93
}),
94
).to.throw("Key is invalid");
95
}
96
}
97
98
@suite
99
class TestEnvVar {
100
@test
101
public testGetGitpodImageAuth_empty() {
102
const result = EnvVar.getGitpodImageAuth([]);
103
expect(result.size).to.equal(0);
104
}
105
106
@test
107
public testGetGitpodImageAuth_noRelevantVar() {
108
const envVars: EnvVarWithValue[] = [{ name: "OTHER_VAR", value: "some_value" }];
109
const result = EnvVar.getGitpodImageAuth(envVars);
110
expect(result.size).to.equal(0);
111
}
112
113
@test
114
public testGetGitpodImageAuth_singleEntryNoPort() {
115
const envVars: EnvVarWithValue[] = [
116
{
117
name: EnvVar.GITPOD_IMAGE_AUTH_ENV_VAR_NAME,
118
value: "my-registry.foo.net:Zm9vOmJhcg==",
119
},
120
];
121
const result = EnvVar.getGitpodImageAuth(envVars);
122
expect(result.size).to.equal(1);
123
expect(result.get("my-registry.foo.net")).to.equal("Zm9vOmJhcg==");
124
}
125
126
@test
127
public testGetGitpodImageAuth_singleEntryWithPort() {
128
const envVars: EnvVarWithValue[] = [
129
{
130
name: EnvVar.GITPOD_IMAGE_AUTH_ENV_VAR_NAME,
131
value: "my-registry.foo.net:5000:Zm9vOmJhcg==",
132
},
133
];
134
const result = EnvVar.getGitpodImageAuth(envVars);
135
expect(result.size).to.equal(1);
136
expect(result.get("my-registry.foo.net:5000")).to.equal("Zm9vOmJhcg==");
137
}
138
139
@test
140
public testGetGitpodImageAuth_multipleEntries() {
141
const envVars: EnvVarWithValue[] = [
142
{
143
name: EnvVar.GITPOD_IMAGE_AUTH_ENV_VAR_NAME,
144
value: "my-registry.foo.net:Zm9vOmJhcg==,my-registry2.bar.com:YWJjOmRlZg==",
145
},
146
];
147
const result = EnvVar.getGitpodImageAuth(envVars);
148
expect(result.size).to.equal(2);
149
expect(result.get("my-registry.foo.net")).to.equal("Zm9vOmJhcg==");
150
expect(result.get("my-registry2.bar.com")).to.equal("YWJjOmRlZg==");
151
}
152
153
@test
154
public testGetGitpodImageAuth_multipleEntriesWithPortAndMalformed() {
155
const envVars: EnvVarWithValue[] = [
156
{
157
name: EnvVar.GITPOD_IMAGE_AUTH_ENV_VAR_NAME,
158
value: "my-registry.foo.net:5000:Zm9vOmJhcg==,my-registry2.bar.com:YWJjOmRlZg==,invalidEntry,another.host:anothercred",
159
},
160
];
161
const result = EnvVar.getGitpodImageAuth(envVars);
162
expect(result.size).to.equal(3);
163
expect(result.get("my-registry2.bar.com")).to.equal("YWJjOmRlZg==");
164
expect(result.get("another.host")).to.equal("anothercred");
165
expect(result.get("my-registry.foo.net:5000")).to.equal("Zm9vOmJhcg==");
166
}
167
168
@test
169
public testGetGitpodImageAuth_emptyValue() {
170
const envVars: EnvVarWithValue[] = [
171
{
172
name: EnvVar.GITPOD_IMAGE_AUTH_ENV_VAR_NAME,
173
value: "",
174
},
175
];
176
const result = EnvVar.getGitpodImageAuth(envVars);
177
expect(result.size).to.equal(0);
178
}
179
180
@test
181
public testGetGitpodImageAuth_malformedEntries() {
182
const envVars: EnvVarWithValue[] = [
183
{
184
name: EnvVar.GITPOD_IMAGE_AUTH_ENV_VAR_NAME,
185
value: "justhost,hostonly:,:credonly,:::,:,",
186
},
187
];
188
const result = EnvVar.getGitpodImageAuth(envVars);
189
expect(result.size).to.equal(0);
190
}
191
192
@test
193
public testGetGitpodImageAuth_entriesWithSpaces() {
194
const envVars: EnvVarWithValue[] = [
195
{
196
name: EnvVar.GITPOD_IMAGE_AUTH_ENV_VAR_NAME,
197
value: " my-registry.foo.net : Zm9vOmJhcg== , my-registry2.bar.com:YWJjOmRlZg== ",
198
},
199
];
200
const result = EnvVar.getGitpodImageAuth(envVars);
201
expect(result.size).to.equal(2);
202
expect(result.get("my-registry.foo.net")).to.equal("Zm9vOmJhcg==");
203
expect(result.get("my-registry2.bar.com")).to.equal("YWJjOmRlZg==");
204
}
205
}
206
207
// Exporting both test suites
208
const testSSHPublicKeyValue = new TestSSHPublicKeyValue();
209
const testEnvVar = new TestEnvVar();
210
module.exports = {
211
testSSHPublicKeyValue,
212
testEnvVar,
213
};
214
215