go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/resultdb/pbutil/common_test.go (about)

     1  // Copyright 2023 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 pbutil
    16  
    17  import (
    18  	"strings"
    19  	"testing"
    20  
    21  	. "github.com/smartystreets/goconvey/convey"
    22  	. "go.chromium.org/luci/common/testing/assertions"
    23  	pb "go.chromium.org/luci/resultdb/proto/v1"
    24  )
    25  
    26  func TestValidate(t *testing.T) {
    27  	t.Parallel()
    28  	Convey(`ValidateGitilesCommit`, t, func() {
    29  		commit := &pb.GitilesCommit{
    30  			Host:       "chromium.googlesource.com",
    31  			Project:    "chromium/src",
    32  			Ref:        "refs/heads/branch",
    33  			CommitHash: "123456789012345678901234567890abcdefabcd",
    34  			Position:   1,
    35  		}
    36  		Convey(`Valid`, func() {
    37  			So(ValidateGitilesCommit(commit), ShouldBeNil)
    38  		})
    39  		Convey(`Nil`, func() {
    40  			So(ValidateGitilesCommit(nil), ShouldErrLike, `unspecified`)
    41  		})
    42  		Convey(`Host`, func() {
    43  			Convey(`Missing`, func() {
    44  				commit.Host = ""
    45  				So(ValidateGitilesCommit(commit), ShouldErrLike, `host: unspecified`)
    46  			})
    47  			Convey(`Invalid format`, func() {
    48  				commit.Host = "https://somehost.com"
    49  				So(ValidateGitilesCommit(commit), ShouldErrLike, `host: does not match`)
    50  			})
    51  			Convey(`Too long`, func() {
    52  				commit.Host = strings.Repeat("a", hostnameMaxLength+1)
    53  				So(ValidateGitilesCommit(commit), ShouldErrLike, `host: exceeds `, ` characters`)
    54  			})
    55  		})
    56  		Convey(`Project`, func() {
    57  			Convey(`Missing`, func() {
    58  				commit.Project = ""
    59  				So(ValidateGitilesCommit(commit), ShouldErrLike, `project: unspecified`)
    60  			})
    61  			Convey(`Too long`, func() {
    62  				commit.Project = strings.Repeat("a", 256)
    63  				So(ValidateGitilesCommit(commit), ShouldErrLike, `project: exceeds 255 characters`)
    64  			})
    65  		})
    66  		Convey(`Refs`, func() {
    67  			Convey(`Missing`, func() {
    68  				commit.Ref = ""
    69  				So(ValidateGitilesCommit(commit), ShouldErrLike, `ref: unspecified`)
    70  			})
    71  			Convey(`Invalid`, func() {
    72  				commit.Ref = "main"
    73  				So(ValidateGitilesCommit(commit), ShouldErrLike, `ref: does not match refs/.*`)
    74  			})
    75  			Convey(`Too long`, func() {
    76  				commit.Ref = "refs/" + strings.Repeat("a", 252)
    77  				So(ValidateGitilesCommit(commit), ShouldErrLike, `ref: exceeds 255 characters`)
    78  			})
    79  		})
    80  		Convey(`Commit Hash`, func() {
    81  			Convey(`Missing`, func() {
    82  				commit.CommitHash = ""
    83  				So(ValidateGitilesCommit(commit), ShouldErrLike, `commit_hash: unspecified`)
    84  			})
    85  			Convey(`Invalid (too long)`, func() {
    86  				commit.CommitHash = strings.Repeat("a", 41)
    87  				So(ValidateGitilesCommit(commit), ShouldErrLike, `commit_hash: does not match "^[a-f0-9]{40}$"`)
    88  			})
    89  			Convey(`Invalid (too short)`, func() {
    90  				commit.CommitHash = strings.Repeat("a", 39)
    91  				So(ValidateGitilesCommit(commit), ShouldErrLike, `commit_hash: does not match "^[a-f0-9]{40}$"`)
    92  			})
    93  			Convey(`Invalid (upper case)`, func() {
    94  				commit.CommitHash = "123456789012345678901234567890ABCDEFABCD"
    95  				So(ValidateGitilesCommit(commit), ShouldErrLike, `commit_hash: does not match "^[a-f0-9]{40}$"`)
    96  			})
    97  		})
    98  		Convey(`Position`, func() {
    99  			Convey(`Missing`, func() {
   100  				commit.Position = 0
   101  				So(ValidateGitilesCommit(commit), ShouldErrLike, `position: unspecified`)
   102  			})
   103  			Convey(`Negative`, func() {
   104  				commit.Position = -1
   105  				So(ValidateGitilesCommit(commit), ShouldErrLike, `position: cannot be negative`)
   106  			})
   107  		})
   108  	})
   109  	Convey(`ValidateGerritChange`, t, func() {
   110  		change := &pb.GerritChange{
   111  			Host:     "chromium-review.googlesource.com",
   112  			Project:  "chromium/src",
   113  			Change:   12345,
   114  			Patchset: 1,
   115  		}
   116  		Convey(`Valid`, func() {
   117  			So(ValidateGerritChange(change), ShouldBeNil)
   118  		})
   119  		Convey(`Nil`, func() {
   120  			So(ValidateGerritChange(nil), ShouldErrLike, `unspecified`)
   121  		})
   122  		Convey(`Host`, func() {
   123  			Convey(`Missing`, func() {
   124  				change.Host = ""
   125  				So(ValidateGerritChange(change), ShouldErrLike, `host: unspecified`)
   126  			})
   127  			Convey(`Invalid format`, func() {
   128  				change.Host = "https://somehost.com"
   129  				So(ValidateGerritChange(change), ShouldErrLike, `host: does not match`)
   130  			})
   131  			Convey(`Too long`, func() {
   132  				change.Host = strings.Repeat("a", hostnameMaxLength+1)
   133  				So(ValidateGerritChange(change), ShouldErrLike, `host: exceeds `, ` characters`)
   134  			})
   135  		})
   136  		Convey(`Project`, func() {
   137  			Convey(`Missing`, func() {
   138  				change.Project = ""
   139  				So(ValidateGerritChange(change), ShouldErrLike, `project: unspecified`)
   140  			})
   141  			Convey(`Too long`, func() {
   142  				change.Project = strings.Repeat("a", 256)
   143  				So(ValidateGerritChange(change), ShouldErrLike, `project: exceeds 255 characters`)
   144  			})
   145  		})
   146  		Convey(`Change`, func() {
   147  			Convey(`Missing`, func() {
   148  				change.Change = 0
   149  				So(ValidateGerritChange(change), ShouldErrLike, `change: unspecified`)
   150  			})
   151  			Convey(`Invalid`, func() {
   152  				change.Change = -1
   153  				So(ValidateGerritChange(change), ShouldErrLike, `change: cannot be negative`)
   154  			})
   155  		})
   156  		Convey(`Patchset`, func() {
   157  			Convey(`Missing`, func() {
   158  				change.Patchset = 0
   159  				So(ValidateGerritChange(change), ShouldErrLike, `patchset: unspecified`)
   160  			})
   161  			Convey(`Invalid`, func() {
   162  				change.Patchset = -1
   163  				So(ValidateGerritChange(change), ShouldErrLike, `patchset: cannot be negative`)
   164  			})
   165  		})
   166  	})
   167  }