github.com/googleapis/api-linter@v1.65.2/rules/aip0140/base64_test.go (about)

     1  // Copyright 2019 Google LLC
     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  //     https://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 aip0140
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/googleapis/api-linter/rules/internal/testutils"
    21  )
    22  
    23  func TestBase64(t *testing.T) {
    24  	tmpl := `
    25  		message Foo {
    26  			// {{.Comment}}
    27  		  {{.ProtoType}} bar = 1;
    28  		}
    29  	`
    30  	errMsg := "Field \"bar\" mentions base64 encoding in comments, so it should probably be type `bytes`, not `string`."
    31  	tests := []struct {
    32  		testName  string
    33  		Comment   string
    34  		ProtoType string
    35  		problems  testutils.Problems
    36  	}{
    37  		{"ValidNoBase64", "Blah blah blah.", "string", testutils.Problems{}},
    38  		{"ValidBytes", "base64 encoded", "bytes", testutils.Problems{}},
    39  		{"ValidBytesHyphen", "base-64 encoded", "bytes", testutils.Problems{}},
    40  		{"Invalid", "base64 encoded", "string", testutils.Problems{{Message: errMsg}}},
    41  		{"InvalidHyphen", "base-64 encoded", "string", testutils.Problems{{Message: errMsg}}},
    42  		{"InvalidCaps", "Base64 encoded", "string", testutils.Problems{{Message: errMsg}}},
    43  		{"InvalidCapsHyphen", "Base-64 encoded", "string", testutils.Problems{{Message: errMsg}}},
    44  	}
    45  	for _, test := range tests {
    46  		t.Run(test.testName, func(t *testing.T) {
    47  			file := testutils.ParseProto3Tmpl(t, tmpl, test)
    48  			field := file.GetMessageTypes()[0].GetFields()[0]
    49  			problems := base64.Lint(file)
    50  			if diff := test.problems.SetDescriptor(field).Diff(problems); diff != "" {
    51  				t.Errorf(diff)
    52  			}
    53  		})
    54  	}
    55  }