From 165fc01acd4f9f89c335dbac9972861e80fee67f Mon Sep 17 00:00:00 2001 From: David Pursehouse Date: Thu, 5 May 2016 06:50:06 +0900 Subject: [PATCH] AccountSshKey.Id: Add isValid() method The isValid() method returns false when the Id's sequence number is invalid (i.e. < 1). Extend AccountSshKey#isValid() to also check its Id's validity. Change-Id: I7b428b06cc5bbb04cceef20d9f284e58071b9abe --- .../gerrit/reviewdb/client/AccountSshKey.java | 8 +++- .../reviewdb/client/AccountSshKeyTest.java | 39 +++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 gerrit-reviewdb/src/test/java/com/google/gerrit/reviewdb/client/AccountSshKeyTest.java diff --git a/gerrit-reviewdb/src/main/java/com/google/gerrit/reviewdb/client/AccountSshKey.java b/gerrit-reviewdb/src/main/java/com/google/gerrit/reviewdb/client/AccountSshKey.java index 7b1e8740f5..a28fc603f7 100644 --- a/gerrit-reviewdb/src/main/java/com/google/gerrit/reviewdb/client/AccountSshKey.java +++ b/gerrit-reviewdb/src/main/java/com/google/gerrit/reviewdb/client/AccountSshKey.java @@ -53,6 +53,10 @@ public final class AccountSshKey { protected void set(int newValue) { seq = newValue; } + + public boolean isValid() { + return seq > 0; + } } @Column(id = 1, name = Column.NONE) @@ -70,7 +74,7 @@ public final class AccountSshKey { public AccountSshKey(final AccountSshKey.Id i, final String pub) { id = i; sshPublicKey = pub; - valid = true; // We can assume it is fine. + valid = id.isValid(); } public Account.Id getAccount() { @@ -125,7 +129,7 @@ public final class AccountSshKey { } public boolean isValid() { - return valid; + return valid && id.isValid(); } public void setInvalid() { diff --git a/gerrit-reviewdb/src/test/java/com/google/gerrit/reviewdb/client/AccountSshKeyTest.java b/gerrit-reviewdb/src/test/java/com/google/gerrit/reviewdb/client/AccountSshKeyTest.java new file mode 100644 index 0000000000..06d5b90d32 --- /dev/null +++ b/gerrit-reviewdb/src/test/java/com/google/gerrit/reviewdb/client/AccountSshKeyTest.java @@ -0,0 +1,39 @@ +// 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"; + + @Test + public void testValidity() throws Exception { + Account.Id accountId = new Account.Id(1); + 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(); + } +}