LCOV - code coverage report
Current view: top level - server/account - AccountSshKey.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 20 21 95.2 %
Date: 2022-11-19 15:00:39 Functions: 11 11 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2008 The Android Open Source Project
       2             : //
       3             : // Licensed under the Apache License, Version 2.0 (the "License");
       4             : // you may not use this file except in compliance with the License.
       5             : // You may obtain a copy of the License at
       6             : //
       7             : // http://www.apache.org/licenses/LICENSE-2.0
       8             : //
       9             : // Unless required by applicable law or agreed to in writing, software
      10             : // distributed under the License is distributed on an "AS IS" BASIS,
      11             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             : // See the License for the specific language governing permissions and
      13             : // limitations under the License.
      14             : 
      15             : package com.google.gerrit.server.account;
      16             : 
      17             : import com.google.auto.value.AutoValue;
      18             : import com.google.common.base.Splitter;
      19             : import com.google.gerrit.entities.Account;
      20             : import java.util.List;
      21             : 
      22             : /** An SSH key approved for use by an {@link Account}. */
      23             : @AutoValue
      24          17 : public abstract class AccountSshKey {
      25             :   public static AccountSshKey create(Account.Id accountId, int seq, String sshPublicKey) {
      26          17 :     return create(accountId, seq, sshPublicKey, true);
      27             :   }
      28             : 
      29             :   public static AccountSshKey createInvalid(Account.Id accountId, int seq, String sshPublicKey) {
      30           2 :     return create(accountId, seq, sshPublicKey, false);
      31             :   }
      32             : 
      33             :   public static AccountSshKey createInvalid(AccountSshKey key) {
      34           1 :     return create(key.accountId(), key.seq(), key.sshPublicKey(), false);
      35             :   }
      36             : 
      37             :   public static AccountSshKey create(
      38             :       Account.Id accountId, int seq, String sshPublicKey, boolean valid) {
      39          17 :     return new AutoValue_AccountSshKey.Builder()
      40          17 :         .setAccountId(accountId)
      41          17 :         .setSeq(seq)
      42          17 :         .setSshPublicKey(stripOffNewLines(sshPublicKey))
      43          17 :         .setValid(valid && seq > 0)
      44          17 :         .build();
      45             :   }
      46             : 
      47             :   private static String stripOffNewLines(String s) {
      48          17 :     return s.replace("\n", "").replace("\r", "");
      49             :   }
      50             : 
      51             :   public abstract Account.Id accountId();
      52             : 
      53             :   public abstract int seq();
      54             : 
      55             :   public abstract String sshPublicKey();
      56             : 
      57             :   public abstract boolean valid();
      58             : 
      59             :   private String publicKeyPart(int index, String defaultValue) {
      60          17 :     String s = sshPublicKey();
      61          17 :     if (s != null && s.length() > 0) {
      62          17 :       List<String> parts = Splitter.on(' ').splitToList(s);
      63          17 :       if (parts.size() > index) {
      64          17 :         return parts.get(index);
      65             :       }
      66             :     }
      67           0 :     return defaultValue;
      68             :   }
      69             : 
      70             :   public String algorithm() {
      71           7 :     return publicKeyPart(0, "none");
      72             :   }
      73             : 
      74             :   public String encodedKey() {
      75          17 :     return publicKeyPart(1, null);
      76             :   }
      77             : 
      78             :   public String comment() {
      79           7 :     return publicKeyPart(2, "");
      80             :   }
      81             : 
      82             :   @AutoValue.Builder
      83          17 :   abstract static class Builder {
      84             :     public abstract Builder setAccountId(Account.Id accountId);
      85             : 
      86             :     public abstract Builder setSeq(int seq);
      87             : 
      88             :     public abstract Builder setSshPublicKey(String sshPublicKey);
      89             : 
      90             :     public abstract Builder setValid(boolean valid);
      91             : 
      92             :     public abstract AccountSshKey build();
      93             :   }
      94             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750