/* * Copyright 2018 Intel Corporation, Inc * * 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 rb import ( "bytes" "io/ioutil" "os" "path/filepath" "strings" "testing" ) func TestIsTarGz(t *testing.T) { t.Run("Valid tar.gz", func(t *testing.T) { content := []byte{ 0x1f, 0x8b, 0x08, 0x08, 0xb0, 0x6b, 0xf4, 0x5b, 0x00, 0x03, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x61, 0x72, 0x00, 0xed, 0xce, 0x41, 0x0a, 0xc2, 0x30, 0x10, 0x85, 0xe1, 0xac, 0x3d, 0x45, 0x4e, 0x50, 0x12, 0xd2, 0xc4, 0xe3, 0x48, 0xa0, 0x01, 0x4b, 0x52, 0x0b, 0xed, 0x88, 0x1e, 0xdf, 0x48, 0x11, 0x5c, 0x08, 0xa5, 0x8b, 0x52, 0x84, 0xff, 0xdb, 0xbc, 0x61, 0x66, 0x16, 0x4f, 0xd2, 0x2c, 0x8d, 0x3c, 0x45, 0xed, 0xc8, 0x54, 0x21, 0xb4, 0xef, 0xb4, 0x67, 0x6f, 0xbe, 0x73, 0x61, 0x9d, 0xb2, 0xce, 0xd5, 0x55, 0xf0, 0xde, 0xd7, 0x3f, 0xdb, 0xd6, 0x49, 0x69, 0xb3, 0x67, 0xa9, 0x8f, 0xfb, 0x2c, 0x71, 0xd2, 0x5a, 0xc5, 0xee, 0x92, 0x73, 0x8e, 0x43, 0x7f, 0x4b, 0x3f, 0xff, 0xd6, 0xee, 0x7f, 0xea, 0x9a, 0x4a, 0x19, 0x1f, 0xe3, 0x54, 0xba, 0xd3, 0xd1, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1b, 0xbc, 0x00, 0xb5, 0xe8, 0x4a, 0xf9, 0x00, 0x28, 0x00, 0x00, } err := isTarGz(bytes.NewBuffer(content)) if err != nil { t.Errorf("Error reading valid tar.gz file %s", err.Error()) } }) t.Run("Invalid tar.gz", func(t *testing.T) { content := []byte{ 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xf2, 0x48, 0xcd, } err := isTarGz(bytes.NewBuffer(content)) if err == nil { t.Errorf("Error should NOT be nil") } }) t.Run("Empty tar.gz", func(t *testing.T) { content := []byte{} err := isTarGz(bytes.NewBuffer(content)) if err == nil { t.Errorf("Error should NOT be nil") } }) } func TestExtractTarBall(t *testing.T) { t.Run("Valid tar.gz", func(t *testing.T) { content := []byte{ 0x1f, 0x8b, 0x08, 0x08, 0xb0, 0x6b, 0xf4, 0x5b, 0x00, 0x03, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x61, 0x72, 0x00, 0xed, 0xce, 0x41, 0x0a, 0xc2, 0x30, 0x10, 0x85, 0xe1, 0xac, 0x3d, 0x45, 0x4e, 0x50, 0x12, 0xd2, 0xc4, 0xe3, 0x48, 0xa0, 0x01, 0x4b, 0x52, 0x0b, 0xed, 0x88, 0x1e, 0xdf, 0x48, 0x11, 0x5c, 0x08, 0xa5, 0x8b, 0x52, 0x84, 0xff, 0xdb, 0xbc, 0x61, 0x66, 0x16, 0x4f, 0xd2, 0x2c, 0x8d, 0x3c, 0x45, 0xed, 0xc8, 0x54, 0x21, 0xb4, 0xef, 0xb4, 0x67, 0x6f, 0xbe, 0x73, 0x61, 0x9d, 0xb2, 0xce, 0xd5, 0x55, 0xf0, 0xde, 0xd7, 0x3f, 0xdb, 0xd6, 0x49, 0x69, 0xb3, 0x67, 0xa9, 0x8f, 0xfb, 0x2c, 0x71, 0xd2, 0x5a, 0xc5, 0xee, 0x92, 0x73, 0x8e, 0x43, 0x7f, 0x4b, 0x3f, 0xff, 0xd6, 0xee, 0x7f, 0xea, 0x9a, 0x4a, 0x19, 0x1f, 0xe3, 0x54, 0xba, 0xd3, 0xd1, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1b, 0xbc, 0x00, 0xb5, 0xe8, 0x4a, 0xf9, 0x00, 0x28, 0x00, 0x00, } path, err := ExtractTarBall(bytes.NewBuffer(content)) defer os.RemoveAll(path) if err != nil { t.Errorf("Error reading valid tar.gz file %s", err.Error()) } fcontent, err := ioutil.ReadFile(filepath.Join(path, "test.txt")) if err != nil { t.Errorf("Error reading content of valid tar.gz file %s", err.Error()) } if strings.Contains(string(fcontent), "helloworld") == false { t.Errorf("Incorrect content read from path: %s", path) } }) t.Run("Invalid tar.gz", func(t *testing.T) { content := []byte{ 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xf2, 0x48, 0xcd, } path, err := ExtractTarBall(bytes.NewBuffer(content)) defer os.RemoveAll(path) if err == nil { t.Errorf("Error should NOT be nil") } }) t.Run("Empty tar.gz", func(t *testing.T) { content := []byte{} path, err := ExtractTarBall(bytes.NewBuffer(content)) defer os.RemoveAll(path) if err == nil { t.Errorf("Error should NOT be nil") } }) t.Run("Specific tar.gz with '.' entry", func(t *testing.T) { content := []byte{ 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xed, 0x94, 0xcd, 0x0a, 0xc3, 0x20, 0x10, 0x84, 0x3d, 0xfb, 0x14, 0x4b, 0xee, 0x29, 0x6b, 0x4c, 0x14, 0x7c, 0x99, 0x22, 0xd4, 0x42, 0x20, 0x3f, 0x25, 0x9a, 0x40, 0xde, 0x3e, 0x92, 0x9c, 0x1a, 0x0a, 0x39, 0x34, 0xb1, 0x87, 0xee, 0x77, 0x51, 0xd6, 0xc3, 0x8c, 0x8c, 0x23, 0x63, 0xd7, 0x83, 0x02, 0x51, 0x95, 0x25, 0xe0, 0xc6, 0x7e, 0x5d, 0xf7, 0x42, 0x2a, 0x89, 0x65, 0xa5, 0xb1, 0x2a, 0xe2, 0x5c, 0x2b, 0x2c, 0x18, 0x60, 0x02, 0x6f, 0x6c, 0xf4, 0xc1, 0x0e, 0x0c, 0xbf, 0xd6, 0xda, 0x5f, 0xee, 0x04, 0x6b, 0x29, 0x68, 0x6d, 0x57, 0x3f, 0x9d, 0x0f, 0xb7, 0xd9, 0xb6, 0xcd, 0x45, 0x1a, 0x87, 0xf9, 0x2b, 0xfd, 0x9e, 0xbf, 0x28, 0x24, 0x6a, 0xca, 0x3f, 0x05, 0x79, 0x9e, 0xf3, 0xc9, 0x0d, 0xbe, 0xee, 0x3b, 0x03, 0x93, 0xe0, 0x61, 0x7e, 0x39, 0xc3, 0x01, 0x26, 0xdb, 0x8c, 0xce, 0x1b, 0xc8, 0xfa, 0x78, 0x3a, 0xd4, 0x0f, 0x77, 0xdf, 0x26, 0xeb, 0x3b, 0xc9, 0xf8, 0xaf, 0x6d, 0x13, 0x27, 0xf1, 0x29, 0xde, 0xb3, 0x35, 0x0e, 0xfb, 0x2f, 0xd4, 0xae, 0xff, 0x52, 0x69, 0x41, 0xfd, 0x4f, 0x41, 0x88, 0x7f, 0xff, 0x96, 0xbd, 0x01, 0x4d, 0xb5, 0x26, 0x08, 0x82, 0xf8, 0x1b, 0x16, 0xb6, 0x17, 0x45, 0x61, 0x00, 0x0e, 0x00, 0x00, } path, err := ExtractTarBall(bytes.NewBuffer(content)) defer os.RemoveAll(path) if err != nil { t.Errorf("This tar.gz should be handled correctly, but '%s' occured", err) } }) }