Skip to content

[skip-changelog] Avoid getting locked up in a perpetual symlink loop when loading a library #2146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add tests for the changes
  • Loading branch information
MatteoPologruto committed May 3, 2023
commit a34f57d34710caff44e1f0923202d4da3e86a32e
68 changes: 68 additions & 0 deletions arduino/libraries/libraries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ package libraries

import (
"encoding/json"
"os"
"testing"
"time"

paths "github.com/arduino/go-paths-helper"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -85,3 +87,69 @@ func TestLibrariesLoader(t *testing.T) {
require.True(t, lib.IsLegacy)
}
}

func TestSymlinkLoop(t *testing.T) {
// Set up directory structure of test library.
testLib := paths.New("testdata", "TestLib")
examplesPath := testLib.Join("examples")
require.NoError(t, examplesPath.Mkdir())
defer examplesPath.RemoveAll()

// It's probably most friendly for contributors using Windows to create the symlinks needed for the test on demand.
err := os.Symlink(examplesPath.Join("..").String(), examplesPath.Join("UpGoer1").String())
require.NoError(t, err, "This test must be run as administrator on Windows to have symlink creation privilege.")
// It's necessary to have multiple symlinks to a parent directory to create the loop.
err = os.Symlink(examplesPath.Join("..").String(), examplesPath.Join("UpGoer2").String())
require.NoError(t, err)

// The failure condition is Load() never returning, testing for which requires setting up a timeout.
done := make(chan bool)
go func() {
_, err = Load(testLib, User)
done <- true
}()
select {
case <-done:
case <-time.After(2 * time.Second):
require.FailNow(t, "Load didn't complete in the allocated time.")
}
require.Error(t, err)
}

func TestLegacySymlinkLoop(t *testing.T) {
// Set up directory structure of test library.
testLib := paths.New("testdata", "LegacyLib")
examplesPath := testLib.Join("examples")
require.NoError(t, examplesPath.Mkdir())
defer examplesPath.RemoveAll()

// It's probably most friendly for contributors using Windows to create the symlinks needed for the test on demand.
err := os.Symlink(examplesPath.Join("..").String(), examplesPath.Join("UpGoer1").String())
require.NoError(t, err, "This test must be run as administrator on Windows to have symlink creation privilege.")
// It's necessary to have multiple symlinks to a parent directory to create the loop.
err = os.Symlink(examplesPath.Join("..").String(), examplesPath.Join("UpGoer2").String())
require.NoError(t, err)

// The failure condition is Load() never returning, testing for which requires setting up a timeout.
done := make(chan bool)
go func() {
_, err = Load(testLib, User)
done <- true
}()

select {
case <-done:
case <-time.After(2 * time.Second):
require.FailNow(t, "Load didn't complete in the allocated time.")
}
require.Error(t, err)
}

func TestLoadExamples(t *testing.T) {
example, err := paths.New(".", "testdata", "TestLibExamples", "examples", "simple").Abs()
require.NoError(t, err)
lib, err := Load(paths.New("testdata", "TestLibExamples"), User)
require.NoError(t, err)
require.Len(t, lib.Examples, 1)
require.True(t, lib.Examples.Contains(example))
}
Empty file.
9 changes: 9 additions & 0 deletions arduino/libraries/testdata/TestLibExamples/library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name=TestLib
version=1.0.3
author=Arduino
maintainer=Arduino <info@arduino.cc>
sentence=A test lib
paragraph=very powerful!
category=Device Control
url=http://www.arduino.cc/en/Reference/TestLib
architectures=avr
Empty file.