go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/client/cmd/cloudkms/common_test.go (about)

     1  // Copyright 2018 The LUCI Authors.
     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 main
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"go.chromium.org/luci/auth"
    22  	. "go.chromium.org/luci/common/testing/assertions"
    23  
    24  	. "github.com/smartystreets/goconvey/convey"
    25  )
    26  
    27  func TestPathValidation(t *testing.T) {
    28  	Convey(`Path too short.`, t, func() {
    29  		err := validateCryptoKeysKMSPath("bad/path")
    30  		So(err, ShouldErrLike, "path should have the form")
    31  	})
    32  	Convey(`Path too long.`, t, func() {
    33  		err := validateCryptoKeysKMSPath("bad/long/long/long/long/long/long/long/long/long/path")
    34  		So(err, ShouldErrLike, "path should have the form")
    35  	})
    36  	Convey(`Path misspelling.`, t, func() {
    37  		err := validateCryptoKeysKMSPath("projects/chromium/oops/global/keyRings/test/cryptoKeys/my_key")
    38  		So(err, ShouldErrLike, "expected component 3")
    39  	})
    40  	Convey(`Good path.`, t, func() {
    41  		err := validateCryptoKeysKMSPath("projects/chromium/locations/global/keyRings/test/cryptoKeys/my_key")
    42  		So(err, ShouldBeNil)
    43  	})
    44  	Convey(`Good path.`, t, func() {
    45  		err := validateCryptoKeysKMSPath("projects/chromium/locations/global/keyRings/test/cryptoKeys/my_key/cryptoKeyVersions/1")
    46  		So(err, ShouldBeNil)
    47  	})
    48  	Convey(`Support leading slash.`, t, func() {
    49  		err := validateCryptoKeysKMSPath("/projects/chromium/locations/global/keyRings/test/cryptoKeys/my_key")
    50  		So(err, ShouldBeNil)
    51  	})
    52  }
    53  
    54  func TestCryptoRunParse(t *testing.T) {
    55  	ctx := context.Background()
    56  	path := "/projects/chromium/locations/global/keyRings/test/cryptoKeys/my_key"
    57  	flags := []string{"-input", "hello", "-output", "goodbye"}
    58  	testParse := func(flags, args []string) error {
    59  		c := cryptRun{}
    60  		c.Init(auth.Options{})
    61  		c.GetFlags().Parse(flags)
    62  		return c.Parse(ctx, args)
    63  	}
    64  	Convey(`Make sure that Parse fails with no positional args.`, t, func() {
    65  		err := testParse(flags, []string{})
    66  		So(err, ShouldErrLike, "positional arguments missing")
    67  	})
    68  	Convey(`Make sure that Parse fails with too many positional args.`, t, func() {
    69  		err := testParse(flags, []string{"one", "two"})
    70  		So(err, ShouldErrLike, "unexpected positional arguments")
    71  	})
    72  	Convey(`Make sure that Parse fails with no input.`, t, func() {
    73  		err := testParse([]string{"-output", "goodbye"}, []string{path})
    74  		So(err, ShouldErrLike, "input file")
    75  	})
    76  	Convey(`Make sure that Parse fails with no output.`, t, func() {
    77  		err := testParse([]string{"-input", "hello"}, []string{path})
    78  		So(err, ShouldErrLike, "output location")
    79  	})
    80  	Convey(`Make sure that Parse fails with bad key path.`, t, func() {
    81  		err := testParse(flags, []string{"abcdefg"})
    82  		So(err, ShouldNotBeNil)
    83  	})
    84  	Convey(`Make sure that Parse works with everything set right.`, t, func() {
    85  		err := testParse(flags, []string{path})
    86  		So(err, ShouldBeNil)
    87  	})
    88  }
    89  
    90  func TestVerifyRunParse(t *testing.T) {
    91  	ctx := context.Background()
    92  	path := "/projects/chromium/locations/global/keyRings/test/cryptoKeys/my_key/cryptoKeyVersions/1"
    93  	flags := []string{"-input", "hello", "-input-sig", "goodbye"}
    94  	testParse := func(flags, args []string) error {
    95  		v := verifyRun{}
    96  		v.Init(auth.Options{})
    97  		v.GetFlags().Parse(flags)
    98  		return v.Parse(ctx, args)
    99  	}
   100  	Convey(`Make sure that Parse fails with no positional args.`, t, func() {
   101  		err := testParse(flags, []string{})
   102  		So(err, ShouldErrLike, "positional arguments missing")
   103  	})
   104  	Convey(`Make sure that Parse fails with too many positional args.`, t, func() {
   105  		err := testParse(flags, []string{"one", "two"})
   106  		So(err, ShouldErrLike, "unexpected positional arguments")
   107  	})
   108  	Convey(`Make sure that Parse fails with no input.`, t, func() {
   109  		err := testParse([]string{"-input-sig", "goodbye"}, []string{path})
   110  		So(err, ShouldErrLike, "input file")
   111  	})
   112  	Convey(`Make sure that Parse fails with no input sig.`, t, func() {
   113  		err := testParse([]string{"-input", "hello"}, []string{path})
   114  		So(err, ShouldErrLike, "input sig")
   115  	})
   116  	Convey(`Make sure that Parse fails with bad key path.`, t, func() {
   117  		err := testParse(flags, []string{"abcdefg"})
   118  		So(err, ShouldNotBeNil)
   119  	})
   120  	Convey(`Make sure that Parse works with everything set right.`, t, func() {
   121  		err := testParse(flags, []string{path})
   122  		So(err, ShouldBeNil)
   123  	})
   124  }
   125  
   126  func TestSignRunParse(t *testing.T) {
   127  	ctx := context.Background()
   128  	path := "/projects/chromium/locations/global/keyRings/test/cryptoKeys/my_key/cryptoKeyVersions/1"
   129  	flags := []string{"-input", "hello", "-output", "goodbye"}
   130  	testParse := func(flags, args []string) error {
   131  		s := signRun{}
   132  		s.Init(auth.Options{})
   133  		s.GetFlags().Parse(flags)
   134  		return s.Parse(ctx, args)
   135  	}
   136  	Convey(`Make sure that Parse fails with no positional args.`, t, func() {
   137  		err := testParse(flags, []string{})
   138  		So(err, ShouldErrLike, "positional arguments missing")
   139  	})
   140  	Convey(`Make sure that Parse fails with too many positional args.`, t, func() {
   141  		err := testParse(flags, []string{"one", "two"})
   142  		So(err, ShouldErrLike, "unexpected positional arguments")
   143  	})
   144  	Convey(`Make sure that Parse fails with no input.`, t, func() {
   145  		err := testParse([]string{"-output", "goodbye"}, []string{path})
   146  		So(err, ShouldErrLike, "input file")
   147  	})
   148  	Convey(`Make sure that Parse fails with no input sig.`, t, func() {
   149  		err := testParse([]string{"-input", "hello"}, []string{path})
   150  		So(err, ShouldErrLike, "output location")
   151  	})
   152  	Convey(`Make sure that Parse fails with bad key path.`, t, func() {
   153  		err := testParse(flags, []string{"abcdefg"})
   154  		So(err, ShouldNotBeNil)
   155  	})
   156  	Convey(`Make sure that Parse works with everything set right.`, t, func() {
   157  		err := testParse(flags, []string{path})
   158  		So(err, ShouldBeNil)
   159  	})
   160  }
   161  
   162  func TestDownloadRunParse(t *testing.T) {
   163  	ctx := context.Background()
   164  	path := "/projects/chromium/locations/global/keyRings/test/cryptoKeys/my_key/cryptoKeyVersions/1"
   165  	flags := []string{"-output", "goodbye"}
   166  	testParse := func(flags, args []string) error {
   167  		d := downloadRun{}
   168  		d.Init(auth.Options{})
   169  		d.GetFlags().Parse(flags)
   170  		return d.Parse(ctx, args)
   171  	}
   172  	Convey(`Make sure that Parse fails with no positional args.`, t, func() {
   173  		err := testParse(flags, []string{})
   174  		So(err, ShouldErrLike, "positional arguments missing")
   175  	})
   176  	Convey(`Make sure that Parse fails with too many positional args.`, t, func() {
   177  		err := testParse(flags, []string{"one", "two"})
   178  		So(err, ShouldErrLike, "unexpected positional arguments")
   179  	})
   180  	Convey(`Make sure that Parse fails with input.`, t, func() {
   181  		err := testParse([]string{"-input", "hello", "-output", "goodbye"}, []string{path})
   182  		So(err, ShouldErrLike, "output location")
   183  	})
   184  	Convey(`Make sure that Parse fails with bad key path.`, t, func() {
   185  		err := testParse(flags, []string{"abcdefg"})
   186  		So(err, ShouldNotBeNil)
   187  	})
   188  	Convey(`Make sure that Parse works with everything set right.`, t, func() {
   189  		err := testParse(flags, []string{path})
   190  		So(err, ShouldBeNil)
   191  	})
   192  }