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 (
	"strings"
	"testing"
)

func TestParseResolve(t *testing.T) {
	cases := []struct {
		name     string
		input    string
		expected []string
	}{
		{
			"empty",
			"",
			nil,
		},
		{
			"comments",
			`# nameserver 127.0.0.1
			 # nameserver 192.168.1.1`,
			nil,
		},
		{
			"invalid IPs",
			`# /etc/resolv.conf
			nameserver not.an.ip
			nameserver
			nameserver 1
			`,
			nil,
		},
		{
			"single",
			`# /etc/resolv.conf
			nameserver 127.0.0.1
			`,
			[]string{"127.0.0.1"},
		},
		{
			"multi",
			`# /etc/resolv.conf
			# IPv4
			nameserver   127.0.0.1
			nameserver 192.168.1.10
			# IPv6
			nameserver   beef::1
			nameserver   ::0
			`,
			[]string{"127.0.0.1", "192.168.1.10", "beef::1", "::0"},
		},
	}

	for _, tc := range cases {
		tc := tc
		t.Run(tc.name, func(t *testing.T) {
			got, err := parseResolve(strings.NewReader(tc.input))
			if err != nil {
				t.Fatalf("parseResolve() err = %v", err)
			}

			if lGot, lExpected := len(got), len(tc.expected); lGot != lExpected {
				t.Fatalf("got %d nameservers; expected %d", lGot, lExpected)
			}

			for i, expectedNS := range tc.expected {
				if gotNS := got[i]; gotNS != expectedNS {
					t.Errorf("got nameserver[%d] = %q; expected %q", i, gotNS, expectedNS)
				}
			}

			t.Logf("parseResolve(): %v", got)
		})
	}
}
