ocket8888 commented on a change in pull request #4379: Rewrite isos to Golang URL: https://github.com/apache/trafficcontrol/pull/4379#discussion_r382735225
########## File path: traffic_ops/traffic_ops_golang/iso/crypt_test.go ########## @@ -0,0 +1,97 @@ +package iso + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +import ( + "fmt" + "testing" +) + +func TestCrypt(t *testing.T) { + // The crypt function is expected to behave like Perl's builtin `crypt` function. + // The following expected values were generated using the following Perl script + // run on the Perl Traffic Ops CiaB Docker container. + /* + #! /usr/bin/perl + + my ($pw, $salt) = @ARGV; + + if ((not defined $pw) || (not defined $salt)) { + die("usage: $0 <password> <salt>\n"); + } + + print crypt("$pw", "\$1\$$salt\$"); + print "\n"; + */ + + cases := []struct { + password, salt string + expected string + }{ + {"password", "salt", "$1$salt$qJH7.N4xYta3aEG/dfqo/0"}, + {"Traffic Ops", "pepper", "$1$pepper$AHauHHBeRPuBP0LCO0oBW0"}, + {"T0p S3cr3T", "N4xYta3a", "$1$N4xYta3a$E4g3CFzttHfxgEvY4PmrI/"}, + {"a", "b", "$1$b$J4vSIPg.1IiJxJ.JOHsOS1"}, + {"", "salt", "$1$salt$UsdFqFVB.FsuinRDK5eE.."}, + {"pw", "", "$1$$F0Fc2lbYpzr3KKdKkM0Wj."}, + {"", "", "$1$$qRPK7m23GJusamGpoGLby/"}, + } + + for _, tc := range cases { + tc := tc + t.Run("", func(t *testing.T) { + got, err := crypt(tc.password, tc.salt) + if err != nil { + t.Fatalf("crypt(%q, %q) err = %v; expected no error", tc.password, tc.salt, err) + } + + if got != tc.expected { + t.Fatalf("crypt(%q, %q) = %q; expected %q", tc.password, tc.salt, got, tc.expected) + } + t.Logf("crypt(%q, %q) = %q", tc.password, tc.salt, got) + }) + } +} + +func TestRndSalt(t *testing.T) { + for i := 0; i < 10; i++ { + t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { + // Ensure salt is correct length and contains valid characters. + + got := rndSalt(i) + if gotL := len(got); gotL != i { + t.Fatalf("rndSalt(%d) = %q (length = %d); expected length = %d", i, got, gotL, i) + } + + // Ensure proper characters + MATCH: Review comment: GOTOs trigger my ptsd, is this avoidable? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
