summaryrefslogtreecommitdiffstats
path: root/gerrit-reviewdb/src/test/java/com/google/gerrit/reviewdb/client/AccountSshKeyTest.java
blob: 07c00b991a6002c7218677779718bc8f4dcdf42e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright (C) 2016 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.reviewdb.client;

import static com.google.common.truth.Truth.assertThat;

import org.junit.Test;

public class AccountSshKeyTest {
  private static final String KEY =
      "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQCgug5VyMXQGnem2H1KVC4/HcRcD4zzBqS"
      + "uJBRWVonSSoz3RoAZ7bWXCVVGwchtXwUURD689wFYdiPecOrWOUgeeyRq754YWRhU+W28"
      + "vf8IZixgjCmiBhaL2gt3wff6pP+NXJpTSA4aeWE5DfNK5tZlxlSxqkKOS8JRSUeNQov5T"
      + "w== john.doe@example.com";

  private static final String KEY_WITH_NEWLINES =
      "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQCgug5VyMXQGnem2H1KVC4/HcRcD4zzBqS\n"
      + "uJBRWVonSSoz3RoAZ7bWXCVVGwchtXwUURD689wFYdiPecOrWOUgeeyRq754YWRhU+W28\n"
      + "vf8IZixgjCmiBhaL2gt3wff6pP+NXJpTSA4aeWE5DfNK5tZlxlSxqkKOS8JRSUeNQov5T\n"
      + "w== john.doe@example.com";

  private final Account.Id accountId = new Account.Id(1);

  @Test
  public void testValidity() throws Exception {
    AccountSshKey key = new AccountSshKey(
        new AccountSshKey.Id(accountId, -1), KEY);
    assertThat(key.isValid()).isFalse();
    key = new AccountSshKey(new AccountSshKey.Id(accountId, 0), KEY);
    assertThat(key.isValid()).isFalse();
    key = new AccountSshKey(new AccountSshKey.Id(accountId, 1), KEY);
    assertThat(key.isValid()).isTrue();
  }

  @Test
  public void testGetters() throws Exception {
    AccountSshKey key = new AccountSshKey(
        new AccountSshKey.Id(accountId, 1), KEY);
    assertThat(key.getSshPublicKey()).isEqualTo(KEY);
    assertThat(key.getAlgorithm()).isEqualTo(KEY.split(" ")[0]);
    assertThat(key.getEncodedKey()).isEqualTo(KEY.split(" ")[1]);
    assertThat(key.getComment()).isEqualTo(KEY.split(" ")[2]);
  }

  @Test
  public void testKeyWithNewLines() throws Exception {
    AccountSshKey key = new AccountSshKey(
        new AccountSshKey.Id(accountId, 1), KEY_WITH_NEWLINES);
    assertThat(key.getSshPublicKey()).isEqualTo(KEY);
    assertThat(key.getAlgorithm()).isEqualTo(KEY.split(" ")[0]);
    assertThat(key.getEncodedKey()).isEqualTo(KEY.split(" ")[1]);
    assertThat(key.getComment()).isEqualTo(KEY.split(" ")[2]);
  }
}